diff --git a/open-api/data/record.md b/open-api/data/record.md index ce7c1123..411a7bc4 100644 --- a/open-api/data/record.md +++ b/open-api/data/record.md @@ -626,3 +626,402 @@ curl_close($ch); **状态码说明** `200` 更新成功,`400` 操作符不支持/请求参数有错 + + +## pointer 查询 + +> **info** +> 目前 pointer 仅支持针对 pointer 本身的查询,不支持嵌套查询(即查询 pointer 指向的数据行的字段) + +**接口** + +`GET https://cloud.minapp.com/oserve/v1.8/table/:table_id/record/?where=query` + +其中 `:table_id` 需替换为你的数据表 ID,query 为查询条件 + +**示例代码** + +假设现在有两张表: order 表和 customer 表,order 表中有一个类型为 pointer,名称为 user 的字段,指向了 customer 表的数据行。 +现在需要查询 order 表中,user 字段指向 customer 表中 id 为 `5bf4f7457fed8d6c2f5c3d6e` 的数据行。 + +{% tabs first="Node", second="Python", third="PHP" %} + +{% content "first" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/oserve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + qs: { // query string, 被附加到uri的参数 + where: JSON.stringify({ // 可选, 参数值应经过 JSON 编码为 JSONString 后,再经过 URL 编码 + "user": {"$eq": "5bf4f7457fed8d6c2f5c3d6e"} + }), + order_by: 'id', // 可选 + offset: 0, // 可选 + limit: 20, // 可选 + } +} + +request(opt, function(err, res, body) { + console.log(body) +}) +``` + +{% content "second" %} + +```python +import json +import urllib + +import requests + + +table_id = '' +BASE_API = r'https://cloud.minapp.com/oserve/v1.8/table/%s/record/' % table_id + +TOKEN = '' +HEADERS = { + 'Authorization': 'Bearer %s' % TOKEN +} + +where_ = { + 'user': {'$eq': "5bf4f7457fed8d6c2f5c3d6e"}, +} + +query_ = urllib.urlencode({ + 'where': json.dumps(where_), + 'order_by': '-id', + 'limit': 10, + 'offset': 0, +}) + +API = '?'.join((BASE_API, query_)) + +resp_ = requests.get(API, headers=HEADERS) +print resp_.content +``` + +{% content "third" %} + +```php + '-id', + 'where' => json_encode(['user' => ['$eq' => '5bf4f7457fed8d6c2f5c3d6e']]), + 'limit' => '10', + 'offset' => '0', +); +$url = "https://cloud.minapp.com/oserve/v1.8/table/{$table_id}/record/?"; +$url .= http_build_query($condition); + +$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, 'GET'); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + +$res = curl_exec($ch); +curl_close($ch); +``` + +{% endtabs %} + + +## 字段扩展 + +支持 expand created_by 以及 pointer 类型字段。返回结果中的 created_by 以及扩展的 pointer 类型字段会被替换为这个字段对应的完整对象。 + +**接口** + +`GET https://cloud.minapp.com/oserve/v1.8/table/:table_id/record/?expand=pointer,created_by` + +其中 `:table_id` 需替换为你的数据表 ID + +### expand 返回结果示例 +不使用 expand +```json +{ + "created_at": 1516118400, + "created_by": 1234, + "id": "5a2fa9b008443e59e0e67829", + "name": "小米无线耳机", + "price": 199, + "pointer": {"id": "5a2fa9b008443e59e0e67889", "_table": "pointer"} +} +``` + +使用 expand +```json +{ + "created_at": 1516118400, + "created_by": { + "avatar": "https://media.ifanrusercontent.com/tavatar/fb/cd/xxxx.jpg", + "id": 62536607, + "nickname": "Larry。" + }, + "id": "5a2fa9b008443e59e0e67829", + "name": "小米无线耳机", + "price": 199, + "pointer": { + "id": "5a2fa9b008443e59e0e67889", + "_table": "pointer", + "customized_field": "field_content" + } +} +``` + +**代码示例** + +{% tabs first="Node", second="Python", third="PHP" %} + +{% content "first" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/oserve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + qs: { // query string, 被附加到uri的参数 + where: JSON.stringify({ // 可选, 参数值应经过 JSON 编码为 JSONString 后,再经过 URL 编码 + "price": {"$eq": 10} + }), + order_by: 'id', // 可选 + offset: 0, // 可选 + limit: 20, // 可选 + expand: pointer,created_by, //必选,表示需要扩展的字段 + } +} + +request(opt, function(err, res, body) { + console.log(body) +}) +``` + +{% content "second" %} + +```python +import json +import urllib + +import requests + + +table_id = '' +BASE_API = r'https://cloud.minapp.com/oserve/v1.8/table/%s/record/' % table_id + +TOKEN = '' +HEADERS = { + 'Authorization': 'Bearer %s' % TOKEN +} + +where_ = { + 'price': {'$gt': 100}, +} + +query_ = urllib.urlencode({ + 'where': json.dumps(where_), + 'order_by': '-id', + 'limit': 10, + 'offset': 0, + 'expand': 'pointer,created_by' +}) + +API = '?'.join((BASE_API, query_)) + +resp_ = requests.get(API, headers=HEADERS) +print resp_.content +``` + +{% content "third" %} + +```php + '-id', + 'where' => json_encode(['price' => ['$gt' => 'test search']]), + 'limit' => '10', + 'offset' => '0', + 'expand' => 'pointer,created_by' +); +$url = "https://cloud.minapp.com/oserve/v1.8/table/{$table_id}/record/?"; +$url .= http_build_query($condition); + +$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, 'GET'); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + +$res = curl_exec($ch); +curl_close($ch); +``` + +{% endtabs %} + + +## 添加 pointer 类型数据 + +**接口** + +`POST https://cloud.minapp.com/oserve/v1.8/table/:table_id/record/` + +其中 `:table_id` 需替换为你的数据表 ID + +**代码示例** + +{% tabs insertNode="Node", insertPHP="PHP" %} + +{% content "insertNode" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/oserve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, + json: { // 指定 data 以 "Content-Type": 'application/json' 传送 + name: 'nickname', + desc: ['description'], + price: 19, + amount: 19, + code: '18814098707' + pointer: '5a2fa9b008443e59e0e67889', // pointer 关联的数据 ID + } +} + +request(opt, function(err, res, body) { + console.log(res.statusCode) +}) +``` + +{% content "insertPHP" %} +```php + '5a2fa9b008443e59e0e67889' +); +$url = "https://cloud.minapp.com/oserve/v1.8/table/{$table_id}/record/"; + +$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_POST, true); +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); + +``` + + +{% endtabs %} + +**状态码说明** + +`201` 写入成功,`400` 请求参数有错 + + +## 更新 pointer 类型数据 + +**接口** + +`PUT https://cloud.minapp.com/oserve/v1.8/table/:table_id/record/:record_id/` + +其中 `:table_id` 需替换为你的数据表 ID,`record_id` 需替换为你的记录 ID + +**代码示例** + +{% tabs updateNode="Node", updatePHP="PHP" %} + +{% content "updateNode" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/oserve/v1.8/table/3906/record/5a6ee2ab4a7baa1fc083e3xx', // 3906 对应 :table_id, 5a6ee2ab4a7baa1fc083e3xx 对应 :record_id + method: 'PUT', + headers: { + Authorization: `Bearer ${token}`, + }, + json: { // 指定 data 以 "Content-Type": 'application/json' 传送 + pointer: '5a2fa9b008443e59e0e67889', // pointer 关联的数据 ID + } +} + +request(opt, function(err, res, body) { + console.log(res.statusCode) +}) +``` + +{% content "updatePHP" %} + +```php + **info** +> 目前 pointer 仅支持针对 pointer 本身的查询,不支持嵌套查询(即查询 pointer 指向的数据行的字段) + +**接口** + +`GET https://cloud.minapp.com/userve/v1.8/table/:table_id/record/?where=query` + +其中 `:table_id` 需替换为你的数据表 ID,query 为查询条件 + +**示例代码** + +假设现在有两张表: order 表和 customer 表,order 表中有一个类型为 pointer,名称为 user 的字段,指向了 customer 表的数据行。 +现在需要查询 order 表中,user 字段指向 customer 表中 id 为 `5bf4f7457fed8d6c2f5c3d6e` 的数据行。 + +{% tabs first="Node", second="Python", third="PHP" %} + +{% content "first" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/userve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + qs: { // query string, 被附加到uri的参数 + where: JSON.stringify({ // 可选, 参数值应经过 JSON 编码为 JSONString 后,再经过 URL 编码 + "user": {"$eq": "5bf4f7457fed8d6c2f5c3d6e"} + }), + order_by: 'id', // 可选 + offset: 0, // 可选 + limit: 20, // 可选 + } +} + +request(opt, function(err, res, body) { + console.log(body) +}) +``` + +{% content "second" %} + +```python +import json +import urllib + +import requests + + +table_id = '' +BASE_API = r'https://cloud.minapp.com/userve/v1.8/table/%s/record/' % table_id + +TOKEN = '' +HEADERS = { + 'Authorization': 'Bearer %s' % TOKEN +} + +where_ = { + 'user': {'$eq': "5bf4f7457fed8d6c2f5c3d6e"}, +} + +query_ = urllib.urlencode({ + 'where': json.dumps(where_), + 'order_by': '-id', + 'limit': 10, + 'offset': 0, +}) + +API = '?'.join((BASE_API, query_)) + +resp_ = requests.get(API, headers=HEADERS) +print resp_.content +``` + +{% content "third" %} + +```php + '-id', + 'where' => json_encode(['user' => ['$eq' => '5bf4f7457fed8d6c2f5c3d6e']]), + 'limit' => '10', + 'offset' => '0', +); +$url = "https://cloud.minapp.com/userve/v1.8/table/{$table_id}/record/?"; +$url .= http_build_query($condition); + +$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, 'GET'); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + +$res = curl_exec($ch); +curl_close($ch); +``` + +{% endtabs %} + + +## 字段扩展 + +支持 expand created_by 以及 pointer 类型字段。返回结果中的 created_by 以及扩展的 pointer 类型字段会被替换为这个字段对应的完整对象。 + +**接口** + +`GET https://cloud.minapp.com/userve/v1.8/table/:table_id/record/?expand=pointer,created_by` + +其中 `:table_id` 需替换为你的数据表 ID + +### expand 返回结果示例 +不使用 expand +```json +{ + "created_at": 1516118400, + "created_by": 1234, + "id": "5a2fa9b008443e59e0e67829", + "name": "小米无线耳机", + "price": 199, + "pointer": {"id": "5a2fa9b008443e59e0e67889", "_table": "pointer"} +} +``` + +使用 expand +```json +{ + "created_at": 1516118400, + "created_by": { + "avatar": "https://media.ifanrusercontent.com/tavatar/fb/cd/xxxx.jpg", + "id": 62536607, + "nickname": "Larry。" + }, + "id": "5a2fa9b008443e59e0e67829", + "name": "小米无线耳机", + "price": 199, + "pointer": { + "id": "5a2fa9b008443e59e0e67889", + "_table": "pointer", + "customized_field": "field_content" + } +} +``` + +**代码示例** + +{% tabs first="Node", second="Python", third="PHP" %} + +{% content "first" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/userve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + qs: { // query string, 被附加到uri的参数 + where: JSON.stringify({ // 可选, 参数值应经过 JSON 编码为 JSONString 后,再经过 URL 编码 + "price": {"$eq": 10} + }), + order_by: 'id', // 可选 + offset: 0, // 可选 + limit: 20, // 可选 + expand: pointer,created_by, //必选,表示需要扩展的字段 + } +} + +request(opt, function(err, res, body) { + console.log(body) +}) +``` + +{% content "second" %} + +```python +import json +import urllib + +import requests + + +table_id = '' +BASE_API = r'https://cloud.minapp.com/userve/v1.8/table/%s/record/' % table_id + +TOKEN = '' +HEADERS = { + 'Authorization': 'Bearer %s' % TOKEN +} + +where_ = { + 'price': {'$gt': 100}, +} + +query_ = urllib.urlencode({ + 'where': json.dumps(where_), + 'order_by': '-id', + 'limit': 10, + 'offset': 0, + 'expand': 'pointer,created_by' +}) + +API = '?'.join((BASE_API, query_)) + +resp_ = requests.get(API, headers=HEADERS) +print resp_.content +``` + +{% content "third" %} + +```php + '-id', + 'where' => json_encode(['price' => ['$gt' => 'test search']]), + 'limit' => '10', + 'offset' => '0', + 'expand' => 'pointer,created_by' +); +$url = "https://cloud.minapp.com/userve/v1.8/table/{$table_id}/record/?"; +$url .= http_build_query($condition); + +$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, 'GET'); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + +$res = curl_exec($ch); +curl_close($ch); +``` + +{% endtabs %} + + +## 添加 pointer 类型数据 + +**接口** + +`POST https://cloud.minapp.com/userve/v1.8/table/:table_id/record/` + +其中 `:table_id` 需替换为你的数据表 ID + +**代码示例** + +{% tabs insertNode="Node", insertPHP="PHP" %} + +{% content "insertNode" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/userve/v1.8/table/3906/record/', // 3906 对应 :table_id + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, + json: { // 指定 data 以 "Content-Type": 'application/json' 传送 + name: 'nickname', + desc: ['description'], + price: 19, + amount: 19, + code: '18814098707' + pointer: '5a2fa9b008443e59e0e67889', // pointer 关联的数据 ID + } +} + +request(opt, function(err, res, body) { + console.log(res.statusCode) +}) +``` + +{% content "insertPHP" %} +```php + '5a2fa9b008443e59e0e67889' +); +$url = "https://cloud.minapp.com/userve/v1.8/table/{$table_id}/record/"; + +$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_POST, true); +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); + +``` + + +{% endtabs %} + +**状态码说明** + +`201` 写入成功,`400` 请求参数有错 + + +## 更新 pointer 类型数据 + +**接口** + +`PUT https://cloud.minapp.com/userve/v1.8/table/:table_id/record/:record_id/` + +其中 `:table_id` 需替换为你的数据表 ID,`record_id` 需替换为你的记录 ID + +**代码示例** + +{% tabs updateNode="Node", updatePHP="PHP" %} + +{% content "updateNode" %} + +```js +var request = require('request'); + +var opt = { + uri: 'https://cloud.minapp.com/userve/v1.8/table/3906/record/5a6ee2ab4a7baa1fc083e3xx', // 3906 对应 :table_id, 5a6ee2ab4a7baa1fc083e3xx 对应 :record_id + method: 'PUT', + headers: { + Authorization: `Bearer ${token}`, + }, + json: { // 指定 data 以 "Content-Type": 'application/json' 传送 + pointer: '5a2fa9b008443e59e0e67889', // pointer 关联的数据 ID + } +} + +request(opt, function(err, res, body) { + console.log(res.statusCode) +}) +``` + +{% content "updatePHP" %} + +```php +