数据表操作

BaaS.TableSchema 对象封装了针对数据表相关的操作,通过实例化 TableSchema,我们可以对数据表进行增删改查。

  1. let tableSchema = new BaaS.TableSchema()

创建数据表

tableSchema.createSchema(schemaInfo)

参数说明

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

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

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

权限参数的说明:

参数类型说明
user:anonymousString所有人(匿名用户 + 登录用户)可写/可读
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. const schemaInfo = {
  2. "name": "Table199",
  3. "schema": {
  4. "fields": [
  5. {
  6. "name": "String",
  7. "type": "string"
  8. }
  9. ]
  10. },
  11. "row_read_perm": [
  12. "user:anonymous"
  13. ],
  14. "row_write_perm": [
  15. "user:*"
  16. ],
  17. "write_perm": [
  18. "user:*"
  19. ]
  20. }
  21. async function createSchema() {
  22. try {
  23. let tableSchema = new BaaS.TableSchema()
  24. let res = await tableSchema.createSchema(schemaInfo)
  25. // success
  26. return res
  27. } catch(err) {
  28. // error
  29. throw err
  30. }
  31. }
  1. const schemaInfo = {
  2. "name": "Table199",
  3. "schema": {
  4. "fields": [
  5. {
  6. "name": "String",
  7. "type": "string"
  8. }
  9. ]
  10. },
  11. "row_read_perm": [
  12. "user:anonymous"
  13. ],
  14. "row_write_perm": [
  15. "user:*"
  16. ],
  17. "write_perm": [
  18. "user:*"
  19. ]
  20. }
  21. function createSchema() {
  22. let tableSchema = new BaaS.TableSchema()
  23. tableSchema.createSchema(schemaInfo).then(res=>{
  24. // success
  25. callback(null, res)
  26. }).catch(err => {
  27. // error
  28. callback(err)
  29. })
  30. }

返回示例

res.data 结构如下

  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:anonymous"
  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: 表名已存在;不合法的数据

获取数据表详情

接口

tableSchema.getSchema(schemaID)

代码示例

  1. async function getSchema() {
  2. try {
  3. let tableSchema = new BaaS.TableSchema()
  4. let res = await tableSchema.getSchema(1)
  5. // success
  6. return res
  7. } catch(err) {
  8. // error
  9. throw err
  10. }
  11. }
  1. function getSchema() {
  2. let tableSchema = new BaaS.TableSchema()
  3. tableSchema.getSchema(1).then(res => {
  4. // success
  5. callback(null, res)
  6. }).catch(err => {
  7. // error
  8. callback(err)
  9. })
  10. }

返回示例

res.data 结构如下

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

获取数据表列表

tableSchema.getSchemaList({offset, limit})

参数说明

参数类型说明
limitNumber限制返回资源的个数,默认为 20 条,最大可设置为 1000
offsetNumber设置返回资源的起始偏移值,默认为 0

代码示例

  1. async funnction getSchemaList() {
  2. try {
  3. let res = await tableSchema.getSchemaList({limit:20, offset: 0})
  4. // success
  5. return res
  6. } catch(err) {
  7. // error
  8. throw err
  9. }
  10. }
  1. function getSchemaList() {
  2. tableSchema.getSchemaList({limit:20, offset: 0}).then(res=>{
  3. // success
  4. callback(null, res)
  5. }).catch(err => {
  6. // error
  7. callback(err)
  8. })
  9. }

返回示例

res.data 结构如下

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

更新数据表

tableSchema.updateSchema(schemaID, schemaInfo)

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

代码示例

  1. const schemaInfo = {
  2. name: "table"
  3. }
  4. async function updateSchema() {
  5. try {
  6. let res = await tableSchema.updateSchema(1, schemaInfo)
  7. // success
  8. return res
  9. } catch(err) {
  10. // error
  11. throw err
  12. }
  13. }
  1. const schemaInfo = {
  2. name: "table"
  3. }
  4. function updateSchema() {
  5. tableSchema.updateSchema(1, schemaInfo).then(res=>{
  6. // success
  7. callback(null, res)
  8. }).catch(err => {
  9. // error
  10. callback(err)
  11. })
  12. }

返回示例

res.data 接口如下

  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: 表名已存在;不合法的数据

删除数据表

tableSchema.deleteSchema(schemaID)

代码示例

  1. async function deleteSchema() {
  2. try {
  3. let res = await tableSchema.deleteSchema(1)
  4. // success
  5. return res
  6. } catch(err) {
  7. // error
  8. throw err
  9. }
  10. }
  1. function deleteSchema() {
  2. tableSchema.deleteSchema(1).then(res => {
  3. // success
  4. callback(null, res)
  5. }).catch(err => {
  6. // error
  7. callback(err)
  8. })
  9. }

状态码说明

204: 删除成功