完成安装嬴图Node.js SDK,设置一个运行的嬴图实例后,可以开始将您的应用连接到嬴图数据库。
ConnectionPool
可以使用ConnectionPool
指定所需的连接池信息,从而建立与嬴图数据库的连接。
以下是ConnectionPool
的所有可用配置项:
项目 |
类型 |
描述 |
---|---|---|
hosts |
string[] | 数据库主机地址或主机URI(不包括https:// 或http:// )。使用逗号分隔多个实例地址。必需项 |
username |
string | 主机验证用户名。必需项 |
password |
string | 主机验证密码。必需项 |
crt |
Buffer | 设置本地证书文件路径。连接时将使用SSL |
defaultConfig |
ULTIPA.UltipaConfig | 其他配置,包括图集设置、超时设置和一致性设置 |
otherParams |
object | 包含两个键:isHttps 和isHttp ,均为布尔值类型。如果两者均设置为true ,优先使用HTTP。若未指定,连接将优先尝试HTTP,若HTTP连接失败,则切换到HTTPS |
连接到集群
以下示例展示如何连接到集群并使用图集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;
{ // 使用证书(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);
连接到嬴图Cloud
以下示例展示如何连接到嬴图Cloud实例并使用图集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
的所有可用配置项:
项目 |
类型 |
描述 |
---|---|---|
graphSetName |
string | 待使用的图集名称。若未设置,则使用建立连接时配置的graphSetName |
timeout |
number | 请求超时阈值,单位为秒 |
consistency |
boolean | 是否使用主节点以确保一致性读取 |
useHost |
string | 发送请求到指定主机节点,若未设置则随机选择主机节点 |
clusterID |
string | 指定待使用的集群 |
timeZone |
string | 待使用的时区 |
timeZoneOffset |
number | 所用时区与UTC的时间差,单位为分钟 |
timestampToString |
boolean | 是否将时间戳转换为字符串 |
logUql |
boolean | 是否打印UQL |
threadNum |
number | 线程数 |
responseWithRequestInfo |
boolean | 是否在响应中包含请求信息 |
测试连接
使用Connection
对象的test()
方法可以检查连接。
let resp = await conn.test();
console.log(resp);
true