Skip to content

[WIP] wa-sqlite support #3

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ts-node": "^10.9.2",
"tsx": "^4.16.2",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
"vitest": "^2.0.5"
},
"packageManager": "[email protected]+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
}
14 changes: 7 additions & 7 deletions packages/api/src/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ export class ConnectionImpl implements SqliteConnection {
}

async begin(options?: TransactionOptions): Promise<SqliteBeginTransaction> {
await this.init();
this.init();

if ((options?.type ?? 'exclusive') == 'exclusive') {
await this._beginExclusive!.select();
await this._beginExclusive!.run();
} else {
await this._begin!.select();
await this._begin!.run();
}

return new BeginTransactionImpl(this);
Expand All @@ -321,18 +321,18 @@ export class ConnectionImpl implements SqliteConnection {
this.init();

if ((options?.type ?? 'exclusive') == 'exclusive') {
await this._beginExclusive!.select();
await this._beginExclusive!.run();
} else {
await this._begin!.select();
await this._begin!.run();
}
try {
const tx = new TransactionImpl(this);
const result = await callback(tx);

await this.commit!.select();
await this.commit!.run();
return result;
} catch (e) {
await this.rollback!.select();
await this.rollback!.run();
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { describeDriverTests } from '@sqlite-js/driver-tests';
import { BetterSqliteDriver } from '../../lib/index.js';
import { deleteDb } from './util.js';

describeDriverTests(
'better-sqlite3-async-pool',
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
(path) => BetterSqliteDriver.open(path)
async (path) => {
await deleteDb(path);
return BetterSqliteDriver.open(path);
}
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { BetterSqliteDriver } from '../../lib/index.js';
import { describeDriverTests } from '@sqlite-js/driver-tests';
import { deleteDb } from './util.js';

describeDriverTests(
'better-sqlite3',
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
(path) => BetterSqliteDriver.openInProcess(path)
async (path) => {
await deleteDb(path);
return BetterSqliteDriver.openInProcess(path);
}
);
12 changes: 12 additions & 0 deletions packages/better-sqlite3-driver/test/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

export async function deleteDb(dbPath: string) {
const dir = path.dirname(dbPath);
try {
await fs.mkdir(dir);
} catch (e) {}
try {
await fs.rm(dbPath);
} catch (e) {}
}
16 changes: 3 additions & 13 deletions packages/driver-tests/src/driver-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { beforeEach, describe, test } from './test.js';
import { expect } from 'expect';
import { beforeEach, describe, test, expect } from './test.js';
import { SqliteDriverConnectionPool } from '@sqlite-js/driver';

export interface DriverFeatures {
Expand All @@ -13,20 +10,13 @@ export interface DriverFeatures {
export function describeDriverTests(
name: string,
features: DriverFeatures,
factory: (path: string) => SqliteDriverConnectionPool
factory: (path: string) => Promise<SqliteDriverConnectionPool>
) {
describe(`${name} - driver tests`, () => {
let dbPath: string;

const open = async () => {
const dir = path.dirname(dbPath);
try {
await fs.mkdir(dir);
} catch (e) {}
try {
await fs.rm(dbPath);
} catch (e) {}
const db = factory(dbPath);
const db = await factory(dbPath);
return db;
};

Expand Down
13 changes: 11 additions & 2 deletions packages/driver-tests/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// A lib to allow testing with vitest or mocha
import type { test as testType, describe as describeType } from 'vitest';
import type {
test as testType,
describe as describeType,
expect as expectType
} from 'vitest';

export interface TestContext {
fullName: string;
Expand All @@ -8,15 +12,19 @@ export interface TestContext {
export const isVitest = process.env.VITEST == 'true';
export const isMocha = !isVitest;

let testImpl, describeImpl, beforeEachImpl;
let testImpl, describeImpl, beforeEachImpl, expectImpl;

if (isMocha) {
const { test, describe, beforeEach } = await import('./setup-mocha.js');
const { expect } = await import('expect');
expectImpl = expect;
testImpl = test;
describeImpl = describe;
beforeEachImpl = beforeEach;
} else {
const { test, describe, beforeEach } = await import('./setup-vitest.js');
const { expect } = await import('vitest');
expectImpl = expect;
testImpl = test;
describeImpl = describe;
beforeEachImpl = beforeEach;
Expand All @@ -28,3 +36,4 @@ export function beforeEach(callback: (context: TestContext) => any) {

export const test = testImpl as typeof testType;
export const describe = describeImpl as typeof describeType;
export const expect = expectImpl as typeof expectType;
6 changes: 5 additions & 1 deletion packages/driver-tests/test/src/node-sqlite-async.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { NodeSqliteDriver } from '@sqlite-js/driver/node';
import { describeDriverTests } from '../../lib/index.js';
import { deleteDb } from './util.js';

describeDriverTests(
'node:sqlite worker',
{ getColumns: false, rawResults: false, allowsMissingParameters: true },
(path) => NodeSqliteDriver.open(path)
async (path) => {
await deleteDb(path);
return NodeSqliteDriver.open(path);
}
);
6 changes: 5 additions & 1 deletion packages/driver-tests/test/src/node-sqlite-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { NodeSqliteDriver } from '@sqlite-js/driver/node';
import { describeDriverTests } from '../../lib/index.js';

import { isMocha, test } from '../../lib/test.js';
import { deleteDb } from './util.js';

if (isMocha) {
describeDriverTests(
'node:sqlite direct',
{ getColumns: false, rawResults: false, allowsMissingParameters: true },
(path) => NodeSqliteDriver.openInProcess(path)
async (path) => {
await deleteDb(path);
return NodeSqliteDriver.openInProcess(path);
}
);
} else {
test.skip('only running in mocha');
Expand Down
12 changes: 12 additions & 0 deletions packages/driver-tests/test/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

export async function deleteDb(dbPath: string) {
const dir = path.dirname(dbPath);
try {
await fs.mkdir(dir);
} catch (e) {}
try {
await fs.rm(dbPath);
} catch (e) {}
}
33 changes: 33 additions & 0 deletions packages/wa-sqlite-driver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@sqlite-js/wa-sqlite-driver",
"version": "0.0.1",
"description": "",
"type": "module",
"scripts": {
"build": "tsc -b",
"test": "pnpm build && vitest",
"clean": "tsc -b --clean && tsc -b ./test/tsconfig.json --clean && rm -rf lib test/lib"
},
"exports": {
".": "./lib/index.js"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@journeyapps/wa-sqlite": "^0.3.0",
"@sqlite-js/driver": "workspace:^",
"async-mutex": "^0.5.0"
},
"devDependencies": {
"vite-plugin-top-level-await": "^1.4.2",
"vite-plugin-wasm": "^3.3.0",
"@sqlite-js/driver-tests": "workspace:^",
"@types/node": "^22.3.0",
"typescript": "^5.5.4",
"@vitest/browser": "^2.0.5",
"vitest": "^2.0.5",
"playwright": "^1.45.3",
"webdriverio": "^8.39.1"
}
}
1 change: 1 addition & 0 deletions packages/wa-sqlite-driver/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './wa-sqlite-driver.js';
Loading