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,137 @@
1
+ import { sql } from "../Sql.ts";
2
+ import { input } from "./Input.ts";
3
+ function eq(left, right) {
4
+ return sql`${input(left)} = ${input(right)}`;
5
+ }
6
+ function ne(left, right) {
7
+ return sql`${input(left)} <> ${input(right)}`;
8
+ }
9
+ function and(...conditions) {
10
+ if (conditions.length === 0)
11
+ return sql`true`;
12
+ if (conditions.length === 1)
13
+ return input(conditions[0]);
14
+ return sql`(${sql.join(conditions.map(input), sql.unsafe(" and "))})`;
15
+ }
16
+ function or(...conditions) {
17
+ if (conditions.length === 0)
18
+ return sql`true`;
19
+ if (conditions.length === 1)
20
+ return input(conditions[0]);
21
+ return sql`(${sql.join(conditions.map(input), sql.unsafe(" or "))})`;
22
+ }
23
+ function not(condition) {
24
+ return sql`not ${input(condition)}`;
25
+ }
26
+ function gt(left, right) {
27
+ return sql`${input(left)} > ${input(right)}`;
28
+ }
29
+ function gte(left, right) {
30
+ return sql`${input(left)} >= ${input(right)}`;
31
+ }
32
+ function lt(left, right) {
33
+ return sql`${input(left)} < ${input(right)}`;
34
+ }
35
+ function lte(left, right) {
36
+ return sql`${input(left)} <= ${input(right)}`;
37
+ }
38
+ function inArray(left, right) {
39
+ if (Array.isArray(right)) {
40
+ if (right.length === 0)
41
+ return sql`false`;
42
+ return sql`${input(left)} in (${sql.join(
43
+ right.map(input),
44
+ sql.unsafe(", ")
45
+ )})`;
46
+ }
47
+ return sql`${input(left)} in ${input(right)}`;
48
+ }
49
+ function notInArray(left, right) {
50
+ if (Array.isArray(right)) {
51
+ if (right.length === 0)
52
+ return sql`true`;
53
+ return sql`${input(left)} not in (${sql.join(right.map(input), sql`, `)})`;
54
+ }
55
+ return sql`${input(left)} not in ${input(right)}`;
56
+ }
57
+ function isNull(value) {
58
+ return sql`${input(value)} is null`;
59
+ }
60
+ function isNotNull(value) {
61
+ return sql`${input(value)} is not null`;
62
+ }
63
+ function between(value, left, right) {
64
+ return sql`${input(value)} between ${input(left)} and ${input(right)}`;
65
+ }
66
+ function notBetween(value, left, right) {
67
+ return sql`${input(value)} not between ${input(left)} and ${input(right)}`;
68
+ }
69
+ function like(left, pattern) {
70
+ return sql`${input(left)} like ${input(pattern)}`;
71
+ }
72
+ function notLike(value, pattern) {
73
+ return sql`${input(value)} not like ${input(pattern)}`;
74
+ }
75
+ function ilike(value, pattern) {
76
+ return sql`${input(value)} ilike ${input(pattern)}`;
77
+ }
78
+ function notILike(value, pattern) {
79
+ return sql`${input(value)} not ilike ${input(pattern)}`;
80
+ }
81
+ function arrayContains(left, right) {
82
+ return sql`${input(left)} @> ${input(right)}`;
83
+ }
84
+ function arrayContained(left, right) {
85
+ return sql`${input(left)} <@ ${input(right)}`;
86
+ }
87
+ function arrayOverlaps(left, right) {
88
+ return sql`${input(left)} && ${input(right)}`;
89
+ }
90
+ function asc(input2) {
91
+ return sql`${input2} asc`;
92
+ }
93
+ function desc(input2) {
94
+ return sql`${input2} desc`;
95
+ }
96
+ function distinct(input2) {
97
+ return sql`distinct ${input2}`;
98
+ }
99
+ const INDEX_PROPERTY = /^\d+$/;
100
+ function jsonExpr(e) {
101
+ return new Proxy(e, {
102
+ get(target, prop) {
103
+ if (typeof prop !== "string")
104
+ return Reflect.get(target, prop);
105
+ const isNumber = INDEX_PROPERTY.test(prop);
106
+ return jsonExpr(sql`${target}`.jsonPath([isNumber ? Number(prop) : prop]));
107
+ }
108
+ });
109
+ }
110
+ export {
111
+ and,
112
+ arrayContained,
113
+ arrayContains,
114
+ arrayOverlaps,
115
+ asc,
116
+ between,
117
+ desc,
118
+ distinct,
119
+ eq,
120
+ gt,
121
+ gte,
122
+ ilike,
123
+ inArray,
124
+ isNotNull,
125
+ isNull,
126
+ jsonExpr,
127
+ like,
128
+ lt,
129
+ lte,
130
+ ne,
131
+ not,
132
+ notBetween,
133
+ notILike,
134
+ notInArray,
135
+ notLike,
136
+ or
137
+ };
@@ -0,0 +1,14 @@
1
+ import { internalField, internalSql, type HasSql } from '../Internal.ts';
2
+ import { type Sql } from '../Sql.ts';
3
+ export interface FieldData {
4
+ targetName: string;
5
+ fieldName: string;
6
+ }
7
+ export declare class Field<Value = unknown, Table extends string = string> implements HasSql<Value> {
8
+ private brand;
9
+ readonly [internalField]: FieldData;
10
+ readonly [internalSql]: Sql<Value>;
11
+ constructor(targetName: string, fieldName: string, options?: {
12
+ mapFromDriverValue?(value: unknown): Value;
13
+ });
14
+ }
@@ -0,0 +1,15 @@
1
+ import { internalField, internalSql } from "../Internal.ts";
2
+ import { sql } from "../Sql.ts";
3
+ class Field {
4
+ [internalField];
5
+ [internalSql];
6
+ constructor(targetName, fieldName, options = {}) {
7
+ const field = { targetName, fieldName };
8
+ this[internalField] = field;
9
+ const expr = sql.field(field).as(fieldName).mapWith(options);
10
+ this[internalSql] = expr;
11
+ }
12
+ }
13
+ export {
14
+ Field
15
+ };
@@ -0,0 +1,6 @@
1
+ import { type Sql } from '../Sql.ts';
2
+ import { type Input } from './Input.ts';
3
+ export declare const Functions: Functions;
4
+ export type Functions = {
5
+ [key: string]: (...args: Array<Input<any>>) => Sql<any>;
6
+ };
@@ -0,0 +1,11 @@
1
+ import { sql } from "../Sql.ts";
2
+ import { input } from "./Input.ts";
3
+ function get(target, method) {
4
+ return target[method] ??= (...args) => {
5
+ return sql`${sql.identifier(method)}(${sql.join(args.map(input), sql`, `)})`;
6
+ };
7
+ }
8
+ const Functions = new Proxy(/* @__PURE__ */ Object.create(null), { get });
9
+ export {
10
+ Functions
11
+ };
@@ -0,0 +1,3 @@
1
+ import { type HasSql } from '../Internal.ts';
2
+ export type Input<T = unknown> = HasSql<T> | T;
3
+ export declare function input<T>(value: Input<T>): HasSql<T>;
@@ -0,0 +1,14 @@
1
+ import { getSql, getTable, hasSql, hasTable } from "../Internal.ts";
2
+ import { sql } from "../Sql.ts";
3
+ function input(value) {
4
+ if (typeof value !== "object" || value === null)
5
+ return sql.value(value);
6
+ if (hasTable(value))
7
+ return sql.identifier(getTable(value).name);
8
+ if (hasSql(value))
9
+ return getSql(value);
10
+ return sql.value(value);
11
+ }
12
+ export {
13
+ input
14
+ };
@@ -0,0 +1,13 @@
1
+ import { internalData, internalQuery, type HasTable } from '../Internal.ts';
2
+ import type { QueryMeta } from '../MetaData.ts';
3
+ import { Query, type QueryData } from '../Query.ts';
4
+ export interface CreateData<Meta extends QueryMeta> extends QueryData<Meta> {
5
+ table: HasTable;
6
+ ifNotExists?: boolean;
7
+ }
8
+ export declare class Create<Meta extends QueryMeta = QueryMeta> extends Query<void, Meta> {
9
+ readonly [internalData]: CreateData<Meta>;
10
+ constructor(data: CreateData<Meta>);
11
+ ifNotExists(): Create<Meta>;
12
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
13
+ }
@@ -0,0 +1,23 @@
1
+ import {
2
+ getData,
3
+ internalData,
4
+ internalQuery
5
+ } from "../Internal.ts";
6
+ import { Query } from "../Query.ts";
7
+ import { sql } from "../Sql.ts";
8
+ class Create extends Query {
9
+ [internalData];
10
+ constructor(data) {
11
+ super(data);
12
+ this[internalData] = data;
13
+ }
14
+ ifNotExists() {
15
+ return new Create({ ...getData(this), ifNotExists: true });
16
+ }
17
+ get [internalQuery]() {
18
+ return sql.chunk("emitCreateTable", this);
19
+ }
20
+ }
21
+ export {
22
+ Create
23
+ };
@@ -0,0 +1,19 @@
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
+ export declare class DeleteData<Meta extends QueryMeta = QueryMeta> extends QueryData<Meta> {
6
+ from: HasTable;
7
+ where?: HasSql;
8
+ returning?: Selection;
9
+ }
10
+ export declare class Delete<Result, Meta extends QueryMeta = QueryMeta> extends Query<Result, Meta> {
11
+ readonly [internalData]: DeleteData<Meta>;
12
+ readonly [internalSelection]?: Selection;
13
+ constructor(data: DeleteData<Meta>);
14
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
15
+ }
16
+ export declare class DeleteFrom<Meta extends QueryMeta> extends Delete<void, Meta> {
17
+ where(condition: HasSql<boolean>): DeleteFrom<Meta>;
18
+ returning<Input extends SelectionInput>(returning: Input): Delete<SelectionRow<Input>, Meta>;
19
+ }
@@ -0,0 +1,44 @@
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
+ class DeleteData extends QueryData {
13
+ from;
14
+ where;
15
+ returning;
16
+ }
17
+ class Delete extends Query {
18
+ [internalData];
19
+ constructor(data) {
20
+ super(data);
21
+ this[internalData] = data;
22
+ if (data.returning)
23
+ this[internalSelection] = data.returning;
24
+ }
25
+ get [internalQuery]() {
26
+ return sql.chunk("emitDelete", this);
27
+ }
28
+ }
29
+ class DeleteFrom extends Delete {
30
+ where(condition) {
31
+ return new DeleteFrom({ ...getData(this), where: condition });
32
+ }
33
+ returning(returning) {
34
+ return new Delete({
35
+ ...getData(this),
36
+ returning: selection(returning)
37
+ });
38
+ }
39
+ }
40
+ export {
41
+ Delete,
42
+ DeleteData,
43
+ DeleteFrom
44
+ };
@@ -0,0 +1,13 @@
1
+ import { internalData, internalQuery, type HasTable } from '../Internal.ts';
2
+ import type { QueryMeta } from '../MetaData.ts';
3
+ import { Query, type QueryData } from '../Query.ts';
4
+ export interface DropData<Meta extends QueryMeta> extends QueryData<Meta> {
5
+ table: HasTable;
6
+ ifExists?: boolean;
7
+ }
8
+ export declare class Drop<Meta extends QueryMeta = QueryMeta> extends Query<void, Meta> {
9
+ readonly [internalData]: DropData<Meta>;
10
+ constructor(data: DropData<Meta>);
11
+ ifExists(): Drop<Meta>;
12
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
13
+ }
@@ -0,0 +1,23 @@
1
+ import {
2
+ getData,
3
+ internalData,
4
+ internalQuery
5
+ } from "../Internal.ts";
6
+ import { Query } from "../Query.ts";
7
+ import { sql } from "../Sql.ts";
8
+ class Drop extends Query {
9
+ [internalData];
10
+ constructor(data) {
11
+ super(data);
12
+ this[internalData] = data;
13
+ }
14
+ ifExists() {
15
+ return new Drop({ ...getData(this), ifExists: true });
16
+ }
17
+ get [internalQuery]() {
18
+ return sql.chunk("emitDrop", this);
19
+ }
20
+ }
21
+ export {
22
+ Drop
23
+ };
@@ -0,0 +1,43 @@
1
+ import { internalData, internalQuery, internalSelection, type HasSql, type HasTable } from '../Internal.ts';
2
+ import type { IsPostgres, 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 { TableDefinition, TableInsert, TableRow, TableUpdate } from '../Table.ts';
6
+ declare class InsertIntoData<Meta extends QueryMeta> extends QueryData<Meta> {
7
+ into: HasTable;
8
+ values?: HasSql;
9
+ }
10
+ export declare class InsertData<Meta extends QueryMeta> extends InsertIntoData<Meta> {
11
+ returning?: Selection;
12
+ onConflict?: HasSql;
13
+ }
14
+ export declare class Insert<Result, Meta extends QueryMeta = QueryMeta> extends Query<Result, Meta> {
15
+ readonly [internalData]: InsertData<Meta>;
16
+ readonly [internalSelection]?: Selection;
17
+ constructor(data: InsertData<Meta>);
18
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
19
+ }
20
+ declare class InsertCanReturn<Definition extends TableDefinition, Meta extends QueryMeta> extends Insert<void, Meta> {
21
+ returning(): Insert<TableRow<Definition>, Meta>;
22
+ returning<Input extends SelectionInput>(returning: Input): Insert<SelectionRow<Input>, Meta>;
23
+ }
24
+ export interface OnConflict {
25
+ target: HasSql | Array<HasSql>;
26
+ targetWhere?: HasSql<boolean>;
27
+ }
28
+ export interface OnConflictUpdate<Definition extends TableDefinition> extends OnConflict {
29
+ set: TableUpdate<Definition>;
30
+ setWhere?: HasSql<boolean>;
31
+ }
32
+ declare class InsertCanConflict<Definition extends TableDefinition, Meta extends QueryMeta> extends InsertCanReturn<Definition, Meta> {
33
+ #private;
34
+ onConflictDoNothing(this: InsertCanConflict<Definition, IsPostgres>, onConflict?: OnConflict): InsertCanReturn<Definition, Meta>;
35
+ onConflictDoUpdate(this: InsertCanConflict<Definition, IsPostgres>, onConflict: OnConflictUpdate<Definition>): InsertCanReturn<Definition, Meta>;
36
+ }
37
+ export declare class InsertInto<Definition extends TableDefinition, Meta extends QueryMeta> {
38
+ [internalData]: InsertData<Meta>;
39
+ constructor(data: InsertData<Meta>);
40
+ values(value: TableInsert<Definition>): InsertCanConflict<Definition, Meta>;
41
+ values(values: Array<TableInsert<Definition>>): InsertCanConflict<Definition, Meta>;
42
+ }
43
+ export {};
@@ -0,0 +1,111 @@
1
+ import {
2
+ getColumn,
3
+ getData,
4
+ getTable,
5
+ internalData,
6
+ internalQuery,
7
+ internalSelection
8
+ } from "../Internal.ts";
9
+ import { Query, QueryData } from "../Query.ts";
10
+ import {
11
+ selection
12
+ } from "../Selection.ts";
13
+ import { sql } from "../Sql.ts";
14
+ import { input } from "../expr/Input.ts";
15
+ class InsertIntoData extends QueryData {
16
+ into;
17
+ values;
18
+ }
19
+ class InsertData extends InsertIntoData {
20
+ returning;
21
+ onConflict;
22
+ }
23
+ class Insert extends Query {
24
+ [internalData];
25
+ constructor(data) {
26
+ super(data);
27
+ this[internalData] = data;
28
+ if (data.returning)
29
+ this[internalSelection] = data.returning;
30
+ }
31
+ get [internalQuery]() {
32
+ return sql.chunk("emitInsert", this);
33
+ }
34
+ }
35
+ class InsertCanReturn extends Insert {
36
+ returning(returning) {
37
+ const data = getData(this);
38
+ return new Insert({
39
+ ...data,
40
+ returning: selection(returning ?? data.into)
41
+ });
42
+ }
43
+ }
44
+ class InsertCanConflict extends InsertCanReturn {
45
+ onConflictDoNothing(onConflict) {
46
+ return this.#onConflict(onConflict ?? {});
47
+ }
48
+ onConflictDoUpdate(onConflict) {
49
+ return this.#onConflict(onConflict);
50
+ }
51
+ #onConflict({
52
+ target,
53
+ targetWhere,
54
+ set,
55
+ setWhere
56
+ }) {
57
+ const update = set && sql.join(
58
+ Object.entries(set).map(
59
+ ([key, value]) => sql`${sql.identifier(key)} = ${input(value)}`
60
+ ),
61
+ sql`, `
62
+ );
63
+ return new InsertCanReturn({
64
+ ...getData(this),
65
+ onConflict: sql.join([
66
+ target && sql`(${Array.isArray(target) ? sql.join(target, sql`, `) : target})`,
67
+ targetWhere && sql`where ${targetWhere}`,
68
+ update ? sql.join([
69
+ sql`do update set ${update}`,
70
+ setWhere && sql`where ${setWhere}`
71
+ ]) : sql`do nothing`
72
+ ])
73
+ });
74
+ }
75
+ }
76
+ class InsertInto {
77
+ [internalData];
78
+ constructor(data) {
79
+ this[internalData] = data;
80
+ }
81
+ values(insert) {
82
+ const { into } = getData(this);
83
+ const rows = Array.isArray(insert) ? insert : [insert];
84
+ const table = getTable(into);
85
+ const values = sql.join(
86
+ rows.map((row) => {
87
+ return sql`(${sql.join(
88
+ Object.entries(table.columns).map(([key, column]) => {
89
+ const value = row[key];
90
+ const { defaultValue, notNull, mapToDriverValue } = getColumn(column);
91
+ if (value !== void 0)
92
+ return input(mapToDriverValue ? mapToDriverValue(value) : value);
93
+ if (defaultValue)
94
+ return defaultValue();
95
+ if (notNull)
96
+ throw new Error(`Column "${key}" is not nullable`);
97
+ return sql.chunk("emitDefaultValue", void 0);
98
+ }),
99
+ sql`, `
100
+ )})`;
101
+ }),
102
+ sql`, `
103
+ );
104
+ return new InsertCanConflict({ ...getData(this), values });
105
+ }
106
+ }
107
+ export {
108
+ Insert,
109
+ InsertData,
110
+ InsertInto
111
+ };
@@ -0,0 +1,93 @@
1
+ import { internalData, internalQuery, internalSelection, type HasSelection, type HasSql, type HasTable, type HasTarget } from '../Internal.ts';
2
+ import type { IsMysql, IsPostgres, QueryMeta } from '../MetaData.ts';
3
+ import { Query, QueryData } from '../Query.ts';
4
+ import { type IsNullable, type MakeNullable, type SelectionInput, type SelectionRecord, type SelectionRow } from '../Selection.ts';
5
+ import type { Table, TableDefinition, TableFields } from '../Table.ts';
6
+ import type { Expand } from '../Types.ts';
7
+ import type { Field } from '../expr/Field.ts';
8
+ import { type Input as UserInput } from '../expr/Input.ts';
9
+ import { Union } from './Union.ts';
10
+ export type SelectionType = 'selection' | 'allFrom' | 'joinTables';
11
+ export declare class SelectData<Meta extends QueryMeta> extends QueryData<Meta> {
12
+ select: {
13
+ type: SelectionType;
14
+ tables: Array<string>;
15
+ nullable: Array<string>;
16
+ input?: SelectionInput;
17
+ };
18
+ distinct?: boolean;
19
+ distinctOn?: Array<HasSql>;
20
+ from?: HasSql;
21
+ subject?: HasSql;
22
+ where?: HasSql;
23
+ groupBy?: HasSql;
24
+ having?: HasSql;
25
+ orderBy?: HasSql;
26
+ limit?: HasSql;
27
+ offset?: HasSql;
28
+ }
29
+ export declare class Select<Input, Meta extends QueryMeta = QueryMeta> extends Query<SelectionRow<Input>, Meta> implements HasSelection, SelectBase<Input, Meta> {
30
+ #private;
31
+ readonly [internalData]: SelectData<Meta>;
32
+ constructor(data: SelectData<Meta>);
33
+ as(alias: string): SubQuery<Input>;
34
+ from(target: HasTarget): Select<Input, Meta>;
35
+ leftJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
36
+ rightJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
37
+ innerJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
38
+ fullJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
39
+ where(where: HasSql<boolean>): Select<Input, Meta>;
40
+ groupBy(...exprs: Array<HasSql>): Select<Input, Meta>;
41
+ having(having: HasSql<boolean>): Select<Input, Meta>;
42
+ orderBy(...exprs: Array<HasSql>): Select<Input, Meta>;
43
+ limit(limit: UserInput<number>): Select<Input, Meta>;
44
+ offset(offset: UserInput<number>): Select<Input, Meta>;
45
+ union(right: SelectBase<Input, Meta>): Union<Input, Meta>;
46
+ unionAll(right: SelectBase<Input, Meta>): Union<Input, Meta>;
47
+ intersect(right: SelectBase<Input, Meta>): Union<Input, Meta>;
48
+ intersectAll(this: SelectBase<Input, IsPostgres | IsMysql>, right: SelectBase<Input, Meta>): Union<Input, Meta>;
49
+ except(right: SelectBase<Input, Meta>): Union<Input, Meta>;
50
+ exceptAll(right: SelectBase<Input, IsPostgres | IsMysql>): Union<Input, Meta>;
51
+ get [internalSelection](): import("../Selection.ts").Selection;
52
+ get [internalQuery](): import("../Sql.ts").Sql<unknown>;
53
+ }
54
+ export type SubQuery<Input> = Input & HasTarget;
55
+ export interface SelectBase<Input, Meta extends QueryMeta = QueryMeta> extends Query<SelectionRow<Input>, Meta>, HasSelection {
56
+ where(where: HasSql<boolean>): Select<Input, Meta>;
57
+ groupBy(...exprs: Array<HasSql>): Select<Input, Meta>;
58
+ having(having: HasSql<boolean>): Select<Input, Meta>;
59
+ orderBy(...exprs: Array<HasSql>): Select<Input, Meta>;
60
+ limit(limit: UserInput<number>): Select<Input, Meta>;
61
+ offset(offset: UserInput<number>): Select<Input, Meta>;
62
+ union(right: SelectBase<Input, Meta>): Union<Input, Meta>;
63
+ unionAll(right: SelectBase<Input, Meta>): Union<Input, Meta>;
64
+ intersect(right: SelectBase<Input, Meta>): Union<Input, Meta>;
65
+ intersectAll(this: SelectBase<Input, IsPostgres | IsMysql>, right: SelectBase<Input, Meta>): Union<Input, Meta>;
66
+ except(right: SelectBase<Input, Meta>): Union<Input, Meta>;
67
+ exceptAll(right: SelectBase<Input, IsPostgres | IsMysql>): Union<Input, Meta>;
68
+ as(name: string): SubQuery<Input>;
69
+ }
70
+ export interface WithoutSelection<Meta extends QueryMeta> {
71
+ from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): AllFrom<TableFields<Definition>, Meta, Record<Name, TableFields<Definition>>>;
72
+ from<Input>(from: SubQuery<Input>): SelectionFrom<Input, Meta>;
73
+ }
74
+ export interface WithSelection<Input, Meta extends QueryMeta> {
75
+ from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): SelectionFrom<Input, Meta>;
76
+ from(from: SubQuery<unknown>): SelectionFrom<Input, Meta>;
77
+ }
78
+ export interface AllFrom<Input, Meta extends QueryMeta, Tables = Input> extends SelectBase<Input, Meta> {
79
+ leftJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
80
+ rightJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & Record<Name, TableFields<Definition>>>, Meta>;
81
+ innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
82
+ fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
83
+ }
84
+ type MarkFieldsAsNullable<Input, TableName extends string> = Expand<{
85
+ [K in keyof Input]: Input[K] extends Field<infer T, TableName> ? HasSql<T | null> : Input[K] extends Table<infer Definition, TableName> ? TableFields<Definition> & IsNullable : Input[K] extends Record<string, Field<unknown, TableName> | HasSql<unknown>> ? Input[K] & IsNullable : Input[K] extends SelectionRecord ? MarkFieldsAsNullable<Input[K], TableName> : Input[K];
86
+ }>;
87
+ export interface SelectionFrom<Input, Meta extends QueryMeta> extends SelectBase<Input, Meta> {
88
+ leftJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
89
+ rightJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
90
+ innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
91
+ fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
92
+ }
93
+ export {};