安装嬴图Node.js驱动并部署一个嬴图实例后,就可以将应用与数据库连接了。
你可以使用UltipaConfig
配置连接,点击查看UltipaConfig属性。
建立连接
使用UltipaDriver()
创建连接:
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/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.86:60061", "192.168.1.87:60061"],
username: "<username>",
password: "<password>",
};
const conn = new UltipaDriver(ultipaConfig);
// Tests the connection
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
};
sdkUsage().catch(console.error);
Connection succeeds: true
使用配置文件
本例演示如何使用配置文件.env
建立连接:
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types";
import * as dotenv from "dotenv";
// Loads the .env file and overrides system environment variables
dotenv.config({override:true});
const hosts = process.env.hosts?.split(',') || [];
const username = process.env.username!;
const password = process.env.password!;
const sdkUsage = async () => {
// URI example: ultipaConfig.hosts: ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
const ultipaConfig: ULTIPA.UltipaConfig = {
hosts: hosts,
username: username,
password: password,
};
const conn = new UltipaDriver(ultipaConfig);
// Tests the connection
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
};
sdkUsage().catch(console.error);
Connection succeeds: true
Example of the .env
file:
// hosts=https://mqj4zouys.us-east-1.cloud.ultipa.com:60010
hosts=192.168.1.85:60061,192.168.1.86:60061,192.168.1.87:60061
username=<username>
password=<password>
passwordEncrypt=MD5
defaultGraph=miniCircle
// crt=F:\\ultipa.crt
// maxRecvSize=10240
UltipaConfig属性
UltipaConfig
类有以下属性:
属性 |
类型 |
默认 |
描述 |
---|---|---|---|
hosts |
string[] | / | 必填,逗号分隔的数据库服务器IP或URL地址;自动识别协议,不允许在URL开头写https:// 或http:// |
username |
string | / | 必填,服务器验证的用户名 |
password |
string | / | 必填,服务器验证的密码 |
defaultGraph |
string | / | 默认使用的图名称 |
crt |
string | / | 建立安全连接所需的SSL证书文件路径 |
passwordEncrypt |
string | MD5 |
驱动的密码加密方式,支持MD5 、LDAP 和NOTHING |
timeout |
number | Maximum | 请求超时阈值(单位:秒) |
heartbeat |
number | 0 | 用于保持连接为活跃状态的心跳间隔(单位:毫秒),设置为0关闭心跳 |
maxRecvSize |
number | 32 | 接收数据的最大大小(单位:MB) |