rado 0.4.2 → 0.4.4

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 CHANGED
@@ -222,7 +222,7 @@ const PostTags = table({
222
222
  ## Connections
223
223
 
224
224
  Currently supported SQLite drivers:
225
- `better-sqlite3`, `sql.js`, `sqlite3`, `bun:sqlite`
225
+ `better-sqlite3`, `sql.js`, `sqlite3`, `bun:sqlite`, `@sqlite.org/sqlite-wasm`
226
226
 
227
227
  Pass an instance of the database to the `connect` function to get started:
228
228
 
@@ -0,0 +1,17 @@
1
+ import { SchemaInstructions } from '../../define/Schema.js';
2
+ import { Driver, DriverOptions } from '../../lib/Driver.js';
3
+ import { Statement } from '../../lib/Statement.js';
4
+ import { SqliteSchema } from '../../sqlite/SqliteSchema.js';
5
+ type DatabaseApi = any;
6
+ export declare class SqliteWasmDriver extends Driver.Sync {
7
+ db: DatabaseApi;
8
+ tableData?: (tableName: string) => Array<SqliteSchema.Column>;
9
+ indexData?: (tableName: string) => Array<SqliteSchema.Index>;
10
+ constructor(db: DatabaseApi, options?: DriverOptions);
11
+ close(): void;
12
+ prepareStatement(stmt: Statement, discardAfter: boolean): Driver.Sync.PreparedStatement;
13
+ schemaInstructions(tableName: string): SchemaInstructions | undefined;
14
+ export(): Uint8Array;
15
+ }
16
+ export declare function connect(db: DatabaseApi, options?: DriverOptions): SqliteWasmDriver;
17
+ export {};
@@ -0,0 +1,80 @@
1
+ import { Driver } from "../../lib/Driver.js";
2
+ import { SqliteFormatter } from "../../sqlite/SqliteFormatter.js";
3
+ import { SqliteSchema } from "../../sqlite/SqliteSchema.js";
4
+ class PreparedStatement {
5
+ constructor(db, stmt, discardAfter) {
6
+ this.db = db;
7
+ this.stmt = stmt;
8
+ this.discardAfter = discardAfter;
9
+ }
10
+ *iterate(params) {
11
+ if (params.length > 0)
12
+ this.stmt.bind(params);
13
+ while (this.stmt.step())
14
+ yield this.stmt.get({});
15
+ if (this.discardAfter)
16
+ this.stmt.finalize();
17
+ else
18
+ this.stmt.reset();
19
+ }
20
+ all(params) {
21
+ return Array.from(this.iterate(params));
22
+ }
23
+ run(params) {
24
+ if (params.length > 0)
25
+ this.stmt.bind(params);
26
+ this.stmt.step();
27
+ if (this.discardAfter)
28
+ this.stmt.finalize();
29
+ else
30
+ this.stmt.reset();
31
+ return { rowsAffected: this.db.changes() };
32
+ }
33
+ get(params) {
34
+ return this.all(params)[0];
35
+ }
36
+ execute(params) {
37
+ if (params.length > 0)
38
+ this.stmt.bind(params);
39
+ this.stmt.step();
40
+ if (this.discardAfter)
41
+ this.stmt.finalize();
42
+ else
43
+ this.stmt.reset();
44
+ }
45
+ }
46
+ class SqliteWasmDriver extends Driver.Sync {
47
+ constructor(db, options) {
48
+ super(new SqliteFormatter(), options);
49
+ this.db = db;
50
+ }
51
+ tableData;
52
+ indexData;
53
+ close() {
54
+ this.db.close();
55
+ }
56
+ prepareStatement(stmt, discardAfter) {
57
+ return new PreparedStatement(
58
+ this.db,
59
+ this.db.prepare(stmt.sql),
60
+ discardAfter
61
+ );
62
+ }
63
+ schemaInstructions(tableName) {
64
+ this.tableData = this.tableData || (this.tableData = this.prepare(SqliteSchema.tableData));
65
+ this.indexData = this.indexData || (this.indexData = this.prepare(SqliteSchema.indexData));
66
+ const columnData = this.tableData(tableName);
67
+ const indexData = this.indexData(tableName);
68
+ return SqliteSchema.createInstructions(columnData, indexData);
69
+ }
70
+ export() {
71
+ throw new Error("Not implemented");
72
+ }
73
+ }
74
+ function connect(db, options) {
75
+ return new SqliteWasmDriver(db, options);
76
+ }
77
+ export {
78
+ SqliteWasmDriver,
79
+ connect
80
+ };
@@ -13,6 +13,8 @@ class PreparedStatement {
13
13
  yield this.stmt.getAsObject();
14
14
  if (this.discardAfter)
15
15
  this.stmt.free();
16
+ else
17
+ this.stmt.reset();
16
18
  }
17
19
  all(params) {
18
20
  return Array.from(this.iterate(params));
@@ -21,6 +23,8 @@ class PreparedStatement {
21
23
  this.stmt.run(params);
22
24
  if (this.discardAfter)
23
25
  this.stmt.free();
26
+ else
27
+ this.stmt.reset();
24
28
  return { rowsAffected: this.db.getRowsModified() };
25
29
  }
26
30
  get(params) {
@@ -30,6 +34,8 @@ class PreparedStatement {
30
34
  this.stmt.run(params);
31
35
  if (this.discardAfter)
32
36
  this.stmt.free();
37
+ else
38
+ this.stmt.reset();
33
39
  }
34
40
  }
35
41
  class SqlJsDriver extends Driver.Sync {
@@ -68,7 +68,7 @@ declare abstract class AsyncDriver extends DriverBase {
68
68
  migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
69
69
  executeQuery(query: QueryData, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<unknown>;
70
70
  iterate<T>(cursor: Select<T>): AsyncIterable<T>;
71
- transaction<T>(run: (query: AsyncDriver) => T): Promise<T>;
71
+ transaction<T>(run: (query: AsyncDriver) => Promise<T>): Promise<T>;
72
72
  }
73
73
  declare class SyncWrapper extends AsyncDriver {
74
74
  private sync;
@@ -713,7 +713,7 @@ class Formatter {
713
713
  }
714
714
  stmt.identifier(field);
715
715
  if (asBoolean)
716
- stmt.add(`, 'true', 'false'))`);
716
+ stmt.raw(`, 'true', 'false'))`);
717
717
  if (asJson)
718
718
  stmt.closeParenthesis();
719
719
  if (jsonColumn && !asJson)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -48,6 +48,7 @@
48
48
  },
49
49
  "devDependencies": {
50
50
  "@cloudflare/workers-types": "^4.20230628.0",
51
+ "@sqlite.org/sqlite-wasm": "^3.42.0-build4",
51
52
  "@types/better-sqlite3": "^5.4.1",
52
53
  "@types/glob": "^8.0.0",
53
54
  "@types/sql.js": "^1.4.2",