rado 0.4.8 → 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 -183
  175. package/dist/define/Expr.js +0 -453
  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 -888
  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,196 @@
1
+ import {
2
+ getData,
3
+ getQuery,
4
+ getSelection,
5
+ getTable,
6
+ getTarget,
7
+ hasTable,
8
+ internalData,
9
+ internalQuery,
10
+ internalSelection,
11
+ internalTarget
12
+ } from "../Internal.ts";
13
+ import { Query, QueryData } from "../Query.ts";
14
+ import {
15
+ selection
16
+ } from "../Selection.ts";
17
+ import { sql } from "../Sql.ts";
18
+ import { input } from "../expr/Input.ts";
19
+ import { Union } from "./Union.ts";
20
+ class SelectData extends QueryData {
21
+ select;
22
+ distinct;
23
+ distinctOn;
24
+ from;
25
+ subject;
26
+ where;
27
+ groupBy;
28
+ having;
29
+ orderBy;
30
+ limit;
31
+ offset;
32
+ }
33
+ class Select extends Query {
34
+ [internalData];
35
+ constructor(data) {
36
+ super(data);
37
+ this[internalData] = data;
38
+ }
39
+ as(alias) {
40
+ const fields = getSelection(this).makeVirtual(alias);
41
+ return Object.assign(fields, {
42
+ [internalTarget]: sql`(${getQuery(this)}) as ${sql.identifier(
43
+ alias
44
+ )}`.inlineFields(true)
45
+ });
46
+ }
47
+ from(target) {
48
+ const { select: current } = getData(this);
49
+ const from = getTarget(target);
50
+ const isTable = hasTable(target);
51
+ const selectionInput = current.input ?? (isTable ? target : sql`*`);
52
+ return new Select({
53
+ ...getData(this),
54
+ select: {
55
+ ...current,
56
+ input: selectionInput,
57
+ tables: isTable ? [getTable(target).aliased] : []
58
+ },
59
+ from
60
+ });
61
+ }
62
+ #join(operator, right, on) {
63
+ const { from, select: current } = getData(this);
64
+ const rightTable = getTable(right);
65
+ const addNullable = [];
66
+ if (operator === "right" || operator === "full")
67
+ addNullable.push(...current.tables);
68
+ if (operator === "left" || operator === "full")
69
+ addNullable.push(rightTable.aliased);
70
+ const select = current.type === "selection" ? current : {
71
+ type: "joinTables",
72
+ input: current.type === "allFrom" ? {
73
+ [getTable(current.input).aliased]: current.input,
74
+ [rightTable.aliased]: right
75
+ } : { ...current.input, [rightTable.aliased]: right },
76
+ tables: [...current.tables, rightTable.aliased],
77
+ nullable: current.nullable.concat(addNullable)
78
+ };
79
+ return new Select({
80
+ ...getData(this),
81
+ select,
82
+ from: sql.join([
83
+ from,
84
+ sql.unsafe(`${operator} join`),
85
+ rightTable.target(),
86
+ sql`on ${on}`
87
+ ])
88
+ });
89
+ }
90
+ leftJoin(right, on) {
91
+ return this.#join("left", right, on);
92
+ }
93
+ rightJoin(right, on) {
94
+ return this.#join("right", right, on);
95
+ }
96
+ innerJoin(right, on) {
97
+ return this.#join("inner", right, on);
98
+ }
99
+ fullJoin(right, on) {
100
+ return this.#join("full", right, on);
101
+ }
102
+ where(where) {
103
+ return new Select({ ...getData(this), where });
104
+ }
105
+ groupBy(...exprs) {
106
+ return new Select({
107
+ ...getData(this),
108
+ groupBy: sql.join(exprs, sql.unsafe(", "))
109
+ });
110
+ }
111
+ having(having) {
112
+ return new Select({ ...getData(this), having });
113
+ }
114
+ orderBy(...exprs) {
115
+ return new Select({
116
+ ...getData(this),
117
+ orderBy: sql.join(exprs, sql.unsafe(", "))
118
+ });
119
+ }
120
+ limit(limit) {
121
+ return new Select({ ...getData(this), limit: input(limit) });
122
+ }
123
+ offset(offset) {
124
+ return new Select({
125
+ ...getData(this),
126
+ offset: input(offset)
127
+ });
128
+ }
129
+ union(right) {
130
+ return new Union({
131
+ ...getData(this),
132
+ selection: getSelection(this),
133
+ left: this,
134
+ operator: sql`union`,
135
+ right
136
+ });
137
+ }
138
+ unionAll(right) {
139
+ return new Union({
140
+ ...getData(this),
141
+ selection: getSelection(this),
142
+ left: this,
143
+ operator: sql`union all`,
144
+ right
145
+ });
146
+ }
147
+ intersect(right) {
148
+ return new Union({
149
+ ...getData(this),
150
+ selection: getSelection(this),
151
+ left: this,
152
+ operator: sql`intersect`,
153
+ right
154
+ });
155
+ }
156
+ intersectAll(right) {
157
+ return new Union({
158
+ ...getData(this),
159
+ selection: getSelection(this),
160
+ left: this,
161
+ operator: sql`intersect all`,
162
+ right
163
+ });
164
+ }
165
+ except(right) {
166
+ return new Union({
167
+ ...getData(this),
168
+ selection: getSelection(this),
169
+ left: this,
170
+ operator: sql`except`,
171
+ right
172
+ });
173
+ }
174
+ exceptAll(right) {
175
+ return new Union({
176
+ ...getData(this),
177
+ selection: getSelection(this),
178
+ left: this,
179
+ operator: sql`except all`,
180
+ right
181
+ });
182
+ }
183
+ get [internalSelection]() {
184
+ const { select } = getData(this);
185
+ if (!select.input)
186
+ throw new Error("No selection defined");
187
+ return selection(select.input, new Set(select.nullable));
188
+ }
189
+ get [internalQuery]() {
190
+ return sql.chunk("emitSelect", this);
191
+ }
192
+ }
193
+ export {
194
+ Select,
195
+ SelectData
196
+ };
@@ -0,0 +1,27 @@
1
+ import { internalData, internalQuery, internalSelection, type HasQuery, type HasSelection, type HasSql } from '../Internal.ts';
2
+ import type { IsMysql, IsPostgres, QueryMeta } from '../MetaData.ts';
3
+ import { Query, QueryData } from '../Query.ts';
4
+ import type { Selection } from '../Selection.ts';
5
+ import type { SelectBase } from './Select.ts';
6
+ export declare class UnionData<Meta extends QueryMeta> extends QueryData<Meta> {
7
+ selection: Selection;
8
+ left: HasQuery;
9
+ operator: HasSql;
10
+ right: HasQuery;
11
+ }
12
+ export declare class Union<Result, Meta extends QueryMeta = QueryMeta> extends Query<Result, Meta> implements HasSelection {
13
+ readonly [internalData]: UnionData<Meta>;
14
+ readonly [internalSelection]: Selection;
15
+ constructor(data: UnionData<Meta>);
16
+ union(right: SelectBase<Result, Meta>): Union<Result, Meta>;
17
+ unionAll(right: SelectBase<Result, Meta>): Union<Result, Meta>;
18
+ intersect(right: SelectBase<Result, Meta>): Union<Result, Meta>;
19
+ except(right: SelectBase<Result, Meta>): Union<Result, Meta>;
20
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
21
+ }
22
+ export declare function union<Result, Meta extends QueryMeta>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
23
+ export declare function unionAll<Result, Meta extends QueryMeta>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
24
+ export declare function intersect<Result, Meta extends QueryMeta>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
25
+ export declare function intersectAll<Result, Meta extends IsPostgres | IsMysql>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
26
+ export declare function except<Result, Meta extends QueryMeta>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
27
+ export declare function exceptAll<Result, Meta extends IsPostgres | IsMysql>(left: SelectBase<Result, Meta>, right: SelectBase<Result, Meta>): Union<Result, Meta>;
@@ -0,0 +1,86 @@
1
+ import {
2
+ getData,
3
+ internalData,
4
+ internalQuery,
5
+ internalSelection
6
+ } from "../Internal.ts";
7
+ import { Query, QueryData } from "../Query.ts";
8
+ import { sql } from "../Sql.ts";
9
+ class UnionData extends QueryData {
10
+ selection;
11
+ left;
12
+ operator;
13
+ right;
14
+ }
15
+ class Union extends Query {
16
+ [internalData];
17
+ [internalSelection];
18
+ constructor(data) {
19
+ super(data);
20
+ this[internalData] = data;
21
+ this[internalSelection] = data.selection;
22
+ }
23
+ union(right) {
24
+ return new Union({
25
+ ...getData(this),
26
+ left: this,
27
+ operator: sql`union`,
28
+ right
29
+ });
30
+ }
31
+ unionAll(right) {
32
+ return new Union({
33
+ ...getData(this),
34
+ left: this,
35
+ operator: sql`union all`,
36
+ right
37
+ });
38
+ }
39
+ intersect(right) {
40
+ return new Union({
41
+ ...getData(this),
42
+ left: this,
43
+ operator: sql`intersect`,
44
+ right
45
+ });
46
+ }
47
+ except(right) {
48
+ return new Union({
49
+ ...getData(this),
50
+ left: this,
51
+ operator: sql`except`,
52
+ right
53
+ });
54
+ }
55
+ get [internalQuery]() {
56
+ return sql.chunk("emitUnion", this);
57
+ }
58
+ }
59
+ function union(left, right) {
60
+ return left.union(right);
61
+ }
62
+ function unionAll(left, right) {
63
+ return left.unionAll(right);
64
+ }
65
+ function intersect(left, right) {
66
+ return left.intersect(right);
67
+ }
68
+ function intersectAll(left, right) {
69
+ return left.intersectAll(right);
70
+ }
71
+ function except(left, right) {
72
+ return left.except(right);
73
+ }
74
+ function exceptAll(left, right) {
75
+ return left.exceptAll(right);
76
+ }
77
+ export {
78
+ Union,
79
+ UnionData,
80
+ except,
81
+ exceptAll,
82
+ intersect,
83
+ intersectAll,
84
+ union,
85
+ unionAll
86
+ };
@@ -0,0 +1,23 @@
1
+ import { internalData, internalQuery, internalSelection, type HasSql, type HasTable } from '../Internal.ts';
2
+ import type { QueryMeta } from '../MetaData.ts';
3
+ import { Query, QueryData } from '../Query.ts';
4
+ import { type Selection, type SelectionInput, type SelectionRow } from '../Selection.ts';
5
+ import { type Sql } from '../Sql.ts';
6
+ import type { TableDefinition, TableUpdate } from '../Table.ts';
7
+ export declare class UpdateData<Meta extends QueryMeta = QueryMeta> extends QueryData<Meta> {
8
+ table: HasTable;
9
+ set?: HasSql;
10
+ where?: HasSql;
11
+ returning?: Selection;
12
+ }
13
+ export declare class Update<Result, Meta extends QueryMeta = QueryMeta> extends Query<Result, Meta> {
14
+ readonly [internalData]: UpdateData<Meta>;
15
+ readonly [internalSelection]?: Selection;
16
+ constructor(data: UpdateData<Meta>);
17
+ get [internalQuery](): Sql;
18
+ }
19
+ export declare class UpdateTable<Definition extends TableDefinition, Meta extends QueryMeta> extends Update<void, Meta> {
20
+ set(values: TableUpdate<Definition>): UpdateTable<Definition, Meta>;
21
+ where(where: HasSql<boolean>): UpdateTable<Definition, Meta>;
22
+ returning<Input extends SelectionInput>(returning: Input): Update<SelectionRow<Input>, Meta>;
23
+ }
@@ -0,0 +1,55 @@
1
+ import {
2
+ getData,
3
+ internalData,
4
+ internalQuery,
5
+ internalSelection
6
+ } from "../Internal.ts";
7
+ import { Query, QueryData } from "../Query.ts";
8
+ import {
9
+ selection
10
+ } from "../Selection.ts";
11
+ import { sql } from "../Sql.ts";
12
+ import { input } from "../expr/Input.ts";
13
+ class UpdateData extends QueryData {
14
+ table;
15
+ set;
16
+ where;
17
+ returning;
18
+ }
19
+ class Update extends Query {
20
+ [internalData];
21
+ constructor(data) {
22
+ super(data);
23
+ this[internalData] = data;
24
+ if (data.returning)
25
+ this[internalSelection] = data.returning;
26
+ }
27
+ get [internalQuery]() {
28
+ return sql.chunk("emitUpdate", this);
29
+ }
30
+ }
31
+ class UpdateTable extends Update {
32
+ set(values) {
33
+ const set = sql.join(
34
+ Object.entries(values).map(
35
+ ([key, value]) => sql`${sql.identifier(key)} = ${input(value)}`
36
+ ),
37
+ sql`, `
38
+ );
39
+ return new UpdateTable({ ...getData(this), set });
40
+ }
41
+ where(where) {
42
+ return new UpdateTable({ ...getData(this), where });
43
+ }
44
+ returning(returning) {
45
+ return new Update({
46
+ ...getData(this),
47
+ returning: selection(returning)
48
+ });
49
+ }
50
+ }
51
+ export {
52
+ Update,
53
+ UpdateData,
54
+ UpdateTable
55
+ };
@@ -1,16 +1,3 @@
1
- import type { Database } from 'better-sqlite3';
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 BetterSqlite3Driver 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): Driver.Sync.PreparedStatement;
13
- schemaInstructions(tableName: string): SchemaInstructions | undefined;
14
- export(): Uint8Array;
15
- }
16
- export declare function connect(db: Database, options?: DriverOptions): BetterSqlite3Driver;
1
+ import type { Database as Client } from 'better-sqlite3';
2
+ import { SyncDatabase } from '../core/Database.ts';
3
+ export declare function connect(db: Client): SyncDatabase<"sqlite">;
@@ -1,54 +1,48 @@
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
4
  constructor(stmt) {
6
5
  this.stmt = stmt;
7
6
  }
