rado 0.4.1 → 0.4.3

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
 
@@ -112,6 +112,7 @@ export declare namespace ExprData {
112
112
  constructor(target: Target, condition: ExprData);
113
113
  }
114
114
  function create(input: any): ExprData;
115
+ function createAll(...inputs: Array<any>): Array<ExprData>;
115
116
  }
116
117
  /** Expression or value of type T */
117
118
  export type EV<T> = Expr<T> | T;
@@ -137,12 +138,12 @@ export declare class Expr<T> {
137
138
  isGreaterOrEqual(that: EV<any>): Expr<boolean>;
138
139
  isLess(that: EV<any>): Expr<boolean>;
139
140
  isLessOrEqual(that: EV<any>): Expr<boolean>;
140
- add(this: Expr<number>, that: EV<number>): Expr<number>;
141
- subtract(this: Expr<number>, that: EV<number>): Expr<number>;
142
- multiply(this: Expr<number>, that: EV<number>): Expr<number>;
141
+ add(this: Expr<number>, that: EV<number>, ...rest: Array<EV<number>>): Expr<number>;
142
+ subtract(this: Expr<number>, that: EV<number>, ...rest: Array<EV<number>>): Expr<number>;
143
+ multiply(this: Expr<number>, that: EV<number>, ...rest: Array<EV<number>>): Expr<number>;
143
144
  remainder(this: Expr<number>, that: EV<number>): Expr<number>;
144
- divide(this: Expr<number>, that: EV<number>): Expr<number>;
145
- concat(this: Expr<string>, that: EV<string>): Expr<string>;
145
+ divide(this: Expr<number>, that: EV<number>, ...rest: Array<EV<number>>): Expr<number>;
146
+ concat(this: Expr<string>, that: EV<string>, ...rest: Array<EV<string>>): Expr<string>;
146
147
  like(this: Expr<string>, that: EV<string>): Expr<boolean>;
147
148
  glob(this: Expr<string>, that: EV<string>): Expr<boolean>;
148
149
  match(this: Expr<string>, that: EV<string>): Expr<boolean>;
@@ -147,6 +147,10 @@ var ExprData;
147
147
  return new ExprData2.Param(new ParamData.Value(input));
148
148
  }
149
149
  ExprData2.create = create;
150
+ function createAll(...inputs) {
151
+ return inputs.map(create);
152
+ }
153
+ ExprData2.createAll = createAll;
150
154
  })(ExprData || (ExprData = {}));
151
155
  class Expr {
152
156
  constructor(expr) {
@@ -273,19 +277,25 @@ class Expr {
273
277
  )
274
278
  );
275
279
  }
276
- add(that) {
280
+ add(that, ...rest) {
277
281
  return new Expr(
278
- new ExprData.BinOp("Add" /* Add */, this[Expr.Data], ExprData.create(that))
282
+ ExprData.createAll(this, that, ...rest).reduce(
283
+ (a, b) => new ExprData.BinOp("Add" /* Add */, a, b)
284
+ )
279
285
  );
280
286
  }
281
- subtract(that) {
287
+ subtract(that, ...rest) {
282
288
  return new Expr(
283
- new ExprData.BinOp("Subt" /* Subt */, this[Expr.Data], ExprData.create(that))
289
+ ExprData.createAll(this, that, ...rest).reduce(
290
+ (a, b) => new ExprData.BinOp("Subt" /* Subt */, a, b)
291
+ )
284
292
  );
285
293
  }
286
- multiply(that) {
294
+ multiply(that, ...rest) {
287
295
  return new Expr(
288
- new ExprData.BinOp("Mult" /* Mult */, this[Expr.Data], ExprData.create(that))
296
+ ExprData.createAll(this, that, ...rest).reduce(
297
+ (a, b) => new ExprData.BinOp("Mult" /* Mult */, a, b)
298
+ )
289
299
  );
290
300
  }
291
301
  remainder(that) {
@@ -293,17 +303,17 @@ class Expr {
293
303
  new ExprData.BinOp("Mod" /* Mod */, this[Expr.Data], ExprData.create(that))
294
304
  );
295
305
  }
296
- divide(that) {
306
+ divide(that, ...rest) {
297
307
  return new Expr(
298
- new ExprData.BinOp("Div" /* Div */, this[Expr.Data], ExprData.create(that))
308
+ ExprData.createAll(this, that, ...rest).reduce(
309
+ (a, b) => new ExprData.BinOp("Div" /* Div */, a, b)
310
+ )
299
311
  );
300
312
  }
301
- concat(that) {
313
+ concat(that, ...rest) {
302
314
  return new Expr(
303
- new ExprData.BinOp(
304
- "Concat" /* Concat */,
305
- this[Expr.Data],
306
- ExprData.create(that)
315
+ ExprData.createAll(this, that, ...rest).reduce(
316
+ (a, b) => new ExprData.BinOp("Concat" /* Concat */, a, b)
307
317
  )
308
318
  );
309
319
  }
@@ -26,10 +26,10 @@ const isGreater = (a, b) => Expr.create(a).isGreater(b);
26
26
  const isGreaterOrEqual = (a, b) => Expr.create(a).isGreaterOrEqual(b);
27
27
  const isLess = (a, b) => Expr.create(a).isLess(b);
28
28
  const isLessOrEqual = (a, b) => Expr.create(a).isLessOrEqual(b);
29
- const add = (a, b, ...rest) => [a, b].concat(rest).map(Expr.create).reduce((a2, b2) => a2.add(b2));
30
- const subtract = (a, b, ...rest) => [a, b].concat(rest).map(Expr.create).reduce((a2, b2) => a2.subtract(b2));
31
- const multiply = (a, b, ...rest) => [a, b].concat(rest).map(Expr.create).reduce((a2, b2) => a2.multiply(b2));
32
- const divide = (a, b, ...rest) => [a, b].concat(rest).map(Expr.create).reduce((a2, b2) => a2.divide(b2));
29
+ const add = (a, b, ...rest) => Expr.create(a).add(b, ...rest);
30
+ const subtract = (a, b, ...rest) => Expr.create(a).subtract(b, ...rest);
31
+ const multiply = (a, b, ...rest) => Expr.create(a).multiply(b, ...rest);
32
+ const divide = (a, b, ...rest) => Expr.create(a).divide(b, ...rest);
33
33
  const remainder = (a, b) => Expr.create(a).remainder(b);
34
34
  const concat = (a, b) => Expr.create(a).concat(b);
35
35
  const like = (a, b) => Expr.create(a).like(b);
@@ -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 {
@@ -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.1",
3
+ "version": "0.4.3",
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",