数据表操作

创建数据表

接口

POST https://cloud.minapp.com/oserve/v1/table/

提交参数

参数类型必填说明
nameString(32)数据表名(以字母开头,字母、数字、下划线的组合)
schemaObject数据表字段的元信息
row_read_permString Array数据表行的默认读权限
row_write_permString Array数据表行的默认写权限
write_permString Array数据表的写权限

参数 row_read_perm 和 row_write_perm 控制数据表数据的读写权限,读权限表示用户是否有权限获取数据,写权限表示用户是否有权限更新数据。

参数 write_perm 控制数据表的写权限,即表示用户是否有权限创建数据。

权限参数的说明:

参数类型说明
user:*String所有人可写/可读
user:<:user_id>String某个用户可写/可读
gid:<:group_id>String某个分组下的用户可写/可读

具体描述与使用场景可参考ACL 访问控制列表

参数 schema 用于存储数据表字段的元信息,其结构遵循JSON-Table-Schema的描述。

例:

  1. {
  2. "fields": [
  3. {
  4. "name": "field_name",
  5. "type": "string",
  6. "description": "description of field_name",
  7. "constraints": {
  8. "required": true # 设置写入/更新必填选项
  9. },
  10. "default": "hello, world", # 设置默认值
  11. "acl": {
  12. "clientVisibile": true, # 设置客户端可见
  13. "clientReadOnly": true, # 设置客户端只读
  14. "creatorVisible": true # 设置创建者可见
  15. }
  16. }
  17. ]
  18. }

数据表列的元信息:

属性类型必填说明
nameString(32)字段名(字母开头,字母、数字、下划线的组合)
typeString字段类型
itemsObject列表元素类型,array 字段类型必填
formatStringgeojson 字段类型必填,值默认为 default
descriptionString字段的描述,不填自动赋值为字段名称
constraintsObject字段的约束属性,仅支持 required 属性
default跟字段类型一致字段的默认值
aclObject字段权限相关的属性
coordinate_typeStringgeojson 字段类型必填
schema_idStringpointer 字段类型必填,表示关联的数据表 ID
  • type 目前支持 string、integer、number、boolean、array、geojson、file、date、reference(pointer 类型字段)等

  • items 目前支持 string、integer、number、boolean 等

  • coordinate_type 目前支持 wgs84(地球坐标)、gcj02(火星坐标)

若字段是 array 类型,字段元信息为:

  1. {
  2. "name": "array_field",
  3. "type": "array",
  4. "items": {
  5. "type": "string"
  6. }
  7. }

若字段是 geojson 类型,字段元信息为:

  1. {
  2. "name": "location",
  3. "type": "geojson",
  4. "format": "default",
  5. "coordinate_type": "gcj02"
  6. }

若字段是 pointer 类型,字段元信息为:

  1. {
  2. "name": "pointer",
  3. "type": "reference",
  4. "schema_id": "1"
  5. }

字段权限相关的属性存储在 acl 中:

属性类型必填说明
clientVisibileBoolean客户端只读的标志位,true 表示字段在客户端只读,不能被写入/更新
clientReadOnlyBoolean客户端可见的标志位, false 表示字段在客户端不可见
creatorVisibleBoolean客户端创建者可见的标志位,true 表示字段在客户端只有创建者可见

