This section introduces methods for managing schemas and properties in a graph.
Schema
showSchema()
Retrieves all schemas from the graph.
Parameters
config: RequestConfig
(Optional): Request configuration.
Returns
Schema[]
: The list of retrieved schemas.
// 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()
Retrieves all node schemas from the graph.
Parameters
config: RequestConfig
(Optional): Request configuration.
Returns
Schema[]
: The list of retrieved schemas.
// 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()
Retrieves all edge schemas from the graph.
Parameters
config: RequestConfig
(Optional): Request configuration.
Returns
Schema[]
: The list of retrieved schemas.
// 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()
Retrieves a specified schema from the graph.
Parameters
schemaName: string
: Name of the schema.dbType: DBType
: Type of the schema (node or edge).config: RequestConfig
(Optional): Request configuration.
Returns
Schema
: The retrieved 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()
Retrieves a specified node schema from the graph.
Parameters
schemaName: string
: Name of the schema.config: RequestConfig
(Optional): Request configuration.
Returns
Schema
: The retrieved 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()
Retrieves a specified edge schema from the graph.
Parameters
schemaName: string
: Name of the schema.config: RequestConfig
(Optional): Request configuration.
Returns
Schema
: The retrieved 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()
Creates a schema in the graph.
Parameters
schema: Schema
: The schema to be created; the attributesname
anddbType
are mandatory,properties
anddescription
are optional.isCreateProperties: boolean
(Optional): Whether to create properties associated with the schema, the default isfalse
.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
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()
Creates a schema in the graph and returns whether a node or edge schema with the same name already exists.
Parameters
schema: Schema
: The schema to be created; the attributesname
anddbType
are mandatory,properties
anddescription
are optional.isCreateProperties: boolean
(Optional): Whether to create properties associated with the schema, the default isFalse
.config: RequestConfig
(Optional): Request configuration.
Returns
ResponseWithExistCheck
: Result of the request.
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()
Alters the name and description a schema in the graph.
Parameters
originalSchema: Schema
: The schema to be altered; the attributesname
anddbType
are mandatory.newSchema: Schema
: ASchema
object used to set the newname
and/ordescription
for the schema.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// 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()
Deletes a specified schema from the graph.
Parameters
schema: Schema
: The schema to be dropped; the attributesname
anddbType
are mandatory.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// 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()
Retrieves properties from the graph.
Parameters
dbType: DBType
(Optional): Type of the property (node or edge).schemaName: string
(Optional): Name of the schema.config: RequestConfig
(Optional): Request configuration.
Returns
AllProperties
: An object that contains two lists:nodeProperties
andedgeProperties
, both of which are lists ofProperty
objects.
// 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()
Retrieves node properties from the graph.
Parameters
schemaName: string
(Optional): Name of the schema.config: RequestConfig
(Optional): Request configuration.
Returns
Property[]
: The list of retrieved properties.
// 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
In Ultipa's Node.js SDK, 1
indicates INT32, and 7
indicates STRING.
showEdgeProperty()
Retrieves edge properties from the graph.
Parameters
schemaName: string
(Optional): Name of the schema.config: RequestConfig
(Optional): Request configuration.
Returns
Property[]
: The list of retrieved properties.
// 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
In Ultipa's Node.js SDK, 1
indicates INT32.
getProperty()
Retrieves a specified property from the graph.
Parameters
dbType: DBType
: Type of the property (node or edge).schemaName: string
: Name of the schema.propertyName: string
: Name of the property.config: RequestConfig
(Optional): Request configuration.
Returns
Property
: The retrieved 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()
Retrieves a specified node property from the graph.
Parameters
schemaName: string
: Name of the schema.propertyName: string
: Name of the property.config: RequestConfig
(Optional): Request configuration.
Returns
Property
: The retrieved 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()
Retrieves a specified edge property from the graph.
Parameters
schemaName: string
: Name of the schema.propertyName: string
: Name of the property.config: RequestConfig
(Optional): Request configuration.
Returns
Property
: The retrieved 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()
Creates a property in the graph.
Parameters
dbType: DBType
: Type of the property (node or edge).property: Property
: The property to be created; the attributesname
,type
(andsubType
if thetype
isSET
orLIST
), andschema
(sets to*
to specify all schemas) are mandatory,encrypt
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// 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()
Creates a property in the graph and returns whether a node or edge property with the same name already exists for the specified schema.
Parameters
dbType: DBType
: Type of the property (node or edge).property: Property
: The property to be created; the attributesname
,type
(andsubType
if thetype
isSET
orLIST
), andschema
(sets to*
to specify all schemas) are mandatory,encrypt
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
ResponseWithExistCheck
: Result of the request.
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()
Alters the name and description of a property in the graph.
Parameters
dbType: DBType
: Type of the property (node or edge).originProp: Property
: The property to be altered; the attributesname
andschema
(writes*
to specify all schemas) are mandatory.newProp: Property
: AProperty
object used to set the newname
and/ordescription
for theproperty
.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// 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()
Deletes specified properties from the graph.
Parameters
dbType: DBType
: Type of the property (node or edge).property: Property
: The property to be droppped; the attributesname
andschema
(writes*
to specify all schemas) are mandatory.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// 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
Full Example
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: "profile", type: ULTIPA.UltipaPropertyType.BLOB, 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);