rado 0.4.9 → 1.0.0-preview.0

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.
Files changed (227) hide show
  1. package/dist/core/Builder.d.ts +36 -0
  2. package/dist/core/Builder.js +99 -0
  3. package/dist/core/Column.d.ts +38 -0
  4. package/dist/core/Column.js +59 -0
  5. package/dist/core/Constraint.d.ts +39 -0
  6. package/dist/core/Constraint.js +87 -0
  7. package/dist/core/Database.d.ts +43 -0
  8. package/dist/core/Database.js +58 -0
  9. package/dist/core/Dialect.d.ts +6 -0
  10. package/dist/core/Dialect.js +16 -0
  11. package/dist/core/Driver.d.ts +28 -0
  12. package/dist/core/Emitter.d.ts +35 -0
  13. package/dist/core/Emitter.js +153 -0
  14. package/dist/core/Index.d.ts +28 -0
  15. package/dist/core/Index.js +50 -0
  16. package/dist/core/Internal.d.ts +67 -0
  17. package/dist/core/Internal.js +62 -0
  18. package/dist/core/MetaData.d.ts +27 -0
  19. package/dist/core/Param.d.ts +9 -0
  20. package/dist/core/Param.js +14 -0
  21. package/dist/core/Query.d.ts +40 -0
  22. package/dist/core/Query.js +70 -0
  23. package/dist/core/Resolver.d.ts +23 -0
  24. package/dist/core/Resolver.js +54 -0
  25. package/dist/core/Schema.d.ts +4 -0
  26. package/dist/core/Schema.js +13 -0
  27. package/dist/core/Selection.d.ts +27 -0
  28. package/dist/core/Selection.js +88 -0
  29. package/dist/core/Sql.d.ts +49 -0
  30. package/dist/core/Sql.js +153 -0
  31. package/dist/core/Table.d.ts +56 -0
  32. package/dist/core/Table.js +103 -0
  33. package/dist/core/Types.d.ts +3 -0
  34. package/dist/core/Virtual.d.ts +2 -0
  35. package/dist/core/Virtual.js +28 -0
  36. package/dist/core/expr/Aggregate.d.ts +10 -0
  37. package/dist/core/expr/Aggregate.js +38 -0
  38. package/dist/core/expr/Conditions.d.ts +40 -0
  39. package/dist/core/expr/Conditions.js +137 -0
  40. package/dist/core/expr/Field.d.ts +14 -0
  41. package/dist/core/expr/Field.js +15 -0
  42. package/dist/core/expr/Functions.d.ts +6 -0
  43. package/dist/core/expr/Functions.js +11 -0
  44. package/dist/core/expr/Input.d.ts +3 -0
  45. package/dist/core/expr/Input.js +14 -0
  46. package/dist/core/query/CreateTable.d.ts +13 -0
  47. package/dist/core/query/CreateTable.js +23 -0
  48. package/dist/core/query/Delete.d.ts +19 -0
  49. package/dist/core/query/Delete.js +44 -0
  50. package/dist/core/query/DropTable.d.ts +13 -0
  51. package/dist/core/query/DropTable.js +23 -0
  52. package/dist/core/query/Insert.d.ts +43 -0
  53. package/dist/core/query/Insert.js +111 -0
  54. package/dist/core/query/Select.d.ts +93 -0
  55. package/dist/core/query/Select.js +196 -0
  56. package/dist/core/query/Union.d.ts +27 -0
  57. package/dist/core/query/Union.js +86 -0
  58. package/dist/core/query/Update.d.ts +23 -0
  59. package/dist/core/query/Update.js +55 -0
  60. package/dist/driver/better-sqlite3.d.ts +3 -16
  61. package/dist/driver/better-sqlite3.js +27 -33
  62. package/dist/driver/bun-sqlite.d.ts +3 -18
  63. package/dist/driver/bun-sqlite.js +26 -42
  64. package/dist/driver/pg.d.ts +25 -0
  65. package/dist/driver/pg.js +81 -0
  66. package/dist/driver/pglite.d.ts +24 -0
  67. package/dist/driver/pglite.js +59 -0
  68. package/dist/driver/sql.js.d.ts +3 -16
  69. package/dist/driver/sql.js.js +41 -47
  70. package/dist/index.d.ts +16 -21
  71. package/dist/index.js +16 -14
  72. package/dist/migrate/Scan.d.ts +1 -0
  73. package/dist/migrate/Scan.js +0 -0
  74. package/dist/postgres/PostgresColumns.d.ts +10 -0
  75. package/dist/postgres/PostgresColumns.js +58 -0
  76. package/dist/postgres/PostgresDialect.d.ts +2 -0
  77. package/dist/postgres/PostgresDialect.js +58 -0
  78. package/dist/postgres/PostgresSchema.d.ts +1 -0
  79. package/dist/postgres/PostgresSchema.js +4 -0
  80. package/dist/postgres/PostgresTable.d.ts +1 -0
  81. package/dist/postgres/PostgresTable.js +4 -0
  82. package/dist/postgres.d.ts +2 -0
  83. package/dist/postgres.js +2 -0
  84. package/dist/sqlite/SqliteColumns.d.ts +9 -0
  85. package/dist/sqlite/SqliteColumns.js +63 -0
  86. package/dist/sqlite/SqliteDialect.d.ts +2 -0
  87. package/dist/sqlite/SqliteDialect.js +51 -0
  88. package/dist/sqlite/SqliteFunctions.d.ts +8 -167
  89. package/dist/sqlite/SqliteFunctions.js +6 -3
  90. package/dist/sqlite/SqliteTable.d.ts +1 -0
  91. package/dist/sqlite/SqliteTable.js +4 -0
  92. package/dist/sqlite.d.ts +3 -2
  93. package/dist/sqlite.js +3 -2
  94. package/dist/src/core/Builder.d.ts +36 -0
  95. package/dist/src/core/Column.d.ts +38 -0
  96. package/dist/src/core/Constraint.d.ts +39 -0
  97. package/dist/src/core/Database.d.ts +43 -0
  98. package/dist/src/core/Dialect.d.ts +6 -0
  99. package/dist/src/core/Driver.d.ts +28 -0
  100. package/dist/src/core/Emitter.d.ts +35 -0
  101. package/dist/src/core/Index.d.ts +28 -0
  102. package/dist/src/core/Internal.d.ts +67 -0
  103. package/dist/src/core/MetaData.d.ts +27 -0
  104. package/dist/src/core/Param.d.ts +9 -0
  105. package/dist/src/core/Query.d.ts +40 -0
  106. package/dist/src/core/Resolver.d.ts +23 -0
  107. package/dist/src/core/Schema.d.ts +4 -0
  108. package/dist/src/core/Selection.d.ts +27 -0
  109. package/dist/src/core/Sql.d.ts +49 -0
  110. package/dist/src/core/Table.d.ts +56 -0
  111. package/dist/src/core/Types.d.ts +3 -0
  112. package/dist/src/core/Virtual.d.ts +2 -0
  113. package/dist/src/core/expr/Aggregate.d.ts +10 -0
  114. package/dist/src/core/expr/Conditions.d.ts +40 -0
  115. package/dist/src/core/expr/Field.d.ts +14 -0
  116. package/dist/src/core/expr/Functions.d.ts +6 -0
  117. package/dist/src/core/expr/Input.d.ts +3 -0
  118. package/dist/src/core/query/CreateTable.d.ts +13 -0
  119. package/dist/src/core/query/Delete.d.ts +19 -0
  120. package/dist/src/core/query/DropTable.d.ts +13 -0
  121. package/dist/src/core/query/Insert.d.ts +43 -0
  122. package/dist/src/core/query/Select.d.ts +93 -0
  123. package/dist/src/core/query/Union.d.ts +27 -0
  124. package/dist/src/core/query/Update.d.ts +23 -0
  125. package/dist/src/driver/better-sqlite3.d.ts +3 -0
  126. package/dist/src/driver/bun-sqlite.d.ts +4 -0
  127. package/dist/src/driver/pg.d.ts +25 -0
  128. package/dist/src/driver/pglite.d.ts +24 -0
  129. package/dist/src/driver/sql.js.d.ts +3 -0
  130. package/dist/src/index.d.ts +16 -0
  131. package/dist/src/migrate/Scan.d.ts +1 -0
  132. package/dist/src/postgres/PostgresColumns.d.ts +10 -0
  133. package/dist/src/postgres/PostgresDialect.d.ts +2 -0
  134. package/dist/src/postgres/PostgresSchema.d.ts +1 -0
  135. package/dist/src/postgres/PostgresTable.d.ts +1 -0
  136. package/dist/src/postgres.d.ts +2 -0
  137. package/dist/src/sqlite/SqliteColumns.d.ts +9 -0
  138. package/dist/src/sqlite/SqliteDialect.d.ts +2 -0
  139. package/dist/src/sqlite/SqliteFunctions.d.ts +8 -0
  140. package/dist/src/sqlite/SqliteTable.d.ts +1 -0
  141. package/dist/src/sqlite.d.ts +3 -0
  142. package/dist/src/universal/UniversalColumns.d.ts +6 -0
  143. package/dist/src/universal/UniversalTransaction.d.ts +3 -0
  144. package/dist/src/universal.d.ts +2 -0
  145. package/dist/test/Suite.d.ts +11 -0
  146. package/dist/test/TestDriver.d.ts +2 -0
  147. package/dist/test/TestUtils.d.ts +4 -0
  148. package/dist/test/core/Expr.test.d.ts +1 -0
  149. package/dist/test/core/Selection.test.d.ts +1 -0
  150. package/dist/test/core/Sql.test.d.ts +1 -0
  151. package/dist/test/core/SqliteFunctions.test.d.ts +1 -0
  152. package/dist/test/core/Table.test.d.ts +1 -0
  153. package/dist/test/driver/better-sqlite3.test.d.ts +1 -0
  154. package/dist/test/driver/bun-sqlite.test.d.ts +1 -0
  155. package/dist/test/driver/pg.test.d.ts +1 -0
  156. package/dist/test/driver/pglite.test.d.ts +1 -0
  157. package/dist/test/driver/sql.js.test.d.ts +1 -0
  158. package/dist/test/query/CreateTable.test.d.ts +1 -0
  159. package/dist/test/query/Delete.test.d.ts +1 -0
  160. package/dist/test/query/DropTable.test.d.ts +1 -0
  161. package/dist/test/query/Insert.test.d.ts +1 -0
  162. package/dist/test/query/Select.test.d.ts +1 -0
  163. package/dist/test/query/Union.test.d.ts +1 -0
  164. package/dist/test/query/Update.test.d.ts +1 -0
  165. package/dist/universal/UniversalColumns.d.ts +6 -0
  166. package/dist/universal/UniversalColumns.js +56 -0
  167. package/dist/universal/UniversalTransaction.d.ts +3 -0
  168. package/dist/universal/UniversalTransaction.js +14 -0
  169. package/dist/universal.d.ts +2 -0
  170. package/dist/universal.js +2 -0
  171. package/package.json +25 -43
  172. package/dist/define/Column.d.ts +0 -122
  173. package/dist/define/Column.js +0 -183
  174. package/dist/define/Expr.d.ts +0 -185
  175. package/dist/define/Expr.js +0 -463
  176. package/dist/define/Fields.d.ts +0 -18
  177. package/dist/define/Functions.d.ts +0 -5
  178. package/dist/define/Functions.js +0 -12
  179. package/dist/define/Id.d.ts +0 -3
  180. package/dist/define/Index.d.ts +0 -17
  181. package/dist/define/Index.js +0 -19
  182. package/dist/define/Ops.d.ts +0 -36
  183. package/dist/define/Ops.js +0 -114
  184. package/dist/define/OrderBy.d.ts +0 -9
  185. package/dist/define/OrderBy.js +0 -8
  186. package/dist/define/Param.d.ts +0 -18
  187. package/dist/define/Param.js +0 -27
  188. package/dist/define/Query.d.ts +0 -268
  189. package/dist/define/Query.js +0 -596
  190. package/dist/define/Schema.d.ts +0 -12
  191. package/dist/define/Schema.js +0 -140
  192. package/dist/define/Selection.d.ts +0 -21
  193. package/dist/define/Selection.js +0 -6
  194. package/dist/define/Sql.d.ts +0 -6
  195. package/dist/define/Sql.js +0 -21
  196. package/dist/define/Table.d.ts +0 -75
  197. package/dist/define/Table.js +0 -138
  198. package/dist/define/Target.d.ts +0 -47
  199. package/dist/define/Target.js +0 -68
  200. package/dist/define/VirtualTable.d.ts +0 -31
  201. package/dist/define/VirtualTable.js +0 -39
  202. package/dist/driver/@sqlite.org/sqlite-wasm.d.ts +0 -17
  203. package/dist/driver/@sqlite.org/sqlite-wasm.js +0 -80
  204. package/dist/driver/d1.d.ts +0 -22
  205. package/dist/driver/d1.js +0 -71
  206. package/dist/driver/sqlite3.d.ts +0 -22
  207. package/dist/driver/sqlite3.js +0 -132
  208. package/dist/lib/Driver.d.ts +0 -111
  209. package/dist/lib/Driver.js +0 -398
  210. package/dist/lib/Formatter.d.ts +0 -106
  211. package/dist/lib/Formatter.js +0 -898
  212. package/dist/lib/Sanitizer.d.ts +0 -5
  213. package/dist/lib/SqlError.d.ts +0 -3
  214. package/dist/lib/SqlError.js +0 -10
  215. package/dist/lib/Statement.d.ts +0 -108
  216. package/dist/lib/Statement.js +0 -190
  217. package/dist/sqlite/SqliteFormatter.d.ts +0 -11
  218. package/dist/sqlite/SqliteFormatter.js +0 -66
  219. package/dist/sqlite/SqliteSchema.d.ts +0 -25
  220. package/dist/sqlite/SqliteSchema.js +0 -46
  221. package/dist/util/Alias.d.ts +0 -1
  222. package/dist/util/Alias.js +0 -6
  223. package/dist/util/Callable.d.ts +0 -16
  224. package/dist/util/Callable.js +0 -9
  225. /package/dist/{define/Fields.js → core/Driver.js} +0 -0
  226. /package/dist/{define/Id.js → core/MetaData.js} +0 -0
  227. /package/dist/{lib/Sanitizer.js → core/Types.js} +0 -0
