Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/kuzu-client/package.json
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/**/*"
]
}
2 changes: 2 additions & 0 deletions packages/kuzu-client/src/kuzu-client.index.ts
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;
114 changes: 114 additions & 0 deletions packages/kuzu-client/src/kuzu-client.lib.ts
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
export namespace KuzuClient {
/**
* 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) {
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();
},
};
}

}
30 changes: 30 additions & 0 deletions packages/kuzu-client/src/kuzu.d.ts
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;
}
3 changes: 3 additions & 0 deletions packages/kuzu-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json",
}
1 change: 1 addition & 0 deletions packages/kuzu-client/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../tsup.config.base.ts";
1 change: 1 addition & 0 deletions packages/markdown-db/src/markdown-db.engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ query:
};

assert.deepEqual(config.status, "ok");
assert.ok(engine);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.ok(engine), the variable is not "used"

});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/markdown-db/src/markdown-db.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class MarkdownDb<
// Parse the header data
const headerData = await new Promise((resolve) =>
resolve(md.getHeaderData(mdText)),
).catch(async (err) => {
).catch((err) => {
throw new Error(t`
Failed to read markdown file header
- File: ${this._cwd.relativePathTo(mdFile)}
Expand Down Expand Up @@ -261,7 +261,7 @@ export class MarkdownDb<
/**
* Refresh the markdown entries from the filesystem
*/
public async refreshEntries() {
public refreshEntries() {
this._entriesPromise = this.readEntries();
this._entriesMapPromise = this._entriesPromise.then(
(entries) => new Map(entries.map((entry) => [entry.$id, entry])),
Expand All @@ -288,7 +288,7 @@ export class MarkdownDb<
(entries) => new Map(entries.map((entry) => [entry.$id, entry])),
);
}
return this._entriesMapPromise!;
return this._entriesMapPromise;
}

/**
Expand Down Expand Up @@ -328,7 +328,7 @@ export class MarkdownDb<
return parentMap;
});
}
return this._parentPatternsMapPromise!;
return this._parentPatternsMapPromise;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/pipe/src/pipe.engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
import type { ZodTypeDef as ZodTypeDefV3, ZodType as ZodTypeV3 } from "zod/v3";
import type { ZodType as ZodTypeV4 } from "zod/v4";

Expand Down Expand Up @@ -44,12 +43,12 @@ export class Pipe<INPUT, OUTPUT> {
// Promises
if (value instanceof Promise) {
return value.then((resolvedValue) => {
return schema.parse(resolvedValue) as T;
return schema.parse(resolvedValue);
});
}

// Synchronous
return schema.parse(value) as T;
return schema.parse(value);
});
}

Expand Down
8 changes: 3 additions & 5 deletions packages/query/src/query.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function queryPredicate<NAME extends string, PARAMS, INPUT = unknown>(
}

type QuerySchemaReturn<EXTRA_SCHEMAS extends z.ZodTypeAny> = z.ZodType<
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
BasePredicates | z.output<EXTRA_SCHEMAS>
>;

Expand Down Expand Up @@ -68,7 +67,6 @@ export function querySchema<EXTRA_SCHEMAS extends z.ZodTypeAny>(
alwaysSchema,
neverSchema,
...extras,
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
]) as z.ZodType<BasePredicates | z.infer<EXTRA_SCHEMAS>>;

return schema;
Expand Down Expand Up @@ -101,16 +99,16 @@ export function queryApply<

if ("and" in query) {
if (query.and.length === 0) return false;
return query.and.every((q: any) => apply(q, input));
return query.and.every((q: BasePredicates) => apply(q, input));
}

if ("or" in query) {
if (query.or.length === 0) return false;
return query.or.some((q: any) => apply(q, input));
return query.or.some((q: BasePredicates) => apply(q, input));
}

if ("not" in query) {
return !apply(query.not, input);
return !apply(query.not as BasePredicates, input);
}

return predicates.some((c) => {
Expand Down
Loading