数据表操作

创建数据表

接口

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

提交参数

参数 类型 必填 说明
name String(32) 数据表名(以字母开头,字母、数字、下划线的组合)
schema Object 数据表字段的元信息
row_read_perm String Array 数据表行的读权限
row_write_perm String Array 数据表行的写权限
write_perm String 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. }

数据表列的元信息:

属性 类型 必填 说明
name String(32) 字段名(字母开头,字母、数字、下划线的组合)
type String 字段类型
items Object 列表元素类型,array 字段类型必填
format String geojson 字段类型必填,值默认为 default
description String 字段的描述,不填自动赋值为字段名称
constraints Object 字段的约束属性,仅支持 required 属性
default 跟字段类型一致 字段的默认值
acl Object 字段权限相关的属性
coordinate_type String geojson 字段类型必填
  • type 目前支持 string、integer、number、boolean、array、geojson、file、date 等

  • 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. }

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

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

代码示例

{% tabs createTableCurl=”Curl”, createTableNode=”Node”, createTablePHP=”PHP” %}

{% content “createTableCurl” %}

  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/

{% content “createTableNode” %}

  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. });

{% content “createTablePHP” %}

  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);

{% endtabs %}

返回示例

  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. }

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

返回参数说明

参数 类型 说明
id Integer 数据表 ID
name String 数据表名
protected_fields String Array 内置表的保护字段,若数据表不是内置表,该字段为 null
schema Object 数据表字段的元信息
write_perm String Array 数据表写权限
default_row_perm Object 数据表行数据权限
created_at Integer 数据表创建时间
updated_at Integer 数据表更新时间

状态码说明

201: 修改成功

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

获取数据表详情

接口

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

代码示例

{% tabs getTableCurl=”Curl”, getTableNode=”Node”, getTablePHP=”PHP” %}

{% content “getTableCurl” %}

  1. curl -X GET \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. https://cloud.minapp.com/oserve/v1/table/1/

{% content “getTableNode” %}

  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. });

{% content “getTablePHP” %}

  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);

{% endtabs %}

返回示例

  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

代码示例

{% tabs getTableListCurl=”Curl”, getTableListNode=”Node”, getTableListPHP=”PHP” %}

{% content “getTableListCurl” %}

  1. curl -X GET \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. https://cloud.minapp.com/oserve/v1/table/

{% content “getTableListNode” %}

  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. });

{% content “getTableListPHP” %}

  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);

{% endtabs %}

返回示例

  1. {
  2. "meta": {
  3. "limit": 20,
  4. "next": null,
  5. "offset": 0,
  6. "previous": null,
  7. "total_count": 1
  8. },
  9. "objects": [
  10. {
  11. "id": 1,
  12. "name": "Table",
  13. "protected_fields": null,
  14. "schema": {
  15. "fields": [
  16. {
  17. "name": "String",
  18. "type": "string"
  19. }
  20. ]
  21. },
  22. "write_perm": [
  23. "user:*"
  24. ],
  25. "default_row_perm": {
  26. "_read_perm": [
  27. "user:*"
  28. ],
  29. "_write_perm": [
  30. "user:*"
  31. ]
  32. },
  33. "created_at": 1519538564,
  34. "updated_at": 1519640477
  35. }
  36. ]
  37. }

状态码说明

200: 成功

更新数据表

接口

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

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

代码示例

{% tabs updateTableCurl=”Curl”, updateTableNode=”Node”, updateTablePHP=”PHP” %}

{% content “updateTableCurl” %}

  1. curl -X PUT \
  2. -H "Authorization: Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4" \
  3. -H "Content-Type: application/json" \
  4. -d '{
  5. "name": "table"
  6. }' \
  7. https://cloud.minapp.com/oserve/v1/table/1/

{% content “updateTableNode” %}

  1. var request = require("request");
  2. var options = { method: 'PUT',
  3. url: 'https://cloud.minapp.com/oserve/v1/table/1/',
  4. headers:
  5. { 'Content-Type': 'application/json',
  6. Authorization: 'Bearer cfb5912724dd7ff0b0c17683cc3074bb548bc7f4' },
  7. body: { name: 'table' },
  8. json: true };
  9. request(options, function (error, response, body) {
  10. if (error) throw new Error(error);
  11. console.log(body);
  12. });

{% content “updateTablePHP” %}

  1. <?php
  2. $table_id = 1; // 数据表 ID
  3. $url = "https://cloud.minapp.com/oserve/v1/table/{$table_id}/";
  4. $param['name'] = 'table';
  5. $ch = curl_init();
  6. $header = array(
  7. "Authorization: Bearer {$token}",
  8. 'Content-Type: application/json; charset=utf-8'
  9. );
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  14. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  17. $res['response'] = curl_exec($ch); // 反馈结果
  18. $res['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 请求状态码
  19. curl_close($ch);

{% endtabs %}

返回示例

  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: 修改成功

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

删除数据表

接口

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

代码示例

{% tabs deleteTableCurl=”Curl”, deleteTablePHP=”PHP” %}

{% content “deleteTableCurl” %}

  1. curl -X DELETE \
  2. -H "cookie: {{ cookie }}" \
  3. -H "Content-Type: application/json" \
  4. https://cloud.minapp.com/oserve/v1/table/1/

{% content “deleteTablePHP” %}

  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, 'DELETE');
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  15. $res['response'] = curl_exec($ch); // 反馈结果
  16. $res['status_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 请求状态码
  17. curl_close($ch);

{% endtabs %}

状态码说明

204: 删除成功