@rwillians/qx 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bun-sqlite.d.ts +47 -0
- package/dist/cjs/bun-sqlite.js +483 -0
- package/dist/cjs/index.d.ts +1174 -0
- package/dist/cjs/index.js +666 -0
- package/dist/cjs/pretty-logger.d.ts +7 -0
- package/dist/cjs/pretty-logger.js +14 -0
- package/dist/cjs/standard-schema.d.ts +157 -0
- package/dist/cjs/standard-schema.js +236 -0
- package/dist/cjs/utils.d.ts +50 -0
- package/dist/cjs/utils.js +76 -0
- package/dist/esm/bun-sqlite.d.ts +47 -0
- package/dist/esm/bun-sqlite.js +483 -0
- package/dist/esm/index.d.ts +1174 -0
- package/dist/esm/index.js +660 -0
- package/dist/esm/pretty-logger.d.ts +7 -0
- package/dist/esm/pretty-logger.js +11 -0
- package/dist/esm/standard-schema.d.ts +157 -0
- package/dist/esm/standard-schema.js +233 -0
- package/dist/esm/utils.d.ts +50 -0
- package/dist/esm/utils.js +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1174 @@
|
|
|
1
|
+
import * as std from './standard-schema';
|
|
2
|
+
/**
|
|
3
|
+
* @private Symbol used to store the table name on aliased tables.
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
* @version 1
|
|
6
|
+
*/
|
|
7
|
+
declare const TABLE_NAME: unique symbol;
|
|
8
|
+
/**
|
|
9
|
+
* @private Symbol used to store the table alias on aliased tables.
|
|
10
|
+
* @since 0.1.0
|
|
11
|
+
* @version 1
|
|
12
|
+
*/
|
|
13
|
+
declare const TABLE_ALIAS: unique symbol;
|
|
14
|
+
/**
|
|
15
|
+
* @private Little trick to force TypeScript to resolve types instead
|
|
16
|
+
* of composing them.
|
|
17
|
+
* @since 0.1.0
|
|
18
|
+
* @version 1
|
|
19
|
+
*/
|
|
20
|
+
type Expand<T> = T extends object ? {
|
|
21
|
+
[K in keyof T]: T[K];
|
|
22
|
+
} : never;
|
|
23
|
+
/**
|
|
24
|
+
* @public The list of primitive types supported by qx.
|
|
25
|
+
* @since 0.1.0
|
|
26
|
+
* @version 1
|
|
27
|
+
*/
|
|
28
|
+
type Primitive = 'BINARY' | 'BOOLEAN' | 'DATETIME' | 'FLOAT' | 'INTEGER' | 'TEXT' | 'VARCHAR';
|
|
29
|
+
/**
|
|
30
|
+
* @private All properties of a column, except its name and the table
|
|
31
|
+
* to which they belong.
|
|
32
|
+
* @since 0.1.0
|
|
33
|
+
* @version 1
|
|
34
|
+
*/
|
|
35
|
+
type ColumnProps<T extends std.Schema = std.Schema> = {
|
|
36
|
+
/**
|
|
37
|
+
* @public whether the column is autoincrementing.
|
|
38
|
+
* @since 0.1.0
|
|
39
|
+
* @version 1
|
|
40
|
+
*/
|
|
41
|
+
autoincrement?: true;
|
|
42
|
+
/**
|
|
43
|
+
* @public The default value for the column or a function that
|
|
44
|
+
* returns it. If a column has no value (nullish) and
|
|
45
|
+
* default function was provided, then it will be called
|
|
46
|
+
* once for each row right before its insertion.
|
|
47
|
+
* @since 0.1.0
|
|
48
|
+
* @version 1
|
|
49
|
+
*/
|
|
50
|
+
default?: std.output<T> | (() => std.output<T>);
|
|
51
|
+
/**
|
|
52
|
+
* @public Whether the column is nullable.
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
* @version 1
|
|
55
|
+
*/
|
|
56
|
+
nullable?: true;
|
|
57
|
+
/**
|
|
58
|
+
* @public Whether the column is a primary key.
|
|
59
|
+
* @since 0.1.0
|
|
60
|
+
* @version 1
|
|
61
|
+
*/
|
|
62
|
+
primaryKey?: true;
|
|
63
|
+
/**
|
|
64
|
+
* @public The standard schema that defines the column's input and
|
|
65
|
+
* output types.
|
|
66
|
+
* @since 0.1.0
|
|
67
|
+
* @version 1
|
|
68
|
+
*/
|
|
69
|
+
schema: T;
|
|
70
|
+
/**
|
|
71
|
+
* @public The size of a string column (VARCHAR). If provided for
|
|
72
|
+
* non-varchar columns, it will be ignored.
|
|
73
|
+
* @since 0.1.0
|
|
74
|
+
* @version 1
|
|
75
|
+
*/
|
|
76
|
+
size?: number;
|
|
77
|
+
/**
|
|
78
|
+
* @public The type of the column, one of the supported primitive
|
|
79
|
+
* types by `qx`. See {@link Primitive} for the complete
|
|
80
|
+
* list of primitives.
|
|
81
|
+
* @since 0.1.0
|
|
82
|
+
* @version 1
|
|
83
|
+
*/
|
|
84
|
+
type: Primitive;
|
|
85
|
+
/**
|
|
86
|
+
* @public Whether the column has a unique constraint.
|
|
87
|
+
* @since 0.1.0
|
|
88
|
+
* @version 1
|
|
89
|
+
*/
|
|
90
|
+
unique?: true;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* @public A column definition.
|
|
94
|
+
* @since 0.1.0
|
|
95
|
+
* @version 1
|
|
96
|
+
*/
|
|
97
|
+
type Column<S extends string = string, T extends ColumnProps = ColumnProps> = Expand<T & {
|
|
98
|
+
/**
|
|
99
|
+
* @public The name of the column.
|
|
100
|
+
* @since 0.1.0
|
|
101
|
+
* @version 1
|
|
102
|
+
*/
|
|
103
|
+
name: S;
|
|
104
|
+
/**
|
|
105
|
+
* @public The name of the table to which the column belongs or its
|
|
106
|
+
* alias when the table has been aliased.
|
|
107
|
+
* @since 0.1.0
|
|
108
|
+
* @version 1
|
|
109
|
+
*/
|
|
110
|
+
table: string;
|
|
111
|
+
/**
|
|
112
|
+
* @public The column's input type, cached.
|
|
113
|
+
* @since 0.1.0
|
|
114
|
+
* @version 1
|
|
115
|
+
*/
|
|
116
|
+
inputType: std.input<T['schema']>;
|
|
117
|
+
/**
|
|
118
|
+
* @public The column's output type, cached.
|
|
119
|
+
* @since 0.1.0
|
|
120
|
+
* @version 1
|
|
121
|
+
*/
|
|
122
|
+
outputType: std.output<T['schema']>;
|
|
123
|
+
}>;
|
|
124
|
+
/**
|
|
125
|
+
* @public A table definition.
|
|
126
|
+
* @since 0.1.0
|
|
127
|
+
* @version 1
|
|
128
|
+
*/
|
|
129
|
+
type Table<S extends string = string, T extends Record<string, ColumnProps> = Record<string, ColumnProps>> = {
|
|
130
|
+
/**
|
|
131
|
+
* @public The name of the table.
|
|
132
|
+
* @since 0.1.0
|
|
133
|
+
* @version 1
|
|
134
|
+
*/
|
|
135
|
+
name: S;
|
|
136
|
+
/**
|
|
137
|
+
* @public The columns of the table.
|
|
138
|
+
* @since 0.1.0
|
|
139
|
+
* @version 1
|
|
140
|
+
*/
|
|
141
|
+
columns: {
|
|
142
|
+
[K in keyof T & string]: Column<K, T[K]>;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* @private Represents a table when it's been aliased in a query.
|
|
147
|
+
* @since 0.1.0
|
|
148
|
+
* @version 1
|
|
149
|
+
*/
|
|
150
|
+
type Aliased<S extends string = string, T extends Table = Table> = T['columns'] & {
|
|
151
|
+
/**
|
|
152
|
+
* @public The original name of the table.
|
|
153
|
+
* @since 0.1.0
|
|
154
|
+
* @version 1
|
|
155
|
+
*/
|
|
156
|
+
[TABLE_NAME]: T['name'];
|
|
157
|
+
/**
|
|
158
|
+
* @public The alias of the table.
|
|
159
|
+
* @since 0.1.0
|
|
160
|
+
* @version 1
|
|
161
|
+
*/
|
|
162
|
+
[TABLE_ALIAS]: S;
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* @private Adds the ability to alias a table.
|
|
166
|
+
* @since 0.1.0
|
|
167
|
+
* @version 1
|
|
168
|
+
*/
|
|
169
|
+
type Aliasable<T extends Table> = T & {
|
|
170
|
+
/**
|
|
171
|
+
* @public Creates an aliased table from a regular table.
|
|
172
|
+
* @since 0.1.0
|
|
173
|
+
* @version 1
|
|
174
|
+
*/
|
|
175
|
+
as: <S extends string>(alias: S) => Aliased<S, T>;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* @private The pattern of columns that should be omitted when
|
|
179
|
+
* inserting a new row.
|
|
180
|
+
* @since 0.1.0
|
|
181
|
+
* @version 1
|
|
182
|
+
*/
|
|
183
|
+
type OmitOnInsert = {
|
|
184
|
+
autoincrement: true;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* @private The pattern of columns that should be optional when
|
|
188
|
+
* inserting a new row.
|
|
189
|
+
* @since 0.1.0
|
|
190
|
+
* @version 1
|
|
191
|
+
*/
|
|
192
|
+
type OptionalOnInsert = {
|
|
193
|
+
default: any;
|
|
194
|
+
} | {
|
|
195
|
+
nullable: true;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* @private The pattern of columns that should be omitted when
|
|
199
|
+
* updating a row.
|
|
200
|
+
* @since 0.1.0
|
|
201
|
+
* @version 1
|
|
202
|
+
*/
|
|
203
|
+
type OmitOnUpdate = {
|
|
204
|
+
autoincrement: true;
|
|
205
|
+
} | {
|
|
206
|
+
primaryKey: true;
|
|
207
|
+
};
|
|
208
|
+
/**
|
|
209
|
+
* @private Infers the output type of a table's row.
|
|
210
|
+
* @since 0.1.0
|
|
211
|
+
* @version 1
|
|
212
|
+
*/
|
|
213
|
+
type Infer<T extends Table> = {
|
|
214
|
+
[K in keyof T['columns']]: T['columns'][K]['outputType'];
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* @private Infers the input type required to insert a new row.
|
|
218
|
+
* @since 0.1.0
|
|
219
|
+
* @version 1
|
|
220
|
+
*/
|
|
221
|
+
type InferForInsert<T extends Table> = Expand<{
|
|
222
|
+
[K in keyof T['columns'] as T['columns'][K] extends OmitOnInsert | OptionalOnInsert ? never : K]: T['columns'][K]['inputType'];
|
|
223
|
+
} & {
|
|
224
|
+
[K in keyof T['columns'] as T['columns'][K] extends OptionalOnInsert ? K : never]?: T['columns'][K]['inputType'];
|
|
225
|
+
}>;
|
|
226
|
+
/**
|
|
227
|
+
* @private Infers the input type required to update an existing row.
|
|
228
|
+
* @since 0.1.0
|
|
229
|
+
* @version 1
|
|
230
|
+
*/
|
|
231
|
+
type InferForUpdate<T extends Table> = {
|
|
232
|
+
[K in keyof T['columns'] as T['columns'][K] extends OmitOnUpdate ? never : K]?: T['columns'][K]['inputType'];
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* @private Adds the ability to infer types of a table.
|
|
236
|
+
* @since 0.1.0
|
|
237
|
+
* @version 1
|
|
238
|
+
*/
|
|
239
|
+
type Inferrable<T extends Table> = T & {
|
|
240
|
+
/**
|
|
241
|
+
* @private The output type of a row, cached.
|
|
242
|
+
* @since 0.1.0
|
|
243
|
+
* @version 1
|
|
244
|
+
*/
|
|
245
|
+
infer: Infer<T>;
|
|
246
|
+
/**
|
|
247
|
+
* @private The input type for a new row, cached.
|
|
248
|
+
* @since 0.1.0
|
|
249
|
+
* @version 1
|
|
250
|
+
*/
|
|
251
|
+
inferForInsert: InferForInsert<T>;
|
|
252
|
+
/**
|
|
253
|
+
* @private The input type for updating an existing row, cached.
|
|
254
|
+
* @since 0.1.0
|
|
255
|
+
* @version 1
|
|
256
|
+
*/
|
|
257
|
+
inferForUpdate: InferForUpdate<T>;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* @private A builder for column properties.
|
|
261
|
+
* @since 0.1.0
|
|
262
|
+
* @version 1
|
|
263
|
+
*/
|
|
264
|
+
declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
|
|
265
|
+
readonly props: T;
|
|
266
|
+
constructor(props: T);
|
|
267
|
+
/**
|
|
268
|
+
* @public Marks the column as autoincrementing.
|
|
269
|
+
* @since 0.1.0
|
|
270
|
+
* @version 1
|
|
271
|
+
*/
|
|
272
|
+
autoincrement(): ColumnPropsBuilder<Expand<T & {
|
|
273
|
+
autoincrement: true;
|
|
274
|
+
}>>;
|
|
275
|
+
/**
|
|
276
|
+
* @public Sets a default value for the column.
|
|
277
|
+
* @since 0.1.0
|
|
278
|
+
* @version 1
|
|
279
|
+
*/
|
|
280
|
+
default<S extends std.output<T['schema']>, U extends S | (() => S)>(value: U): ColumnPropsBuilder<Expand<T & {
|
|
281
|
+
default: U;
|
|
282
|
+
}>>;
|
|
283
|
+
/**
|
|
284
|
+
* @public Marks the column as nullable.
|
|
285
|
+
* @since 0.1.0
|
|
286
|
+
* @version 1
|
|
287
|
+
* @throws {Error} if the column is a primary key.
|
|
288
|
+
*/
|
|
289
|
+
nullable(): ColumnPropsBuilder<Expand<Omit<T, "schema"> & {
|
|
290
|
+
nullable: true;
|
|
291
|
+
schema: std.QxNullable<T["schema"]>;
|
|
292
|
+
}>>;
|
|
293
|
+
/**
|
|
294
|
+
* @public Marks the column as a primary key.
|
|
295
|
+
* @since 0.1.0
|
|
296
|
+
* @version 1
|
|
297
|
+
* @throws {Error} if the column is nullable.
|
|
298
|
+
*/
|
|
299
|
+
primaryKey(): ColumnPropsBuilder<Expand<T & {
|
|
300
|
+
primaryKey: true;
|
|
301
|
+
}>>;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* @public Built-in column types.
|
|
305
|
+
* @since 0.1.0
|
|
306
|
+
* @version 1
|
|
307
|
+
*/
|
|
308
|
+
declare const types: {
|
|
309
|
+
/**
|
|
310
|
+
* @public Defines a column type that accepts binary data.
|
|
311
|
+
* @since 0.1.0
|
|
312
|
+
* @version 1
|
|
313
|
+
*/
|
|
314
|
+
binary: () => ColumnPropsBuilder<{
|
|
315
|
+
type: "BINARY";
|
|
316
|
+
schema: std.QxInstanceOf<Uint8Array<ArrayBuffer>>;
|
|
317
|
+
}>;
|
|
318
|
+
/**
|
|
319
|
+
* @public Defines a column type that accepts boolean values.
|
|
320
|
+
* @since 0.1.0
|
|
321
|
+
* @version 1
|
|
322
|
+
*/
|
|
323
|
+
boolean: () => ColumnPropsBuilder<{
|
|
324
|
+
type: "BOOLEAN";
|
|
325
|
+
schema: std.QxBoolean;
|
|
326
|
+
}>;
|
|
327
|
+
/**
|
|
328
|
+
* @public Defines a column type that accepts a Date object, an
|
|
329
|
+
* ISO 8601 date string, or a Unix timestamp in milliseconds.
|
|
330
|
+
* @since 0.1.0
|
|
331
|
+
* @version 1
|
|
332
|
+
*/
|
|
333
|
+
datetime: () => ColumnPropsBuilder<{
|
|
334
|
+
type: "DATETIME";
|
|
335
|
+
schema: std.QxCoercibleDate;
|
|
336
|
+
}>;
|
|
337
|
+
/**
|
|
338
|
+
* @public Defines a column type that accepts floating-point numbers.
|
|
339
|
+
* @since 0.1.0
|
|
340
|
+
* @version 1
|
|
341
|
+
*/
|
|
342
|
+
float: () => ColumnPropsBuilder<{
|
|
343
|
+
type: "FLOAT";
|
|
344
|
+
schema: std.QxNumber;
|
|
345
|
+
}>;
|
|
346
|
+
/**
|
|
347
|
+
* @public Defines a column type that accepts integer numbers.
|
|
348
|
+
* @since 0.1.0
|
|
349
|
+
* @version 1
|
|
350
|
+
*/
|
|
351
|
+
integer: () => ColumnPropsBuilder<{
|
|
352
|
+
type: "INTEGER";
|
|
353
|
+
schema: std.QxInteger;
|
|
354
|
+
}>;
|
|
355
|
+
/**
|
|
356
|
+
* @public Defines a column type that accepts small strings (VARCHAR,
|
|
357
|
+
* up to 255 characters).
|
|
358
|
+
* @since 0.1.0
|
|
359
|
+
* @version 1
|
|
360
|
+
* @throws {Error} if the specified size is greater than 255.
|
|
361
|
+
*/
|
|
362
|
+
string: ({ size }?: {
|
|
363
|
+
size?: number;
|
|
364
|
+
}) => ColumnPropsBuilder<{
|
|
365
|
+
type: "VARCHAR";
|
|
366
|
+
schema: std.QxString;
|
|
367
|
+
size: number;
|
|
368
|
+
}>;
|
|
369
|
+
/**
|
|
370
|
+
* @public Defines a column type that accepts large strings (TEXT).
|
|
371
|
+
* @since 0.1.0
|
|
372
|
+
* @version 1
|
|
373
|
+
*/
|
|
374
|
+
text: () => ColumnPropsBuilder<{
|
|
375
|
+
type: "TEXT";
|
|
376
|
+
schema: std.QxString;
|
|
377
|
+
}>;
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* @public Defines a new table.
|
|
381
|
+
* @since 0.1.0
|
|
382
|
+
* @version 1
|
|
383
|
+
*/
|
|
384
|
+
declare const defineTable: <S extends string, T extends Record<string, ColumnPropsBuilder>>(tname: S, shapeFn: (t: typeof types) => T) => Aliasable<Inferrable<Table<S, { [K in keyof T & string]: T[K]["props"]; }>>>;
|
|
385
|
+
/**
|
|
386
|
+
* @public Represents an equality expression.
|
|
387
|
+
* @since 0.1.0
|
|
388
|
+
* @version 1
|
|
389
|
+
*/
|
|
390
|
+
type ExprEq = {
|
|
391
|
+
lhs: Expr;
|
|
392
|
+
op: '=';
|
|
393
|
+
rhs: Expr;
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* @public Represents a not-equal expression.
|
|
397
|
+
* @since 0.1.0
|
|
398
|
+
* @version 1
|
|
399
|
+
*/
|
|
400
|
+
type ExprNe = {
|
|
401
|
+
lhs: Expr;
|
|
402
|
+
op: '!=';
|
|
403
|
+
rhs: Expr;
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* @public Represents a less-than expression.
|
|
407
|
+
* @since 0.1.0
|
|
408
|
+
* @version 1
|
|
409
|
+
*/
|
|
410
|
+
type ExprLt = {
|
|
411
|
+
lhs: Expr;
|
|
412
|
+
op: '<';
|
|
413
|
+
rhs: Expr;
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* @public Represents a less-than-or-equal expression.
|
|
417
|
+
* @since 0.1.0
|
|
418
|
+
* @version 1
|
|
419
|
+
*/
|
|
420
|
+
type ExprLte = {
|
|
421
|
+
lhs: Expr;
|
|
422
|
+
op: '<=';
|
|
423
|
+
rhs: Expr;
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* @public Represents a greater-than expression.
|
|
427
|
+
* @since 0.1.0
|
|
428
|
+
* @version 1
|
|
429
|
+
*/
|
|
430
|
+
type ExprGt = {
|
|
431
|
+
lhs: Expr;
|
|
432
|
+
op: '>';
|
|
433
|
+
rhs: Expr;
|
|
434
|
+
};
|
|
435
|
+
/**
|
|
436
|
+
* @public Represents a greater-than-or-equal expression.
|
|
437
|
+
* @since 0.1.0
|
|
438
|
+
* @version 1
|
|
439
|
+
*/
|
|
440
|
+
type ExprGte = {
|
|
441
|
+
lhs: Expr;
|
|
442
|
+
op: '>=';
|
|
443
|
+
rhs: Expr;
|
|
444
|
+
};
|
|
445
|
+
/**
|
|
446
|
+
* @public Represents a LIKE expression.
|
|
447
|
+
* @since 0.1.0
|
|
448
|
+
* @version 1
|
|
449
|
+
*/
|
|
450
|
+
type ExprLike = {
|
|
451
|
+
lhs: Expr;
|
|
452
|
+
op: 'LIKE';
|
|
453
|
+
rhs: string;
|
|
454
|
+
};
|
|
455
|
+
/**
|
|
456
|
+
* @public Represents a NOT LIKE expression.
|
|
457
|
+
* @since 0.1.0
|
|
458
|
+
* @version 1
|
|
459
|
+
*/
|
|
460
|
+
type ExprNotLike = {
|
|
461
|
+
lhs: Expr;
|
|
462
|
+
op: 'NOT LIKE';
|
|
463
|
+
rhs: string;
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* @public Represents an IN expression.
|
|
467
|
+
* @since 0.1.0
|
|
468
|
+
* @version 1
|
|
469
|
+
*/
|
|
470
|
+
type ExprIn = {
|
|
471
|
+
lhs: Expr;
|
|
472
|
+
op: 'IN';
|
|
473
|
+
rhs: Exclude<Expr, ExprLiteral> | ExprLiteral[];
|
|
474
|
+
};
|
|
475
|
+
/**
|
|
476
|
+
* @public Represents a NOT IN expression.
|
|
477
|
+
* @since 0.1.0
|
|
478
|
+
* @version 1
|
|
479
|
+
*/
|
|
480
|
+
type ExprNotIn = {
|
|
481
|
+
lhs: Expr;
|
|
482
|
+
op: 'NOT IN';
|
|
483
|
+
rhs: Exclude<Expr, ExprLiteral> | ExprLiteral[];
|
|
484
|
+
};
|
|
485
|
+
/**
|
|
486
|
+
* @public Represents an AND expression.
|
|
487
|
+
* @since 0.1.0
|
|
488
|
+
* @version 1
|
|
489
|
+
*/
|
|
490
|
+
type ExprAnd = {
|
|
491
|
+
and: Expr[];
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* @public Represents an OR expression.
|
|
495
|
+
* @since 0.1.0
|
|
496
|
+
* @version 1
|
|
497
|
+
*/
|
|
498
|
+
type ExprOr = {
|
|
499
|
+
or: Expr[];
|
|
500
|
+
};
|
|
501
|
+
/**
|
|
502
|
+
* @public Represents a NOT expression.
|
|
503
|
+
* @since 0.1.0
|
|
504
|
+
* @version 1
|
|
505
|
+
*/
|
|
506
|
+
type ExprNot = {
|
|
507
|
+
not: Expr;
|
|
508
|
+
};
|
|
509
|
+
/**
|
|
510
|
+
* @public Represents an IS expression.
|
|
511
|
+
* @since 0.1.0
|
|
512
|
+
* @version 1
|
|
513
|
+
*/
|
|
514
|
+
type ExprIs = {
|
|
515
|
+
lhs: Expr;
|
|
516
|
+
op: 'IS';
|
|
517
|
+
rhs: true | false | null;
|
|
518
|
+
};
|
|
519
|
+
/**
|
|
520
|
+
* @public Represents an IS NOT expression.
|
|
521
|
+
* @since 0.1.0
|
|
522
|
+
* @version 1
|
|
523
|
+
*/
|
|
524
|
+
type ExprIsNot = {
|
|
525
|
+
lhs: Expr;
|
|
526
|
+
op: 'IS NOT';
|
|
527
|
+
rhs: true | false | null;
|
|
528
|
+
};
|
|
529
|
+
/**
|
|
530
|
+
* @private Represents a literal value in a query expression.
|
|
531
|
+
* @since 0.1.0
|
|
532
|
+
* @version 1
|
|
533
|
+
*/
|
|
534
|
+
type ExprLiteral = boolean | Date | number | string | null;
|
|
535
|
+
/**
|
|
536
|
+
* @private Represents any binary expressions.
|
|
537
|
+
* @since 0.1.0
|
|
538
|
+
* @version 1
|
|
539
|
+
*/
|
|
540
|
+
type ExprBinaryOp = ExprEq | ExprNe | ExprLt | ExprLte | ExprGt | ExprGte | ExprLike | ExprNotLike | ExprIn | ExprNotIn | ExprIs | ExprIsNot;
|
|
541
|
+
/**
|
|
542
|
+
* @private Represents any supported expressions.
|
|
543
|
+
* @since 0.1.0
|
|
544
|
+
* @version 1
|
|
545
|
+
*/
|
|
546
|
+
type Expr = Column | ExprEq | ExprNe | ExprLt | ExprLte | ExprGt | ExprGte | ExprLike | ExprNotLike | ExprIn | ExprNotIn | ExprIs | ExprIsNot | ExprAnd | ExprOr | ExprNot | ExprLiteral;
|
|
547
|
+
/**
|
|
548
|
+
* @public Expression builders.
|
|
549
|
+
* @since 0.1.0
|
|
550
|
+
* @version 1
|
|
551
|
+
*/
|
|
552
|
+
declare const expr: {
|
|
553
|
+
/**
|
|
554
|
+
* @public Builds an equality expression.
|
|
555
|
+
* @since 0.1.0
|
|
556
|
+
* @version 1
|
|
557
|
+
*/
|
|
558
|
+
eq: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprEq;
|
|
559
|
+
/**
|
|
560
|
+
* @public Builds a not-equal expression.
|
|
561
|
+
* @since 0.1.0
|
|
562
|
+
* @version 1
|
|
563
|
+
*/
|
|
564
|
+
ne: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprNe;
|
|
565
|
+
/**
|
|
566
|
+
* @public Builds a less-than expression.
|
|
567
|
+
* @since 0.1.0
|
|
568
|
+
* @version 1
|
|
569
|
+
*/
|
|
570
|
+
lt: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprLt;
|
|
571
|
+
/**
|
|
572
|
+
* @public Builds a less-than-or-equal expression.
|
|
573
|
+
* @since 0.1.0
|
|
574
|
+
* @version 1
|
|
575
|
+
*/
|
|
576
|
+
lte: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprLte;
|
|
577
|
+
/**
|
|
578
|
+
* @public Builds a greater-than expression.
|
|
579
|
+
* @since 0.1.0
|
|
580
|
+
* @version 1
|
|
581
|
+
*/
|
|
582
|
+
gt: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprGt;
|
|
583
|
+
/**
|
|
584
|
+
* @public Builds a greater-than-or-equal expression.
|
|
585
|
+
* @since 0.1.0
|
|
586
|
+
* @version 1
|
|
587
|
+
*/
|
|
588
|
+
gte: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprGte;
|
|
589
|
+
/**
|
|
590
|
+
* @public Builds a LIKE expression.
|
|
591
|
+
* @since 0.1.0
|
|
592
|
+
* @version 1
|
|
593
|
+
*/
|
|
594
|
+
like: <L extends Expr>(lhs: L, rhs: string) => ExprLike;
|
|
595
|
+
/**
|
|
596
|
+
* @public Builds a NOT LIKE expression.
|
|
597
|
+
* @since 0.1.0
|
|
598
|
+
* @version 1
|
|
599
|
+
*/
|
|
600
|
+
notLike: <L extends Expr>(lhs: L, rhs: string) => ExprNotLike;
|
|
601
|
+
/**
|
|
602
|
+
* @public Builds an IN expression.
|
|
603
|
+
* @since 0.1.0
|
|
604
|
+
* @version 1
|
|
605
|
+
*/
|
|
606
|
+
in: <L extends Expr, R extends Exclude<Expr, ExprLiteral> | ExprLiteral[]>(lhs: L, rhs: R) => ExprIn;
|
|
607
|
+
/**
|
|
608
|
+
* @public Builds a NOT IN expression.
|
|
609
|
+
* @since 0.1.0
|
|
610
|
+
* @version 1
|
|
611
|
+
*/
|
|
612
|
+
notIn: <L extends Expr, R extends Exclude<Expr, ExprLiteral> | ExprLiteral[]>(lhs: L, rhs: R) => ExprNotIn;
|
|
613
|
+
/**
|
|
614
|
+
* @public Builds an IS expression.
|
|
615
|
+
* @since 0.1.0
|
|
616
|
+
* @version 1
|
|
617
|
+
*/
|
|
618
|
+
is: (lhs: Expr, rhs: true | false | null) => ExprIs;
|
|
619
|
+
/**
|
|
620
|
+
* @public Builds an IS NOT expression.
|
|
621
|
+
* @since 0.1.0
|
|
622
|
+
* @version 1
|
|
623
|
+
*/
|
|
624
|
+
isNot: (lhs: Expr, rhs: true | false | null) => ExprIsNot;
|
|
625
|
+
/**
|
|
626
|
+
* @public Builds an AND expression.
|
|
627
|
+
* @since 0.1.0
|
|
628
|
+
* @version 1
|
|
629
|
+
*/
|
|
630
|
+
and: <E extends Expr>(exprs: E[]) => ExprAnd;
|
|
631
|
+
/**
|
|
632
|
+
* @public Builds an OR expression.
|
|
633
|
+
* @since 0.1.0
|
|
634
|
+
* @version 1
|
|
635
|
+
*/
|
|
636
|
+
or: <E extends Expr>(exprs: E[]) => ExprOr;
|
|
637
|
+
/**
|
|
638
|
+
* @public Builds a NOT expression.
|
|
639
|
+
* @since 0.1.0
|
|
640
|
+
* @version 1
|
|
641
|
+
*/
|
|
642
|
+
not: <E extends Expr>(expr: E) => ExprNot;
|
|
643
|
+
};
|
|
644
|
+
/**
|
|
645
|
+
* @public Expression type guards.
|
|
646
|
+
* @since 0.1.0
|
|
647
|
+
* @version 1
|
|
648
|
+
*/
|
|
649
|
+
declare const is: {
|
|
650
|
+
/**
|
|
651
|
+
* @public Checks whether the given value is a binary op expression.
|
|
652
|
+
* @since 0.1.0
|
|
653
|
+
* @version 1
|
|
654
|
+
*/
|
|
655
|
+
binaryOp: (expr: Expr) => expr is ExprBinaryOp;
|
|
656
|
+
/**
|
|
657
|
+
* @public Checks whether the given value is an equality expression.
|
|
658
|
+
* @since 0.1.0
|
|
659
|
+
* @version 1
|
|
660
|
+
*/
|
|
661
|
+
eq: (expr: Expr) => expr is ExprEq;
|
|
662
|
+
/**
|
|
663
|
+
* @public Checks whether the given value is a not-equal expression.
|
|
664
|
+
* @since 0.1.0
|
|
665
|
+
* @version 1
|
|
666
|
+
*/
|
|
667
|
+
ne: (expr: Expr) => expr is ExprNe;
|
|
668
|
+
/**
|
|
669
|
+
* @public Checks whether the given value is a less-than expression.
|
|
670
|
+
* @since 0.1.0
|
|
671
|
+
* @version 1
|
|
672
|
+
*/
|
|
673
|
+
lt: (expr: Expr) => expr is ExprLt;
|
|
674
|
+
/**
|
|
675
|
+
* @public Checks whether the given value is a less-than-or-equal expression.
|
|
676
|
+
* @since 0.1.0
|
|
677
|
+
* @version 1
|
|
678
|
+
*/
|
|
679
|
+
lte: (expr: Expr) => expr is ExprLte;
|
|
680
|
+
/**
|
|
681
|
+
* @public Checks whether the given value is a greater-than expression.
|
|
682
|
+
* @since 0.1.0
|
|
683
|
+
* @version 1
|
|
684
|
+
*/
|
|
685
|
+
gt: (expr: Expr) => expr is ExprGt;
|
|
686
|
+
/**
|
|
687
|
+
* @public Checks whether the given value is a greater-than-or-equal expression.
|
|
688
|
+
* @since 0.1.0
|
|
689
|
+
* @version 1
|
|
690
|
+
*/
|
|
691
|
+
gte: (expr: Expr) => expr is ExprGte;
|
|
692
|
+
/**
|
|
693
|
+
* @public Checks whether the given value is a LIKE expression.
|
|
694
|
+
* @since 0.1.0
|
|
695
|
+
* @version 1
|
|
696
|
+
*/
|
|
697
|
+
like: (expr: Expr) => expr is ExprLike;
|
|
698
|
+
/**
|
|
699
|
+
* @public Checks whether the given value is a NOT LIKE expression.
|
|
700
|
+
* @since 0.1.0
|
|
701
|
+
* @version 1
|
|
702
|
+
*/
|
|
703
|
+
notLike: (expr: Expr) => expr is ExprNotLike;
|
|
704
|
+
/**
|
|
705
|
+
* @public Checks whether the given value is an IN expression.
|
|
706
|
+
* @since 0.1.0
|
|
707
|
+
* @version 1
|
|
708
|
+
*/
|
|
709
|
+
in: (expr: Expr) => expr is ExprIn;
|
|
710
|
+
/**
|
|
711
|
+
* @public Checks whether the given value is a NOT IN expression.
|
|
712
|
+
* @since 0.1.0
|
|
713
|
+
* @version 1
|
|
714
|
+
*/
|
|
715
|
+
notIn: (expr: Expr) => expr is ExprNotIn;
|
|
716
|
+
/**
|
|
717
|
+
* @public Checks whether the given value is an IS expression.
|
|
718
|
+
* @since 0.1.0
|
|
719
|
+
* @version 1
|
|
720
|
+
*/
|
|
721
|
+
is: (expr: Expr) => expr is ExprIs;
|
|
722
|
+
/**
|
|
723
|
+
* @public Checks whether the given value is an AND expression.
|
|
724
|
+
* @since 0.1.0
|
|
725
|
+
* @version 1
|
|
726
|
+
*/
|
|
727
|
+
and: (expr: Expr) => expr is ExprAnd;
|
|
728
|
+
/**
|
|
729
|
+
* @public Checks whether the given value is an OR expression.
|
|
730
|
+
* @since 0.1.0
|
|
731
|
+
* @version 1
|
|
732
|
+
*/
|
|
733
|
+
or: (expr: Expr) => expr is ExprOr;
|
|
734
|
+
/**
|
|
735
|
+
* @public Checks whether the given value is a NOT expression.
|
|
736
|
+
* @since 0.1.0
|
|
737
|
+
* @version 1
|
|
738
|
+
*/
|
|
739
|
+
not: (expr: Expr) => expr is ExprNot;
|
|
740
|
+
/**
|
|
741
|
+
* @public Checks whether the given value is a boolean literal.
|
|
742
|
+
* @since 0.1.0
|
|
743
|
+
* @version 1
|
|
744
|
+
*/
|
|
745
|
+
boolean: (value: Expr) => value is boolean;
|
|
746
|
+
/**
|
|
747
|
+
* @public Checks whether the given value is a date literal.
|
|
748
|
+
* @since 0.1.0
|
|
749
|
+
* @version 1
|
|
750
|
+
*/
|
|
751
|
+
date: (value: Expr) => value is Date;
|
|
752
|
+
/**
|
|
753
|
+
* @public Checks whether the given value is a null literal.
|
|
754
|
+
* @since 0.1.0
|
|
755
|
+
* @version 1
|
|
756
|
+
*/
|
|
757
|
+
null: (expr: Expr) => expr is null;
|
|
758
|
+
/**
|
|
759
|
+
* @public Checks whether the given value is a number literal.
|
|
760
|
+
* @since 0.1.0
|
|
761
|
+
* @version 1
|
|
762
|
+
*/
|
|
763
|
+
number: (expr: Expr) => expr is number;
|
|
764
|
+
/**
|
|
765
|
+
* @public Checks whether the given value is a string literal.
|
|
766
|
+
* @since 0.1.0
|
|
767
|
+
* @version 1
|
|
768
|
+
*/
|
|
769
|
+
string: (expr: Expr) => expr is string;
|
|
770
|
+
/**
|
|
771
|
+
* @public Checks whether the given value is a literal expression.
|
|
772
|
+
* @since 0.1.0
|
|
773
|
+
* @version 1
|
|
774
|
+
*/
|
|
775
|
+
literal: (expr: Expr) => expr is ExprLiteral;
|
|
776
|
+
/**
|
|
777
|
+
* @public Checks whether the given value is a column expression.
|
|
778
|
+
* @since 0.1.0
|
|
779
|
+
* @version 1
|
|
780
|
+
*/
|
|
781
|
+
column: (expr: Expr) => expr is Column;
|
|
782
|
+
};
|
|
783
|
+
/**
|
|
784
|
+
* @public Represents the direction of ordering in an ORDER BY
|
|
785
|
+
* clause.
|
|
786
|
+
* @since 0.1.0
|
|
787
|
+
* @version 1
|
|
788
|
+
*/
|
|
789
|
+
type OrderDirection = 'ASC' | 'DESC';
|
|
790
|
+
/**
|
|
791
|
+
* @public Represents a create table statement.
|
|
792
|
+
* @since 0.1.0
|
|
793
|
+
* @version 1
|
|
794
|
+
*/
|
|
795
|
+
type CreateTableStatement = {
|
|
796
|
+
/**
|
|
797
|
+
* @public The name of the table to create.
|
|
798
|
+
* @since 0.1.0
|
|
799
|
+
* @version 1
|
|
800
|
+
*/
|
|
801
|
+
table: string;
|
|
802
|
+
/**
|
|
803
|
+
* @public The columns of the table.
|
|
804
|
+
* @since 0.1.0
|
|
805
|
+
* @version 1
|
|
806
|
+
*/
|
|
807
|
+
columns: Column[];
|
|
808
|
+
/**
|
|
809
|
+
* @public Whether to include an IF NOT EXISTS clause in the
|
|
810
|
+
* create table statement.
|
|
811
|
+
* @since 0.1.0
|
|
812
|
+
* @version 1
|
|
813
|
+
*/
|
|
814
|
+
ifNotExists?: true;
|
|
815
|
+
/**
|
|
816
|
+
* @public Whether to create the table as unlogged (won't produce
|
|
817
|
+
* WAL entries).
|
|
818
|
+
* @since 0.1.0
|
|
819
|
+
* @version 1
|
|
820
|
+
*/
|
|
821
|
+
unlogged?: true;
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* @public Represents an insert statement.
|
|
825
|
+
* @since 0.1.0
|
|
826
|
+
* @version 1
|
|
827
|
+
*/
|
|
828
|
+
type InsertStatement = {
|
|
829
|
+
/**
|
|
830
|
+
* @public The table to insert to.
|
|
831
|
+
* @since 0.1.0
|
|
832
|
+
* @version 1
|
|
833
|
+
*/
|
|
834
|
+
table: string;
|
|
835
|
+
/**
|
|
836
|
+
* @public The records to insert.
|
|
837
|
+
* @since 0.1.0
|
|
838
|
+
* @version 1
|
|
839
|
+
*/
|
|
840
|
+
records: Record<string, any>[];
|
|
841
|
+
/**
|
|
842
|
+
* @public The shape of the records being inserted.
|
|
843
|
+
* @since 0.1.0
|
|
844
|
+
* @version 1
|
|
845
|
+
*/
|
|
846
|
+
insertShape: Record<string, Column>;
|
|
847
|
+
/**
|
|
848
|
+
* @public The shape of the returned rows.
|
|
849
|
+
* @since 0.1.0
|
|
850
|
+
* @version 1
|
|
851
|
+
*/
|
|
852
|
+
returnShape: Record<string, Column>;
|
|
853
|
+
};
|
|
854
|
+
/**
|
|
855
|
+
* @public Represents a select statement.
|
|
856
|
+
* @since 0.1.0
|
|
857
|
+
* @version 1
|
|
858
|
+
*/
|
|
859
|
+
type SelectStatement = {
|
|
860
|
+
/**
|
|
861
|
+
* @public A map of table aliases to their actual name.
|
|
862
|
+
* @since 0.1.0
|
|
863
|
+
* @version 1
|
|
864
|
+
*/
|
|
865
|
+
registry: Record<string, string>;
|
|
866
|
+
/**
|
|
867
|
+
* @public The alias of the table from which to select.
|
|
868
|
+
* @since 0.1.0
|
|
869
|
+
* @version 1
|
|
870
|
+
*/
|
|
871
|
+
from: string;
|
|
872
|
+
/**
|
|
873
|
+
* @public The query's selection.
|
|
874
|
+
* @since 0.1.0
|
|
875
|
+
* @version 1
|
|
876
|
+
*/
|
|
877
|
+
select: Record<string, Column>;
|
|
878
|
+
/**
|
|
879
|
+
* @public The query's where clause.
|
|
880
|
+
* @since 0.1.0
|
|
881
|
+
* @version 1
|
|
882
|
+
*/
|
|
883
|
+
where?: Expr;
|
|
884
|
+
/**
|
|
885
|
+
* @public The query's order by clause.
|
|
886
|
+
* @since 0.1.0
|
|
887
|
+
* @version 1
|
|
888
|
+
*/
|
|
889
|
+
orderBy?: (readonly [Expr, OrderDirection])[];
|
|
890
|
+
/**
|
|
891
|
+
* @public The maximum number of rows to return.
|
|
892
|
+
* @since 0.1.0
|
|
893
|
+
* @version 1
|
|
894
|
+
*/
|
|
895
|
+
limit?: number;
|
|
896
|
+
/**
|
|
897
|
+
* @public The number of rows to skip before starting to return
|
|
898
|
+
* rows.
|
|
899
|
+
* @since 0.1.0
|
|
900
|
+
* @version 1
|
|
901
|
+
*/
|
|
902
|
+
offset?: number;
|
|
903
|
+
};
|
|
904
|
+
/**
|
|
905
|
+
* @private A codec for encoding data to the database's expected type,
|
|
906
|
+
* and decoding data from the database to the application's
|
|
907
|
+
* expected type.
|
|
908
|
+
* @since 0.1.0
|
|
909
|
+
* @version 1
|
|
910
|
+
*/
|
|
911
|
+
type Codec<Decoded = any, Encoded = any> = {
|
|
912
|
+
/**
|
|
913
|
+
* @private Encodes a value fomr the application's type into the
|
|
914
|
+
* database's expected type.
|
|
915
|
+
* @since 0.1.0
|
|
916
|
+
* @version 1
|
|
917
|
+
*/
|
|
918
|
+
encode: (value: Decoded) => Encoded;
|
|
919
|
+
/**
|
|
920
|
+
* @private Decodes a value from the database's type into the
|
|
921
|
+
* application's expected type.
|
|
922
|
+
* @since 0.1.0
|
|
923
|
+
* @version 1
|
|
924
|
+
*/
|
|
925
|
+
decode: (value: Encoded) => Decoded;
|
|
926
|
+
};
|
|
927
|
+
/**
|
|
928
|
+
* @public A registry of {@link Codec} for all supported primitive
|
|
929
|
+
* types.
|
|
930
|
+
* @since 0.1.0
|
|
931
|
+
* @version 1
|
|
932
|
+
*/
|
|
933
|
+
type CodecsRegistry = {
|
|
934
|
+
[K in Primitive]: Codec<any, any>;
|
|
935
|
+
};
|
|
936
|
+
/**
|
|
937
|
+
* @public A mapping of qx's primitive types to their native database
|
|
938
|
+
* types.
|
|
939
|
+
*/
|
|
940
|
+
type PrimitiveToNativeTypeFactory = {
|
|
941
|
+
[K in Primitive]: (col: Column) => string;
|
|
942
|
+
};
|
|
943
|
+
/**
|
|
944
|
+
* @public Represents the result of a statement rendered into DDL.
|
|
945
|
+
* @since 0.1.0
|
|
946
|
+
* @version 1
|
|
947
|
+
*/
|
|
948
|
+
type DDL = {
|
|
949
|
+
sql: string;
|
|
950
|
+
params: any[];
|
|
951
|
+
};
|
|
952
|
+
/**
|
|
953
|
+
* @public Query logger interface.
|
|
954
|
+
* @since 0.1.0
|
|
955
|
+
* @version 1
|
|
956
|
+
*/
|
|
957
|
+
interface ILogger {
|
|
958
|
+
/**
|
|
959
|
+
* @public Logs a query that has executed successfully.
|
|
960
|
+
* @since 0.1.0
|
|
961
|
+
* @version 1
|
|
962
|
+
*/
|
|
963
|
+
debug(sql: string, params: any[]): void;
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* @public The interface that all database adapters must implement.
|
|
967
|
+
* @since 0.1.0
|
|
968
|
+
* @version 1
|
|
969
|
+
*/
|
|
970
|
+
interface IDatabase {
|
|
971
|
+
/**
|
|
972
|
+
* @public Attaches a query logger to the database adapter.
|
|
973
|
+
* @since 0.1.0
|
|
974
|
+
* @version 1
|
|
975
|
+
*/
|
|
976
|
+
attachLogger(logger: ILogger): this;
|
|
977
|
+
/**
|
|
978
|
+
* @public Executes a create table statement.
|
|
979
|
+
* @since 0.1.0
|
|
980
|
+
* @version 1
|
|
981
|
+
*/
|
|
982
|
+
createTable(op: CreateTableStatement): Promise<void>;
|
|
983
|
+
/**
|
|
984
|
+
* @public Executes an insert statement, returning the newly
|
|
985
|
+
* inserted rows.
|
|
986
|
+
* @since 0.1.0
|
|
987
|
+
* @version 1
|
|
988
|
+
*/
|
|
989
|
+
insert(op: InsertStatement): Promise<any>;
|
|
990
|
+
/**
|
|
991
|
+
* @public Executes a select statement returning all matching rows.
|
|
992
|
+
* @since 0.1.0
|
|
993
|
+
* @version 1
|
|
994
|
+
*/
|
|
995
|
+
query(op: SelectStatement): Promise<any[]>;
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* @public Create statement builder.
|
|
999
|
+
* @since 0.1.0
|
|
1000
|
+
* @version 1
|
|
1001
|
+
*/
|
|
1002
|
+
declare const create: {
|
|
1003
|
+
/**
|
|
1004
|
+
* @public Prepares a create table statement.
|
|
1005
|
+
* @since 0.1.0
|
|
1006
|
+
* @version 1
|
|
1007
|
+
*/
|
|
1008
|
+
table: <T extends Table, S extends {
|
|
1009
|
+
ifNotExists?: true;
|
|
1010
|
+
unlogged?: true;
|
|
1011
|
+
}>(table: T, options?: S) => {
|
|
1012
|
+
/**
|
|
1013
|
+
* @public Executes the create table statement onto the given
|
|
1014
|
+
* database.
|
|
1015
|
+
* @since 0.1.0
|
|
1016
|
+
* @version 1
|
|
1017
|
+
*/
|
|
1018
|
+
onto: (db: IDatabase) => Promise<void>;
|
|
1019
|
+
};
|
|
1020
|
+
};
|
|
1021
|
+
/**
|
|
1022
|
+
* @public Insert statement builder.
|
|
1023
|
+
* @since 0.1.0
|
|
1024
|
+
* @version 1
|
|
1025
|
+
*/
|
|
1026
|
+
declare class InsertBuilder<T extends Table> {
|
|
1027
|
+
private readonly table;
|
|
1028
|
+
private rows;
|
|
1029
|
+
constructor(table: T, rows?: InferForInsert<T>[]);
|
|
1030
|
+
/**
|
|
1031
|
+
* @public Adds one or more rows to be inserted.
|
|
1032
|
+
* @since 0.1.0
|
|
1033
|
+
* @version 1
|
|
1034
|
+
*/
|
|
1035
|
+
insert(rows: InferForInsert<T> | InferForInsert<T>[]): this;
|
|
1036
|
+
/**
|
|
1037
|
+
* @public Executes the insert statement onto the given database.
|
|
1038
|
+
* @since 0.1.0
|
|
1039
|
+
* @version 1
|
|
1040
|
+
*/
|
|
1041
|
+
run(db: IDatabase): Promise<Expand<Infer<T>>[]>;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* @public Starts an insert statement for the given table.
|
|
1045
|
+
* @since 0.1.0
|
|
1046
|
+
* @version 1
|
|
1047
|
+
*/
|
|
1048
|
+
declare const into: <T extends Table>(table: T) => InsertBuilder<T>;
|
|
1049
|
+
/**
|
|
1050
|
+
* @private It's the object type for a query being built, that will
|
|
1051
|
+
* later been transformed into a {@link SelectStatement}.
|
|
1052
|
+
* @since 0.1.0
|
|
1053
|
+
* @version 1
|
|
1054
|
+
*/
|
|
1055
|
+
type Query<T extends Record<string, Aliased<string, Table>> = Record<string, Aliased<string, Table>>, S extends Record<string, Column> = Record<string, Column>> = {
|
|
1056
|
+
/**
|
|
1057
|
+
* @private A registry of all aliased tables in the query.
|
|
1058
|
+
* @since 0.1.0
|
|
1059
|
+
* @version 1
|
|
1060
|
+
*/
|
|
1061
|
+
registry: T;
|
|
1062
|
+
/**
|
|
1063
|
+
* @private The alias of the table from which to select.
|
|
1064
|
+
* @since 0.1.0
|
|
1065
|
+
* @version 1
|
|
1066
|
+
*/
|
|
1067
|
+
from: string;
|
|
1068
|
+
/**
|
|
1069
|
+
* @private The query's selection.
|
|
1070
|
+
* @since 0.1.0
|
|
1071
|
+
* @version 1
|
|
1072
|
+
*/
|
|
1073
|
+
select: S;
|
|
1074
|
+
/**
|
|
1075
|
+
* @private The query's where clause.
|
|
1076
|
+
* @since 0.1.0
|
|
1077
|
+
* @version 1
|
|
1078
|
+
*/
|
|
1079
|
+
where?: Expr;
|
|
1080
|
+
/**
|
|
1081
|
+
* @private The query's order by clause.
|
|
1082
|
+
* @since 0.1.0
|
|
1083
|
+
* @version 1
|
|
1084
|
+
*/
|
|
1085
|
+
orderBy?: (readonly [Expr, OrderDirection])[];
|
|
1086
|
+
/**
|
|
1087
|
+
* @private The query's limit.
|
|
1088
|
+
* @since 0.1.0
|
|
1089
|
+
* @version 1
|
|
1090
|
+
*/
|
|
1091
|
+
limit?: number;
|
|
1092
|
+
/**
|
|
1093
|
+
* @private The query's offset.
|
|
1094
|
+
* @since 0.1.0
|
|
1095
|
+
* @version 1
|
|
1096
|
+
*/
|
|
1097
|
+
offset?: number;
|
|
1098
|
+
};
|
|
1099
|
+
/**
|
|
1100
|
+
* @private Infers the selection output type of given query.
|
|
1101
|
+
* @since 0.1.0
|
|
1102
|
+
* @version 1
|
|
1103
|
+
*/
|
|
1104
|
+
type InferSelection<T extends Query> = {
|
|
1105
|
+
[K in keyof T['select'] & string]: T['select'][K]['outputType'];
|
|
1106
|
+
};
|
|
1107
|
+
/**
|
|
1108
|
+
* @public Select statement builder with a fluent API.
|
|
1109
|
+
*
|
|
1110
|
+
* We only need to keep track of two types in here:
|
|
1111
|
+
* - the registry of aliased tables, so they can be
|
|
1112
|
+
* referenced in expressions and selection; and
|
|
1113
|
+
* - the selected columns, so we can infer the returning
|
|
1114
|
+
* type of the query.
|
|
1115
|
+
* @since 0.1.0
|
|
1116
|
+
* @version 1
|
|
1117
|
+
*/
|
|
1118
|
+
declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S extends Record<string, Column>> {
|
|
1119
|
+
readonly query: Query<T, S>;
|
|
1120
|
+
constructor(query: Query<T, S>);
|
|
1121
|
+
/**
|
|
1122
|
+
* @public Executes the select statement against the given database,
|
|
1123
|
+
* returning all matching rows.
|
|
1124
|
+
* @since 0.1.0
|
|
1125
|
+
* @version 1
|
|
1126
|
+
*/
|
|
1127
|
+
all(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>>[]>;
|
|
1128
|
+
/**
|
|
1129
|
+
* @public Sets a limit on the number of rows to be returned.
|
|
1130
|
+
* @since 0.1.0
|
|
1131
|
+
* @version 1
|
|
1132
|
+
*/
|
|
1133
|
+
limit(n: number): QueryBuilder<T, S>;
|
|
1134
|
+
/**
|
|
1135
|
+
* @public Sets an offset for the rows to be returned.
|
|
1136
|
+
* @since 0.1.0
|
|
1137
|
+
* @version 1
|
|
1138
|
+
*/
|
|
1139
|
+
offset(n: number): QueryBuilder<T, S>;
|
|
1140
|
+
/**
|
|
1141
|
+
* @public Executes the select statement against the given database,
|
|
1142
|
+
* returning the first matching row.
|
|
1143
|
+
* @since 0.1.0
|
|
1144
|
+
* @version 1
|
|
1145
|
+
*/
|
|
1146
|
+
one(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>> | null>;
|
|
1147
|
+
/**
|
|
1148
|
+
* @public Defines the order by clause of the query.
|
|
1149
|
+
* @since 0.1.0
|
|
1150
|
+
* @version 1
|
|
1151
|
+
*/
|
|
1152
|
+
orderBy(fn: (registry: T) => (readonly [Expr, OrderDirection])[]): QueryBuilder<T, S>;
|
|
1153
|
+
/**
|
|
1154
|
+
* @public Defines the selection of the query.
|
|
1155
|
+
* @since 0.1.0
|
|
1156
|
+
* @version 1
|
|
1157
|
+
*/
|
|
1158
|
+
select<U extends Record<string, Column>>(fn: (r: T) => U): QueryBuilder<T, U>;
|
|
1159
|
+
/**
|
|
1160
|
+
* @public Defines the where clause of the query. If there's already
|
|
1161
|
+
* a WHERE clause in the query, the new clause will be
|
|
1162
|
+
* ANDed to the existing one.
|
|
1163
|
+
* @since 0.1.0
|
|
1164
|
+
* @version 1
|
|
1165
|
+
*/
|
|
1166
|
+
where<E extends Expr>(fn: (tables: T) => E): QueryBuilder<T, S>;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* @public Starts a select statement from the given table.
|
|
1170
|
+
* @since 0.1.0
|
|
1171
|
+
* @version 1
|
|
1172
|
+
*/
|
|
1173
|
+
declare const from: <S extends string, T extends Aliased<S, Table>>(table: T) => QueryBuilder<{ [K in T[typeof TABLE_ALIAS]]: T; }, T>;
|
|
1174
|
+
export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, create, defineTable as table, expr, from, into, is, types as t, };
|