Skip to content

Commit

Permalink
feat: add callback feature
Browse files Browse the repository at this point in the history
  • Loading branch information
didierfranc committed Jan 6, 2025
1 parent 6d19870 commit d723f79
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
22 changes: 5 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"test:browser": "vitest --workspace=vitest.workspace.ts"
},
"devDependencies": {
"@playwright/test": "^1.49.1",
"@types/node": "^22.10.2",
"@vitest/browser": "^2.1.8",
"jsdom": "^25.0.1",
Expand Down
8 changes: 7 additions & 1 deletion src/sql.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { SQLite } from "./sqlite";

describe("SQLite database operations", () => {
it("should perform database operations and verify exports match", async () => {
const db = new SQLite();

const execCallback = vi.fn();
db.on("exec", execCallback);

await db.exec(`
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
`);

const result = await db.exec(`SELECT * FROM users;`);

expect(execCallback).toHaveBeenCalledTimes(2);
expect(result).toBeDefined();

const exportedBytes = await db.export_db();
Expand Down
41 changes: 37 additions & 4 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
import type { BindingSpec, Database, FlexibleString, Sqlite3Static, SqlValue } from "@sqlite.org/sqlite-wasm";
import type {
BindingSpec,
Database,
FlexibleString,
Sqlite3Static,
SqlValue,
} from "@sqlite.org/sqlite-wasm";

type Args = any[];

export class SQLite {
private db: Database | undefined;
private sqlite3: Sqlite3Static | undefined;
private initPromise: Promise<void>;
private callbacks: { [key: string]: ((args: Args) => void)[] } = {};

constructor() {
this.initPromise = this.initialize();
Expand All @@ -19,11 +28,35 @@ export class SQLite {
this.db = new this.sqlite3.oo1.DB(":memory:");
}


async exec(query: FlexibleString, bind?: BindingSpec): Promise<{ [columnName: string]: SqlValue }[]> {
async exec(
query: FlexibleString,
bind?: BindingSpec,
): Promise<{ [columnName: string]: SqlValue }[]> {
await this.initPromise;
if (!this.db) return [];
return this.db.exec(query, { bind, rowMode: 'object', returnValue: 'resultRows' });
const result = this.db.exec(query, {
bind,
rowMode: "object",
returnValue: "resultRows",
});

this.call("exec", [query, bind, result]);

return result;
}

private call(event: string, args: Args): void {
if (!this.callbacks[event]) return;
queueMicrotask(() => {
for (const callback of this.callbacks[event]) {
callback(args);
}
});
}

on(event: "exec", callback: (args: Args) => void): void {
if (!this.callbacks[event]) this.callbacks[event] = [];
this.callbacks[event].push(callback);
}

async export_db(): Promise<Uint8Array> {
Expand Down

0 comments on commit d723f79

Please sign in to comment.