Skip to content
Blog

Node.js API

See the following link for the full documentation of the Node.js API.

Sync and Async APIs

Kuzu provides both a sync and an async Node.js API. The async API is the default and more commonly used when building Kuzu applications in Node.js.

The asynchronous API is commonly used when writing Kuzu applications in Node.js.

const kuzu = require("kuzu");
(async () => {
// Create an empty on-disk database and connect to it
const db = new kuzu.Database("./demo_db");
const conn = new kuzu.Connection(db);
// Create the tables
await conn.query("CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))");
await conn.query("CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name))");
await conn.query("CREATE REL TABLE Follows(FROM User TO User, since INT64)");
await conn.query("CREATE REL TABLE LivesIn(FROM User TO City)");
// Load the data
await conn.query('COPY User FROM "./data/user.csv"');
await conn.query('COPY City FROM "./data/city.csv"');
await conn.query('COPY Follows FROM "./data/follows.csv"');
await conn.query('COPY LivesIn FROM "./data/lives-in.csv"');
const queryResult = await conn.query("MATCH (a:User)-[f:Follows]->(b:User) RETURN a.name, f.since, b.name;");
// Get all rows from the query result
const rows = await queryResult.getAll();
// Print the rows
for (const row of rows) {
console.log(row);
}
})();

See the Node.js API documentation for more details on the methods available in the sync and async APIs.

Run multiple queries in one execution

Similarly, in the Node.js API, you can execute multiple Cypher queries separated by semicolons in a single execution. The API will return a list of QueryResult objects.

const results = await conn.query("RETURN 1; RETURN 2; RETURN 3");
for (const result of results) {
const rows = await result.getAll();
console.log(rows);
}

This will return:

[[1]]
[[2]]
[[3]]