代码示例

  1. curl -X POST \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. -d '{
  5. "name": "Table199",
  6. "schema": {
  7. "fields": [
  8. {
  9. "name": "String",
  10. "type": "string"
  11. }
  12. ]
  13. },
  14. "row_read_perm": ["user:*"],
  15. "row_write_perm": ["user:*"],
  16. "write_perm": ["user:*"]
  17. }' \
  18. https://cloud.minapp.com/oserve/v1/table/
  1. var request = require("request");
  2. var options = { method: 'POST',
  3. url: 'https://cloud.minapp.com/oserve/v1/table/',
  4. headers:
  5. { 'Content-Type': 'application/json',
  6. Authorization: 'Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4' },
  7. body:
  8. { name: 'Table199',
  9. schema: { fields: [ { name: 'String', type: 'string' } ] },
  10. row_read_perm: [ 'user:*' ],
  11. row_write_perm: [ 'user:*' ],
  12. write_perm: [ 'user:*' ] },
  13. json: true };
  14. request(options, function (error, response, body) {
  15. if (error) throw new Error(error);
  16. console.log(body);
  17. });
  1. <?php
  2. $schema['fields'] = [array(
  3. 'name' => 'String',
  4. 'type' => 'string'
  5. )];
  6. $param['name'] = 'Table199';
  7. $param['schema'] = $schema;
  8. $param['row_read_perm'] = ['user:*'];
  9. $param['row_write_perm'] = ['user:*'];
  10. $param['write_perm'] = ['user:*'];
  11. $url = 'https://cloud.minapp.com/oserve/v1/table/';
  12. $ch = curl_init();
  13. $header = array(
  14. "Authorization: Bearer {$token}",
  15. 'Content-Type: application/json; charset=utf-8'
  16. );
  17. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  18. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  19. curl_setopt($ch, CURLOPT_URL, $url);
  20. curl_setopt($ch, CURLOPT_POST, true);
  21. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  24. $res['response'] = curl_exec($ch); // 反馈结果
  25. $res['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 请求状态码
  26. curl_close($ch);

返回示例

  1. {
  2. "id": 1,
  3. "name": "Table",
  4. "protected_fields": null,
  5. "schema": {
  6. "fields": [
  7. {
  8. "description": "id",
  9. "name": "id",
  10. "type": "id"
  11. },
  12. {
  13. "description": "created_by",
  14. "name": "created_by",
  15. "type": "integer"
  16. },
  17. {
  18. "description": "created_at",
  19. "name": "created_at",
  20. "type": "integer"
  21. },
  22. {
  23. "description": "updated_at",
  24. "name": "updated_at",
  25. "type": "integer"
  26. },
  27. {
  28. "name": "String",
  29. "type": "string",
  30. "description": "string",
  31. }
  32. ]
  33. },
  34. "write_perm": [
  35. "user:*"
  36. ],
  37. "default_row_perm": {
  38. "_read_perm": [
  39. "user:*"
  40. ],
  41. "_write_perm": [
  42. "user:*"
  43. ]
  44. },
  45. "created_at": 1519538564,
  46. "updated_at": 1519640477
  47. }

字段如 id、created_by、created_at、updated_at 为自动添加的内置字段

返回参数说明

参数类型说明
idInteger数据表 ID
nameString数据表名
protected_fieldsString Array内置表的保护字段,若数据表不是内置表,该字段为 null
schemaObject数据表字段的元信息
write_permString Array数据表写权限
default_row_permObject数据表行数据权限
created_atInteger数据表创建时间
updated_atInteger数据表更新时间

状态码说明

201: 修改成功

400: 表名已存在;不合法的数据

获取数据表详情

接口

GET https://cloud.minapp.com/oserve/v1/table/:table_id/

代码示例

  1. curl -X GET \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. https://cloud.minapp.com/oserve/v1/table/1/
  1. var request = require("request");
  2. var options = { method: 'GET',
  3. url: 'https://cloud.minapp.com/oserve/v1/table/1/',
  4. headers:
  5. { 'Content-Type': 'application/json',
  6. Authorization: 'Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4' } };
  7. request(options, function (error, response, body) {
  8. if (error) throw new Error(error);
  9. console.log(body);
  10. });
  1. <?php
  2. $table_id = 1; // 数据表 ID
  3. $url = "https://cloud.minapp.com/oserve/v1/table/{$table_id}/";
  4. $ch = curl_init();
  5. $header = array(
  6. "Authorization: Bearer {$token}",
  7. 'Content-Type: application/json; charset=utf-8'
  8. );
  9. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  10. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  15. $res = curl_exec($ch);
  16. curl_close($ch);

返回示例

  1. {
  2. "id": 1,
  3. "name": "Table",
  4. "protected_fields": null,
  5. "schema": {
  6. "fields": [
  7. {
  8. "name": "String",
  9. "type": "string"
  10. }
  11. ]
  12. },
  13. "write_perm": [
  14. "user:*"
  15. ],
  16. "default_row_perm": {
  17. "_read_perm": [
  18. "user:*"
  19. ],
  20. "_write_perm": [
  21. "user:*"
  22. ]
  23. },
  24. "created_at": 1519538564,
  25. "updated_at": 1519640477
  26. }

状态码说明

200: 成功

获取数据表列表

接口

GET https://cloud.minapp.com/oserve/v1/table/

提交参数

  • name 支持对数据表名的等值查询

https://cloud.minapp.com/oserve/v1/table/?name=Table

代码示例

  1. curl -X GET \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. https://cloud.minapp.com/oserve/v1/table/
  1. var request = require("request");
  2. var options = { method: 'GET',
  3. url: 'https://cloud.minapp.com/oserve/v1/table/',
  4. headers:
  5. { 'Content-Type': 'application/json',
  6. Authorization: 'Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4' } };
  7. request(options, function (error, response, body) {
  8. if (error) throw new Error(error);
  9. console.log(body);
  10. });
  1. <?php
  2. $url = "https://cloud.minapp.com/oserve/v1/table/";
  3. $ch = curl_init();
  4. $header = array(
  5. "Authorization: Bearer {$token}",
  6. 'Content-Type: application/json; charset=utf-8'
  7. );
  8. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  9. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  10. curl_setopt($ch, CURLOPT_URL, $url);
  11. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  14. $res = curl_exec($ch);
  15. curl_close($ch);

返回示例

{
  "meta": {
    "limit": 20,
    "next": null,
    "offset": 0,
    "previous": null,
    "total_count": 1
  },
  "objects": [
    {
      "id": 1,
      "name": "Table",
      "protected_fields": null,
      "schema": {
        "fields": [
          {
            "name": "String",
            "type": "string"
          }
        ]
      },
      "write_perm": [
        "user:*"
      ],
      "default_row_perm": {
        "_read_perm": [
          "user:*"
        ],
        "_write_perm": [
          "user:*"
        ]
      },
      "created_at": 1519538564,
      "updated_at": 1519640477
    }
  ]
}

状态码说明

200: 成功

更新数据表

接口

PUT https://cloud.minapp.com/oserve/v1/table/:table_id/

数据表更新接口支持一次更新一个或多个字段

代码示例

curl -X PUT \
-H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
-H "Content-Type: application/json" \
-d '{
      "name": "table"
    }' \
https://cloud.minapp.com/oserve/v1/table/1/
var request = require("request");

