@tursodatabase/database-wasm 0.2.0-pre.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -0
- package/bundle/main.es.js +9100 -0
- package/dist/index-bundle.d.ts +9 -0
- package/dist/index-bundle.d.ts.map +1 -0
- package/dist/index-bundle.js +20 -0
- package/dist/index-default.d.ts +9 -0
- package/dist/index-default.d.ts.map +1 -0
- package/dist/index-default.js +18 -0
- package/dist/index-turbopack-hack.d.ts +9 -0
- package/dist/index-turbopack-hack.d.ts.map +1 -0
- package/dist/index-turbopack-hack.js +20 -0
- package/dist/index-vite-dev-hack.d.ts +15 -0
- package/dist/index-vite-dev-hack.d.ts.map +1 -0
- package/dist/index-vite-dev-hack.js +36 -0
- package/dist/promise-bundle.d.ts +23 -0
- package/dist/promise-bundle.d.ts.map +1 -0
- package/dist/promise-bundle.js +55 -0
- package/dist/promise-default.d.ts +23 -0
- package/dist/promise-default.d.ts.map +1 -0
- package/dist/promise-default.js +55 -0
- package/dist/promise-turbopack-hack.d.ts +23 -0
- package/dist/promise-turbopack-hack.d.ts.map +1 -0
- package/dist/promise-turbopack-hack.js +55 -0
- package/dist/promise-vite-dev-hack.d.ts +23 -0
- package/dist/promise-vite-dev-hack.d.ts.map +1 -0
- package/dist/promise-vite-dev-hack.js +55 -0
- package/dist/promise.test.d.ts +2 -0
- package/dist/promise.test.d.ts.map +1 -0
- package/dist/promise.test.js +106 -0
- package/dist/turso.wasm32-wasi.wasm +0 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +22 -0
- package/dist/wasm-inline.d.ts +2 -0
- package/dist/wasm-inline.d.ts.map +1 -0
- package/dist/wasm-inline.js +8 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +2 -0
- package/package.json +57 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare let MainWorker: any;
|
|
2
|
+
declare const _default: any;
|
|
3
|
+
export default _default;
|
|
4
|
+
export declare const Database: any;
|
|
5
|
+
export declare const Opfs: any;
|
|
6
|
+
export declare const OpfsFile: any;
|
|
7
|
+
export declare const Statement: any;
|
|
8
|
+
export declare const initThreadPool: any;
|
|
9
|
+
//# sourceMappingURL=index-bundle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-bundle.d.ts","sourceRoot":"","sources":["../index-bundle.ts"],"names":[],"mappings":"AAOA,eAAO,IAAI,UAAU,KAAO,CAAC;;AAU7B,wBAAiC;AACjC,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,IAAI,KAA0B,CAAA;AAC3C,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,SAAS,KAA+B,CAAA;AACrD,eAAO,MAAM,cAAc,KAAoC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { setupMainThread } from "@tursodatabase/database-wasm-common";
|
|
2
|
+
//@ts-ignore
|
|
3
|
+
import TursoWorker from "./worker.js?worker&inline";
|
|
4
|
+
const __wasmUrl = new URL('./turso.wasm32-wasi.wasm', import.meta.url).href;
|
|
5
|
+
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer());
|
|
6
|
+
export let MainWorker = null;
|
|
7
|
+
const napiModule = await setupMainThread(__wasmFile, () => {
|
|
8
|
+
const worker = new TursoWorker({
|
|
9
|
+
name: 'turso-database',
|
|
10
|
+
type: 'module',
|
|
11
|
+
});
|
|
12
|
+
MainWorker = worker;
|
|
13
|
+
return worker;
|
|
14
|
+
});
|
|
15
|
+
export default napiModule.exports;
|
|
16
|
+
export const Database = napiModule.exports.Database;
|
|
17
|
+
export const Opfs = napiModule.exports.Opfs;
|
|
18
|
+
export const OpfsFile = napiModule.exports.OpfsFile;
|
|
19
|
+
export const Statement = napiModule.exports.Statement;
|
|
20
|
+
export const initThreadPool = napiModule.exports.initThreadPool;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare let MainWorker: any;
|
|
2
|
+
declare const _default: any;
|
|
3
|
+
export default _default;
|
|
4
|
+
export declare const Database: any;
|
|
5
|
+
export declare const Opfs: any;
|
|
6
|
+
export declare const OpfsFile: any;
|
|
7
|
+
export declare const Statement: any;
|
|
8
|
+
export declare const initThreadPool: any;
|
|
9
|
+
//# sourceMappingURL=index-default.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-default.d.ts","sourceRoot":"","sources":["../index-default.ts"],"names":[],"mappings":"AAKA,eAAO,IAAI,UAAU,KAAO,CAAC;;AAU7B,wBAAiC;AACjC,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,IAAI,KAA0B,CAAA;AAC3C,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,SAAS,KAA+B,CAAA;AACrD,eAAO,MAAM,cAAc,KAAoC,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { setupMainThread } from "@tursodatabase/database-wasm-common";
|
|
2
|
+
const __wasmUrl = new URL('./turso.wasm32-wasi.wasm', import.meta.url).href;
|
|
3
|
+
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer());
|
|
4
|
+
export let MainWorker = null;
|
|
5
|
+
const napiModule = await setupMainThread(__wasmFile, () => {
|
|
6
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
7
|
+
name: 'turso-database',
|
|
8
|
+
type: 'module',
|
|
9
|
+
});
|
|
10
|
+
MainWorker = worker;
|
|
11
|
+
return worker;
|
|
12
|
+
});
|
|
13
|
+
export default napiModule.exports;
|
|
14
|
+
export const Database = napiModule.exports.Database;
|
|
15
|
+
export const Opfs = napiModule.exports.Opfs;
|
|
16
|
+
export const OpfsFile = napiModule.exports.OpfsFile;
|
|
17
|
+
export const Statement = napiModule.exports.Statement;
|
|
18
|
+
export const initThreadPool = napiModule.exports.initThreadPool;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare let MainWorker: any;
|
|
2
|
+
declare const _default: any;
|
|
3
|
+
export default _default;
|
|
4
|
+
export declare const Database: any;
|
|
5
|
+
export declare const Opfs: any;
|
|
6
|
+
export declare const OpfsFile: any;
|
|
7
|
+
export declare const Statement: any;
|
|
8
|
+
export declare const initThreadPool: any;
|
|
9
|
+
//# sourceMappingURL=index-turbopack-hack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-turbopack-hack.d.ts","sourceRoot":"","sources":["../index-turbopack-hack.ts"],"names":[],"mappings":"AAOA,eAAO,IAAI,UAAU,KAAO,CAAC;;AAW7B,wBAAiC;AACjC,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,IAAI,KAA0B,CAAA;AAC3C,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,SAAS,KAA+B,CAAA;AACrD,eAAO,MAAM,cAAc,KAAoC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { setupMainThread } from "@tursodatabase/database-wasm-common";
|
|
2
|
+
import { tursoWasm } from "./wasm-inline.js";
|
|
3
|
+
// Next (turbopack) has issues with loading wasm module: https://github.com/vercel/next.js/issues/82520
|
|
4
|
+
// So, we inline wasm binary in the source code in order to avoid issues with loading it from the file
|
|
5
|
+
const __wasmFile = await tursoWasm();
|
|
6
|
+
export let MainWorker = null;
|
|
7
|
+
const napiModule = await setupMainThread(__wasmFile, () => {
|
|
8
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
9
|
+
name: 'turso-database',
|
|
10
|
+
type: 'module',
|
|
11
|
+
});
|
|
12
|
+
MainWorker = worker;
|
|
13
|
+
return worker;
|
|
14
|
+
});
|
|
15
|
+
export default napiModule.exports;
|
|
16
|
+
export const Database = napiModule.exports.Database;
|
|
17
|
+
export const Opfs = napiModule.exports.Opfs;
|
|
18
|
+
export const OpfsFile = napiModule.exports.OpfsFile;
|
|
19
|
+
export const Statement = napiModule.exports.Statement;
|
|
20
|
+
export const initThreadPool = napiModule.exports.initThreadPool;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare let MainWorker: any;
|
|
2
|
+
declare const _default: {
|
|
3
|
+
Database: any;
|
|
4
|
+
Opfs: any;
|
|
5
|
+
OpfsFile: any;
|
|
6
|
+
Statement: any;
|
|
7
|
+
initThreadPool: any;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
10
|
+
export declare const Database: any;
|
|
11
|
+
export declare const Opfs: any;
|
|
12
|
+
export declare const OpfsFile: any;
|
|
13
|
+
export declare const Statement: any;
|
|
14
|
+
export declare const initThreadPool: any;
|
|
15
|
+
//# sourceMappingURL=index-vite-dev-hack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-vite-dev-hack.d.ts","sourceRoot":"","sources":["../index-vite-dev-hack.ts"],"names":[],"mappings":"AAaA,eAAO,IAAI,UAAU,KAAO,CAAC;;cART,GAAG;UACP,GAAG;cACC,GAAG;eACF,GAAG;oBACE,GAAG;;AAwB7B,wBAAiC;AACjC,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,IAAI,KAA0B,CAAA;AAC3C,eAAO,MAAM,QAAQ,KAA8B,CAAA;AACnD,eAAO,MAAM,SAAS,KAA+B,CAAA;AACrD,eAAO,MAAM,cAAc,KAAoC,CAAA"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isWebWorker, setupMainThread, setupWebWorker } from "@tursodatabase/database-wasm-common";
|
|
2
|
+
import { tursoWasm } from "./wasm-inline.js";
|
|
3
|
+
let napiModule = {
|
|
4
|
+
exports: {
|
|
5
|
+
Database: {},
|
|
6
|
+
Opfs: {},
|
|
7
|
+
OpfsFile: {},
|
|
8
|
+
Statement: {},
|
|
9
|
+
initThreadPool: {},
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export let MainWorker = null;
|
|
13
|
+
if (isWebWorker()) {
|
|
14
|
+
setupWebWorker();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
// Vite has issues with loading wasm modules and worker in dev server: https://github.com/vitejs/vite/issues/8427
|
|
18
|
+
// So, the mitigation for dev server only is:
|
|
19
|
+
// 1. inline wasm binary in the source code in order to avoid issues with loading it from the file
|
|
20
|
+
// 2. use same file as worker entry point
|
|
21
|
+
const __wasmFile = await tursoWasm();
|
|
22
|
+
napiModule = await setupMainThread(__wasmFile, () => {
|
|
23
|
+
const worker = new Worker(import.meta.url, {
|
|
24
|
+
name: 'turso-database',
|
|
25
|
+
type: 'module',
|
|
26
|
+
});
|
|
27
|
+
MainWorker = worker;
|
|
28
|
+
return worker;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export default napiModule.exports;
|
|
32
|
+
export const Database = napiModule.exports.Database;
|
|
33
|
+
export const Opfs = napiModule.exports.Opfs;
|
|
34
|
+
export const OpfsFile = napiModule.exports.OpfsFile;
|
|
35
|
+
export const Statement = napiModule.exports.Statement;
|
|
36
|
+
export const initThreadPool = napiModule.exports.initThreadPool;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DatabasePromise, DatabaseOpts, SqliteError } from "@tursodatabase/database-common";
|
|
2
|
+
declare class Database extends DatabasePromise {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(path: string, opts?: DatabaseOpts);
|
|
5
|
+
/**
|
|
6
|
+
* connect database and pre-open necessary files in the OPFS
|
|
7
|
+
*/
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* close the database and relevant files
|
|
11
|
+
*/
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new database connection asynchronously.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} path - Path to the database file.
|
|
18
|
+
* @param {Object} opts - Options for database behavior.
|
|
19
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
20
|
+
*/
|
|
21
|
+
declare function connect(path: string, opts?: DatabaseOpts): Promise<Database>;
|
|
22
|
+
export { connect, Database, SqliteError };
|
|
23
|
+
//# sourceMappingURL=promise-bundle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise-bundle.d.ts","sourceRoot":"","sources":["../promise-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAG,MAAM,gCAAgC,CAAA;AAY5F,cAAM,QAAS,SAAQ,eAAe;;gBAEtB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;IAGjD;;OAEG;IACY,OAAO;IAWtB;;OAEG;IACG,KAAK;CASd;AAED;;;;;;GAMG;AACH,iBAAe,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI/E;AAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DatabasePromise, SqliteError, } from "@tursodatabase/database-common";
|
|
2
|
+
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common";
|
|
3
|
+
import { initThreadPool, MainWorker, Database as NativeDatabase } from "./index-bundle.js";
|
|
4
|
+
async function init() {
|
|
5
|
+
await initThreadPool();
|
|
6
|
+
if (MainWorker == null) {
|
|
7
|
+
throw new Error("panic: MainWorker is not initialized");
|
|
8
|
+
}
|
|
9
|
+
return MainWorker;
|
|
10
|
+
}
|
|
11
|
+
class Database extends DatabasePromise {
|
|
12
|
+
#worker;
|
|
13
|
+
constructor(path, opts = {}) {
|
|
14
|
+
super(new NativeDatabase(path, opts));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* connect database and pre-open necessary files in the OPFS
|
|
18
|
+
*/
|
|
19
|
+
async connect() {
|
|
20
|
+
if (!this.memory) {
|
|
21
|
+
const worker = await init();
|
|
22
|
+
await Promise.all([
|
|
23
|
+
registerFileAtWorker(worker, this.name),
|
|
24
|
+
registerFileAtWorker(worker, `${this.name}-wal`)
|
|
25
|
+
]);
|
|
26
|
+
this.#worker = worker;
|
|
27
|
+
}
|
|
28
|
+
await super.connect();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* close the database and relevant files
|
|
32
|
+
*/
|
|
33
|
+
async close() {
|
|
34
|
+
if (this.name != null && this.#worker != null) {
|
|
35
|
+
await Promise.all([
|
|
36
|
+
unregisterFileAtWorker(this.#worker, this.name),
|
|
37
|
+
unregisterFileAtWorker(this.#worker, `${this.name}-wal`)
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
await super.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new database connection asynchronously.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} path - Path to the database file.
|
|
47
|
+
* @param {Object} opts - Options for database behavior.
|
|
48
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
49
|
+
*/
|
|
50
|
+
async function connect(path, opts = {}) {
|
|
51
|
+
const db = new Database(path, opts);
|
|
52
|
+
await db.connect();
|
|
53
|
+
return db;
|
|
54
|
+
}
|
|
55
|
+
export { connect, Database, SqliteError };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DatabasePromise, DatabaseOpts, SqliteError } from "@tursodatabase/database-common";
|
|
2
|
+
declare class Database extends DatabasePromise {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(path: string, opts?: DatabaseOpts);
|
|
5
|
+
/**
|
|
6
|
+
* connect database and pre-open necessary files in the OPFS
|
|
7
|
+
*/
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* close the database and relevant files
|
|
11
|
+
*/
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new database connection asynchronously.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} path - Path to the database file.
|
|
18
|
+
* @param {Object} opts - Options for database behavior.
|
|
19
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
20
|
+
*/
|
|
21
|
+
declare function connect(path: string, opts?: DatabaseOpts): Promise<Database>;
|
|
22
|
+
export { connect, Database, SqliteError };
|
|
23
|
+
//# sourceMappingURL=promise-default.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise-default.d.ts","sourceRoot":"","sources":["../promise-default.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAG,MAAM,gCAAgC,CAAA;AAY5F,cAAM,QAAS,SAAQ,eAAe;;gBAEtB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;IAGjD;;OAEG;IACY,OAAO;IAWtB;;OAEG;IACG,KAAK;CASd;AAED;;;;;;GAMG;AACH,iBAAe,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI/E;AAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DatabasePromise, SqliteError, } from "@tursodatabase/database-common";
|
|
2
|
+
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common";
|
|
3
|
+
import { initThreadPool, MainWorker, Database as NativeDatabase } from "./index-default.js";
|
|
4
|
+
async function init() {
|
|
5
|
+
await initThreadPool();
|
|
6
|
+
if (MainWorker == null) {
|
|
7
|
+
throw new Error("panic: MainWorker is not initialized");
|
|
8
|
+
}
|
|
9
|
+
return MainWorker;
|
|
10
|
+
}
|
|
11
|
+
class Database extends DatabasePromise {
|
|
12
|
+
#worker;
|
|
13
|
+
constructor(path, opts = {}) {
|
|
14
|
+
super(new NativeDatabase(path, opts));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* connect database and pre-open necessary files in the OPFS
|
|
18
|
+
*/
|
|
19
|
+
async connect() {
|
|
20
|
+
if (!this.memory) {
|
|
21
|
+
const worker = await init();
|
|
22
|
+
await Promise.all([
|
|
23
|
+
registerFileAtWorker(worker, this.name),
|
|
24
|
+
registerFileAtWorker(worker, `${this.name}-wal`)
|
|
25
|
+
]);
|
|
26
|
+
this.#worker = worker;
|
|
27
|
+
}
|
|
28
|
+
await super.connect();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* close the database and relevant files
|
|
32
|
+
*/
|
|
33
|
+
async close() {
|
|
34
|
+
if (this.name != null && this.#worker != null) {
|
|
35
|
+
await Promise.all([
|
|
36
|
+
unregisterFileAtWorker(this.#worker, this.name),
|
|
37
|
+
unregisterFileAtWorker(this.#worker, `${this.name}-wal`)
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
await super.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new database connection asynchronously.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} path - Path to the database file.
|
|
47
|
+
* @param {Object} opts - Options for database behavior.
|
|
48
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
49
|
+
*/
|
|
50
|
+
async function connect(path, opts = {}) {
|
|
51
|
+
const db = new Database(path, opts);
|
|
52
|
+
await db.connect();
|
|
53
|
+
return db;
|
|
54
|
+
}
|
|
55
|
+
export { connect, Database, SqliteError };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DatabasePromise, DatabaseOpts, SqliteError } from "@tursodatabase/database-common";
|
|
2
|
+
declare class Database extends DatabasePromise {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(path: string, opts?: DatabaseOpts);
|
|
5
|
+
/**
|
|
6
|
+
* connect database and pre-open necessary files in the OPFS
|
|
7
|
+
*/
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* close the database and relevant files
|
|
11
|
+
*/
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new database connection asynchronously.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} path - Path to the database file.
|
|
18
|
+
* @param {Object} opts - Options for database behavior.
|
|
19
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
20
|
+
*/
|
|
21
|
+
declare function connect(path: string, opts?: DatabaseOpts): Promise<Database>;
|
|
22
|
+
export { connect, Database, SqliteError };
|
|
23
|
+
//# sourceMappingURL=promise-turbopack-hack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise-turbopack-hack.d.ts","sourceRoot":"","sources":["../promise-turbopack-hack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAG,MAAM,gCAAgC,CAAA;AAY5F,cAAM,QAAS,SAAQ,eAAe;;gBAEtB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;IAGjD;;OAEG;IACY,OAAO;IAWtB;;OAEG;IACG,KAAK;CASd;AAED;;;;;;GAMG;AACH,iBAAe,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI/E;AAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DatabasePromise, SqliteError, } from "@tursodatabase/database-common";
|
|
2
|
+
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common";
|
|
3
|
+
import { initThreadPool, MainWorker, Database as NativeDatabase } from "./index-turbopack-hack.js";
|
|
4
|
+
async function init() {
|
|
5
|
+
await initThreadPool();
|
|
6
|
+
if (MainWorker == null) {
|
|
7
|
+
throw new Error("panic: MainWorker is not initialized");
|
|
8
|
+
}
|
|
9
|
+
return MainWorker;
|
|
10
|
+
}
|
|
11
|
+
class Database extends DatabasePromise {
|
|
12
|
+
#worker;
|
|
13
|
+
constructor(path, opts = {}) {
|
|
14
|
+
super(new NativeDatabase(path, opts));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* connect database and pre-open necessary files in the OPFS
|
|
18
|
+
*/
|
|
19
|
+
async connect() {
|
|
20
|
+
if (!this.memory) {
|
|
21
|
+
const worker = await init();
|
|
22
|
+
await Promise.all([
|
|
23
|
+
registerFileAtWorker(worker, this.name),
|
|
24
|
+
registerFileAtWorker(worker, `${this.name}-wal`)
|
|
25
|
+
]);
|
|
26
|
+
this.#worker = worker;
|
|
27
|
+
}
|
|
28
|
+
await super.connect();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* close the database and relevant files
|
|
32
|
+
*/
|
|
33
|
+
async close() {
|
|
34
|
+
if (this.name != null && this.#worker != null) {
|
|
35
|
+
await Promise.all([
|
|
36
|
+
unregisterFileAtWorker(this.#worker, this.name),
|
|
37
|
+
unregisterFileAtWorker(this.#worker, `${this.name}-wal`)
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
await super.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new database connection asynchronously.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} path - Path to the database file.
|
|
47
|
+
* @param {Object} opts - Options for database behavior.
|
|
48
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
49
|
+
*/
|
|
50
|
+
async function connect(path, opts = {}) {
|
|
51
|
+
const db = new Database(path, opts);
|
|
52
|
+
await db.connect();
|
|
53
|
+
return db;
|
|
54
|
+
}
|
|
55
|
+
export { connect, Database, SqliteError };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DatabasePromise, DatabaseOpts, SqliteError } from "@tursodatabase/database-common";
|
|
2
|
+
declare class Database extends DatabasePromise {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(path: string, opts?: DatabaseOpts);
|
|
5
|
+
/**
|
|
6
|
+
* connect database and pre-open necessary files in the OPFS
|
|
7
|
+
*/
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* close the database and relevant files
|
|
11
|
+
*/
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new database connection asynchronously.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} path - Path to the database file.
|
|
18
|
+
* @param {Object} opts - Options for database behavior.
|
|
19
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
20
|
+
*/
|
|
21
|
+
declare function connect(path: string, opts?: DatabaseOpts): Promise<Database>;
|
|
22
|
+
export { connect, Database, SqliteError };
|
|
23
|
+
//# sourceMappingURL=promise-vite-dev-hack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise-vite-dev-hack.d.ts","sourceRoot":"","sources":["../promise-vite-dev-hack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAG,MAAM,gCAAgC,CAAA;AAY5F,cAAM,QAAS,SAAQ,eAAe;;gBAEtB,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB;IAGjD;;OAEG;IACY,OAAO;IAWtB;;OAEG;IACG,KAAK;CASd;AAED;;;;;;GAMG;AACH,iBAAe,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,YAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI/E;AAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DatabasePromise, SqliteError, } from "@tursodatabase/database-common";
|
|
2
|
+
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common";
|
|
3
|
+
import { initThreadPool, MainWorker, Database as NativeDatabase } from "./index-vite-dev-hack.js";
|
|
4
|
+
async function init() {
|
|
5
|
+
await initThreadPool();
|
|
6
|
+
if (MainWorker == null) {
|
|
7
|
+
throw new Error("panic: MainWorker is not initialized");
|
|
8
|
+
}
|
|
9
|
+
return MainWorker;
|
|
10
|
+
}
|
|
11
|
+
class Database extends DatabasePromise {
|
|
12
|
+
#worker;
|
|
13
|
+
constructor(path, opts = {}) {
|
|
14
|
+
super(new NativeDatabase(path, opts));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* connect database and pre-open necessary files in the OPFS
|
|
18
|
+
*/
|
|
19
|
+
async connect() {
|
|
20
|
+
if (!this.memory) {
|
|
21
|
+
const worker = await init();
|
|
22
|
+
await Promise.all([
|
|
23
|
+
registerFileAtWorker(worker, this.name),
|
|
24
|
+
registerFileAtWorker(worker, `${this.name}-wal`)
|
|
25
|
+
]);
|
|
26
|
+
this.#worker = worker;
|
|
27
|
+
}
|
|
28
|
+
await super.connect();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* close the database and relevant files
|
|
32
|
+
*/
|
|
33
|
+
async close() {
|
|
34
|
+
if (this.name != null && this.#worker != null) {
|
|
35
|
+
await Promise.all([
|
|
36
|
+
unregisterFileAtWorker(this.#worker, this.name),
|
|
37
|
+
unregisterFileAtWorker(this.#worker, `${this.name}-wal`)
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
await super.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new database connection asynchronously.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} path - Path to the database file.
|
|
47
|
+
* @param {Object} opts - Options for database behavior.
|
|
48
|
+
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
|
49
|
+
*/
|
|
50
|
+
async function connect(path, opts = {}) {
|
|
51
|
+
const db = new Database(path, opts);
|
|
52
|
+
await db.connect();
|
|
53
|
+
return db;
|
|
54
|
+
}
|
|
55
|
+
export { connect, Database, SqliteError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise.test.d.ts","sourceRoot":"","sources":["../promise.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { connect, Database } from './promise-default.js';
|
|
3
|
+
test('in-memory db', async () => {
|
|
4
|
+
const db = await connect(":memory:");
|
|
5
|
+
await db.exec("CREATE TABLE t(x)");
|
|
6
|
+
await db.exec("INSERT INTO t VALUES (1), (2), (3)");
|
|
7
|
+
const stmt = db.prepare("SELECT * FROM t WHERE x % 2 = ?");
|
|
8
|
+
const rows = await stmt.all([1]);
|
|
9
|
+
expect(rows).toEqual([{ x: 1 }, { x: 3 }]);
|
|
10
|
+
});
|
|
11
|
+
test('explicit connect', async () => {
|
|
12
|
+
const db = new Database(':memory:');
|
|
13
|
+
expect(() => db.prepare("SELECT 1")).toThrowError(/database must be connected/g);
|
|
14
|
+
await db.connect();
|
|
15
|
+
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
|
|
16
|
+
});
|
|
17
|
+
test('on-disk db large inserts', async () => {
|
|
18
|
+
const path = `test-${(Math.random() * 10000) | 0}.db`;
|
|
19
|
+
const db1 = await connect(path);
|
|
20
|
+
await db1.prepare("CREATE TABLE t(x)").run();
|
|
21
|
+
await db1.prepare("INSERT INTO t VALUES (randomblob(10 * 4096 + 0))").run();
|
|
22
|
+
await db1.prepare("INSERT INTO t VALUES (randomblob(10 * 4096 + 1))").run();
|
|
23
|
+
await db1.prepare("INSERT INTO t VALUES (randomblob(10 * 4096 + 2))").run();
|
|
24
|
+
const stmt1 = db1.prepare("SELECT length(x) as l FROM t");
|
|
25
|
+
expect(stmt1.columns()).toEqual([{ name: "l", column: null, database: null, table: null, type: null }]);
|
|
26
|
+
const rows1 = await stmt1.all();
|
|
27
|
+
expect(rows1).toEqual([{ l: 10 * 4096 }, { l: 10 * 4096 + 1 }, { l: 10 * 4096 + 2 }]);
|
|
28
|
+
await db1.exec("BEGIN");
|
|
29
|
+
await db1.exec("INSERT INTO t VALUES (1)");
|
|
30
|
+
await db1.exec("ROLLBACK");
|
|
31
|
+
const rows2 = await db1.prepare("SELECT length(x) as l FROM t").all();
|
|
32
|
+
expect(rows2).toEqual([{ l: 10 * 4096 }, { l: 10 * 4096 + 1 }, { l: 10 * 4096 + 2 }]);
|
|
33
|
+
await db1.prepare("PRAGMA wal_checkpoint(TRUNCATE)").run();
|
|
34
|
+
});
|
|
35
|
+
test('on-disk db', async () => {
|
|
36
|
+
const path = `test-${(Math.random() * 10000) | 0}.db`;
|
|
37
|
+
const db1 = await connect(path);
|
|
38
|
+
await db1.exec("CREATE TABLE t(x)");
|
|
39
|
+
await db1.exec("INSERT INTO t VALUES (1), (2), (3)");
|
|
40
|
+
const stmt1 = db1.prepare("SELECT * FROM t WHERE x % 2 = ?");
|
|
41
|
+
expect(stmt1.columns()).toEqual([{ name: "x", column: null, database: null, table: null, type: null }]);
|
|
42
|
+
const rows1 = await stmt1.all([1]);
|
|
43
|
+
expect(rows1).toEqual([{ x: 1 }, { x: 3 }]);
|
|
44
|
+
stmt1.close();
|
|
45
|
+
await db1.close();
|
|
46
|
+
const db2 = await connect(path);
|
|
47
|
+
const stmt2 = db2.prepare("SELECT * FROM t WHERE x % 2 = ?");
|
|
48
|
+
expect(stmt2.columns()).toEqual([{ name: "x", column: null, database: null, table: null, type: null }]);
|
|
49
|
+
const rows2 = await stmt2.all([1]);
|
|
50
|
+
expect(rows2).toEqual([{ x: 1 }, { x: 3 }]);
|
|
51
|
+
db2.close();
|
|
52
|
+
});
|
|
53
|
+
// attach is not supported in browser for now
|
|
54
|
+
// test('attach', async () => {
|
|
55
|
+
// const path1 = `test-${(Math.random() * 10000) | 0}.db`;
|
|
56
|
+
// const path2 = `test-${(Math.random() * 10000) | 0}.db`;
|
|
57
|
+
// const db1 = await connect(path1);
|
|
58
|
+
// await db1.exec("CREATE TABLE t(x)");
|
|
59
|
+
// await db1.exec("INSERT INTO t VALUES (1), (2), (3)");
|
|
60
|
+
// const db2 = await connect(path2);
|
|
61
|
+
// await db2.exec("CREATE TABLE q(x)");
|
|
62
|
+
// await db2.exec("INSERT INTO q VALUES (4), (5), (6)");
|
|
63
|
+
// await db1.exec(`ATTACH '${path2}' as secondary`);
|
|
64
|
+
// const stmt = db1.prepare("SELECT * FROM t UNION ALL SELECT * FROM secondary.q");
|
|
65
|
+
// expect(stmt.columns()).toEqual([{ name: "x", column: null, database: null, table: null, type: null }]);
|
|
66
|
+
// const rows = await stmt.all([1]);
|
|
67
|
+
// expect(rows).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }, { x: 6 }]);
|
|
68
|
+
// })
|
|
69
|
+
test('blobs', async () => {
|
|
70
|
+
const db = await connect(":memory:");
|
|
71
|
+
const rows = await db.prepare("SELECT x'1020' as x").all();
|
|
72
|
+
expect(rows).toEqual([{ x: new Uint8Array([16, 32]) }]);
|
|
73
|
+
});
|
|
74
|
+
test('example-1', async () => {
|
|
75
|
+
const db = await connect(':memory:');
|
|
76
|
+
await db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
|
|
77
|
+
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
|
|
78
|
+
await insert.run('Alice', 'alice@example.com');
|
|
79
|
+
await insert.run('Bob', 'bob@example.com');
|
|
80
|
+
const users = await db.prepare('SELECT * FROM users').all();
|
|
81
|
+
expect(users).toEqual([
|
|
82
|
+
{ id: 1, name: 'Alice', email: 'alice@example.com' },
|
|
83
|
+
{ id: 2, name: 'Bob', email: 'bob@example.com' }
|
|
84
|
+
]);
|
|
85
|
+
});
|
|
86
|
+
test('example-2', async () => {
|
|
87
|
+
const db = await connect(':memory:');
|
|
88
|
+
await db.exec('CREATE TABLE users (name, email)');
|
|
89
|
+
// Using transactions for atomic operations
|
|
90
|
+
const transaction = db.transaction(async (users) => {
|
|
91
|
+
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
|
|
92
|
+
for (const user of users) {
|
|
93
|
+
await insert.run(user.name, user.email);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
// Execute transaction
|
|
97
|
+
await transaction([
|
|
98
|
+
{ name: 'Alice', email: 'alice@example.com' },
|
|
99
|
+
{ name: 'Bob', email: 'bob@example.com' }
|
|
100
|
+
]);
|
|
101
|
+
const rows = await db.prepare('SELECT * FROM users').all();
|
|
102
|
+
expect(rows).toEqual([
|
|
103
|
+
{ name: 'Alice', email: 'alice@example.com' },
|
|
104
|
+
{ name: 'Bob', email: 'bob@example.com' }
|
|
105
|
+
]);
|
|
106
|
+
});
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.config.d.ts","sourceRoot":"","sources":["../vitest.config.ts"],"names":[],"mappings":";AAEA,wBAoBE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
export default defineConfig({
|
|
3
|
+
define: {
|
|
4
|
+
'process.env.NODE_DEBUG_NATIVE': 'false',
|
|
5
|
+
},
|
|
6
|
+
server: {
|
|
7
|
+
headers: {
|
|
8
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
9
|
+
"Cross-Origin-Opener-Policy": "same-origin"
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
test: {
|
|
13
|
+
browser: {
|
|
14
|
+
enabled: true,
|
|
15
|
+
provider: 'playwright',
|
|
16
|
+
instances: [
|
|
17
|
+
{ browser: 'chromium' },
|
|
18
|
+
{ browser: 'firefox' }
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm-inline.d.ts","sourceRoot":"","sources":["../wasm-inline.ts"],"names":[],"mappings":"AAMA,wBAAsB,SAAS,IAAI,OAAO,CAAC,WAAW,CAAC,CAEtD"}
|