ConnectionPool
ConnectionPool
defines the information of the connection pool needed when connecting to Ultipa Graph.
Item |
Type |
Description |
---|---|---|
hosts | string[] | IP addresses of cluster servers (comma-separated) or host URL (excluding "https://" or "http://") |
username | string | Username of server |
password | string | Password of server |
crt | buffer | Local certificate, it's necessary for establishing the SSL connection |
defaultConfig | ULTIPA.UltipaConfig | Other configurations, such as the graphset, timeout and consistency |
otherParams | object | Two keys are included: isHttps and isHttp , where the values are both boolean. If both are set to true, HTTP is used first. If ignored, HTTP will be tried first, and HTTPS if HHTP fails |
Connect to a Cluster
Example: Connect to a cluster, and use graphset 'default'
import { ConnectionPool } from "@ultipa-graph/ultipa-node-sdk";
import fs from "fs";
let sdkUsage = async () => {
let hosts = [
"192.168.1.85:60061",
"192.168.1.86:60061",
"192.168.1.87:60061"
];
let username = "***";
let password = "***";
let crt: Buffer;
{ // if uses crt
let crt_file_path = "./ultipa.crt";
crt = fs.readFileSync(crt_file_path);
}
let connPool = new ConnectionPool(hosts, username, password, crt);
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
};
sdkUsage().then(console.log).catch(console.log);
Connect to Ultipa Cloud
Example: Connect to an instance on Ultipa Cloud, and use graphset 'default'
import { ConnectionPool } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
let hosts = ["3xbotdjas.us-east-1.cloud.ultipa.com:60010"];
let username = "***";
let password = "***";
let otherParams = {
isHttps: true,
isHttp: false
};
let connPool = new ConnectionPool(hosts, username, password, undefined, undefined, otherParams);
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
};
sdkUsage().then(console.log).catch(console.log);
UltipaConfig
ULTIPA.UltipaConfig
defines the information of graphset and etc. needed when connecting to an Ultipa graph database.
Item | Type | Description |
---|---|---|
graphSetName | string | name of graphset to use, or use 'default' if not set |
timeout | number | timeout seconds for any request |
consistency | boolean | if use leader host to guarantee Consistency Read |
Example: Create a server connection and use graphset 'amz'
import { ConnectionPool } from "@ultipa-graph/ultipa-node-sdk";
let sdkUsage = async () => {
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);
connPool.defaultConfig = { graphSetName: "amz" };
let conn = await connPool.getActive();
let isSuccess = await conn.test();
console.log(isSuccess);
};
sdkUsage().then(console.log).catch(console.log);
RequestConfig
RequestType.RequestConfig
defines the information needed when sending non-insert type of requests to an Ultipa graph database.
Item | Type | Description |
---|---|---|
graphSetName | string | name of graphset to use, or use graphSetName configured when establishing server connection if not set |
timeout | number | timeout seconds for the request |
useHost | string | send the request to a designated host, or sent to a random host if not set |
useMaster | boolean | if send the request to the leader to guarantee Consistency Read |
retry | object | retry config when request fails |
stream | Stream | if return stream |
threadNum | number | number of thread |
clusterId | string | cluster ID |
Example: Use graphset 'default' when establishing server connection, use graphset 'amz' when sending UQL to the the leader
import { ConnectionPool } from "@ultipa-graph/ultipa-node-sdk";
import { RequestType } from "@ultipa-graph/ultipa-node-sdk/dist/types";
let sdkUsage = async () => {
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 requestConfig = <RequestType.RequestConfig>{
graphSetName: "amz",
useMaster: true,
};
let uqlResult = await conn.uql(`find().nodes([1]) as n return n.name`, requestConfig);
console.log(uqlResult.data?.alias("n.name").asAttrs());
};
sdkUsage().then(console.log).catch(console.log);
InsertRequestConfig
RequestType.InsertRequestConfig
defines the information needed when sending insert type of requests to an Ultipa graph database.
Item | Type | Description |
---|---|---|
graphSetName | string | name of graphset to use, or use graphSetName configured when establishing server connection if not set |
timeout | number | timeout seconds for the request |
useHost | string | send the request to a designated host, or sent to a random host if not set |
useMaster | boolean | if send the request to the leader to guarantee Consistency Read |
retry | object | retry config when request fails |
stream | Stream | if return stream |
threadNum | number | number of thread |
clusterId | string | cluster ID |
insertType | ULTIPA.InsertType | insert mode (INSERT_TYPE_NORMAL, INSERT_TYPE_UPSERT, INSERT_TYPE_OVERWRITE) |
createNodeIfNotExist | boolean | if create start/end nodes of edge when the end nodes do not exist in the graphset |
Example: Create a server connection using graphset 'default', and insert nodes into graphset 'test' under overwrite mode
import { ConnectionPool, ULTIPA } from "@ultipa-graph/ultipa-node-sdk";
import { RequestType } from "@ultipa-graph/ultipa-node-sdk/dist/types";
let sdkUsage = async () => {
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 node1 = new ULTIPA.Node();
node1.schema = "card";
node1.id = "ULTIPA8000000000000001";
node1.values = {balance: Number(3235.2)};
let node2 = new ULTIPA.Node();
node2.schema = "client";
node2.id = "ULTIPA800000000000000B";
node2.values = {level: Number(7)};
let nodes = [node1, node2];
let insertRequestConfig = <RequestType.InsertRequestConfig>{
graphSetName: "test",
insertType: ULTIPA.InsertType.INSERT_TYPE_OVERWRITE,
};
let uqlResult = await conn.insertNodesBatchAuto(nodes, insertRequestConfig);
console.log(uqlResult.status.code);
};
sdkUsage().then(console.log).catch(console.log);