8
- iterate(params) {
9
- return this.stmt.iterate(...params);
10
- }
11
7
  all(params) {
12
- return this.stmt.all(...params);
8
+ return this.stmt.raw(false).all(...params);
13
9
  }
14
10
  run(params) {
15
- return { rowsAffected: this.stmt.run(...params).changes };
11
+ return this.stmt.run(...params);
16
12
  }
17
13
  get(params) {
18
- return this.stmt.get(...params);
14
+ return this.stmt.raw(false).get(...params);
15
+ }
16
+ values(params) {
17
+ return this.stmt.raw(true).all(...params);
19
18
  }
20
- execute(params) {
21
- this.stmt.run(...params);
19
+ free() {
22
20
  }
23
21
  }
24
- class BetterSqlite3Driver extends Driver.Sync {
25
- constructor(db, options) {
26
- super(new SqliteFormatter(), options);
27
- this.db = db;
28
- this.tableData = this.prepare(SqliteSchema.tableData);
29
- this.indexData = this.prepare(SqliteSchema.indexData);
30
- }
31
- tableData;
32
- indexData;
33
- close() {
34
- this.db.close();
22
+ class BetterSqlite3Driver {
23
+ constructor(client) {
24
+ this.client = client;
35
25
  }
36
- prepareStatement(stmt) {
37
- return new PreparedStatement(this.db.prepare(stmt.sql));
26
+ exec(query) {
27
+ this.client.exec(query);
28
+ }
29
+ close() {
30
+ this.client.close();
38
31
  }
39
- schemaInstructions(tableName) {
40
- const columnData = this.tableData(tableName);
41
- const indexData = this.indexData(tableName);
42
- return SqliteSchema.createInstructions(columnData, indexData);
32
+ prepare(sql) {
33
+ return new PreparedStatement(this.client.prepare(sql));
43
34
  }
44
- export() {
45
- return this.db.serialize();
35
+ transaction(run, options) {
36
+ let result;
37
+ this.client.transaction(() => {
38
+ result = run(this);
39
+ })[options.behavior ?? "deferred"]();
40
+ return result;
46
41
  }
47
42
  }
48
- function connect(db, options) {
49
- return new BetterSqlite3Driver(db, options);
43
+ function connect(db) {
44
+ return new SyncDatabase(new BetterSqlite3Driver(db), sqliteDialect);
50
45
  }
51
46
  export {
52
- BetterSqlite3Driver,
53
47
  connect
54
48
  };
@@ -1,19 +1,4 @@
1
1
  /// <reference types="bun-types" />
2
- import type { Database } from 'bun:sqlite';
3
- import { SchemaInstructions } from '../define/Schema.js';
4
- import { Driver, DriverOptions } from '../lib/Driver.js';
5
- import { Statement } from '../lib/Statement.js';
6
- import { SqliteSchema } from '../sqlite/SqliteSchema.js';
7
- export declare class BunSqliteDriver extends Driver.Sync {
8
- db: Database;
9
- tableData: (tableName: string) => Array<SqliteSchema.Column>;
10
- indexData: (tableName: string) => Array<SqliteSchema.Index>;
11
- lastChanges: () => {
12
- rowsAffected: number;
13
- };
14
- constructor(db: Database, options?: DriverOptions);
15
- close(): void;
16
- prepareStatement(stmt: Statement): Driver.Sync.PreparedStatement;
17
- schemaInstructions(tableName: string): SchemaInstructions | undefined;
18
- }
19
- export declare function connect(db: Database, options?: DriverOptions): BunSqliteDriver;
2
+ import type { Database as Client } from 'bun:sqlite';
3
+ import { SyncDatabase } from '../core/Database.ts';
4
+ export declare function connect(db: Client): SyncDatabase<"sqlite">;
@@ -1,65 +1,49 @@
1
- import { QueryData, SelectFirst } from "../define/Query.js";
2
- import { Driver } from "../lib/Driver.js";
3
- import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
4
- import { SqliteSchema } from "../sqlite/SqliteSchema.js";
1
+ import { SyncDatabase } from "../core/Database.ts";
2
+ import { sqliteDialect } from "../sqlite.ts";
5
3
  class PreparedStatement {
6
- constructor(lastChanges, stmt) {
7
- this.lastChanges = lastChanges;
4
+ constructor(stmt) {
8
5
  this.stmt = stmt;
9
6
  }
10
- *iterate(params) {
11
- for (const row of this.stmt.all(...params))
12
- yield row;
13
- }
14
7
  all(params) {
15
8
  return this.stmt.all(...params);
16
9
  }
17
10
  run(params) {
18
- this.stmt.run(...params);
19
- return this.lastChanges();
11
+ return this.stmt.run(...params);
20
12
  }
21
13
  get(params) {
22
14
  return this.stmt.get(...params);
23
15
  }
24
- execute(params) {
25
- this.stmt.run(...params);
16
+ values(params) {
17
+ return this.stmt.values(...params);
18
+ }
19
+ free() {
20
+ this.stmt.finalize();
26
21
  }
27
22
  }
28
- class BunSqliteDriver extends Driver.Sync {
29
- constructor(db, options) {
30
- super(new SqliteFormatter(), options);
31
- this.db = db;
32
- this.tableData = this.prepare(SqliteSchema.tableData);
33
- this.indexData = this.prepare(SqliteSchema.indexData);
34
- this.lastChanges = this.prepare(() => {
35
- return new SelectFirst(
36
- new QueryData.Raw({
37
- expectedReturn: "row",
38
- strings: ["SELECT changes() as rowsAffected"],
39
- params: []
40
- })
41
- );
42
- });
23
+ class BunSqliteDriver {
24
+ constructor(client) {
25
+ this.client = client;
26
+ }
27
+ exec(query) {
28
+ this.client.exec(query);
43
29
  }
44
- tableData;
45
- indexData;
46
- lastChanges;
47
30
  close() {
48
- this.db.close();
31
+ this.client.close();
49
32
  }
50
- prepareStatement(stmt) {
51
- return new PreparedStatement(this.lastChanges, this.db.prepare(stmt.sql));
33
+ prepare(sql) {
34
+ return new PreparedStatement(this.client.prepare(sql));
52
35
  }
53
- schemaInstructions(tableName) {
54
- const columnData = this.tableData(tableName);
55
- const indexData = this.indexData(tableName);
56
- return SqliteSchema.createInstructions(columnData, indexData);
36
+ transaction(run, options) {
37
+ let result;
38
+ this.client.transaction(() => {
39
+ result = run(this);
40
+ })[options.behavior ?? "deferred"]();
41
+ return result;
57
42
  }
58
43
  }
59
- function connect(db, options) {
60
- return new BunSqliteDriver(db, options);
44
+ function connect(db) {
45
+ return new SyncDatabase(new BunSqliteDriver(db), sqliteDialect);
61
46
  }
62
47
  export {
63
- BunSqliteDriver,
64
48
  connect
65
49
  };
@@ -0,0 +1,25 @@
1
+ import pg, { type Client, type PoolClient } from 'pg';
2
+ import { AsyncDatabase, type TransactionOptions } from '../core/Database.ts';
3
+ import type { AsyncDriver, AsyncStatement } from '../core/Driver.ts';
4
+ type Queryable = Client | pg.Pool | PoolClient;
5
+ declare class PreparedStatement implements AsyncStatement {
6
+ private client;
7
+ private sql;
8
+ private name?;
9
+ constructor(client: Queryable, sql: string, name?: string | undefined);
10
+ all(params: Array<unknown>): Promise<Array<object>>;
11
+ run(params: Array<unknown>): Promise<void>;
12
+ get(params: Array<unknown>): Promise<object>;
13
+ values(params: Array<unknown>): Promise<any[][]>;
14
+ free(): void;
15
+ }
16
+ export declare class PgDriver implements AsyncDriver {
17
+ private client;
18
+ constructor(client: Queryable);
19
+ exec(query: string): Promise<void>;
20
+ prepare(sql: string, name: string): PreparedStatement;
21
+ close(): Promise<void>;
22
+ transaction<T>(run: (inner: AsyncDriver) => Promise<T>, options: TransactionOptions['postgres'], depth: number): Promise<T>;
23
+ }
24
+ export declare function connect(client: Queryable): AsyncDatabase<"postgres">;
25
+ export {};