Logo
Section 1 / 3

创建表

学习如何使用 HubDB API 创建数据库表。

导入

创建表

学习如何使用 HubDB API 创建数据库表。

代码示例

1https://api.hubapi.com/cms/v3/hubdb/tables

请求参数

参数类型分类描述
namestring必填表的内部名称。表创建后此值无法更改。
labelstring必填用户可见的表标签
useForPagesboolean可选指定表数据是否可用于创建动态页面
columnsarray[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();

响应字段

名称类型描述
idstring发布表的 ID
namestring发布表的名称
labelstring发布表的标签
publishedAtstring表发布的时间
createdAtstring表创建的时间
updatedAtstring表最后一次更新的时间
columnsobject包含表中列的详细信息
archivedboolean表已归档时为 true
rowCountinteger表中的总行数,包含已删除的行
allowPublicApiAccessboolean公共用户无需授权即可读取表时为 true
useForPagesboolean表数据可用于创建动态页面时为 true
enableChildTablePagesboolean可通过子表创建多层级动态页面时为 true
enableChildTablesboolean可创建子表时为 true
columnCountinteger表中的总列数,包含已删除的列

代码示例

1https://api.hubspot.com/cms/v3/hubdb/tables/{tableIdOrName}/rows

请求参数

参数类型参数类型分类描述
tableIDstring路径必填要添加行的表的 ID
valuesobject正文必填用于向表中插入数据的对象。以键值对格式定义
pathstring正文可选如果启用了动态页面,此表所使用的页面路径
namestring正文可选如果启用了动态页面,此表所使用的页面名称
childTableIdstring正文可选如果表用于创建多层级动态页面,则为子表的 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();

响应字段

属性类型描述
idstring行的唯一 ID
createdAtstring行创建的时间
updatedAtstring行最后一次更新的时间
valuesobject添加到行中的数据
pathstring如果启用了动态页面,此表所使用的页面路径
namestring如果启用了动态页面,此表所使用的页面名称
childTableIdstring如果表用于创建多层级动态页面,则为子表的 ID

代码示例

1https://api.hubapi.com/cms/v3/hubdb/tables/{tableIdOrName}/draft/publish

请求参数

参数类型参数类型分类描述
tableIdOrNamestring路径必填要发布的表的名称或 ID
includeForeignIdsboolean正文可选如果希望在响应中包含外键 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();

响应字段

名称类型描述
idstring发布表的 ID
namestring发布表的名称
labelstring发布表的标签
columns.idstring列的 ID
columns.namestring列的名称
columns.labelstring列的标签
columns.foreignIdsByIDobject当存在外键表时,包含其 ID
columns.foreignIdsByNameobject当存在外键表时,包含其名称
columns.archivedboolean表已归档时为 true
columns.typestring列的数据类型
publishedboolean表是否已发布
columnCountinteger表中的总列数,包含已删除的列
rowCountinteger表中的总行数,包含已删除的行
createdByobject包含创建该表的账户信息
updatedByobject包含最近更新该表的账户信息
publishedAtstring表发布的时间
createdAtstring表创建的时间
archivedboolean表已归档时为 true
allowPublicApiAccessboolean公共用户无需授权即可读取表时为 true
useForPagesboolean表数据可用于创建动态页面时为 true
enableChildTablePagesboolean可通过子表创建多层级动态页面时为 true
enableChildTablesboolean可创建子表时为 true
updatedAtstring表最后一次更新的时间

页面

获取表格