本节为您介绍如何使用Connection
对象的方法向图集插入或从图集删除点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
图数据模型示例
以下各示例为您展示如何在具有以下schema和属性的图集里插入或删除点和边:
Property Type Mapping
插入点或边时,需指定不同属性类型。嬴图属性类型和Node.js/驱动程序数据类型的映射关系如下:
嬴图支持的属性类型 | Node.js/驱动程序支持的数据类型 |
---|---|
int32 | number |
uint32 | number |
int64 | string |
uint64 | string |
float | string |
double | string |
decimal | string |
string | string |
text | string |
datetime | string |
timestamp | number |
point | string |
blob | Buffer |
list | Array |
set | Array |
Insertion
insertNodes()
向当前图集的某个schema插入新的点。
参数:
string
:schema名称。Node[]
: 待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个nodes
别名,所有插入的点均在其中。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入点的信息
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let resp = await conn.insertNodes(
"user",
[
{
_id: "U001",
_uuid: 1,
name: "Alice",
age: 18,
score: "65.32",
birthday: "1993-5-4",
location: `POINT(23.63 104)`,
profile: "abc",
interests: ["tennis", "violin"],
permissionCodes: [2004, 3025, 1025],
},
{ _id: "U002", _uuid: 2, name: "Bob" },
],
insertRequestConfig
);
console.log(resp.status.code_desc);
console.log(resp.data);
SUCCESS
[
Node { id: 'U001', uuid: '1', schema: 'user', values: {} },
Node { id: 'U002', uuid: '2', schema: 'user', values: {} }
]
insertEdges()
向当前图集的某个schema插入新的边。
参数:
string
:schema名称。Edge[]
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。 若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个edges
别名,所有插入的边均在其中。
// 向图集lcc名为follows的schema插入两条边,打印错误代码和插入边的信息
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: true,
graphSetName: "lcc",
useMaster: true,
};
let resp = await conn.insertEdges(
"follows",
[
{
_uuid: 1,
_from: "U001",
_to: "U002",
createdOn: "2024-5-6",
},
{
_uuid: 2,
_from: "U002",
_to: "U001",
createdOn: "2024-5-8",
},
],
insertRequestConfig
);
console.log(resp.status.code_desc);
console.log(resp.data);
SUCCESS
[
Edge {
from: 'U001',
to: 'U002',
uuid: '1',
from_uuid: '1',
to_uuid: '2',
schema: 'follows',
values: {}
},
Edge {
from: 'U002',
to: 'U001',
uuid: '2',
from_uuid: '2',
to_uuid: '1',
schema: 'follows',
values: {}
}
]
insertNodesBatchBySchema()
通过gRPC向当前图集的某个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。Node[]
:待插入的Node
列表。InsertRequestConfig
:配置请求。
返回值:
Response
:请求的结果。 若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入结果
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let proInfo: ULTIPA.Property[] = [];
let pro1: ULTIPA.Property = {
name: "name",
type: ULTIPA.PropertyType.string,
};
let pro2: ULTIPA.Property = {
name: "age",
type: ULTIPA.PropertyType.int32,
};
let pro3: ULTIPA.Property = {
name: "score",
type: ULTIPA.PropertyType.decimal,
};
let pro4: ULTIPA.Property = {
name: "birthday",
type: ULTIPA.PropertyType.datetime,
};
let pro5: ULTIPA.Property = {
name: "location",
type: ULTIPA.PropertyType.point,
};
let pro6: ULTIPA.Property = {
name: "profile",
type: ULTIPA.PropertyType.blob,
};
let pro7: ULTIPA.Property = {
name: "interests",
type: ULTIPA.PropertyType.list,
};
let pro8: ULTIPA.Property = {
name: "permissionCodes",
type: ULTIPA.PropertyType.set,
};
const pros = [pro1, pro2, pro3, pro4, pro5, pro6, pro7, pro8];
for (const item of pros) {
proInfo.push(item);
}
let nodeInfo1 = new ULTIPA.Node();
nodeInfo1.id = "U001";
nodeInfo1.uuid = "1";
nodeInfo1.set("name", "Alice");
nodeInfo1.set("age", 18);
nodeInfo1.set("score", "65.32");
nodeInfo1.set("birthday", "1993-5-4");
nodeInfo1.set("location", `POINT(23.63 104)`);
nodeInfo1.set("profile", "abc");
nodeInfo1.set("interests", ["tennis", "violin"]);
nodeInfo1.set("permissionCodes", [2004, 3025, 1025]);
let node1: ULTIPA.Node[] = [];
node1.push(nodeInfo1);
let insert1 = await conn.insertNodesBatchBySchema(
{ dbType: ULTIPA.DBType.DBNODE, name: "user", properties: proInfo },
node1,
insertRequestConfig
);
console.log(insert1.status.code_desc);
let nodeInfo2 = new ULTIPA.Node();
nodeInfo2.id = "U002";
nodeInfo2.uuid = "2";
nodeInfo2.set("name", "Bob");
nodeInfo2.set("age", null);
nodeInfo2.set("score", null);
nodeInfo2.set("birthday", null);
nodeInfo2.set("location", null);
nodeInfo2.set("profile", null);
nodeInfo2.set("interests", null);
nodeInfo2.set("permissionCodes", null);
let node2: ULTIPA.Node[] = [];
node2.push(nodeInfo2);
let insert2 = await conn.insertNodesBatchBySchema(
{ dbType: ULTIPA.DBType.DBNODE, name: "user", properties: proInfo },
node2,
insertRequestConfig
);
console.log(insert2.status.code_desc);
SUCCESS
SUCCESS
insertEdgesBatchBySchema()
通过gRPC向当前图集的某个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
Schema
: The target schema.Edge[]
: The list ofEdge
objects to be inserted.InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。 若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为follows的schema插入两条边,打印错误代码和插入结果
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let proInfo: ULTIPA.Property[] = [];
let pro: ULTIPA.Property = {
name: "createdOn",
type: ULTIPA.PropertyType.PROPERTY_TIMESTAMP,
};
proInfo.push(pro);
let edgeInfo1 = new ULTIPA.Edge();
edgeInfo1.uuid = "1";
edgeInfo1.from = "U001";
edgeInfo1.to = "U002";
edgeInfo1.set("createdOn", 1714953600);
let edge1: ULTIPA.Edge[] = [];
edge1.push(edgeInfo1);
let insert1 = await conn.insertEdgesBatchBySchema(
{ dbType: ULTIPA.DBType.DBEDGE, name: "follows", properties: proInfo },
edge1,
insertRequestConfig
);
console.log(insert1.status.code_desc);
let edgeInfo2 = new ULTIPA.Edge();
edgeInfo2.uuid = "2";
edgeInfo2.from = "U002";
edgeInfo2.to = "U001";
edgeInfo2.set("createdOn", 1715126400);
let edge2: ULTIPA.Edge[] = [];
edge2.push(edgeInfo2);
let insert2 = await conn.insertEdgesBatchBySchema(
{ dbType: ULTIPA.DBType.DBEDGE, name: "follows", properties: proInfo },
edge2,
insertRequestConfig
);
console.log(insert2.status.code_desc);
SUCCESS
SUCCESS
insertNodesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
Node[]
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。 若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为user和product的两个schema各插入一个点,打印错误代码和插入结果
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let nodeInfo1 = new ULTIPA.Node();
nodeInfo1.schema = "user";
nodeInfo1.id = "U001";
nodeInfo1.uuid = "1";
nodeInfo1.set("name", "Alice");
nodeInfo1.set("age", 18);
nodeInfo1.set("score", "65.32");
nodeInfo1.set("birthday", "1993-5-4");
nodeInfo1.set("location", `POINT(23.63 104)`);
nodeInfo1.set("profile", "abc");
nodeInfo1.set("interests", ["tennis", "violin"]);
nodeInfo1.set("permissionCodes", [2004, 3025, 1025]);
let node1: ULTIPA.Node[] = [];
node1.push(nodeInfo1);
let insert1 = await conn.insertNodesBatchAuto(node1, insertRequestConfig);
console.log(insert1.status.code_desc);
let nodeInfo2 = new ULTIPA.Node();
nodeInfo2.schema = "user";
nodeInfo2.id = "U002";
nodeInfo2.uuid = "2";
nodeInfo2.set("name", "Bob");
nodeInfo2.set("age", null);
nodeInfo2.set("score", null);
nodeInfo2.set("birthday", null);
nodeInfo2.set("location", null);
nodeInfo2.set("profile", null);
nodeInfo2.set("interests", null);
nodeInfo2.set("permissionCodes", null);
let node2: ULTIPA.Node[] = [];
node2.push(nodeInfo2);
let insert2 = await conn.insertNodesBatchAuto(node2, insertRequestConfig);
console.log(insert2.status.code_desc);
let nodeInfo3 = new ULTIPA.Node();
nodeInfo3.schema = "product";
nodeInfo3.id = "P001";
nodeInfo3.uuid = "3";
nodeInfo3.set("name", "Wireless Earbud");
nodeInfo3.set("price", 93.2);
let node3: ULTIPA.Node[] = [];
node3.push(nodeInfo3);
let insert3 = await conn.insertNodesBatchAuto(node3, insertRequestConfig);
console.log(insert3.status.code_desc);
SUCCESS
SUCCESS
SUCCESS
insertEdgesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
Edge[]
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为follows的schema插入两条边,向名为purchased的schema插入一条边,打印错误代码和插入结果
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let edgeInfo1 = new ULTIPA.Edge();
edgeInfo1.schema = "follows";
edgeInfo1.uuid = "1";
edgeInfo1.from = "U001";
edgeInfo1.to = "U002";
edgeInfo1.set("createdOn", 1714953600);
let edge1: ULTIPA.Edge[] = [];
edge1.push(edgeInfo1);
let insert1 = await conn.insertEdgesBatchAuto(edge1, insertRequestConfig);
console.log(insert1.status.code_desc);
let edgeInfo2 = new ULTIPA.Edge();
edgeInfo2.schema = "follows";
edgeInfo2.uuid = "2";
edgeInfo2.from = "U002";
edgeInfo2.to = "U001";
edgeInfo2.set("createdOn", 1715126400);
let edge2: ULTIPA.Edge[] = [];
edge2.push(edgeInfo2);
let insert2 = await conn.insertEdgesBatchAuto(edge2, insertRequestConfig);
console.log(insert2.status.code_desc);
let edgeInfo3 = new ULTIPA.Edge();
edgeInfo3.schema = "purchased";
edgeInfo3.uuid = "3";
edgeInfo3.from = "U002";
edgeInfo3.to = "P001";
edgeInfo3.set("qty", 1);
let edge3: ULTIPA.Edge[] = [];
edge3.push(edgeInfo3);
let insert3 = await conn.insertEdgesBatchAuto(edge3, insertRequestConfig);
console.log(insert3.status.code_desc);
SUCCESS
SUCCESS
SUCCESS
Deletion
deleteNodes()
从图集删除符合指定条件的点。需特别注意,删除点的同时也会删除与点相连的边。
参数:
- `string:待删除的点的条件。
InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个nodes
别名,所有删除的点均在其中。
// 删除图集lcc里@user下名为Alice的点,打印错误代码和已删除的点的信息
// 与删除的点相连的边也会全部删除
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let resp = await conn.deleteNodes(
"{@user.name == 'Alice'}",
insertRequestConfig
);
console.log(resp.status.code_desc);
SUCCESS
deleteEdges()
从图集删除符合指定条件的边。
参数:
string
:待删除的边的条件。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个edges
别名,所有删除的边均在其中。
// 从图集lcc删除@purchased中的全部边,打印错误代码和已删除的边的信息
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
let resp = await conn.deleteEdges("{@purchased}", insertRequestConfig);
console.log(resp.status.code_desc);
SUCCESS
完整示例
import { ConnectionPool, ULTIPA } from "@ultipa-graph/ultipa-node-sdk";
import { GraphExra } from "@ultipa-graph/ultipa-node-sdk/dist/connection/extra/graph.extra";
import { getEdgesPrintInfo } from "@ultipa-graph/ultipa-node-sdk/dist/printers/edge";
import { RequestType } from "@ultipa-graph/ultipa-node-sdk/dist/types";
import { ListFormat } from "typescript";
let sdkUsage = async () => {
// 设置连接
//URI示例: hosts="mqj4zouys.us-east-1.cloud.ultipa.com:60010"
let hosts = [
"192.168.1.85:60061",
"192.168.1.86:60061",
"192.168.1.87:60061",
];
let username = "***";
let password = "***";
let connPool = new ConnectionPool(hosts, username, password);
// 建立与数据库的连接
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
// 配置插入请求
let insertRequestConfig = <RequestType.InsertRequestConfig>{
insertType: ULTIPA.InsertType.INSERT_TYPE_NORMAL,
silent: false,
graphSetName: "lcc",
useMaster: true,
};
// 向图集lcc名为user的schema插入两个点,向名为product的schema插入一个点,打印错误代码和插入报告
let nodeInfo1 = new ULTIPA.Node();
nodeInfo1.schema = "user";
nodeInfo1.id = "U001";
nodeInfo1.uuid = "1";
nodeInfo1.set("name", "Alice");
nodeInfo1.set("age", 18);
nodeInfo1.set("score", "65.32");
nodeInfo1.set("birthday", "1993-5-4");
nodeInfo1.set("location", `POINT(23.63 104)`);
nodeInfo1.set("profile", "abc");
nodeInfo1.set("interests", ["tennis", "violin"]);
nodeInfo1.set("permissionCodes", [2004, 3025, 1025]);
let node1: ULTIPA.Node[] = [];
node1.push(nodeInfo1);
let insert1 = await conn.insertNodesBatchAuto(node1, insertRequestConfig);
console.log(insert1.status.code_desc);
let nodeInfo2 = new ULTIPA.Node();
nodeInfo2.schema = "user";
nodeInfo2.id = "U002";
nodeInfo2.uuid = "2";
nodeInfo2.set("name", "Bob");
nodeInfo2.set("age", null);
nodeInfo2.set("score", null);
nodeInfo2.set("birthday", null);
nodeInfo2.set("location", null);
nodeInfo2.set("profile", null);
nodeInfo2.set("interests", null);
nodeInfo2.set("permissionCodes", null);
let node2: ULTIPA.Node[] = [];
node2.push(nodeInfo2);
let insert2 = await conn.insertNodesBatchAuto(node2, insertRequestConfig);
console.log(insert2.status.code_desc);
let nodeInfo3 = new ULTIPA.Node();
nodeInfo3.schema = "product";
nodeInfo3.id = "P001";
nodeInfo3.uuid = "3";
nodeInfo3.set("name", "Wireless Earbud");
nodeInfo3.set("price", 93.2);
let node3: ULTIPA.Node[] = [];
node3.push(nodeInfo3);
let insert3 = await conn.insertNodesBatchAuto(node3, insertRequestConfig);
console.log(insert3.status.code_desc);
};
sdkUsage().then(console.log).catch(console.log);