-
Notifications
You must be signed in to change notification settings - Fork 0
Add kuzu client #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dannydan78
wants to merge
7
commits into
main
Choose a base branch
from
add-kuzu-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,545
−4,892
Open
Add kuzu client #16
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2eebd8c
feat(kuzu): create packages.json
Dannydan78 b696c97
feat(config): add tsconfig & tsup
Dannydan78 af2a0e5
feat(kuzu): create kuzu client
Dannydan78 180b71b
feat(kuzu): export index
Dannydan78 290c4cd
fix(packages-markdown,-pipe-&-query): fix files for CI
Dannydan78 eaa42ea
feat(package): add synstack/fs on package
Dannydan78 3be5ab6
refactor(kuzu): update namespace to interface
Dannydan78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "@synstack/kuzu-client", | ||
"type": "module", | ||
"version": "0.1.0", | ||
"description": "Kuzu graph database client", | ||
"author": { | ||
"name": "pAIrprog", | ||
"url": "https://pairprog.io" | ||
}, | ||
"homepage": "https://github.com/pAIrprogio/synscript/tree/main/packages/kuzu-client", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pAIrprogio/synscript.git", | ||
"directory": "packages/kuzu-client" | ||
}, | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"build": "tsup", | ||
"prepack": "pnpm build", | ||
"build:watch": "tsup --watch", | ||
"test:types": "tsc --noEmit", | ||
"test:unit": "node --experimental-strip-types --experimental-test-snapshots --no-warnings --test src/**/*.test.ts", | ||
"test:unit:watch": "node --experimental-strip-types --experimental-test-snapshots --no-warnings --watch --test --watch src/**/*.test.ts", | ||
"test": "pnpm test:types && pnpm test:unit" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/kuzu-client.index.d.ts", | ||
"default": "./dist/kuzu-client.index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/kuzu-client.index.d.cts", | ||
"default": "./dist/kuzu-client.index.cjs" | ||
} | ||
} | ||
}, | ||
"peerDependencies": { | ||
"kuzu": "^0.11.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.15.32", | ||
"kuzu": "^0.11.1", | ||
"tsup": "^8.5.0", | ||
"typescript": "^5.8.3" | ||
}, | ||
"files": [ | ||
"src/**/*.ts", | ||
"!src/**/*.test.ts", | ||
"dist/**/*" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { KuzuClient } from "./kuzu-client.lib.js"; | ||
export type KuzuClientOptions = import("./kuzu-client.lib.js").KuzuClient.Options; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { existsSync } from "node:fs"; | ||
import { resolve } from "node:path"; | ||
import type { Database, Connection } from "kuzu"; | ||
import kuzu from "kuzu"; | ||
|
||
export class KuzuClient { | ||
private dbPath: string; | ||
private db: Database; | ||
private conn: Connection; | ||
|
||
constructor(options: KuzuClient.Options) { | ||
this.dbPath = resolve(options.databasePath); | ||
const dbExists = existsSync(this.dbPath); | ||
|
||
if (!dbExists && !options.createIfNotExists) { | ||
throw new Error( | ||
`Database path ${this.dbPath} does not exist, check your configuration` | ||
); | ||
} | ||
|
||
this.db = new kuzu.Database(this.dbPath); | ||
this.conn = new kuzu.Connection(this.db); | ||
} | ||
|
||
/** | ||
* Returns the raw kuzu query result | ||
* Call `getAll` on the result to get the data | ||
*/ | ||
async query<T = unknown>(query: string) { | ||
return await this.conn.query<T>(query); | ||
} | ||
|
||
/** | ||
* Returns the query results rows directly | ||
*/ | ||
async queryAll<T = unknown>(query: string): Promise<T[]> { | ||
const queryResult = await this.query<T>(query); | ||
return queryResult.getAll(); | ||
} | ||
|
||
/** | ||
* Returns the first row of the query result | ||
*/ | ||
async queryOne<T = unknown>(query: string): Promise<T | null> { | ||
const result = await this.query<T>(query); | ||
return result.getNext() || null; | ||
} | ||
} | ||
|
||
/** | ||
* Namespace for types and utilities | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
Dannydan78 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export namespace KuzuClient { | ||
Dannydan78 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Options for creating a kuzu client | ||
*/ | ||
export interface Options { | ||
|
||
databasePath: string; | ||
createIfNotExists?: boolean; | ||
} | ||
|
||
/** | ||
* Factory to create a client instance | ||
*/ | ||
export function create(options: Options): KuzuClient { | ||
return new KuzuClient(options); | ||
} | ||
|
||
/** | ||
* Factory to create an object-style client | ||
*/ | ||
export function createObject(options: Options) { | ||
Dannydan78 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const dbPath = resolve(options.databasePath); | ||
const dbExists = existsSync(dbPath); | ||
|
||
if (!dbExists && !options.createIfNotExists) { | ||
throw new Error( | ||
`Database path ${dbPath} does not exist, check your configuration` | ||
); | ||
} | ||
|
||
const db = new kuzu.Database(dbPath); | ||
const conn = new kuzu.Connection(db); | ||
|
||
return { | ||
/** | ||
* Returns the raw kuzu query result | ||
* Call `getAll` on the result to get the data | ||
*/ | ||
async query<T = unknown>(query: string) { | ||
return await conn.query<T>(query); | ||
}, | ||
|
||
/** | ||
* Returns the query results rows directly | ||
*/ | ||
async queryAll<T = unknown>(query: string) { | ||
const queryResult = await this.query<T>(query); | ||
return queryResult.getAll(); | ||
}, | ||
|
||
/** | ||
* Returns the first row of the query result | ||
*/ | ||
async queryOne<T = unknown>(query: string) { | ||
const result = await this.query<T>(query); | ||
return result.getNext(); | ||
}, | ||
}; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
declare module "kuzu" { | ||
export class Database { | ||
constructor( | ||
databasePath: string, | ||
bufferManagerSize?: number, | ||
enableCompression?: boolean, | ||
readOnly?: boolean, | ||
maxDBSize?: number, | ||
autoCheckpoint?: boolean, | ||
checkpointThreshold?: number, | ||
); | ||
} | ||
|
||
export class Connection { | ||
constructor(db: Database); | ||
query<T = unknown>(query: string): Promise<QueryResult<T>>; | ||
} | ||
|
||
export interface QueryResult<T> { | ||
getAll(): T[]; | ||
getNext(): T | null; | ||
} | ||
|
||
const kuzu: { | ||
Database: typeof Database; | ||
Connection: typeof Connection; | ||
}; | ||
|
||
export default kuzu; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "../../tsup.config.base.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,6 +405,7 @@ query: | |
}; | ||
|
||
assert.deepEqual(config.status, "ok"); | ||
assert.ok(engine); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assert.ok(engine), the variable is not "used" |
||
}); | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.