effect-qb 0.17.0 → 0.19.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.
- package/README.md +4 -0
- package/dist/index.js +8065 -0
- package/dist/mysql.js +3053 -2505
- package/dist/postgres/metadata.js +1366 -1250
- package/dist/postgres.js +2020 -2719
- package/dist/sqlite.js +3226 -2732
- package/dist/standard.js +8019 -0
- package/package.json +10 -3
- package/src/casing.ts +71 -0
- package/src/index.ts +2 -0
- package/src/internal/casing.ts +89 -0
- package/src/internal/column-state.ts +11 -6
- package/src/internal/column.ts +44 -7
- package/src/internal/datatypes/define.ts +2 -1
- package/src/internal/datatypes/enrich.ts +23 -0
- package/src/internal/datatypes/lookup.ts +14 -7
- package/src/internal/derived-table.ts +4 -36
- package/src/{mysql/internal/sql-expression-renderer.ts → internal/dialect-renderers/mysql.ts} +548 -359
- package/src/{postgres/internal/sql-expression-renderer.ts → internal/dialect-renderers/postgres.ts} +654 -399
- package/src/{sqlite/internal/sql-expression-renderer.ts → internal/dialect-renderers/sqlite.ts} +501 -345
- package/src/internal/dialect.ts +35 -0
- package/src/internal/dsl-mutation-runtime.ts +12 -162
- package/src/internal/dsl-plan-runtime.ts +10 -138
- package/src/internal/dsl-query-runtime.ts +5 -79
- package/src/internal/dsl-transaction-ddl-runtime.ts +41 -65
- package/src/internal/executor.ts +10 -6
- package/src/internal/grouping-key.ts +87 -20
- package/src/internal/implication-runtime.ts +1 -1
- package/src/internal/predicate/runtime.ts +3 -0
- package/src/internal/query.d.ts +38 -11
- package/src/internal/query.ts +64 -25
- package/src/internal/renderer.ts +26 -14
- package/src/internal/runtime/normalize.ts +12 -5
- package/src/internal/scalar.ts +6 -1
- package/src/internal/schema-derivation.d.ts +12 -61
- package/src/internal/schema-derivation.ts +90 -38
- package/src/internal/schema-expression.ts +2 -2
- package/src/internal/sql-expression-renderer.ts +19 -0
- package/src/internal/standard-dsl.ts +6885 -0
- package/src/internal/table-options.ts +126 -66
- package/src/internal/table.d.ts +33 -32
- package/src/internal/table.ts +406 -155
- package/src/mysql/column-extension.ts +3 -0
- package/src/mysql/column.ts +10 -11
- package/src/mysql/datatypes/index.ts +3 -2
- package/src/mysql/executor.ts +7 -5
- package/src/mysql/internal/dialect.ts +9 -4
- package/src/mysql/internal/dsl.ts +219 -155
- package/src/mysql/internal/renderer.ts +6 -2
- package/src/mysql/json.ts +37 -0
- package/src/mysql/query-extension.ts +16 -0
- package/src/mysql/renderer.ts +31 -4
- package/src/mysql.ts +4 -12
- package/src/postgres/column-extension.ts +28 -0
- package/src/postgres/column.ts +5 -11
- package/src/postgres/datatypes/index.d.ts +2 -1
- package/src/postgres/datatypes/index.ts +3 -2
- package/src/postgres/executor.ts +7 -5
- package/src/postgres/function/core.ts +1 -3
- package/src/postgres/function/index.ts +1 -17
- package/src/postgres/internal/dialect.ts +9 -4
- package/src/postgres/internal/dsl.ts +208 -160
- package/src/postgres/internal/renderer.ts +6 -2
- package/src/postgres/internal/schema-ddl.ts +22 -10
- package/src/postgres/internal/schema-model.ts +238 -7
- package/src/postgres/json.ts +43 -7
- package/src/postgres/jsonb.ts +38 -0
- package/src/postgres/query-extension.ts +2 -0
- package/src/postgres/renderer.ts +31 -4
- package/src/postgres/schema-management.ts +17 -12
- package/src/postgres/schema.ts +98 -15
- package/src/postgres/table.ts +193 -524
- package/src/postgres/type.ts +8 -7
- package/src/postgres.ts +9 -11
- package/src/sqlite/column-extension.ts +3 -0
- package/src/sqlite/column.ts +10 -11
- package/src/sqlite/datatypes/index.ts +3 -2
- package/src/sqlite/executor.ts +7 -5
- package/src/sqlite/internal/dialect.ts +9 -4
- package/src/sqlite/internal/dsl.ts +208 -155
- package/src/sqlite/internal/renderer.ts +6 -2
- package/src/sqlite/json.ts +37 -0
- package/src/sqlite/query-extension.ts +2 -0
- package/src/sqlite/renderer.ts +31 -4
- package/src/sqlite.ts +4 -12
- package/src/standard/column.ts +163 -0
- package/src/standard/datatypes/index.ts +83 -0
- package/src/standard/datatypes/spec.ts +98 -0
- package/src/standard/dialect.ts +40 -0
- package/src/standard/function/aggregate.ts +2 -0
- package/src/standard/function/core.ts +2 -0
- package/src/standard/function/index.ts +18 -0
- package/src/standard/function/string.ts +2 -0
- package/src/standard/function/temporal.ts +78 -0
- package/src/standard/function/window.ts +2 -0
- package/src/standard/internal/renderer.ts +45 -0
- package/src/standard/query.ts +152 -0
- package/src/standard/renderer.ts +21 -0
- package/src/standard/table.ts +147 -0
- package/src/standard.ts +18 -0
- package/src/internal/aggregation-validation.ts +0 -57
- package/src/mysql/table.ts +0 -183
- package/src/sqlite/table.ts +0 -183
|
@@ -4,7 +4,10 @@ import {
|
|
|
4
4
|
type AnyColumnDefinition,
|
|
5
5
|
type IsNullable
|
|
6
6
|
} from "./column-state.js"
|
|
7
|
+
import type * as Casing from "./casing.js"
|
|
8
|
+
import * as Expression from "./scalar.js"
|
|
7
9
|
import type { Any as AnyExpression } from "./scalar.js"
|
|
10
|
+
import * as SchemaExpression from "./schema-expression.js"
|
|
8
11
|
import type { Any as AnySchemaExpression } from "./schema-expression.js"
|
|
9
12
|
import type { TableFieldMap } from "./schema-derivation.js"
|
|
10
13
|
|
|
@@ -39,6 +42,27 @@ const validateReferentialAction = (action: unknown): void => {
|
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
const requireColumnArray = (
|
|
46
|
+
value: unknown,
|
|
47
|
+
message: string
|
|
48
|
+
): readonly string[] => {
|
|
49
|
+
if (!Array.isArray(value) || value.some((column) => typeof column !== "string" || column.length === 0)) {
|
|
50
|
+
throw new Error(message)
|
|
51
|
+
}
|
|
52
|
+
return value
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const requireOptionalColumnArray = (
|
|
56
|
+
value: unknown,
|
|
57
|
+
message: string
|
|
58
|
+
): readonly string[] =>
|
|
59
|
+
value === undefined ? [] : requireColumnArray(value, message)
|
|
60
|
+
|
|
61
|
+
const isDdlExpressionLike = (value: unknown): value is DdlExpressionLike =>
|
|
62
|
+
typeof value === "object" &&
|
|
63
|
+
value !== null &&
|
|
64
|
+
(Expression.TypeId in value || SchemaExpression.TypeId in value)
|
|
65
|
+
|
|
42
66
|
export type IndexKeySpec =
|
|
43
67
|
| {
|
|
44
68
|
readonly kind: "column"
|
|
@@ -91,6 +115,7 @@ export type TableOptionSpec =
|
|
|
91
115
|
readonly references: () => {
|
|
92
116
|
readonly tableName: string
|
|
93
117
|
readonly schemaName?: string
|
|
118
|
+
readonly casing?: Casing.Options
|
|
94
119
|
readonly columns: ColumnList
|
|
95
120
|
readonly knownColumns?: readonly string[]
|
|
96
121
|
}
|
|
@@ -134,6 +159,83 @@ type TupleFromColumns<Columns> = Columns extends readonly [infer Head extends st
|
|
|
134
159
|
export type NonEmptyColumnInput<Columns extends string | readonly string[]> =
|
|
135
160
|
TupleFromColumns<Columns> extends never ? never : Columns
|
|
136
161
|
|
|
162
|
+
export type NonEmptyStringInput<Value extends string> =
|
|
163
|
+
string extends Value ? Value : Value extends "" ? never : Value
|
|
164
|
+
|
|
165
|
+
export type LiteralStringInput<Value extends string> =
|
|
166
|
+
string extends Value ? never : NonEmptyStringInput<Value>
|
|
167
|
+
|
|
168
|
+
type LowerAlpha =
|
|
169
|
+
| "a"
|
|
170
|
+
| "b"
|
|
171
|
+
| "c"
|
|
172
|
+
| "d"
|
|
173
|
+
| "e"
|
|
174
|
+
| "f"
|
|
175
|
+
| "g"
|
|
176
|
+
| "h"
|
|
177
|
+
| "i"
|
|
178
|
+
| "j"
|
|
179
|
+
| "k"
|
|
180
|
+
| "l"
|
|
181
|
+
| "m"
|
|
182
|
+
| "n"
|
|
183
|
+
| "o"
|
|
184
|
+
| "p"
|
|
185
|
+
| "q"
|
|
186
|
+
| "r"
|
|
187
|
+
| "s"
|
|
188
|
+
| "t"
|
|
189
|
+
| "u"
|
|
190
|
+
| "v"
|
|
191
|
+
| "w"
|
|
192
|
+
| "x"
|
|
193
|
+
| "y"
|
|
194
|
+
| "z"
|
|
195
|
+
type UpperAlpha = Uppercase<LowerAlpha>
|
|
196
|
+
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
|
|
197
|
+
type IdentifierStart = LowerAlpha | UpperAlpha | "_"
|
|
198
|
+
type IdentifierRest = IdentifierStart | Digit
|
|
199
|
+
|
|
200
|
+
type SafeIdentifierRest<Value extends string> =
|
|
201
|
+
Value extends ""
|
|
202
|
+
? Value
|
|
203
|
+
: Value extends `${infer Head}${infer Tail}`
|
|
204
|
+
? Head extends IdentifierRest ? SafeIdentifierRest<Tail> : never
|
|
205
|
+
: never
|
|
206
|
+
|
|
207
|
+
type SafeSqlIdentifier<Value extends string> =
|
|
208
|
+
Value extends `${infer Head}${infer Tail}`
|
|
209
|
+
? Head extends IdentifierStart
|
|
210
|
+
? SafeIdentifierRest<Tail> extends never ? never : Value
|
|
211
|
+
: never
|
|
212
|
+
: never
|
|
213
|
+
|
|
214
|
+
export type SafeSqlIdentifierPathInput<Value extends string> =
|
|
215
|
+
string extends Value
|
|
216
|
+
? never
|
|
217
|
+
: Value extends `${infer Head}.${infer Tail}`
|
|
218
|
+
? SafeSqlIdentifier<Head> extends never
|
|
219
|
+
? never
|
|
220
|
+
: SafeSqlIdentifierPathInput<Tail> extends never ? never : Value
|
|
221
|
+
: SafeSqlIdentifier<Value> extends never ? never : Value
|
|
222
|
+
|
|
223
|
+
export type SafeSqlIdentifierInput<Value extends string> =
|
|
224
|
+
string extends Value
|
|
225
|
+
? never
|
|
226
|
+
: SafeSqlIdentifier<Value> extends never ? never : Value
|
|
227
|
+
|
|
228
|
+
type LiteralStringTupleInput<Values extends readonly string[]> = {
|
|
229
|
+
readonly [K in keyof Values]: Values[K] extends string ? LiteralStringInput<Values[K]> : never
|
|
230
|
+
} & Values
|
|
231
|
+
|
|
232
|
+
export type CollationIdentifierInput<Value extends string | readonly [string, ...string[]]> =
|
|
233
|
+
Value extends string
|
|
234
|
+
? LiteralStringInput<Value>
|
|
235
|
+
: Value extends readonly [string, ...string[]]
|
|
236
|
+
? LiteralStringTupleInput<Value>
|
|
237
|
+
: never
|
|
238
|
+
|
|
137
239
|
export type MatchingColumnArityInput<
|
|
138
240
|
Left extends string | readonly string[],
|
|
139
241
|
Right extends string | readonly string[]
|
|
@@ -206,11 +308,10 @@ type InlinePrimaryKeyKeys<Fields extends TableFieldMap> = Extract<{
|
|
|
206
308
|
|
|
207
309
|
/** Normalizes a string or tuple input into a non-empty column list. */
|
|
208
310
|
export const normalizeColumnList = (columns: string | readonly string[]): ColumnList => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
throw new Error("Table options require at least one column")
|
|
311
|
+
if (typeof columns === "string") {
|
|
312
|
+
return [columns]
|
|
212
313
|
}
|
|
213
|
-
return
|
|
314
|
+
return [columns[0] as string, ...columns.slice(1)]
|
|
214
315
|
}
|
|
215
316
|
|
|
216
317
|
/** Converts inline column flags into normalized table option records. */
|
|
@@ -236,8 +337,6 @@ export const collectInlineOptions = <Fields extends TableFieldMap>(
|
|
|
236
337
|
})
|
|
237
338
|
}
|
|
238
339
|
if (column.metadata.references) {
|
|
239
|
-
validateReferentialAction(column.metadata.references.onUpdate)
|
|
240
|
-
validateReferentialAction(column.metadata.references.onDelete)
|
|
241
340
|
const local = [columnName] as ColumnList
|
|
242
341
|
options.push({
|
|
243
342
|
kind: "foreignKey",
|
|
@@ -248,6 +347,7 @@ export const collectInlineOptions = <Fields extends TableFieldMap>(
|
|
|
248
347
|
return {
|
|
249
348
|
tableName: bound.baseTableName,
|
|
250
349
|
schemaName: bound.schemaName,
|
|
350
|
+
casing: bound.casing,
|
|
251
351
|
columns: [bound.columnName]
|
|
252
352
|
}
|
|
253
353
|
},
|
|
@@ -287,24 +387,16 @@ export const resolvePrimaryKeyColumns = <Fields extends TableFieldMap>(
|
|
|
287
387
|
const inline = Object.entries(fields)
|
|
288
388
|
.filter(([, column]) => column.metadata.primaryKey)
|
|
289
389
|
.map(([key]) => key) as (keyof Fields & string)[]
|
|
290
|
-
const explicit = declaredOptions
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
390
|
+
const explicit = declaredOptions.flatMap((option) => {
|
|
391
|
+
if (typeof option !== "object" || option === null || !("kind" in option) || option.kind !== "primaryKey") {
|
|
392
|
+
return []
|
|
393
|
+
}
|
|
394
|
+
return Array.isArray(option.columns) ? [option.columns] : []
|
|
395
|
+
})
|
|
296
396
|
if (explicit.length === 0) {
|
|
297
397
|
return inline
|
|
298
398
|
}
|
|
299
399
|
const tablePrimaryKey = [...explicit[0]!] as (keyof Fields & string)[]
|
|
300
|
-
if (inline.length > 0) {
|
|
301
|
-
const same =
|
|
302
|
-
inline.length === tablePrimaryKey.length &&
|
|
303
|
-
inline.every((column) => tablePrimaryKey.includes(column))
|
|
304
|
-
if (!same) {
|
|
305
|
-
throw new Error("Inline primary keys conflict with table-level primary key declaration")
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
400
|
return tablePrimaryKey
|
|
309
401
|
}
|
|
310
402
|
|
|
@@ -312,67 +404,35 @@ export const resolvePrimaryKeyColumns = <Fields extends TableFieldMap>(
|
|
|
312
404
|
export const validateOptions = <Fields extends TableFieldMap>(
|
|
313
405
|
tableName: string,
|
|
314
406
|
fields: Fields,
|
|
315
|
-
options:
|
|
407
|
+
options: unknown
|
|
316
408
|
): void => {
|
|
317
|
-
const
|
|
318
|
-
|
|
409
|
+
const tableOptions = (Array.isArray(options)
|
|
410
|
+
? options
|
|
411
|
+
: [options]) as readonly TableOptionSpec[]
|
|
412
|
+
for (const option of tableOptions) {
|
|
413
|
+
if (typeof option !== "object" || option === null || !("kind" in option)) {
|
|
414
|
+
continue
|
|
415
|
+
}
|
|
319
416
|
switch (option.kind) {
|
|
320
417
|
case "index":
|
|
321
418
|
case "primaryKey":
|
|
322
419
|
case "unique":
|
|
323
420
|
case "foreignKey": {
|
|
324
|
-
const columns = option.kind === "index"
|
|
325
|
-
? option.columns ?? []
|
|
326
|
-
: option.columns
|
|
327
|
-
if (columns.length === 0 && option.kind !== "index") {
|
|
328
|
-
throw new Error(`Option '${option.kind}' on table '${tableName}' requires at least one column`)
|
|
329
|
-
}
|
|
330
|
-
for (const column of columns) {
|
|
331
|
-
if (!knownColumns.has(column)) {
|
|
332
|
-
throw new Error(`Unknown column '${column}' on table '${tableName}'`)
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
if (option.kind === "foreignKey") {
|
|
336
|
-
validateReferentialAction(option.onUpdate)
|
|
337
|
-
validateReferentialAction(option.onDelete)
|
|
338
|
-
const reference = option.references()
|
|
339
|
-
if (reference.columns.length !== columns.length) {
|
|
340
|
-
throw new Error(`Foreign key on table '${tableName}' must reference the same number of columns`)
|
|
341
|
-
}
|
|
342
|
-
if (reference.knownColumns) {
|
|
343
|
-
const referenced = new Set(reference.knownColumns)
|
|
344
|
-
for (const column of reference.columns) {
|
|
345
|
-
if (!referenced.has(column)) {
|
|
346
|
-
throw new Error(`Unknown referenced column '${column}' on table '${reference.tableName}'`)
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
421
|
if (option.kind === "index") {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
for (const key of option.keys ?? []) {
|
|
358
|
-
if (key.kind === "column" && !knownColumns.has(key.column)) {
|
|
359
|
-
throw new Error(`Unknown index key column '${key.column}' on table '${tableName}'`)
|
|
422
|
+
const keys = Array.isArray(option.keys) ? option.keys : []
|
|
423
|
+
for (const key of keys) {
|
|
424
|
+
if (typeof key !== "object" || key === null || !("kind" in key)) {
|
|
425
|
+
continue
|
|
360
426
|
}
|
|
361
427
|
}
|
|
362
|
-
if (columns.length === 0 && (option.keys === undefined || option.keys.length === 0)) {
|
|
363
|
-
throw new Error(`Index on table '${tableName}' requires at least one column or key`)
|
|
364
|
-
}
|
|
365
428
|
}
|
|
366
429
|
break
|
|
367
430
|
}
|
|
368
431
|
case "check": {
|
|
369
432
|
break
|
|
370
433
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
for (const column of resolvePrimaryKeyColumns(fields, options)) {
|
|
374
|
-
if (fields[column]!.metadata.nullable) {
|
|
375
|
-
throw new Error(`Primary key column '${String(column)}' cannot be nullable`)
|
|
434
|
+
default:
|
|
435
|
+
break
|
|
376
436
|
}
|
|
377
437
|
}
|
|
378
438
|
}
|
package/src/internal/table.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import * as Schema from "effect/Schema";
|
|
|
3
3
|
import * as Plan from "./row-set.js";
|
|
4
4
|
import type { TrueFormula } from "./predicate/formula.js";
|
|
5
5
|
import type { BoundColumnFrom } from "./column-state.js";
|
|
6
|
-
import { type DdlExpressionLike, type NormalizeColumns, type TableOptionSpec, type ValidateKnownColumns, type ValidatePrimaryKeyColumns } from "./table-options.js";
|
|
6
|
+
import { type DdlExpressionLike, type NonEmptyStringInput, type NormalizeColumns, type TableOptionSpec, type ValidateKnownColumns, type ValidatePrimaryKeyColumns } from "./table-options.js";
|
|
7
7
|
import { type InsertRow, type SelectRow, type TableFieldMap, type UpdateRow } from "./schema-derivation.js";
|
|
8
8
|
/** Symbol used to attach table-definition metadata. */
|
|
9
9
|
export declare const TypeId: unique symbol;
|
|
@@ -59,13 +59,9 @@ interface TableState<Name extends string, Fields extends TableFieldMap, PrimaryK
|
|
|
59
59
|
readonly primaryKey: readonly PrimaryKeyColumns[];
|
|
60
60
|
readonly kind: Kind;
|
|
61
61
|
}
|
|
62
|
-
/** Namespace-scoped table builder. */
|
|
63
|
-
export interface TableSchemaNamespace<SchemaName extends string> {
|
|
64
|
-
readonly schemaName: SchemaName;
|
|
65
|
-
readonly table: <Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>>(name: Name, fields: Fields, ...options: DeclaredTableOptions) => TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>;
|
|
66
|
-
}
|
|
67
62
|
export type DeclaredTableOptions = readonly TableOptionBuilderLike[];
|
|
68
63
|
export type { DdlExpressionLike, IndexKeySpec, NormalizeColumns, ReferentialAction } from "./table-options.js";
|
|
64
|
+
export type NonEmptySchemaNameInput<Value extends string | undefined> = Value extends string ? NonEmptyStringInput<Value> : Value;
|
|
69
65
|
export type TableDefinition<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>, Kind extends TableKind = "schema", SchemaName extends string | undefined = DefaultSchemaName> = Pipeable & {
|
|
70
66
|
readonly name: Name;
|
|
71
67
|
readonly columns: BoundColumns<Name, Fields>;
|
|
@@ -92,7 +88,10 @@ export type TableClassStatic<Name extends string, Fields extends TableFieldMap,
|
|
|
92
88
|
readonly tableName: Name;
|
|
93
89
|
} & BoundColumns<Name, Fields> & Plan.RowSet<BoundColumns<Name, Fields>, never, Record<Name, Plan.Source<Name, "required", TrueFormula>>, TableDialect<Fields>>;
|
|
94
90
|
/** Minimal structural table-like contract used across helper APIs. */
|
|
95
|
-
export type AnyTable
|
|
91
|
+
export type AnyTable<Dialect extends string = string> = {
|
|
92
|
+
readonly [TypeId]: TableState<string, TableFieldMap, string, TableKind, string | undefined>;
|
|
93
|
+
readonly [OptionsSymbol]: readonly TableOptionSpec[];
|
|
94
|
+
} & Plan.RowSet<any, any, Record<string, Plan.AnySource>, Dialect>;
|
|
96
95
|
/** Public table-option builder type used by `Table.index`, `Table.primaryKey`, and friends. */
|
|
97
96
|
export type TableOption<Spec extends TableOptionSpec = TableOptionSpec> = {
|
|
98
97
|
<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string>(table: OptionInputTable<TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", any>, Spec>): ApplyOption<TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", any>, Spec>;
|
|
@@ -101,11 +100,7 @@ export type TableOption<Spec extends TableOptionSpec = TableOptionSpec> = {
|
|
|
101
100
|
export declare const option: <Spec extends TableOptionSpec>(spec: Spec) => TableOption<Spec>;
|
|
102
101
|
export declare const optionFromTable: <Spec extends TableOptionSpec>(spec: Spec, resolve: (table: TableDefinition<any, any, any, "schema", any>) => Spec) => TableOption<Spec>;
|
|
103
102
|
/** Creates a table definition from a name and field map. */
|
|
104
|
-
export declare function make<Name extends string, Fields extends TableFieldMap, SchemaName extends string | undefined = DefaultSchemaName>(name: Name
|
|
105
|
-
/**
|
|
106
|
-
* Creates a namespace-scoped builder for a concrete SQL schema/database.
|
|
107
|
-
*/
|
|
108
|
-
export declare const schema: <SchemaName extends string>(schemaName: SchemaName) => TableSchemaNamespace<SchemaName>;
|
|
103
|
+
export declare function make<Name extends string, Fields extends TableFieldMap, const SchemaName extends string | undefined = DefaultSchemaName>(name: NonEmptyStringInput<Name>, fields: Fields, schemaName?: NonEmptySchemaNameInput<SchemaName>): TableDefinition<Name, Fields, InlinePrimaryKeyKeys<Fields>, "schema", SchemaName>;
|
|
109
104
|
/**
|
|
110
105
|
* Creates an aliased source from an existing table definition.
|
|
111
106
|
*
|
|
@@ -114,13 +109,21 @@ export declare const schema: <SchemaName extends string>(schemaName: SchemaName)
|
|
|
114
109
|
* downstream SQL rendering work.
|
|
115
110
|
*/
|
|
116
111
|
export declare const alias: <Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string, SchemaName extends string, AliasName extends string>(table: TableClassStatic<Name, Fields, PrimaryKeyColumns, SchemaName> | TableDefinition<Name, Fields, PrimaryKeyColumns, any, SchemaName>, aliasName: AliasName) => TableDefinition<AliasName, Fields, PrimaryKeyColumns, "alias", SchemaName>;
|
|
112
|
+
/** Returns the lazily derived select schema for a table. */
|
|
113
|
+
export declare function selectSchema<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string>(table: TableDefinition<Name, Fields, PrimaryKeyColumns, any, any> | TableClassStatic<Name, Fields, PrimaryKeyColumns, any>): Schema.Schema<SelectRow<Name, Fields>>;
|
|
114
|
+
/** Returns the lazily derived insert schema for a table. */
|
|
115
|
+
export declare function insertSchema<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string>(table: TableDefinition<Name, Fields, PrimaryKeyColumns, any, any> | TableClassStatic<Name, Fields, PrimaryKeyColumns, any>): Schema.Schema<InsertRow<Name, Fields>>;
|
|
116
|
+
/** Returns the lazily derived update schema for a table. */
|
|
117
|
+
export declare function updateSchema<Name extends string, Fields extends TableFieldMap, PrimaryKeyColumns extends keyof Fields & string>(table: TableDefinition<Name, Fields, PrimaryKeyColumns, any, any> | TableClassStatic<Name, Fields, PrimaryKeyColumns, any>): Schema.Schema<UpdateRow<Name, Fields, PrimaryKeyColumns>>;
|
|
117
118
|
/**
|
|
118
119
|
* Class-based table constructor mirroring `Schema.Class`.
|
|
119
120
|
*
|
|
120
121
|
* The returned base class can be extended and configured with
|
|
121
122
|
* `static readonly [Table.options]`.
|
|
122
123
|
*/
|
|
123
|
-
export declare function Class<Self = never
|
|
124
|
+
export declare function Class<Self = never>(name: "", schemaName?: string | undefined): never;
|
|
125
|
+
export declare function Class<Self = never>(name: string, schemaName: ""): never;
|
|
126
|
+
export declare function Class<Self = never, const SchemaName extends string | undefined = DefaultSchemaName, const Name extends string = string>(name: NonEmptyStringInput<Name>, schemaName?: NonEmptySchemaNameInput<SchemaName>): <Fields extends TableFieldMap>(fields: Fields) => [Self] extends [never] ? "Missing `Self` generic - use `class Self extends Table.Class<Self>(...) {}`" : TableClassStatic<Name, Fields, Extract<{ [K in keyof Fields]: Fields[K]["metadata"]["primaryKey"] extends true ? K : never; }[keyof Fields], string>, SchemaName>;
|
|
124
127
|
/** Declares a table-level primary key. */
|
|
125
128
|
export declare const primaryKey: <const Columns extends string | readonly string[]>(columns: Columns) => TableOption<{
|
|
126
129
|
readonly kind: "primaryKey";
|
|
@@ -148,26 +151,24 @@ export declare const foreignKey: <const LocalColumns extends string | readonly s
|
|
|
148
151
|
};
|
|
149
152
|
}>;
|
|
150
153
|
/** Declares a check constraint expression. */
|
|
151
|
-
export declare const check: <Name extends string>(name: Name
|
|
154
|
+
export declare const check: <const Name extends string>(name: NonEmptyStringInput<Name>, predicate: DdlExpressionLike) => TableOption<{
|
|
152
155
|
readonly kind: "check";
|
|
153
156
|
readonly name: Name;
|
|
154
157
|
readonly predicate: DdlExpressionLike;
|
|
155
158
|
}>;
|
|
156
|
-
/** Extracts the row type
|
|
157
|
-
export type SelectOf<Table extends {
|
|
158
|
-
readonly
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
readonly
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
readonly
|
|
171
|
-
|
|
172
|
-
};
|
|
173
|
-
}> = Schema.Schema.Type<Table["schemas"]["update"]>;
|
|
159
|
+
/** Extracts the row type produced by `selectSchema(table)`. */
|
|
160
|
+
export type SelectOf<Table extends AnyTable> = Table[typeof TypeId] extends {
|
|
161
|
+
readonly name: infer Name extends string;
|
|
162
|
+
readonly fields: infer Fields extends TableFieldMap;
|
|
163
|
+
} ? SelectRow<Name, Fields> : never;
|
|
164
|
+
/** Extracts the payload type produced by `insertSchema(table)`. */
|
|
165
|
+
export type InsertOf<Table extends AnyTable> = Table[typeof TypeId] extends {
|
|
166
|
+
readonly name: infer Name extends string;
|
|
167
|
+
readonly fields: infer Fields extends TableFieldMap;
|
|
168
|
+
} ? InsertRow<Name, Fields> : never;
|
|
169
|
+
/** Extracts the payload type produced by `updateSchema(table)`. */
|
|
170
|
+
export type UpdateOf<Table extends AnyTable> = Table[typeof TypeId] extends {
|
|
171
|
+
readonly name: infer Name extends string;
|
|
172
|
+
readonly fields: infer Fields extends TableFieldMap;
|
|
173
|
+
readonly primaryKey: readonly (infer PrimaryKeyColumns)[];
|
|
174
|
+
} ? UpdateRow<Name, Fields, Extract<PrimaryKeyColumns, keyof Fields & string>> : never;
|