var options = { method: 'PUT',
  url: 'https://cloud.minapp.com/oserve/v1/table/1/',
  headers: 
   { 'Content-Type': 'application/json',
     Authorization: 'Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4' },
  body: { name: 'table' },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
<?php
$table_id = 1; // 数据表 ID
$url = "https://cloud.minapp.com/oserve/v1/table/{$table_id}/";
$param['name'] = 'table';

$ch = curl_init();
$header = array(
  "Authorization: Bearer {$token}",
  'Content-Type: application/json; charset=utf-8'
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

$res['response'] = curl_exec($ch); // 反馈结果
$res['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 请求状态码
curl_close($ch);

返回示例

{
  "id": 1,
  "name": "table",
  "protected_fields": null,
  "schema": {
    "fields": [
      {
        "name": "String",
        "type": "string"
      }
    ]
  },
  "write_perm": [
    "user:*"
  ],
  "default_row_perm": {
    "_read_perm": [
      "user:*"
    ],
    "_write_perm": [
      "user:*"
    ]
  },
  "created_at": 1519538564,
  "updated_at": 1519640477
}

状态码说明

200: 修改成功

400: 表名已存在;不合法的数据

删除数据表

接口

DELETE https://cloud.minapp.com/oserve/v1/table/:table_id/

代码示例

curl -X DELETE \
-H "cookie: {{ cookie }}" \
-H "Content-Type: application/json" \
https://cloud.minapp.com/oserve/v1/table/1/
<?php
$table_id = 1; // 表 ID
$url = "https://cloud.minapp.com/oserve/v1/table/{$table_id}/";

$ch = curl_init();
$header = array(
  "Authorization: Bearer {$token}",
  'Content-Type: application/json; charset=utf-8'
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

$res['response'] = curl_exec($ch); // 反馈结果
$res['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 请求状态码
curl_close($ch);

状态码说明

204: 删除成功