@bitnine-oss/ag-driver

中文文档
0.1.0发布于 2021/04/15Apache-2.0CommonJS近 30 天下载 6387

AgensGraph client for node.js

原始使用文档
文档目录6 节
  1. Usage
  2. Install
  3. Connection
  4. Queries
  5. Creating Data
  6. Graph Data Types

@bitnine-oss/agensgraph-driver

AgensGraph client for node.js. To be used concurrently with node-postgres. Adds AgensGraph compatibility for node-postgres. Support for the Cypher query language, and vertex, edge and path graph data types.

Usage

Install

# if you are using yarn
$ yarn add pg @bitnine-oss/ag-driver

# or npm
$ npm install pg @bitnine-oss/ag-driver

Connection

To connect to AgensGraph via node-js, one can connect via environment variables, programatically, or using a connection URI.

Connecting programmatically can be done by passing an object with config values to your AgensGraph client and using client.connect() to establish a connection:

var ag = require('@bitnine-oss/agensgraph-driver');
var config = {
    user: 'username',
    password: 'password',
    database: 'database name of agensgraph',
    host: '127.0.0.1',
    port: 5432
};

const client = new ag.Client(config);
client.connect();
client.end();

Use client.end() to terminate the connection to the database. Calling client.end() without a callback function will return a promise.

Queries

To retrieve data from AgensGraph, use the query() API function to query the database. There are multiple ways to send queries, including passing text directly, using parameters, passing in objects, and using prepared statements. The following examples illustrate the different possible methods of querying.

Users can retrieve Cypher data by passing text directly to client.query():

var ag = require('@bitnine-oss/agensgraph-driver');
const client = new ag.Client(Config);

client.connect();
client.query('SET graph_path = gpt'); //set graph path to gpt
client.query("MATCH (a:person)-[r:knows]->(b:person) WHERE b.name = 'Adam' RETURN a,r,b;", function (err, res) {
    if (err) {
        throw err;
    } else {
        console.log(res.rows);
    }
});
client.end();

Users can also send parameterized queries:

const text = "MATCH (a:person)-[:knows]->(b:person) WHERE b.name = $1 RETURN a;"
const values = ['Adam'];

client.connect();
client.query('SET graph_path = gpt'); //set graph path to gpt
client.query(text, values, function (err, res) {
    if (err) {
        throw err;
    } else {
        console.log(res.rows);
    }
});

Passing a config object:

const query = {
    text: "MATCH (a:person)-[:knows]->(b:person) WHERE b.name = $1 RETURN a;",
    values: ['Adam']
}

client.connect();
client.query('SET graph_path = gpt'); //set graph path to gpt
client.query(query, function (err, res) {
    if (err) {
        throw err;
    } else {
        console.log(res.rows);
    }
});

Users can also use named prepared statements. Using prepared statements allows query execution plans to be cached on the server on a per connection statement. Once a query name is sent to the server, it will be parsed and executed. On subsequent sends the query will skip the parse request and instead send the name of the query:

const query = {
    name: 'fetch-data',
    text: "MATCH(n) WHERE n.name = 'Eve' RETURN n"
}

client.query(query, (err, res) => {
    if (err) {
        throw err;
    } else {
        console.log(res.rows);
    }
});

Creating Data

To create data in AgensGraph, the query() API function is again used to query the database. Below is an example of creating Cypher data by passing text directly to client.query():

var ag = require('@bitnine-oss/ag-driver');
const client = new ag.Client(Config);

client.connect();
client.query('CREATE GRAPH gpt');
client.query('SET graph_path = gpt');
client.query("CREATE (a:person {name:'Alice'})-[r:knows {from: 'school'}]->(b:person {name: 'Bob'}) RETURN a,r,b", function (err, res) {
    if (err) {
        throw err;
    } else {
        a = res.rows[0].a;
        r = res.rows[0].r;
        b = res.rows[0].b;
        console.log(a.label);
        console.log(a.id);
        console.log(a.props.name);

        console.log(r.label);
        console.log(r.id);
        console.log(r.startVertexId);
        console.log(r.endVertexID);
        console.log(r.props.from);

        console.log(b.label);
        console.log(b.id);
        console.log(b.props.name);
    }
});

Graph Data Types

The following is a brief explanation detailing the different datatypes handled by agensgraph-nodejs extension.

Class FunctionDescriptionProperties
GraphIdCorresponds to the graphid type in AgensGraph.oid, id
VertexCorresponds to vertex type in AgensGraph. It supports access methods for the label and properties.label, id, props
EdgeCorresponds to edge type in AgensGraph. It supports access methods for the label and properties.label, id, start, end, props
PathCorresponds to graphpath type in AgensGraph. It supports access methods for the length of the path, and accessing for vertexes and edges in the path.vertices, edges, start, end, len

版本与发布记录

@bitnine-oss/ag-driver 共发布 1 个版本,首次发布于 2021/04/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.0latest2021/04/155 年前

关于 @bitnine-oss/ag-driver 的常见问题

@bitnine-oss/ag-driver 是什么?

AgensGraph client for node.js

如何安装 @bitnine-oss/ag-driver?

在项目目录运行 npm install @bitnine-oss/ag-driver,或使用 pnpm add / yarn add / bun add。

@bitnine-oss/ag-driver 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bitnine-oss__ag-driver 是否存在。

@bitnine-oss/ag-driver 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bitnine-oss/ag-driver 可以商用吗?

@bitnine-oss/ag-driver 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bitnine-oss/ag-driver 的下载量有多少?

根据 npm 官方下载统计,@bitnine-oss/ag-driver 在 2026/10/08 – 2026/11/06 这 30 天内被下载 6,387 次,日均约 213 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bitnine-oss/ag-driver 还在维护吗?

最新版本 0.1.0 发布于 2021/04/15(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。