字段过滤与扩展

字段过滤

  1. 使用 select 来控制请求返回的字段

    例如:

    query.select('created_by')

    query.select(['pointer', 'created_by'])

  2. 通过 select('pointer.attr') 指定 pointer 展开后只返回 pointer 中的某些字段

    info pointer 字段已经设置了 expand,该操作才生效,否则会被忽略。

    expand 的使用请参照下文「字段扩展」章节

    例如:

    1. query.limit(1);
    2. query.select(['created_by.nickname', 'pointer.count']);
    3. query.expand(['created_by', 'pointer']);

info select('field') select(['field_a', 'field_b']) 为“规定返回”,

select('-field') select(['-field_a', '-field_b']) 为“规定不返回”,

“规定返回”和“规定不返回”不能同时使用,否则该操作不生效,接口将会直接返回所有字段。

在 get 方法中使用

  1. TableObject product = new TableObject(tableName);
  2. String recordId = 'xxxxxxxx' // 数据行 id
  3. // 规定返回特定字段
  4. product.get(recordId, select: 'created_at');
  5. // or
  6. product.get(recordId, select: ['created_at', 'created_by']);
  7. // 规定不返回特定字段
  8. product.get(recordId, select: '-created_at');
  9. // or
  10. product.get(recordId, select: ['-created_at', '-created_by']);

在 find 方法中使用

  1. TableObject product = new TableObject(tableName);
  2. Query query = new Query();
  3. query.where(Where.compare('amount', '>', 0));
  4. // 规定返回特定字段
  5. query.select('created_at');
  6. product.find(query: query);
  7. // or
  8. query.select(['created_at', 'created_by']);
  9. product.find(query: query);
  10. // 规定不返回特定字段
  11. query.select('-created_at');
  12. product.find(query: query);
  13. // or
  14. query.select(['-created_at', '-created_by']);
  15. product.find(query: query);

字段扩展

开发者可以通过 expand pointer 来查询该字段的更多信息,返回结果中的 pointer 字段会被替换为这个字段对应的完整的数据行对象。

使用 expand 来控制 pointer 展开

例如:

query.expand('created_by')

query.expand(['pointer', 'created_by'])

info created_by 字段是一个特殊的 pointer,开发者无需配置,默认指向了 _userpofile 表

用户自定义 pointer

使用 expand 方法会增加一次数据表查询,api call 计费 +1

expand 返回结果示例

pointer_value 为指向其他表的 pointer 类型字段

info 未 expand 的情况下,created_by 的值是 int 类型,而其他 pointer 的值是 object

不使用 expand

  1. {
  2. "created_at": 1516118400,
  3. "created_by": 62536607,
  4. "id": "5a2fa9b008443e59e0e67829",
  5. "name": "小米无线耳机",
  6. "price": 199,
  7. "pointer_value": {
  8. "id": "5a2fa9xxxxxxxxxxxxxx",
  9. "_table": "table-name"
  10. }
  11. }

使用 expand

  1. {
  2. "created_at": 1516118400,
  3. "created_by": {
  4. "avatar": "https://media.ifanrusercontent.com/tavatar/fb/cd/xxxx.jpg",
  5. "id": 62536607,
  6. "nickname": "Larry。"
  7. },
  8. "pointer_value": {
  9. "created_at": 1516118400,
  10. "name": "123",
  11. "id": "5a2fa9xxxxxxxxxxxxxx",
  12. "_table": "table-name"
  13. },
  14. "id": "5a2fa9b008443e59e0e67829",
  15. "name": "小米无线耳机",
  16. "price": 199
  17. }

使用方法

在 get 方法中使用

  1. TableObject product = new TableObject(tableName);
  2. product.get('5acc2904da6b737322a82f78', expand: ['created_by', 'pointer_value']);

在 find 方法中使用

  1. TableObject product = new TableObject(tableName);
  2. Query query = new Query();
  3. query.where(Where.compare('amount', '>', 0));
  4. // 扩展特定字段
  5. query.expand(['-created_at', '-created_by']);
  6. product.find(query: query);