安装驱动并部署一个嬴图实例后,就可以将应用连接到数据库了。
建立连接
使用包含数据库连接配置的ULTIPA.UltipaConfig
实例化UltipaDriver()
,从而创建一个连接。
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import type { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types/index.js";
let sdkUsage = async () => {
const ultipaConfig: ULTIPA.UltipaConfig = {
// URI example: hosts: ["xxxx.us-east-1.cloud.ultipa.com:60010"]
hosts: ["10.xx.xx.xx:60010"],
username: "<username>",
password: "<password>"
};
const driver = new UltipaDriver(ultipaConfig);
// Tests the connection
const isSuccess = await driver.test();
console.log(`Connection succeeds: ${isSuccess}`);
};
sdkUsage().catch(console.error);
Connection succeeds: true
使用配置文件
本例演示如何使用配置文件.env
建立连接:
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import type { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types/index.js";
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 () => {
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
.env
文件示例:
// hosts=xxxx.us-east-1.cloud.ultipa.com:60010
hosts=10.xx.xx.xx:60010
username=<username>
password=<password>
passwordEncrypt=MD5
defaultGraph=miniCircle
// crt=F:\\ultipa.crt
// maxRecvSize=10240
连接配置
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) |