Skip to content

Commit 23f0a51

Browse files
committed
feat(kuzu): create kuzu client
1 parent 795dc72 commit 23f0a51

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { existsSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
import type { Database, Connection } from "kuzu";
4+
import kuzu from "kuzu";
5+
6+
export class KuzuClient {
7+
private dbPath: string;
8+
private db: Database;
9+
private conn: Connection;
10+
11+
constructor(options: KuzuClient.Options) {
12+
this.dbPath = resolve(options.databasePath);
13+
const dbExists = existsSync(this.dbPath);
14+
15+
if (!dbExists && !options.createIfNotExists) {
16+
throw new Error(
17+
`Database path ${this.dbPath} does not exist, check your configuration`
18+
);
19+
}
20+
21+
this.db = new kuzu.Database(this.dbPath);
22+
this.conn = new kuzu.Connection(this.db);
23+
}
24+
25+
/**
26+
* Returns the raw kuzu query result
27+
* Call `getAll` on the result to get the data
28+
*/
29+
async query<T = unknown>(query: string) {
30+
return await this.conn.query<T>(query);
31+
}
32+
33+
/**
34+
* Returns the query results rows directly
35+
*/
36+
async queryAll<T = unknown>(query: string): Promise<T[]> {
37+
const queryResult = await this.query<T>(query);
38+
return queryResult.getAll();
39+
}
40+
41+
/**
42+
* Returns the first row of the query result
43+
*/
44+
async queryOne<T = unknown>(query: string): Promise<T | null> {
45+
const result = await this.query<T>(query);
46+
return result.getNext() || null;
47+
}
48+
}
49+
50+
/**
51+
* Namespace for types and utilities
52+
*/
53+
// eslint-disable-next-line @typescript-eslint/no-namespace
54+
export namespace KuzuClient {
55+
/**
56+
* Options for creating a kuzu client
57+
*/
58+
export interface Options {
59+
60+
databasePath: string;
61+
createIfNotExists?: boolean;
62+
}
63+
64+
/**
65+
* Factory to create a client instance
66+
*/
67+
export function create(options: Options): KuzuClient {
68+
return new KuzuClient(options);
69+
}
70+
71+
/**
72+
* Factory to create an object-style client
73+
*/
74+
export function createObject(options: Options) {
75+
const dbPath = resolve(options.databasePath);
76+
const dbExists = existsSync(dbPath);
77+
78+
if (!dbExists && !options.createIfNotExists) {
79+
throw new Error(
80+
`Database path ${dbPath} does not exist, check your configuration`
81+
);
82+
}
83+
84+
const db = new kuzu.Database(dbPath);
85+
const conn = new kuzu.Connection(db);
86+
87+
return {
88+
/**
89+
* Returns the raw kuzu query result
90+
* Call `getAll` on the result to get the data
91+
*/
92+
async query<T = unknown>(query: string) {
93+
return await conn.query<T>(query);
94+
},
95+
96+
/**
97+
* Returns the query results rows directly
98+
*/
99+
async queryAll<T = unknown>(query: string) {
100+
const queryResult = await this.query<T>(query);
101+
return queryResult.getAll();
102+
},
103+
104+
/**
105+
* Returns the first row of the query result
106+
*/
107+
async queryOne<T = unknown>(query: string) {
108+
const result = await this.query<T>(query);
109+
return result.getNext();
110+
},
111+
};
112+
}
113+
114+
}

packages/kuzu-client/src/kuzu.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
declare module "kuzu" {
2+
export class Database {
3+
constructor(
4+
databasePath: string,
5+
bufferManagerSize?: number,
6+
enableCompression?: boolean,
7+
readOnly?: boolean,
8+
maxDBSize?: number,
9+
autoCheckpoint?: boolean,
10+
checkpointThreshold?: number,
11+
);
12+
}
13+
14+
export class Connection {
15+
constructor(db: Database);
16+
query<T = unknown>(query: string): Promise<QueryResult<T>>;
17+
}
18+
19+
export interface QueryResult<T> {
20+
getAll(): T[];
21+
getNext(): T | null;
22+
}
23+
24+
const kuzu: {
25+
Database: typeof Database;
26+
Connection: typeof Connection;
27+
};
28+
29+
export default kuzu;
30+
}

0 commit comments

Comments
 (0)