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,88 @@
1
+ import {
2
+ getField,
3
+ getSql,
4
+ hasField,
5
+ hasSql,
6
+ internalSql
7
+ } from "./Internal.ts";
8
+ import { sql } from "./Sql.ts";
9
+ import { virtual } from "./Virtual.ts";
10
+ class Selection {
11
+ #input;
12
+ #nullable;
13
+ constructor(input, nullable) {
14
+ this.#input = input;
15
+ this.#nullable = nullable;
16
+ }
17
+ makeVirtual(name) {
18
+ return virtual(name, this.#input);
19
+ }
20
+ mapRow = (values) => {
21
+ return this.#mapResult(this.#input, values);
22
+ };
23
+ get [internalSql]() {
24
+ return this.#selectionToSql(this.#input, /* @__PURE__ */ new Set());
25
+ }
26
+ #mapResult(input, values) {
27
+ const expr = this.#exprOf(input);
28
+ if (expr) {
29
+ const value = values.shift();
30
+ if (value !== null && expr.mapFromDriverValue)
31
+ return expr.mapFromDriverValue(value);
32
+ return value;
33
+ }
34
+ const result = {};
35
+ let isNullable = this.#nullable.size > 0;
36
+ for (const name in input) {
37
+ const expr2 = input[name];
38
+ const value = this.#mapResult(expr2, values);
39
+ result[name] = value;
40
+ if (isNullable) {
41
+ if (value === null) {
42
+ const field = getField(expr2);
43
+ if (field && !this.#nullable.has(field.targetName))
44
+ isNullable = false;
45
+ } else {
46
+ isNullable = false;
47
+ }
48
+ }
49
+ }
50
+ if (isNullable)
51
+ return null;
52
+ return result;
53
+ }
54
+ #selectionToSql(input, names, name) {
55
+ const expr = this.#exprOf(input);
56
+ if (expr) {
57
+ let exprName = name ?? expr.alias;
58
+ if (exprName) {
59
+ while (names.has(exprName))
60
+ exprName = `${exprName}_`;
61
+ names.add(exprName);
62
+ if (hasField(input)) {
63
+ const field = getField(input);
64
+ if (field.fieldName === exprName)
65
+ return expr;
66
+ }
67
+ return sql`${expr} as ${sql.identifier(exprName)}`;
68
+ }
69
+ return expr;
70
+ }
71
+ return sql.join(
72
+ Object.entries(input).map(
73
+ ([name2, value]) => this.#selectionToSql(value, names, name2)
74
+ ),
75
+ sql`, `
76
+ );
77
+ }
78
+ #exprOf(input) {
79
+ return hasSql(input) ? getSql(input) : void 0;
80
+ }
81
+ }
82
+ function selection(input, nullable = /* @__PURE__ */ new Set()) {
83
+ return new Selection(input, nullable);
84
+ }
85
+ export {
86
+ Selection,
87
+ selection
88
+ };
@@ -0,0 +1,49 @@
1
+ import type { Emitter } from './Emitter.ts';
2
+ import { internalSql, type HasSql } from './Internal.ts';
3
+ import type { FieldData } from './expr/Field.ts';
4
+ type EmitMethods = {
5
+ [K in keyof Emitter as K extends `emit${string}` ? K : never]: Emitter[K];
6
+ };
7
+ declare class Chunk<Type = keyof EmitMethods, Inner = unknown> {
8
+ type: Type;
9
+ inner: Inner;
10
+ constructor(type: Type, inner: Inner);
11
+ }
12
+ export type Decoder<T> = ((value: unknown) => T) | {
13
+ mapFromDriverValue?(value: unknown): T;
14
+ };
15
+ export declare class Sql<Value = unknown> implements HasSql<Value> {
16
+ #private;
17
+ private brand;
18
+ alias?: string;
19
+ mapFromDriverValue?: (input: unknown) => Value;
20
+ readonly [internalSql]: this;
21
+ constructor(chunks?: Array<Chunk>);
22
+ as(name: string): Sql<Value>;
23
+ mapWith<T = Value>(decoder: Decoder<T>): Sql<T>;
24
+ chunk<Type extends keyof EmitMethods>(type: Type, inner: Parameters<EmitMethods[Type]>[0]): Sql<Value>;
25
+ unsafe(sql: string): Sql<Value>;
26
+ field(field: FieldData): Sql<Value>;
27
+ add(sql: HasSql): Sql<Value>;
28
+ value(value: unknown): Sql<Value>;
29
+ inline(value: unknown): Sql<Value>;
30
+ jsonPath(path: Array<string | number>): Sql<Value>;
31
+ placeholder(name: string): Sql<Value>;
32
+ identifier(identifier: string): Sql<Value>;
33
+ inlineFields(withTableName: boolean): Sql<Value>;
34
+ emit(emitter: Emitter): void;
35
+ }
36
+ export declare function sql<T>(strings: TemplateStringsArray, ...inner: Array<HasSql>): Sql<T>;
37
+ export declare namespace sql {
38
+ function empty<T>(): Sql<T>;
39
+ function unsafe<T>(directSql: string): Sql<T>;
40
+ function value<T>(value: T): Sql<T>;
41
+ function inline<T>(value: T): Sql<T>;
42
+ function placeholder<T>(name: string): Sql<T>;
43
+ function identifier<T>(identifier: string): Sql<T>;
44
+ function field<T>(field: FieldData): Sql<T>;
45
+ function chunk<Type extends keyof EmitMethods>(type: Type, inner: Parameters<EmitMethods[Type]>[0]): Sql;
46
+ function query(ast: Record<string, HasSql | undefined>): Sql;
47
+ function join<T>(items: Array<Sql | HasSql | undefined | false>, separator?: Sql): Sql<T>;
48
+ }
49
+ export {};
@@ -0,0 +1,153 @@
1
+ import { getSql, internalSql } from "./Internal.ts";
2
+ class Chunk {
3
+ constructor(type, inner) {
4
+ this.type = type;
5
+ this.inner = inner;
6
+ }
7
+ }
8
+ class Sql {
9
+ alias;
10
+ mapFromDriverValue;
11
+ [internalSql] = this;
12
+ #chunks;
13
+ constructor(chunks = []) {
14
+ this.#chunks = chunks;
15
+ }
16
+ as(name) {
17
+ this.alias = name;
18
+ return this;
19
+ }
20
+ mapWith(decoder) {
21
+ const res = this;
22
+ res.mapFromDriverValue = typeof decoder === "function" ? decoder : decoder.mapFromDriverValue;
23
+ return res;
24
+ }
25
+ chunk(type, inner) {
26
+ this.#chunks.push(new Chunk(type, inner));
27
+ return this;
28
+ }
29
+ unsafe(sql2) {
30
+ if (sql2.length > 0)
31
+ this.chunk("emitUnsafe", sql2);
32
+ return this;
33
+ }
34
+ field(field) {
35
+ return this.chunk("emitField", field);
36
+ }
37
+ add(sql2) {
38
+ const inner = getSql(sql2);
39
+ if (!(inner instanceof Sql))
40
+ throw new Error("Invalid SQL");
41
+ this.#chunks.push(...inner.#chunks);
42
+ return this;
43
+ }
44
+ value(value) {
45
+ return this.chunk("emitValue", value);
46
+ }
47
+ inline(value) {
48
+ return this.chunk("emitInline", value);
49
+ }
50
+ jsonPath(path) {
51
+ const last = this.#chunks.at(-1);
52
+ if (last?.type === "emitJsonPath")
53
+ last.inner.push(...path);
54
+ else
55
+ this.chunk("emitJsonPath", path);
56
+ return this;
57
+ }
58
+ placeholder(name) {
59
+ return this.chunk("emitPlaceholder", name);
60
+ }
61
+ identifier(identifier) {
62
+ return this.chunk("emitIdentifier", identifier);
63
+ }
64
+ inlineFields(withTableName) {
65
+ return new Sql(
66
+ this.#chunks.flatMap((chunk) => {
67
+ if (chunk.type !== "emitField")
68
+ return [chunk];
69
+ const data = chunk.inner;
70
+ if (withTableName)
71
+ return [
72
+ new Chunk("emitIdentifier", data.targetName),
73
+ new Chunk("emitUnsafe", "."),
74
+ new Chunk("emitIdentifier", data.fieldName)
75
+ ];
76
+ return [new Chunk("emitIdentifier", data.fieldName)];
77
+ })
78
+ );
79
+ }
80
+ emit(emitter) {
81
+ for (const chunk of this.#chunks)
82
+ emitter[chunk.type](chunk.inner);
83
+ }
84
+ }
85
+ function sql(strings, ...inner) {
86
+ const sql2 = new Sql();
87
+ for (let i = 0; i < strings.length; i++) {
88
+ sql2.unsafe(strings[i]);
89
+ if (i < inner.length) {
90
+ const insert = inner[i];
91
+ sql2.add(insert);
92
+ }
93
+ }
94
+ return sql2;
95
+ }
96
+ ((sql2) => {
97
+ function empty() {
98
+ return new Sql();
99
+ }
100
+ sql2.empty = empty;
101
+ function unsafe(directSql) {
102
+ return empty().unsafe(directSql);
103
+ }
104
+ sql2.unsafe = unsafe;
105
+ function value(value2) {
106
+ return empty().value(value2);
107
+ }
108
+ sql2.value = value;
109
+ function inline(value2) {
110
+ return empty().inline(value2);
111
+ }
112
+ sql2.inline = inline;
113
+ function placeholder(name) {
114
+ return empty().placeholder(name);
115
+ }
116
+ sql2.placeholder = placeholder;
117
+ function identifier(identifier2) {
118
+ return empty().identifier(identifier2);
119
+ }
120
+ sql2.identifier = identifier;
121
+ function field(field2) {
122
+ return empty().field(field2);
123
+ }
124
+ sql2.field = field;
125
+ function chunk(type, inner) {
126
+ return empty().chunk(type, inner);
127
+ }
128
+ sql2.chunk = chunk;
129
+ function query(ast) {
130
+ return join(
131
+ Object.entries(ast).map(([key, value2]) => {
132
+ const statement = key.replace(/([A-Z])/g, " $1").toLocaleLowerCase();
133
+ return value2 && sql2`${sql2.unsafe(statement)} ${value2}`;
134
+ })
135
+ );
136
+ }
137
+ sql2.query = query;
138
+ function join(items, separator = sql2` `) {
139
+ const parts = items.filter(Boolean);
140
+ const sql3 = new Sql();
141
+ for (let i = 0; i < parts.length; i++) {
142
+ if (i > 0)
143
+ sql3.add(separator);
144
+ sql3.add(parts[i]);
145
+ }
146
+ return sql3;
147
+ }
148
+ sql2.join = join;
149
+ })(sql || (sql = {}));
150
+ export {
151
+ Sql,
152
+ sql
153
+ };
@@ -0,0 +1,56 @@
1
+ import type { Column, JsonColumn, RequiredColumn } from './Column.ts';
2
+ import type { ForeignKeyConstraint, PrimaryKeyConstraint, UniqueConstraint } from './Constraint.ts';
3
+ import type { Index } from './Index.ts';
4
+ import { type HasSql, type HasTable, type HasTarget } from './Internal.ts';
5
+ import { type Sql } from './Sql.ts';
6
+ import { type JsonExpr } from './expr/Conditions.ts';
7
+ import { Field } from './expr/Field.ts';
8
+ import type { Input } from './expr/Input.ts';
9
+ export type TableDefinition = {
10
+ [name: string]: Column;
11
+ };
12
+ declare class TableData {
13
+ name: string;
14
+ alias?: string;
15
+ schemaName?: string;
16
+ columns: TableDefinition;
17
+ config?: TableConfig;
18
+ }
19
+ export declare class TableApi<Definition extends TableDefinition = TableDefinition, Name extends string = string> extends TableData {
20
+ private brand;
21
+ get aliased(): string;
22
+ target(): Sql;
23
+ createDefinition(): Sql;
24
+ listColumns(): Sql;
25
+ fields(): Record<string, HasSql>;
26
+ }
27
+ export type Table<Definition extends TableDefinition = TableDefinition, Name extends string = string> = TableFields<Definition, Name> & HasTable<Definition, Name> & HasTarget;
28
+ export type TableFields<Definition extends TableDefinition, TableName extends string = string> = {
29
+ [K in keyof Definition]: Definition[K] extends JsonColumn<infer T> ? JsonExpr<T> : Definition[K] extends Column<infer T> ? Field<T, TableName> : never;
30
+ };
31
+ export type TableRow<Definition extends TableDefinition> = {
32
+ [K in keyof Definition]: Definition[K] extends Column<infer T> ? T : never;
33
+ } & {};
34
+ type IsReq<Col> = Col extends RequiredColumn ? true : false;
35
+ type Required<D> = {
36
+ [K in keyof D as true extends IsReq<D[K]> ? K : never]: D[K];
37
+ };
38
+ type Optional<D> = {
39
+ [K in keyof D as false extends IsReq<D[K]> ? K : never]: D[K];
40
+ };
41
+ type RequiredInput<D> = {
42
+ readonly [K in keyof Required<D>]: D[K] extends Column<infer V> ? Input<V> : never;
43
+ };
44
+ type OptionalInput<D> = {
45
+ readonly [K in keyof Optional<D>]?: D[K] extends Column<infer V> ? Input<V> : never;
46
+ };
47
+ export type TableInsert<Definition extends TableDefinition> = RequiredInput<Definition> & OptionalInput<Definition>;
48
+ export type TableUpdate<Definition extends TableDefinition> = {
49
+ readonly [K in keyof Definition]?: Definition[K] extends Column<infer T> ? Input<T> : never;
50
+ };
51
+ export type TableConfigSetting<Name extends string> = UniqueConstraint<Name> | PrimaryKeyConstraint<Name> | ForeignKeyConstraint<Name> | Index<Name>;
52
+ export interface TableConfig<Name extends string = string> extends Record<string, TableConfigSetting<Name>> {
53
+ }
54
+ export declare function table<Definition extends TableDefinition, Name extends string>(name: Name, columns: Definition, config?: (self: Table<Definition, Name>) => TableConfig<Name>, schemaName?: string): Table<Definition, Name>;
55
+ export declare function alias<Definition extends TableDefinition, Alias extends string>(table: Table<Definition>, alias: Alias): Table<Definition, Alias>;
56
+ export {};
@@ -0,0 +1,103 @@
1
+ import {
2
+ getColumn,
3
+ getConstraint,
4
+ getTable,
5
+ hasConstraint,
6
+ internalTable,
7
+ internalTarget
8
+ } from "./Internal.ts";
9
+ import { sql } from "./Sql.ts";
10
+ import { jsonExpr } from "./expr/Conditions.ts";
11
+ import { Field } from "./expr/Field.ts";
12
+ const { assign, fromEntries, entries } = Object;
13
+ class TableData {
14
+ name;
15
+ alias;
16
+ schemaName;
17
+ columns;
18
+ config;
19
+ }
20
+ class TableApi extends TableData {
21
+ get aliased() {
22
+ return this.alias ?? this.name;
23
+ }
24
+ target() {
25
+ const name = sql.join(
26
+ [
27
+ this.schemaName ? sql.identifier(this.schemaName) : void 0,
28
+ sql.identifier(this.name)
29
+ ],
30
+ sql`.`
31
+ );
32
+ return sql.join([
33
+ name,
34
+ this.alias ? sql`as ${sql.identifier(this.alias)}` : void 0
35
+ ]);
36
+ }
37
+ createDefinition() {
38
+ const createColumns = entries(this.columns).map(([name, isColumn]) => {
39
+ const column = getColumn(isColumn);
40
+ const columnName = sql.identifier(column.name ?? name);
41
+ return sql`${columnName} ${sql.chunk("emitColumn", column)}`;
42
+ });
43
+ const createConstraints = entries(this.config ?? {}).filter(([, constraint]) => hasConstraint(constraint)).map(
44
+ ([name, constraint]) => sql`constraint ${sql.identifier(name)} ${getConstraint(
45
+ constraint
46
+ )}`
47
+ );
48
+ return sql.join(createColumns.concat(createConstraints), sql`, `);
49
+ }
50
+ listColumns() {
51
+ return sql.join(
52
+ entries(this.columns).map(([name, column]) => {
53
+ const columnApi = getColumn(column);
54
+ const { name: givenName } = columnApi;
55
+ return sql.identifier(givenName ?? name);
56
+ }),
57
+ sql`, `
58
+ );
59
+ }
60
+ fields() {
61
+ return fromEntries(
62
+ entries(this.columns).map(([name, column]) => {
63
+ const columnApi = getColumn(column);
64
+ const { name: givenName } = columnApi;
65
+ const field = new Field(this.aliased, givenName ?? name, columnApi);
66
+ if (columnApi.json)
67
+ return [name, jsonExpr(field)];
68
+ return [name, field];
69
+ })
70
+ );
71
+ }
72
+ }
73
+ function table(name, columns, config, schemaName) {
74
+ const api = assign(new TableApi(), {
75
+ name,
76
+ schemaName,
77
+ columns
78
+ });
79
+ const table2 = {
80
+ [internalTable]: api,
81
+ [internalTarget]: api.target(),
82
+ ...api.fields()
83
+ };
84
+ if (config)
85
+ api.config = config(table2);
86
+ return table2;
87
+ }
88
+ function alias(table2, alias2) {
89
+ const api = assign(new TableApi(), {
90
+ ...getTable(table2),
91
+ alias: alias2
92
+ });
93
+ return {
94
+ [internalTable]: api,
95
+ [internalTarget]: api.target(),
96
+ ...api.fields()
97
+ };
98
+ }
99
+ export {
100
+ TableApi,
101
+ alias,
102
+ table
103
+ };
@@ -0,0 +1,3 @@
1
+ export type Expand<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & {};
@@ -0,0 +1,2 @@
1
+ import type { SelectionInput } from './Selection.ts';
2
+ export declare function virtual<Input extends SelectionInput>(alias: string, source?: Input): Input;
@@ -0,0 +1,28 @@
1
+ import { getSql, hasSql } from "./Internal.ts";
2
+ import { Field } from "./expr/Field.ts";
3
+ function virtual(alias, source) {
4
+ if (source && hasSql(source)) {
5
+ const expr = getSql(source);
6
+ const name = expr.alias;
7
+ if (!name)
8
+ throw new Error("Cannot alias a virtual field without a name");
9
+ return new Field(alias, name, expr);
10
+ }
11
+ return new Proxy(/* @__PURE__ */ Object.create(null), {
12
+ get(target, field) {
13
+ if (field in target)
14
+ return target[field];
15
+ const from = source?.[field];
16
+ if (typeof field !== "string")
17
+ return from;
18
+ return target[field] = new Field(
19
+ alias,
20
+ field,
21
+ from ? getSql(from) : void 0
22
+ );
23
+ }
24
+ });
25
+ }
26
+ export {
27
+ virtual
28
+ };
@@ -0,0 +1,10 @@
1
+ import { type HasSql } from '../Internal.ts';
2
+ import { type Sql } from '../Sql.ts';
3
+ export declare function count(input?: HasSql): Sql<number>;
4
+ export declare function countDistinct(input: HasSql): Sql<number>;
5
+ export declare function avg(input: HasSql): Sql<string | null>;
6
+ export declare function avgDistinct(input: HasSql): Sql<string | null>;
7
+ export declare function sum(input: HasSql): HasSql<string | null>;
8
+ export declare function sumDistinct(input: HasSql): HasSql<string | null>;
9
+ export declare function max<T>(input: HasSql<T>): Sql<T>;
10
+ export declare function min<T>(input: HasSql<T>): Sql<T>;
@@ -0,0 +1,38 @@
1
+ import { getSql } from "../Internal.ts";
2
+ import { sql } from "../Sql.ts";
3
+ import { distinct } from "./Conditions.ts";
4
+ import { Functions } from "./Functions.ts";
5
+ function count(input) {
6
+ return Functions.count(input ?? sql`*`).mapWith(Number);
7
+ }
8
+ function countDistinct(input) {
9
+ return Functions.count(distinct(input)).mapWith(Number);
10
+ }
11
+ function avg(input) {
12
+ return Functions.avg(input).mapWith(String);
13
+ }
14
+ function avgDistinct(input) {
15
+ return Functions.avg(distinct(input)).mapWith(String);
16
+ }
17
+ function sum(input) {
18
+ return Functions.sum(input).mapWith(String);
19
+ }
20
+ function sumDistinct(input) {
21
+ return Functions.sum(distinct(input)).mapWith(String);
22
+ }
23
+ function max(input) {
24
+ return Functions.max(input).mapWith(getSql(input));
25
+ }
26
+ function min(input) {
27
+ return Functions.min(input).mapWith(getSql(input));
28
+ }
29
+ export {
30
+ avg,
31
+ avgDistinct,
32
+ count,
33
+ countDistinct,
34
+ max,
35
+ min,
36
+ sum,
37
+ sumDistinct
38
+ };
@@ -0,0 +1,40 @@
1
+ import type { HasSql } from '../Internal.ts';
2
+ import { type Sql } from '../Sql.ts';
3
+ import { type Input } from './Input.ts';
4
+ export declare function eq<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
5
+ export declare function ne<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
6
+ export declare function and(...conditions: Array<Input<boolean>>): HasSql<boolean>;
7
+ export declare function or(...conditions: Array<Input<boolean>>): HasSql<boolean>;
8
+ export declare function not(condition: Input): HasSql<boolean>;
9
+ export declare function gt<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
10
+ export declare function gte<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
11
+ export declare function lt<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
12
+ export declare function lte<T>(left: Input<T>, right: Input<T>): HasSql<boolean>;
13
+ export declare function inArray<T>(left: Input<T>, right: Input<Array<T>>): HasSql<boolean>;
14
+ export declare function notInArray<T>(left: Input<T>, right: Input<Array<T>>): HasSql<boolean>;
15
+ export declare function isNull(value: Input): HasSql<boolean>;
16
+ export declare function isNotNull(value: Input): HasSql<boolean>;
17
+ export declare function between<T>(value: Input<T>, left: Input<T>, right: Input<T>): HasSql<boolean>;
18
+ export declare function notBetween<T>(value: Input<T>, left: Input<T>, right: Input<T>): HasSql<boolean>;
19
+ export declare function like(left: Input<string>, pattern: Input<string>): HasSql<boolean>;
20
+ export declare function notLike(value: Input<string>, pattern: Input<string>): HasSql<boolean>;
21
+ export declare function ilike(value: Input<string>, pattern: Input<string>): HasSql<boolean>;
22
+ export declare function notILike(value: Input<string>, pattern: Input<string>): HasSql<boolean>;
23
+ export declare function arrayContains<T>(left: Input<Array<T>>, right: Input<T>): HasSql<boolean>;
24
+ export declare function arrayContained<T>(left: Input<T>, right: Input<Array<T>>): HasSql<boolean>;
25
+ export declare function arrayOverlaps<T>(left: Input<Array<T>>, right: Input<Array<T>>): HasSql<boolean>;
26
+ export declare function asc<T>(input: HasSql<T>): Sql;
27
+ export declare function desc<T>(input: HasSql<T>): Sql;
28
+ export declare function distinct<T>(input: HasSql<T>): Sql;
29
+ export interface JsonArrayHasSql<Value> extends HasSql<Value> {
30
+ [index: number]: JsonExpr<Value>;
31
+ }
32
+ export type JsonRecordHasSql<Row> = HasSql<Row> & {
33
+ [K in keyof Row]: JsonExpr<Row[K]>;
34
+ };
35
+ type Nullable<T> = {
36
+ [P in keyof T]: T[P] | null;
37
+ };
38
+ export type JsonExpr<Value> = [NonNullable<Value>] extends [Array<infer V>] ? JsonArrayHasSql<null extends Value ? V | null : V> : [NonNullable<Value>] extends [object] ? JsonRecordHasSql<null extends Value ? Nullable<Value> : Value> : HasSql<Value>;
39
+ export declare function jsonExpr<Value>(e: HasSql<Value>): JsonExpr<Value>;
40
+ export {};