@arcote.tech/arc-adapter-db-sqlite-wasm 0.5.6 → 0.5.8
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/dist/index.d.ts +2 -0
- package/dist/index.js +24 -0
- package/dist/sqlite-wasm-adapter.d.ts +82 -0
- package/dist/types.d.ts +40 -0
- package/dist/worker.d.ts +13 -0
- package/package.json +18 -2
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -5273,6 +5273,30 @@ class SQLiteWasmAdapter {
|
|
|
5273
5273
|
}
|
|
5274
5274
|
async createTableIfNotExistsNew(tableName, columns) {
|
|
5275
5275
|
await this.db.exec(this.generateCreateTableSQL(tableName, columns));
|
|
5276
|
+
await this.addMissingColumns(tableName, columns);
|
|
5277
|
+
}
|
|
5278
|
+
async addMissingColumns(tableName, columns) {
|
|
5279
|
+
const existing = await this.db.exec(`PRAGMA table_info("${tableName}")`);
|
|
5280
|
+
const existingNames = new Set(existing.map((row) => row.name));
|
|
5281
|
+
for (const col of columns) {
|
|
5282
|
+
if (existingNames.has(col.name))
|
|
5283
|
+
continue;
|
|
5284
|
+
const sql = this.generateAlterAddColumnSQL(tableName, col);
|
|
5285
|
+
await this.db.exec(sql);
|
|
5286
|
+
}
|
|
5287
|
+
}
|
|
5288
|
+
generateAlterAddColumnSQL(tableName, col) {
|
|
5289
|
+
const type = this.mapType(col.type, col.storeData);
|
|
5290
|
+
const constraints = [];
|
|
5291
|
+
if (col.defaultValue !== undefined) {
|
|
5292
|
+
const def = typeof col.defaultValue === "string" ? `'${col.defaultValue.replace(/'/g, "''")}'` : String(col.defaultValue);
|
|
5293
|
+
constraints.push(`DEFAULT ${def}`);
|
|
5294
|
+
}
|
|
5295
|
+
if (!col.storeData?.isNullable && col.defaultValue === undefined) {} else if (!col.storeData?.isNullable) {
|
|
5296
|
+
constraints.push("NOT NULL");
|
|
5297
|
+
}
|
|
5298
|
+
const constraintsSql = constraints.length ? " " + constraints.join(" ") : "";
|
|
5299
|
+
return `ALTER TABLE "${tableName}" ADD COLUMN "${col.name}" ${type}${constraintsSql}`;
|
|
5276
5300
|
}
|
|
5277
5301
|
async createVersionCounterTable() {
|
|
5278
5302
|
await this.db.exec(`CREATE TABLE IF NOT EXISTS __arc_version_counters (table_name TEXT PRIMARY KEY, last_version INTEGER NOT NULL DEFAULT 0)`);
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite WASM Adapter
|
|
3
|
+
*
|
|
4
|
+
* Communicates with SQLite running in a Web Worker via postMessage.
|
|
5
|
+
* Implements the same interface as the server-side SQLite adapter.
|
|
6
|
+
*/
|
|
7
|
+
import { type ArcContextAny, type DatabaseAdapter, type DBAdapterFactory, type FindOptions, type ReadTransaction, type ReadWriteTransaction, type StoreColumn, type StoreTable, type WhereCondition } from "@arcote.tech/arc";
|
|
8
|
+
/**
|
|
9
|
+
* Wrapper that communicates with the SQLite Worker
|
|
10
|
+
*/
|
|
11
|
+
export declare class SQLiteWasmDatabase {
|
|
12
|
+
private worker;
|
|
13
|
+
private dbName;
|
|
14
|
+
private requestId;
|
|
15
|
+
private pendingRequests;
|
|
16
|
+
private initialized;
|
|
17
|
+
private initPromise;
|
|
18
|
+
constructor(worker: Worker, dbName: string);
|
|
19
|
+
private handleMessage;
|
|
20
|
+
init(): Promise<void>;
|
|
21
|
+
private sendRequest;
|
|
22
|
+
exec(sql: string, params?: any[]): Promise<any[]>;
|
|
23
|
+
execBatch(queries: Array<{
|
|
24
|
+
sql: string;
|
|
25
|
+
params?: any[];
|
|
26
|
+
}>): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
declare class SQLiteWasmReadTransaction implements ReadTransaction {
|
|
29
|
+
protected db: SQLiteWasmDatabase;
|
|
30
|
+
protected tables: Map<string, StoreTable>;
|
|
31
|
+
protected adapter?: SQLiteWasmAdapter;
|
|
32
|
+
constructor(db: SQLiteWasmDatabase, tables: Map<string, StoreTable>, adapter?: SQLiteWasmAdapter);
|
|
33
|
+
protected hasSoftDelete(tableName: string): boolean;
|
|
34
|
+
protected deserializeValue(value: any, column: StoreColumn): any;
|
|
35
|
+
protected deserializeRow(row: any, table: StoreTable): any;
|
|
36
|
+
protected buildWhereClause(where?: WhereCondition, tableName?: string): {
|
|
37
|
+
sql: string;
|
|
38
|
+
params: any[];
|
|
39
|
+
};
|
|
40
|
+
protected getOperatorSymbol(operator: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Serialize a value for use in WHERE clause params
|
|
43
|
+
* Handles Date objects and other non-primitive types
|
|
44
|
+
*/
|
|
45
|
+
protected serializeWhereValue(value: any): any;
|
|
46
|
+
protected buildOrderByClause(orderBy?: Record<string, "asc" | "desc">): string;
|
|
47
|
+
find<T>(store: string, options: FindOptions<T>): Promise<T[]>;
|
|
48
|
+
}
|
|
49
|
+
declare class SQLiteWasmReadWriteTransaction extends SQLiteWasmReadTransaction implements ReadWriteTransaction {
|
|
50
|
+
protected adapter: SQLiteWasmAdapter;
|
|
51
|
+
private queries;
|
|
52
|
+
constructor(db: SQLiteWasmDatabase, tables: Map<string, StoreTable>, adapter: SQLiteWasmAdapter);
|
|
53
|
+
remove(store: string, id: any): Promise<void>;
|
|
54
|
+
set(store: string, item: any): Promise<void>;
|
|
55
|
+
private setWithoutVersioning;
|
|
56
|
+
private setWithVersioning;
|
|
57
|
+
commit(): Promise<void>;
|
|
58
|
+
private serializeValue;
|
|
59
|
+
}
|
|
60
|
+
export declare class SQLiteWasmAdapter implements DatabaseAdapter {
|
|
61
|
+
private db;
|
|
62
|
+
private context;
|
|
63
|
+
private tables;
|
|
64
|
+
private pendingReinitTables;
|
|
65
|
+
private mapType;
|
|
66
|
+
private buildConstraints;
|
|
67
|
+
private generateColumnSQL;
|
|
68
|
+
private generateCreateTableSQL;
|
|
69
|
+
constructor(db: SQLiteWasmDatabase, context: ArcContextAny);
|
|
70
|
+
initialize(): Promise<void>;
|
|
71
|
+
private createTableIfNotExistsNew;
|
|
72
|
+
private createVersionCounterTable;
|
|
73
|
+
private createTableVersionsTable;
|
|
74
|
+
private getPhysicalTableName;
|
|
75
|
+
hasVersioning(tableName: string): boolean;
|
|
76
|
+
hasSoftDelete(tableName: string): boolean;
|
|
77
|
+
executeReinitTables(dataStorage: any): Promise<void>;
|
|
78
|
+
readWriteTransaction(_stores?: string[]): SQLiteWasmReadWriteTransaction;
|
|
79
|
+
readTransaction(_stores?: string[]): SQLiteWasmReadTransaction;
|
|
80
|
+
}
|
|
81
|
+
export declare const createSQLiteWasmAdapterFactory: (worker: Worker, dbName: string) => DBAdapterFactory;
|
|
82
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message types for Worker <-> Main thread communication
|
|
3
|
+
*/
|
|
4
|
+
export type WorkerRequest = {
|
|
5
|
+
type: "init";
|
|
6
|
+
dbName: string;
|
|
7
|
+
} | {
|
|
8
|
+
type: "exec";
|
|
9
|
+
id: number;
|
|
10
|
+
sql: string;
|
|
11
|
+
params?: any[];
|
|
12
|
+
} | {
|
|
13
|
+
type: "execBatch";
|
|
14
|
+
id: number;
|
|
15
|
+
queries: Array<{
|
|
16
|
+
sql: string;
|
|
17
|
+
params?: any[];
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
export type WorkerResponse = {
|
|
21
|
+
type: "init-success";
|
|
22
|
+
} | {
|
|
23
|
+
type: "init-error";
|
|
24
|
+
error: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "exec-success";
|
|
27
|
+
id: number;
|
|
28
|
+
result: any[];
|
|
29
|
+
} | {
|
|
30
|
+
type: "exec-error";
|
|
31
|
+
id: number;
|
|
32
|
+
error: string;
|
|
33
|
+
} | {
|
|
34
|
+
type: "execBatch-success";
|
|
35
|
+
id: number;
|
|
36
|
+
} | {
|
|
37
|
+
type: "execBatch-error";
|
|
38
|
+
id: number;
|
|
39
|
+
error: string;
|
|
40
|
+
};
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite WASM Worker
|
|
3
|
+
*
|
|
4
|
+
* This worker runs SQLite in a Web Worker using OPFS for persistence.
|
|
5
|
+
* Communication happens via postMessage.
|
|
6
|
+
*
|
|
7
|
+
* Usage in app:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import SQLiteWorker from '@arcote.tech/arc-adapter-db-sqlite-wasm/worker?worker'
|
|
10
|
+
* const worker = new SQLiteWorker()
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcote.tech/arc-adapter-db-sqlite-wasm",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -17,13 +17,29 @@
|
|
|
17
17
|
"types": "./dist/worker.d.ts"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
|
+
"arc": {
|
|
21
|
+
"browserAssets": [
|
|
22
|
+
{
|
|
23
|
+
"from": "./dist/worker.js",
|
|
24
|
+
"to": "sqlite-worker.js"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"from": "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm",
|
|
28
|
+
"to": "sqlite3.wasm"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"from": "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js",
|
|
32
|
+
"to": "sqlite3-opfs-async-proxy.js"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
},
|
|
20
36
|
"scripts": {
|
|
21
37
|
"build": "bun build ./src/index.ts ./src/worker.ts --outdir ./dist --target browser --format esm && bun run build:types",
|
|
22
38
|
"build:types": "tsc --emitDeclarationOnly --declaration --outDir ./dist",
|
|
23
39
|
"dev": "bun build ./src/index.ts ./src/worker.ts --outdir ./dist --target browser --format esm --watch"
|
|
24
40
|
},
|
|
25
41
|
"peerDependencies": {
|
|
26
|
-
"@arcote.tech/arc": "^0.5.
|
|
42
|
+
"@arcote.tech/arc": "^0.5.8",
|
|
27
43
|
"@sqlite.org/sqlite-wasm": "^3.46.0-build1"
|
|
28
44
|
},
|
|
29
45
|
"devDependencies": {
|