导入
创建表
学习如何使用 HubDB API 创建数据库表。
代码示例
1https://api.hubapi.com/cms/v3/hubdb/tables
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| name | string | 必填 | 表的内部名称。表创建后此值无法更改。 |
| label | string | 必填 | 用户可见的表标签 |
| useForPages | boolean | 可选 | 指定表数据是否可用于创建动态页面 |
| columns | array[object] | 可选 | 包含列详细信息的数组。包括名称、标签、类型和选项。 |
1import fetch from "node-fetch"; 2 3const endpointUrl = new URL("https://api.hubapi.com/cms/v3/hubdb/tables"); 4 5const headerParameters = { 6 Authorization: "Bearer {{ACCESS_TOKEN}}", 7 "Content-Type": "application/json", 8}; 9 10// 定义请求体 11const bodyParameters = JSON.stringify({ 12 name: "fruits", 13 label: "Fruits", 14 useForPages: true, 15 columns: [ 16 { 17 name: "id", 18 label: "ID", 19 type: "TEXT", 20 }, 21 { 22 name: "name", 23 label: "Name", 24 type: "TEXT", 25 }, 26 { 27 name: "color", 28 label: "Color", 29 type: "TEXT", 30 }, 31 { 32 name: "flavor", 33 label: "Flavor", 34 type: "TEXT", 35 }, 36 ], 37}); 38 39const options = { 40 method: "POST", 41 headers: headerParameters, 42 body: bodyParameters, 43}; 44 45async function createTable() { 46 try { 47 const response = await fetch(endpointUrl, options); 48 printResponse(response); 49 } catch (error) { 50 printError(error); 51 } 52} 53 54createTable();
响应字段
| 名称 | 类型 | 描述 |
|---|---|---|
| id | string | 发布表的 ID |
| name | string | 发布表的名称 |
| label | string | 发布表的标签 |
| publishedAt | string | 表发布的时间 |
| createdAt | string | 表创建的时间 |
| updatedAt | string | 表最后一次更新的时间 |
| columns | object | 包含表中列的详细信息 |
| archived | boolean | 表已归档时为 true |
| rowCount | integer | 表中的总行数,包含已删除的行 |
| allowPublicApiAccess | boolean | 公共用户无需授权即可读取表时为 true |
| useForPages | boolean | 表数据可用于创建动态页面时为 true |
| enableChildTablePages | boolean | 可通过子表创建多层级动态页面时为 true |
| enableChildTables | boolean | 可创建子表时为 true |
| columnCount | integer | 表中的总列数,包含已删除的列 |
代码示例
1https://api.hubspot.com/cms/v3/hubdb/tables/{tableIdOrName}/rows
请求参数
| 参数 | 类型 | 参数类型 | 分类 | 描述 |
|---|---|---|---|---|
| tableID | string | 路径 | 必填 | 要添加行的表的 ID |
| values | object | 正文 | 必填 | 用于向表中插入数据的对象。以键值对格式定义 |
| path | string | 正文 | 可选 | 如果启用了动态页面,此表所使用的页面路径 |
| name | string | 正文 | 可选 | 如果启用了动态页面,此表所使用的页面名称 |
| childTableId | string | 正文 | 可选 | 如果表用于创建多层级动态页面,则为子表的 ID |
1import fetch from "node-fetch"; 2 3const endpointUrl = new URL("https://api.hubspot.com/cms/v3/hubdb/tables/{{TABLE_ID}}/rows"); 4 5const headerParameters = { 6 Authorization: "Bearer {{ACCESS_TOKEN}}", 7 "Content-Type": "application/json", 8}; 9 10// 定义请求体 11const bodyParameters = JSON.stringify({ 12 values: { 13 id: "1", 14 name: "mango", 15 color: "yellow", 16 flavor: "sweet", 17 }, 18}); 19 20const options = { 21 method: "POST", 22 headers: headerParameters, 23 body: bodyParameters, 24}; 25 26async function addRow() { 27 try { 28 const response = await fetch(endpointUrl, options); 29 printResponse(response); 30 } catch (error) { 31 printError(error); 32 } 33} 34 35addRow();
响应字段
| 属性 | 类型 | 描述 |
|---|---|---|
| id | string | 行的唯一 ID |
| createdAt | string | 行创建的时间 |
| updatedAt | string | 行最后一次更新的时间 |
| values | object | 添加到行中的数据 |
| path | string | 如果启用了动态页面,此表所使用的页面路径 |
| name | string | 如果启用了动态页面,此表所使用的页面名称 |
| childTableId | string | 如果表用于创建多层级动态页面,则为子表的 ID |
代码示例
1https://api.hubapi.com/cms/v3/hubdb/tables/{tableIdOrName}/draft/publish
请求参数
| 参数 | 类型 | 参数类型 | 分类 | 描述 |
|---|---|---|---|---|
| tableIdOrName | string | 路径 | 必填 | 要发布的表的名称或 ID |
| includeForeignIds | boolean | 正文 | 可选 | 如果希望在响应中包含外键 ID 的值,则为 true |
1import fetch from "node-fetch"; 2 3const endpointUrl = new URL("https://api.hubapi.com/cms/v3/hubdb/tables/{{TABLE_ID}}/draft/publish"); 4 5const headerParameters = { 6 Authorization: "Bearer {{ACCESS_TOKEN}}", 7 "Content-Type": "application/json", 8}; 9 10const options = { 11 method: "POST", 12 headers: headerParameters, 13}; 14 15async function PublishTable() { 16 try { 17 const response = await fetch(endpointUrl, options); 18 printResponse(response); 19 } catch (error) { 20 printError(error); 21 } 22} 23 24PublishTable();
响应字段
| 名称 | 类型 | 描述 |
|---|---|---|
| id | string | 发布表的 ID |
| name | string | 发布表的名称 |
| label | string | 发布表的标签 |
| columns.id | string | 列的 ID |
| columns.name | string | 列的名称 |
| columns.label | string | 列的标签 |
| columns.foreignIdsByID | object | 当存在外键表时,包含其 ID |
| columns.foreignIdsByName | object | 当存在外键表时,包含其名称 |
| columns.archived | boolean | 表已归档时为 true |
| columns.type | string | 列的数据类型 |
| published | boolean | 表是否已发布 |
| columnCount | integer | 表中的总列数,包含已删除的列 |
| rowCount | integer | 表中的总行数,包含已删除的行 |
| createdBy | object | 包含创建该表的账户信息 |
| updatedBy | object | 包含最近更新该表的账户信息 |
| publishedAt | string | 表发布的时间 |
| createdAt | string | 表创建的时间 |
| archived | boolean | 表已归档时为 true |
| allowPublicApiAccess | boolean | 公共用户无需授权即可读取表时为 true |
| useForPages | boolean | 表数据可用于创建动态页面时为 true |
| enableChildTablePages | boolean | 可通过子表创建多层级动态页面时为 true |
| enableChildTables | boolean | 可创建子表时为 true |
| updatedAt | string | 表最后一次更新的时间 |