@@ -0,0 +1,81 @@
1
+ import pg from "pg";
2
+ import { AsyncDatabase } from "../core/Database.ts";
3
+ import { postgresDialect } from "../postgres/PostgresDialect.ts";
4
+ class PreparedStatement {
5
+ constructor(client, sql, name) {
6
+ this.client = client;
7
+ this.sql = sql;
8
+ this.name = name;
9
+ }
10
+ all(params) {
11
+ return this.client.query({
12
+ name: this.name,
13
+ text: this.sql,
14
+ values: params
15
+ }).then((res) => res.rows);
16
+ }
17
+ async run(params) {
18
+ await this.client.query(
19
+ {
20
+ name: this.name,
21
+ text: this.sql,
22
+ values: params
23
+ },
24
+ params
25
+ );
26
+ }
27
+ get(params) {
28
+ return this.all(params).then((rows) => rows[0] ?? null);
29
+ }
30
+ values(params) {
31
+ return this.client.query({
32
+ name: this.name,
33
+ text: this.sql,
34
+ values: params,
35
+ rowMode: "array"
36
+ }).then((res) => res.rows);
37
+ }
38
+ free() {
39
+ }
40
+ }
41
+ class PgDriver {
42
+ constructor(client) {
43
+ this.client = client;
44
+ }
45
+ async exec(query) {
46
+ await this.client.query(query);
47
+ }
48
+ prepare(sql, name) {
49
+ return new PreparedStatement(this.client, sql, name);
50
+ }
51
+ async close() {
52
+ if ("end" in this.client)
53
+ return this.client.end();
54
+ if ("release" in this.client)
55
+ return this.client.release();
56
+ }
57
+ async transaction(run, options, depth) {
58
+ const client = this.client instanceof pg.Pool ? await this.client.connect() : this.client;
59
+ try {
60
+ await client.query(depth > 0 ? `savepoint d${depth}` : "begin");
61
+ const result = await run(new PgDriver(client));
62
+ await client.query(depth > 0 ? `release savepoint d${depth}` : "commit");
63
+ return result;
64
+ } catch (error) {
65
+ await client.query(
66
+ depth > 0 ? `rollback to savepoint d${depth}` : "rollback"
67
+ );
68
+ throw error;
69
+ } finally {
70
+ if ("release" in client)
71
+ client.release();
72
+ }
73
+ }
74
+ }
75
+ function connect(client) {
76
+ return new AsyncDatabase(new PgDriver(client), postgresDialect);
77
+ }
78
+ export {
79
+ PgDriver,
80
+ connect
81
+ };
@@ -0,0 +1,24 @@
1
+ import type { PGlite, Transaction } from '@electric-sql/pglite';
2
+ import type { AsyncDriver, AsyncStatement } from '../core/Driver.ts';
3
+ import { AsyncDatabase, type TransactionOptions } from '../index.ts';
4
+ type Queryable = PGlite | Transaction;
5
+ declare class PreparedStatement implements AsyncStatement {
6
+ private client;
7
+ private sql;
8
+ constructor(client: Queryable, sql: string);
9
+ all(params: Array<unknown>): Promise<Array<object>>;
10
+ run(params: Array<unknown>): Promise<void>;
11
+ get(params: Array<unknown>): Promise<object>;
12
+ values(params: Array<unknown>): Promise<unknown[][]>;
13
+ free(): void;
14
+ }
15
+ export declare class PGliteDriver implements AsyncDriver {
16
+ private client;
17
+ constructor(client: Queryable);
18
+ exec(query: string): Promise<void>;
19
+ close(): Promise<void>;
20
+ prepare(sql: string): PreparedStatement;
21
+ transaction<T>(run: (inner: AsyncDriver) => Promise<T>, options: TransactionOptions['postgres']): Promise<T>;
22
+ }
23
+ export declare function connect(db: PGlite): AsyncDatabase<"postgres">;
24
+ export {};
@@ -0,0 +1,59 @@
1
+ import { AsyncDatabase } from "../index.ts";
2
+ import { postgresDialect } from "../postgres/PostgresDialect.ts";
3
+ class PreparedStatement {
4
+ constructor(client, sql) {
5
+ this.client = client;
6
+ this.sql = sql;
7
+ }
8
+ all(params) {
9
+ return this.client.query(this.sql, params, {
10
+ rowMode: "object"
11
+ }).then((res) => res.rows);
12
+ }
13
+ async run(params) {
14
+ await this.client.query(this.sql, params, {
15
+ rowMode: "array"
16
+ });
17
+ }
18
+ get(params) {
19
+ return this.all(params).then((rows) => rows[0] ?? null);
20
+ }
21
+ values(params) {
22
+ return this.client.query(this.sql, params, {
23
+ rowMode: "array"
24
+ }).then((res) => res.rows);
25
+ }
26
+ free() {
27
+ }
28
+ }
29
+ class PGliteDriver {
30
+ constructor(client) {
31
+ this.client = client;
32
+ }
33
+ async exec(query) {
34
+ await this.client.exec(query);
35
+ }
36
+ close() {
37
+ if ("close" in this.client) {
38
+ return Promise.resolve();
39
+ }
40
+ throw new Error("Cannot close a transaction");
41
+ }
42
+ prepare(sql) {
43
+ return new PreparedStatement(this.client, sql);
44
+ }
45
+ async transaction(run, options) {
46
+ if ("transaction" in this.client)
47
+ return this.client.transaction((tx) => {
48
+ return run(new PGliteDriver(tx));
49
+ });
50
+ throw new Error("Cannot nest transactions in PGLite");
51
+ }
52
+ }
53
+ function connect(db) {
54
+ return new AsyncDatabase(new PGliteDriver(db), postgresDialect);
55
+ }
56
+ export {
57
+ PGliteDriver,
58
+ connect
59
+ };
@@ -1,16 +1,3 @@
1
- import type { Database } from 'sql.js';
2
- import { SchemaInstructions } from '../define/Schema.js';
3
- import { Driver, DriverOptions } from '../lib/Driver.js';
4
- import { Statement } from '../lib/Statement.js';
5
- import { SqliteSchema } from '../sqlite/SqliteSchema.js';
6
- export declare class SqlJsDriver extends Driver.Sync {
7
- db: Database;
8
- tableData?: (tableName: string) => Array<SqliteSchema.Column>;
9
- indexData?: (tableName: string) => Array<SqliteSchema.Index>;
10
- constructor(db: Database, 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: Database, options?: DriverOptions): SqlJsDriver;
1
+ import type { Database as Client } from 'sql.js';
2
+ import { SyncDatabase } from '../core/Database.ts';
3
+ export declare function connect(db: Client): SyncDatabase<import("../core/MetaData.ts").QueryDialect>;
@@ -1,75 +1,69 @@
1
- import { Driver } from "../lib/Driver.js";
2
- import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
3
- import { SqliteSchema } from "../sqlite/SqliteSchema.js";
1
+ import { SyncDatabase } from "../core/Database.ts";
2
+ import { sqliteDialect } from "../sqlite.ts";
4
3
  class PreparedStatement {
5
- constructor(db, stmt, discardAfter) {
6
- this.db = db;
4
+ constructor(client, stmt) {
5
+ this.client = client;
7
6
  this.stmt = stmt;
8
- this.discardAfter = discardAfter;
9
7
  }
10
8
  *iterate(params) {
11
9
  this.stmt.bind(params);
12
10
  while (this.stmt.step())
13
11
  yield this.stmt.getAsObject();
14
- if (this.discardAfter)
15
- this.stmt.free();
16
- else
17
- this.stmt.reset();
12
+ this.stmt.reset();
13
+ }
14
+ *iterateValues(params) {
15
+ this.stmt.bind(params);
16
+ while (this.stmt.step())
17
+ yield this.stmt.get();
18
+ this.stmt.reset();
18
19
  }
19
20
  all(params) {
20
21
  return Array.from(this.iterate(params));
21
22
  }
22
23
  run(params) {
23
24
  this.stmt.run(params);
24
- if (this.discardAfter)
25
- this.stmt.free();
26
- else
27
- this.stmt.reset();
28
- return { rowsAffected: this.db.getRowsModified() };
25
+ this.stmt.reset();
26
+ return { rowsAffected: this.client.getRowsModified() };
29
27
  }
30
28
  get(params) {
31
- return this.all(params)[0];
29
+ return this.all(params)[0] ?? null;
32
30
  }
33
- execute(params) {
34
- this.stmt.run(params);
35
- if (this.discardAfter)
36
- this.stmt.free();
37
- else
38
- this.stmt.reset();
31
+ values(params) {
32
+ return Array.from(this.iterateValues(params));
33
+ }
34
+ free() {
35
+ this.stmt.free();
39
36
  }
40
37
  }
41
- class SqlJsDriver extends Driver.Sync {
42
- constructor(db, options) {
43
- super(new SqliteFormatter(), options);
44
- this.db = db;
38
+ class SqlJsDriver {
39
+ constructor(client) {
40
+ this.client = client;
45
41
  }
46
- tableData;
47
- indexData;
48
- close() {
49
- this.db.close();
42
+ exec(query) {
43
+ this.client.exec(query);
50
44
  }
51
- prepareStatement(stmt, discardAfter) {
52
- return new PreparedStatement(
53
- this.db,
54
- this.db.prepare(stmt.sql),
55
- discardAfter
56
- );
45
+ close() {
46
+ this.client.close();
57
47
  }
58
- schemaInstructions(tableName) {
59
- this.tableData = this.tableData || (this.tableData = this.prepare(SqliteSchema.tableData));
60
- this.indexData = this.indexData || (this.indexData = this.prepare(SqliteSchema.indexData));
61
- const columnData = this.tableData(tableName);
62
- const indexData = this.indexData(tableName);
63
- return SqliteSchema.createInstructions(columnData, indexData);
48
+ prepare(sql) {
49
+ return new PreparedStatement(this.client, this.client.prepare(sql));
64
50
  }
65
- export() {
66
- return this.db.export();
51
+ transaction(run, options, depth) {
52
+ const behavior = options.behavior ?? "deferred";
53
+ this.exec(depth > 0 ? `savepoint d${depth}` : `begin ${behavior}`);
54
+ try {
55
+ const result = run(this);
56
+ this.exec(depth > 0 ? `release d${depth}` : "commit");
57
+ return result;
58
+ } catch (error) {
59
+ this.exec(depth > 0 ? `rollback to d${depth}` : "rollback");
60
+ throw error;
61
+ }
67
62
  }
68
63
  }
69
- function connect(db, options) {
70
- return new SqlJsDriver(db, options);
64
+ function connect(db) {
65
+ return new SyncDatabase(new SqlJsDriver(db), sqliteDialect);
71
66
  }
72
67
  export {
73
- SqlJsDriver,
74
68
  connect
75
69
  };
package/dist/index.d.ts CHANGED
@@ -1,21 +1,16 @@
1
- export * from './define/Column.js';
2
- export * from './define/Expr.js';
3
- export type { Fields } from './define/Fields.js';
4
- export * from './define/Functions.js';
5
- export type { Id } from './define/Id.js';
6
- export * from './define/Index.js';
7
- export * from './define/Ops.js';
8
- export * from './define/OrderBy.js';
9
- export * from './define/Param.js';
10
- export * from './define/Query.js';
11
- export * from './define/Schema.js';
12
- export type { Selection } from './define/Selection.js';
13
- export * from './define/Sql.js';
14
- export * from './define/Table.js';
15
- export * from './define/Target.js';
16
- export * from './define/VirtualTable.js';
17
- export type { Driver, DriverOptions } from './lib/Driver.js';
18
- export type { FormatContext, Formatter } from './lib/Formatter.js';
19
- export type { Sanitizer } from './lib/Sanitizer.js';
20
- export * from './lib/SqlError.js';
21
- export type { Statement } from './lib/Statement.js';
1
+ export * from './core/Column.ts';
2
+ export * from './core/Constraint.ts';
3
+ export * from './core/Database.ts';
4
+ export * from './core/Driver.ts';
5
+ export * from './core/Index.ts';
6
+ export * from './core/Query.ts';
7
+ export * from './core/Selection.ts';
8
+ export * from './core/Sql.ts';
9
+ export * from './core/Table.ts';
10
+ export * from './core/expr/Conditions.ts';
11
+ export * from './core/query/CreateTable.ts';
12
+ export * from './core/query/Delete.ts';
13
+ export * from './core/query/Insert.ts';
14
+ export * from './core/query/Select.ts';
15
+ export * from './core/query/Union.ts';
16
+ export * from './core/query/Update.ts';
package/dist/index.js CHANGED
@@ -1,14 +1,16 @@
1
- export * from "./define/Column.js";
2
- export * from "./define/Expr.js";
3
- export * from "./define/Functions.js";
4
- export * from "./define/Index.js";
5
- export * from "./define/Ops.js";
6
- export * from "./define/OrderBy.js";
7
- export * from "./define/Param.js";
8
- export * from "./define/Query.js";
9
- export * from "./define/Schema.js";
10
- export * from "./define/Sql.js";
11
- export * from "./define/Table.js";
12
- export * from "./define/Target.js";
13
- export * from "./define/VirtualTable.js";
14
- export * from "./lib/SqlError.js";
1
+ export * from "./core/Column.ts";
2
+ export * from "./core/Constraint.ts";
3
+ export * from "./core/Database.ts";
4
+ export * from "./core/Driver.ts";
5
+ export * from "./core/Index.ts";
6
+ export * from "./core/Query.ts";
7
+ export * from "./core/Selection.ts";
8
+ export * from "./core/Sql.ts";
9
+ export * from "./core/Table.ts";
10
+ export * from "./core/expr/Conditions.ts";
11
+ export * from "./core/query/CreateTable.ts";
12
+ export * from "./core/query/Delete.ts";
13
+ export * from "./core/query/Insert.ts";
14
+ export * from "./core/query/Select.ts";
15
+ export * from "./core/query/Union.ts";
16
+ export * from "./core/query/Update.ts";
@@ -0,0 +1 @@
1
+ export {};
File without changes
@@ -0,0 +1,10 @@
1
+ import { Column, JsonColumn } from '../core/Column.ts';
2
+ export declare function serial(name?: string): Column<number | null>;
3
+ export declare function boolean(name?: string): Column<boolean | null>;
4
+ export declare function integer(name?: string): Column<number | null>;
5
+ export declare function blob(name?: string): Column<Uint8Array | null>;
6
+ export declare function text(name?: string): Column<string | null>;
7
+ export declare function real(name?: string): Column<number | null>;
8
+ export declare function numeric(name?: string): Column<number | null>;
9
+ export declare function json<T>(name?: string): JsonColumn<T | null>;
10
+ export declare function jsonb<T>(name?: string): JsonColumn<T | null>;
@@ -0,0 +1,58 @@
1
+ import { Column, JsonColumn } from "../core/Column.ts";
2
+ import { sql } from "../core/Sql.ts";
3
+ function serial(name) {
4
+ return new Column({
5
+ name,
6
+ type: sql`serial`
7
+ });
8
+ }
9
+ function boolean(name) {
10
+ return new Column({
11
+ name,
12
+ type: sql`boolean`
13
+ });
14
+ }
15
+ function integer(name) {
16
+ return new Column({ name, type: sql`integer` });
17
+ }
18
+ function blob(name) {
19
+ return new Column({ name, type: sql`blob` });
20
+ }
21
+ function text(name) {
22
+ return new Column({ name, type: sql`text` });
23
+ }
24
+ function real(name) {
25
+ return new Column({ name, type: sql`real` });
26
+ }
27
+ function numeric(name) {
28
+ return new Column({ name, type: sql`numeric` });
29
+ }
30
+ function json(name) {
31
+ return new JsonColumn({
32
+ name,
33
+ type: sql`json`,
34
+ mapToDriverValue(value) {
35
+ return JSON.stringify(value);
36
+ }
37
+ });
38
+ }
39
+ function jsonb(name) {
40
+ return new JsonColumn({
41
+ name,
42
+ type: sql`jsonb`,
43
+ mapToDriverValue(value) {
44
+ return JSON.stringify(value);
45
+ }
46
+ });
47
+ }
48
+ export {
49
+ blob,
50
+ boolean,
51
+ integer,
52
+ json,
53
+ jsonb,
54
+ numeric,
55
+ real,
56
+ serial,
57
+ text
58
+ };
@@ -0,0 +1,2 @@
1
+ import { Emitter } from '../core/Emitter.ts';
2
+ export declare const postgresDialect: (input: import("../core/Internal.ts").HasSql<unknown> | import("../core/Internal.ts").HasQuery) => Emitter;
@@ -0,0 +1,58 @@
1
+ import { dialect } from "../core/Dialect.ts";
2
+ import { Emitter } from "../core/Emitter.ts";
3
+ import { NamedParam, ValueParam } from "../core/Param.ts";
4
+ const DOUBLE_QUOTE = '"';
5
+ const ESCAPE_DOUBLE_QUOTE = '""';
6
+ const MATCH_DOUBLE_QUOTE = /"/g;
7
+ const SINGLE_QUOTE = "'";
8
+ const ESCAPE_SINGLE_QUOTE = "''";
9
+ const MATCH_SINGLE_QUOTE = /'/g;
10
+ class PostgresEmitter extends Emitter {
11
+ paramIndex = 0;
12
+ emitValue(value) {
13
+ this.sql += `$${++this.paramIndex}`;
14
+ this.params.push(new ValueParam(value));
15
+ }
16
+ emitJsonPath(path) {
17
+ for (let i = 0; i < path.length; i++) {
18
+ const access = path[i];
19
+ if (i <= path.length - 2)
20
+ this.sql += "->";
21
+ else if (i === path.length - 1)
22
+ this.sql += "->>";
23
+ if (typeof access === "number")
24
+ this.sql += access;
25
+ else
26
+ this.sql += this.quoteString(access);
27
+ }
28
+ }
29
+ emitInline(value) {
30
+ if (value === null || value === void 0)
31
+ return this.sql += "null";
32
+ if (typeof value === "number" || typeof value === "boolean")
33
+ return this.sql += value;
34
+ if (typeof value === "string")
35
+ return this.sql += this.quoteString(value);
36
+ this.sql += `json(${this.quoteString(JSON.stringify(value))})`;
37
+ }
38
+ emitPlaceholder(name) {
39
+ this.sql += `$${++this.paramIndex}`;
40
+ this.params.push(new NamedParam(name));
41
+ }
42
+ quoteString(input) {
43
+ return SINGLE_QUOTE + input.replace(MATCH_SINGLE_QUOTE, ESCAPE_SINGLE_QUOTE) + SINGLE_QUOTE;
44
+ }
45
+ emitIdentifier(identifier) {
46
+ this.sql += DOUBLE_QUOTE + identifier.replace(MATCH_DOUBLE_QUOTE, ESCAPE_DOUBLE_QUOTE) + DOUBLE_QUOTE;
47
+ }
48
+ emitDefaultValue() {
49
+ this.sql += "default";
50
+ }
51
+ emitIdColumn() {
52
+ this.sql += "int generated always as identity";
53
+ }
54
+ }
55
+ const postgresDialect = dialect(PostgresEmitter);
56
+ export {
57
+ postgresDialect
58
+ };
@@ -0,0 +1 @@
1
+ export { schema as pgSchema } from '../core/Schema.ts';
@@ -0,0 +1,4 @@
1
+ import { schema } from "../core/Schema.ts";
2
+ export {
3
+ schema as pgSchema
4
+ };
@@ -0,0 +1 @@
1
+ export { table as pgTable } from '../core/Table.ts';
@@ -0,0 +1,4 @@
1
+ import { table } from "../core/Table.ts";
2
+ export {
3
+ table as pgTable
4
+ };
@@ -0,0 +1,2 @@
1
+ export * from './postgres/PostgresColumns.ts';
2
+ export * from './postgres/PostgresDialect.ts';
@@ -0,0 +1,2 @@
1
+ export * from "./postgres/PostgresColumns.ts";
2
+ export * from "./postgres/PostgresDialect.ts";
@@ -0,0 +1,9 @@
1
+ import { Column, JsonColumn } from '../core/Column.ts';
2
+ export declare function boolean(name?: string): Column<boolean | null>;
3
+ export declare function integer(name?: string): Column<number | null>;
4
+ export declare function blob(name?: string): Column<Uint8Array | null>;
5
+ export declare function text(name?: string): Column<string | null>;
6
+ export declare function real(name?: string): Column<number | null>;
7
+ export declare function numeric(name?: string): Column<number | null>;
8
+ export declare function json<T>(name?: string): JsonColumn<T | null>;
9
+ export declare function jsonb<T>(name?: string): JsonColumn<T | null>;
@@ -0,0 +1,63 @@
1
+ import { Column, JsonColumn } from "../core/Column.ts";
2
+ import { sql } from "../core/Sql.ts";
3
+ function boolean(name) {
4
+ return new Column({
5
+ name,
6
+ type: sql`integer`,
7
+ mapFromDriverValue(value) {
8
+ return value === 1;
9
+ },
10
+ mapToDriverValue(value) {
11
+ return value ? 1 : 0;
12
+ }
13
+ });
14
+ }
15
+ function integer(name) {
16
+ return new Column({ name, type: sql`integer` });
17
+ }
18
+ function blob(name) {
19
+ return new Column({ name, type: sql`blob` });
20
+ }
21
+ function text(name) {
22
+ return new Column({ name, type: sql`text` });
23
+ }
24
+ function real(name) {
25
+ return new Column({ name, type: sql`real` });
26
+ }
27
+ function numeric(name) {
28
+ return new Column({ name, type: sql`numeric` });
29
+ }
30
+ function json(name) {
31
+ return new JsonColumn({
32
+ name,
33
+ type: sql`json`,
34
+ mapFromDriverValue(value) {
35
+ return JSON.parse(value);
36
+ },
37
+ mapToDriverValue(value) {
38
+ return JSON.stringify(value);
39
+ }
40
+ });
41
+ }
42
+ function jsonb(name) {
43
+ return new JsonColumn({
44
+ name,
45
+ type: sql`jsonb`,
46
+ mapFromDriverValue(value) {
47
+ return JSON.parse(value);
48
+ },
49
+ mapToDriverValue(value) {
50
+ return JSON.stringify(value);
51
+ }
52
+ });
53
+ }
54
+ export {
55
+ blob,
56
+ boolean,
57
+ integer,
58
+ json,
59
+ jsonb,
60
+ numeric,
61
+ real,
62
+ text
63
+ };
@@ -0,0 +1,2 @@
1
+ import { Emitter } from '../core/Emitter.ts';
2
+ export declare const sqliteDialect: (input: import("../core/Internal.ts").HasSql<unknown> | import("../core/Internal.ts").HasQuery) => Emitter;
@@ -0,0 +1,51 @@
1
+ import { dialect } from "../core/Dialect.ts";
2
+ import { Emitter } from "../core/Emitter.ts";
3
+ import { NamedParam, ValueParam } from "../core/Param.ts";
4
+ const DOUBLE_QUOTE = '"';
5
+ const ESCAPE_DOUBLE_QUOTE = '""';
6
+ const MATCH_DOUBLE_QUOTE = /"/g;
7
+ const SINGLE_QUOTE = "'";
8
+ const ESCAPE_SINGLE_QUOTE = "''";
9
+ const MATCH_SINGLE_QUOTE = /'/g;
10
+ class SqliteEmitter extends Emitter {
11
+ emitValue(value) {
12
+ this.sql += "?";
13
+ this.params.push(
14
+ new ValueParam(typeof value === "boolean" ? value ? 1 : 0 : value)
15
+ );
16
+ }
17
+ emitJsonPath(path) {
18
+ this.sql += `->>${this.quoteString(`$.${path.join(".")}`)}`;
19
+ }
20
+ emitInline(value) {
21
+ if (value === null || value === void 0)
22
+ return this.sql += "null";
23
+ if (typeof value === "number")
24
+ return this.sql += value;
25
+ if (typeof value === "string")
26
+ return this.sql += this.quoteString(value);
27
+ if (typeof value === "boolean")
28
+ return this.sql += value ? "1" : "0";
29
+ this.sql += `json(${this.quoteString(JSON.stringify(value))})`;
30
+ }
31
+ emitPlaceholder(name) {
32
+ this.sql += "?";
33
+ this.params.push(new NamedParam(name));
34
+ }
35
+ emitIdentifier(identifier) {
36
+ this.sql += DOUBLE_QUOTE + identifier.replace(MATCH_DOUBLE_QUOTE, ESCAPE_DOUBLE_QUOTE) + DOUBLE_QUOTE;
37
+ }
38
+ emitDefaultValue() {
39
+ this.sql += "null";
40
+ }
41
+ quoteString(input) {
42
+ return SINGLE_QUOTE + input.replace(MATCH_SINGLE_QUOTE, ESCAPE_SINGLE_QUOTE) + SINGLE_QUOTE;
43
+ }
44
+ emitIdColumn() {
45
+ this.sql += "integer";
46
+ }
47
+ }
48
+ const sqliteDialect = dialect(SqliteEmitter);
49
+ export {
50
+ sqliteDialect
51
+ };