本节介绍管理图中Schema与属性的方法。
Schema
showSchema()
获取图中全部Schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
Schema[]
:获取的Schema列表。
// Retrieves all schemas in the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schemas = await conn.showSchema(requestConfig)
schemas.forEach((schema:any) => {
const typeLabel = schema.dbType === 0? 'DBNODE' : 'DBEDGE';
console.log(`${schema.name}, ${typeLabel}`)
})
default, DBNODE
account, DBNODE
celebrity, DBNODE
country, DBNODE
movie, DBNODE
default, DBEDGE
direct, DBEDGE
disagree, DBEDGE
filmedIn, DBEDGE
follow, DBEDGE
rate, DBEDGE
wishlist, DBEDGE
response, DBEDGE
agree, DBEDGE
review, DBEDGE
showNodeSchema()
获取图中全部点Schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
Schema[]
:获取的Schema列表。
// Retrieves all node schemas in the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schemas = await conn.showNodeSchema(requestConfig)
schemas.forEach((schema:any) => {
const typeLabel = schema.dbType === 0? 'DBNODE' : 'DBEDGE';
console.log(`${schema.name}, ${typeLabel}`)
})
default, DBNODE
account, DBNODE
celebrity, DBNODE
country, DBNODE
movie, DBNODE
showEdgeSchema()
获取图中全部边Schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
Schema[]
:获取的Schema列表。
// Retrieves all edge schemas in the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schemas = await conn.showEdgeSchema(requestConfig)
schemas.forEach((schema:any) => {
const typeLabel = schema.dbType === 0? 'DBNODE' : 'DBEDGE';
console.log(`${schema.name}, ${typeLabel}`)
})
default, DBEDGE
direct, DBEDGE
disagree, DBEDGE
filmedIn, DBEDGE
follow, DBEDGE
rate, DBEDGE
wishlist, DBEDGE
response, DBEDGE
agree, DBEDGE
review, DBEDGE
getSchema()
获取图中一个指定的Schema。
参数
schemaName: string
:Schema名称。dbType: DBType
:Schema类型(点或边)。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的Schema。
// Retrieves the node schema named 'account'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schema = await conn.getSchema("account",DBType.DBNODE,requestConfig)
console.log(schema.total)
111
getNodeSchema()
获取图中一个指定的点Schema。
参数
schemaName: string
:Schema名称。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的Schema。
// Retrieves the node schema named 'account'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schema = await conn.getNodeSchema("account", requestConfig);
if (schema) {
schema.properties?.forEach((property: any) => {
console.log(property.name);
});
} else {
console.log("Not found");
}
gender
year
industry
name
getEdgeSchema()
获取图中一个指定的边Schema。
参数
schemaName: string
:Schema名称。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的Schema。
// Retrieves the edge schema named 'disagree'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schema = await conn.getEdgeSchema("disagree", requestConfig);
if (schema) {
schema.properties?.forEach((property: any) => {
console.log(property.name);
});
} else {
console.log("Not found");
}
datetime
timestamp
targetPost
createSchema()
在图中创建一个Schema。
参数
schema: Schema
待创建的schema;name
和dbType
属性必填,properties
和description
可选。isCreateProperties: boolean
(可选):是否创建属性,默认为false
。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
const requestConfig: ULTIPA.RequestConfig = {
graph: "miniCircle",
};
// Creates node schema 'utility' (with properties)
const utility = {
name: "utility",
dbType: DBType.DBNODE,
properties: [
{ name: "name", type: ULTIPA.UltipaPropertyType.STRING, schema: "utility"},
{ name: "type", type: ULTIPA.UltipaPropertyType.UINT32, schema: "utility"},
]
}
const response1 = await conn.createSchema(utility, true, requestConfig);
console.log(response1.status?.message);
await new Promise((resolve) => setTimeout(resolve, 3000));
// Creates edge schema 'vote' (without properties)
const vote: Schema = { name: "vote", dbType: DBType.DBEDGE };
const response2 = await conn.createSchema(vote, false, requestConfig);
console.log(response2.status?.message);
SUCCESS
SUCCESS
createSchemaIfNotExist()
在图中创建一个Schema,并返回是否图中已有同名点或边Schema存在。
参数
schema: Schema
:待创建的schema;name
和dbType
属性必填,properties
和description
可选。isCreateProperties: boolean
(可选):是否创建属性,默认为false
.config: RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。
requestConfig = RequestConfig(graph="miniCircle")
const schema = {
name: "utility",
dbType: DBType.DBNODE,
properties: [
{ name: "name", type: ULTIPA.UltipaPropertyType.STRING, schema: "utility"},
{ name: "type", type: ULTIPA.UltipaPropertyType.UINT32, schema: "utility"},
]
}
const result = await conn.createSchemaIfNotExist(schema, true, requestConfig);
console.log("Does the schema already exist?", result.exist);
if(result.exist){console.log("Schema creation status: No response")} else {console.log("Schema creation status:", result.response.status?.message)}
console.log("----- Creates the schema again -----")
const result_1 = await conn.createSchemaIfNotExist(schema,true,requestConfig)
console.log("Does the schema already exist?", result_1.exist);
if(result_1.exist){console.log("Schema creation status: No response")} else {console.log("Schema creation status:", result_1.response.status?.message)}
};
Does the schema already exist? false
Schema creation status: SUCCESS
----- Creates the schema again -----
Does the schema already exist? true
Schema creation status: No response
alterSchema()
修改图中一个Schema的名称和描述。
参数
originalSchema: Schema
:待修改的Schema;name
和dbType
属性必填。newSchema: Schema
:用于设置新的Schemaname
和/或description
的Schema
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Renames the node schema 'utility' to 'securityUtility' in the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const oldSchema: Schema = { name: "utility", dbType: DBType.DBNODE };
const newSchema = new ULTIPA.Schema();
newSchema.name = "securityUtility";
const response = await conn.alterSchema(oldSchema, newSchema, requestConfig);
console.log(response.status?.message);
SUCCESS
dropSchema()
从图中删除一个指定的Schema。
参数
schema: Schema
:待删除的Schema;name
和dbType
属性必填。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Drops the edge schema 'vote' from the graph 'miniCircle'
const requestConfig: RequestConfig = {
graph: "miniCircle",
};
const schema: Schema = { name: "vote", dbType: DBType.DBEDGE };
const response = await conn.dropSchema(schema,requestConfig);
console.log(response.status?.message);
SUCCESS
Property
showProperty()
获取图中全部属性。
参数
dbType: DBType
(可选):属性类型(点或边)。schemaName: string
(可选):Schema名称。config: RequestConfig
(可选):请求配置。
返回值
AllProperties
:一个包含了两个Property
对象列表的类,分别为nodeProperties
和edgeProperties
。
// Retrieves all properties in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const properties = await conn.showProperty(undefined,undefined,requestConfig)
console.log("Node properties:")
properties.nodeProperties.forEach((item) => {
console.log(`${item.name} is associated with schema ${item.schema}`)
})
console.log("Edge properties:")
properties.edgeProperties.forEach((item) => {
console.log(`${item.name} is associated with schema ${item.schema}`)
})
Node Properties:
_id is associated with schema default
_id is associated with schema Paper
title is associated with schema Paper
score is associated with schema Paper
author is associated with schema Paper
Edge Properties:
weight is associated with schema Cites
showNodeProperty()
获取图中全部点属性。
参数
schemaName: string
(可选):Schema名称。config: RequestConfig
(可选):请求配置。
返回值
Property[]
:获取的属性列表。
// Retrieves properties associated with the node schema 'Paper' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const properties = await conn.showNodeProperty("Paper",requestConfig)
for (const property of properties){
console.log(`${property.name} - ${property.type}` )
}
_id - 7
uuid - 1
title - 7
score - 1
author - 7
在嬴图Node.js的SDK中,1
代表数据类型为INT32,7
代表数据类型为STRING。
showEdgeProperty()
获取图中全部边属性。
参数
schemaName: string
(可选):Schema名称。config: RequestConfig
(可选):请求配置。
返回值
Property[]
:获取的属性列表。
// Retrieves properties associated with the edge schema 'Cites' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const properties = await conn.showEdgeProperty("Cites",requestConfig)
for (const property of properties){
console.log(`${property.name} - ${property.type}` )
}
uuid - 1
fromUuid - 1
toUuid - 1
weight - 1
在嬴图Node.js的SDK中,1
代表数据类型为INT32。
getProperty()
获取图中一个指定的属性。
参数
dbType: DBType
:属性类型(点或边)。schemaName: string
:Schema名称。propertyName: string
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const property = await conn.getProperty(DBType.DBNODE,"Paper","title",requestConfig)
console.log(property)
{ name: 'title', type: 7, subType: undefined, lte: 'false', read: true, write: true, schema: 'Paper', description: '', encrypt: '', decimalExtra: undefined}
getNodeProperty()
获取图中一个指定的点属性。
参数
schemaName: string
:Schema名称。propertyName: string
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const property = await conn.getNodeProperty("Paper","title",requestConfig)
console.log(property)
{ name: 'title', type: 7, subType: undefined, lte: 'false', read: true, write: true, schema: 'Paper', description: '', encrypt: '', decimalExtra: undefined}
getEdgeProperty()
获取图中一个指定的边属性。
参数
schemaName: string
:Schema名称。propertyName: string
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves edge property 'weight' associated with the edge schema 'Cites' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const property = await conn.getEdgeProperty("Cites","weight",requestConfig)
console.log(property)
{ name: 'weight', type: 1, subType: undefined, lte: 'false', read: true, write: true, schema: 'Cites', description: '', encrypt: '', decimalExtra: undefined}
createProperty()
在图中创建一个属性。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待创建的属性;name
、type
(以及subType
,如果type
为SET
或LIST
)和schema
(用*
指定全部Schema)属性必填,encrypt
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Creates a property 'year' for all node schemas, creates a property 'tags' for the node schema 'Paper'
const requestConfig: RequestConfig = {
graph: "citation",
};
const property1:Property ={name:"year", type:UltipaPropertyType.UINT32,encrypt:"AES128",schema:"*"}
const property2:Property = {name:"tags", type:UltipaPropertyType.SET, subType:[UltipaPropertyType.STRING],schema:"Paper"}
const response1 = await conn.createProperty(DBType.DBNODE,property1,requestConfig)
console.log(response1.status?.message)
const response2 = await conn.createProperty(DBType.DBEDGE,property2,requestConfig)
console.log(response2.status?.message)
SUCCESS
SUCCESS
createPropertyIfNotExist()
在图中创建一个属性,并返回是否图中指定点或边Schema下已有同名属性存在。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待创建的属性;name
、type
(以及subType
,如果type
为SET
或LIST
)和schema
(用*
指定全部Schema)属性必填,encrypt
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。
const requestConfig: RequestConfig = {
graph: "citation",
};
const property: Property = {
name: "tags",
type: UltipaPropertyType.SET,
subType: [UltipaPropertyType.STRING],
encrypt: "AES128",
schema: "Paper",
};
const result = await conn.createPropertyIfNotExist(
DBType.DBNODE,
property,
requestConfig
);
console.log("Does the property already exist?", result.exist);
if (result.exist) {
console.log("Property creation status: No response");
} else {
console.log("Property creation status:", result.response.status?.message);
}
console.log("----- Creates the property again -----");
const result_1 = await conn.createPropertyIfNotExist(
DBType.DBNODE,
property,
requestConfig
);
console.log("Does the property already exist?", result_1.exist);
if (result_1.exist) {
console.log("Property creation status: No response");
} else {
console.log("Property creation status:", result_1.response.status?.message);
}
Does the property already exist? false
Property creation status: SUCCESS
----- Creates the property again -----
Does the property already exist? true
Property creation status: No response
alterProperty()
修改图中指定属性的名称和描述。
参数
dbType: DBType
:属性类型(点或边)。originProp: Property
:待修改的属性;name
和schema
属性必填(用*
可指定全部Schema)。newProp: Property
:用于设置新的属性name
和/或description
的Property
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Renames the property 'tags' of the node schema 'Paper' to 'keywords' in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const oldProperty = new Property();
oldProperty.name = "tags";
oldProperty.schema = "Paper"
const newProperty = new Property();
newProperty.name = "keywords"
const response = await conn.alterProperty(DBType.DBNODE,oldProperty,newProperty,requestConfig);
console.log(response.status?.message)
SUCCESS
dropProperty()
从图中删除指定的属性。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待删除的属性;name
和schema
属性必填(用*
可指定全部Schema)。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Drops the property 'tags' of the node schema in the graph 'citation'
const requestConfig: RequestConfig = {
graph: "citation",
};
const property = new Property();
property.name = "tags";
property.schema = "Paper";
const response = await conn.dropProperty(DBType.DBNODE, property, requestConfig);
console.log(response.status?.message);
SUCCESS
完整示例
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types";
import { GraphSet } from "@ultipa-graph/ultipa-driver/dist/types/types";
let sdkUsage = async () => {
// URI example: ultipaConfig.hosts: ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
const ultipaConfig: ULTIPA.UltipaConfig = {
hosts: ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"],
username: "<username>",
password: "<password>"
};
const conn = new UltipaDriver(ultipaConfig);
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
// Creates schemas and properties in the graph 'social'
const requestConfig: RequestConfig = {
graph: "social",
};
const user = {
name: "user",
dbType: DBType.DBNODE,
properties: [
{ name: "name", type: ULTIPA.UltipaPropertyType.STRING, schema: "user" },
{ name: "age", type: ULTIPA.UltipaPropertyType.INT32, schema: "user" },
{
name: "score",
type: ULTIPA.UltipaPropertyType.DECIMAL,
decimalExtra: {precision:25, scale:10},
schema: "user",
},
{
name: "birthday",
type: ULTIPA.UltipaPropertyType.DATETIME,
schema: "user",
},
{ name: "active", type: ULTIPA.UltipaPropertyType.BOOL, schema: "user" },
{
name: "location",
type: ULTIPA.UltipaPropertyType.POINT,
schema: "user",
},
{
name: "interests",
type: ULTIPA.UltipaPropertyType.LIST,
subType: [UltipaPropertyType.STRING],
schema: "user",
},
{
name: "permissionCodes",
type: ULTIPA.UltipaPropertyType.SET,
subType: [UltipaPropertyType.INT32],
schema: "user",
},
],
};
const product = {
name: "product",
dbType: DBType.DBNODE,
properties: [
{
name: "name",
type: ULTIPA.UltipaPropertyType.STRING,
schema: "product",
},
{
name: "price",
type: ULTIPA.UltipaPropertyType.FLOAT,
schema: "product",
},
],
};
const follows = {
name: "follows",
dbType: DBType.DBEDGE,
properties: [
{
name: "createdOn",
type: ULTIPA.UltipaPropertyType.TIMESTAMP,
schema: "follows",
},
{
name: "weight",
type: ULTIPA.UltipaPropertyType.FLOAT,
schema: "follows",
},
],
};
const purchased = {
name: "purchased",
dbType: DBType.DBEDGE,
};
const schemas = [user, product, follows, purchased];
for (const schema of schemas) {
const response = await conn.createSchema(schema, true, requestConfig);
console.log(response.status?.message);
}
};
sdkUsage().catch(console.error);