uql-orm 0.61.0 → 0.62.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/dist/browser/uql-browser.min.js.map +1 -1
- package/dist/dialect/abstractSqlDialect.d.ts +12 -7
- package/dist/dialect/abstractSqlDialect.js +51 -50
- package/dist/dialect/mysqlLikeSqlDialect.js +1 -1
- package/dist/dialect/pgLikeSqlDialect.d.ts +1 -0
- package/dist/dialect/pgLikeSqlDialect.js +8 -7
- package/dist/dialect/queryContext.d.ts +5 -6
- package/dist/dialect/queryContext.js +8 -8
- package/dist/entity/decorator/entity.d.ts +3 -3
- package/dist/entity/decorator/entity.js +2 -2
- package/dist/entity/decorator/members.d.ts +3 -2
- package/dist/entity/decorator/members.js +1 -0
- package/dist/entity/metadata/definition.js +12 -6
- package/dist/migrate/builder/migrationBuilder.js +3 -5
- package/dist/migrate/builder/tableBuilder.js +2 -4
- package/dist/migrate/builder/types.d.ts +11 -3
- package/dist/migrate/codegen/indexDecoratorSource.js +3 -1
- package/dist/migrate/generator/definitionToNode.d.ts +8 -5
- package/dist/migrate/generator/definitionToNode.js +13 -4
- package/dist/migrate/generator/mongoSchemaGenerator.d.ts +2 -0
- package/dist/migrate/generator/mongoSchemaGenerator.js +6 -1
- package/dist/migrate/schemaGenerator.d.ts +5 -3
- package/dist/migrate/schemaGenerator.js +9 -2
- package/dist/mongo/mongoDialect.js +1 -1
- package/dist/mssql/mssqlDialect.js +3 -5
- package/dist/schema/schemaASTBuilder.d.ts +6 -1
- package/dist/schema/schemaASTBuilder.js +17 -16
- package/dist/type/dialect.d.ts +15 -6
- package/dist/type/entity.d.ts +94 -34
- package/dist/type/migration.d.ts +9 -2
- package/dist/type/query.d.ts +3 -3
- package/dist/type/queryLock.d.ts +7 -7
- package/dist/type/queryLock.js +8 -6
- package/dist/type/queryRaw.d.ts +22 -30
- package/dist/type/queryRaw.js +3 -9
- package/dist/util/ddlExpression.util.d.ts +8 -13
- package/dist/util/ddlExpression.util.js +17 -19
- package/dist/util/dialect.util.d.ts +1 -1
- package/dist/util/dialect.util.js +3 -4
- package/dist/util/field.util.d.ts +1 -1
- package/dist/util/raw.d.ts +19 -17
- package/dist/util/raw.js +43 -17
- package/package.json +1 -1
package/dist/type/dialect.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { UpdatePayload } from './entity.js';
|
|
1
|
+
import type { EntityMeta, UpdatePayload } from './entity.js';
|
|
2
2
|
import type { Query, QueryConflictPaths, QueryFilter, QueryOptions, QuerySearch } from './query.js';
|
|
3
3
|
import type { QueryAggMap, QueryAggregate, QueryGroupMap } from './queryAggregate.js';
|
|
4
4
|
import type { Type } from './utility.js';
|
|
@@ -46,7 +46,13 @@ export interface QueryContext {
|
|
|
46
46
|
createFragment(): QueryContext;
|
|
47
47
|
readonly sql: string;
|
|
48
48
|
readonly values: unknown[];
|
|
49
|
+
/** Whether a value is written as its literal rather than bound: DDL has no placeholder to bind into. */
|
|
50
|
+
readonly inlineValues: boolean;
|
|
49
51
|
}
|
|
52
|
+
export type QueryContextOptions = {
|
|
53
|
+
/** See {@link QueryContext.inlineValues}. */
|
|
54
|
+
readonly inlineValues?: boolean;
|
|
55
|
+
};
|
|
50
56
|
/**
|
|
51
57
|
* Capabilities of the database driver (transport layer).
|
|
52
58
|
*/
|
|
@@ -219,11 +225,10 @@ export interface QueryDialect {
|
|
|
219
225
|
*/
|
|
220
226
|
escape(val: unknown): string;
|
|
221
227
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @param value the value to add
|
|
228
|
+
* The SQL `value` takes in `ctx`: a raw expression rendered in place, the literal where `ctx` inlines
|
|
229
|
+
* values, and otherwise a placeholder for the value bound.
|
|
225
230
|
*/
|
|
226
|
-
addValue(
|
|
231
|
+
addValue(ctx: QueryContext, value: unknown): string;
|
|
227
232
|
/**
|
|
228
233
|
* normalizes a value according to the dialect.
|
|
229
234
|
* @param value the value to normalize
|
|
@@ -232,7 +237,11 @@ export interface QueryDialect {
|
|
|
232
237
|
/**
|
|
233
238
|
* create a new query context.
|
|
234
239
|
*/
|
|
235
|
-
createContext(): QueryContext;
|
|
240
|
+
createContext(options?: QueryContextOptions): QueryContext;
|
|
241
|
+
/**
|
|
242
|
+
* The column a field of `meta` is stored in, named the way this dialect names columns.
|
|
243
|
+
*/
|
|
244
|
+
columnOf<E>(meta: EntityMeta<E>, key: string): string;
|
|
236
245
|
}
|
|
237
246
|
/**
|
|
238
247
|
* Supported SQL dialect identifiers.
|
package/dist/type/entity.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { EnumValues, ForeignKeyAction, IndexType } from '../schema/types.js';
|
|
2
2
|
import type { FilterOptions } from './query.js';
|
|
3
3
|
import type { QueryRaw } from './queryRaw.js';
|
|
4
|
+
import type { QueryWhere } from './queryWhere.js';
|
|
4
5
|
import type { Except, IsMany, Json, Scalar, Type, Unpacked } from './utility.js';
|
|
5
6
|
import type { VectorDistance, VectorIndexOptions, VectorIndexType } from './vector.js';
|
|
6
7
|
/**
|
|
@@ -299,7 +300,9 @@ export type TypeFor<V, T = NonNullable<V>> = IsJsonColumn<T> extends true ? Json
|
|
|
299
300
|
* live there behind an `@internal` tag and a "do not set this" note, which is a comment standing in
|
|
300
301
|
* for a type boundary.
|
|
301
302
|
*/
|
|
302
|
-
export type FieldMeta<V = TsTypeOf<FieldType>> = FieldOptions<V> & {
|
|
303
|
+
export type FieldMeta<V = TsTypeOf<FieldType>> = Except<FieldOptions<V>, 'computed'> & {
|
|
304
|
+
/** {@link FieldOptions.computed}, a callback resolved to the SQL it returns. */
|
|
305
|
+
readonly computed?: QueryRaw;
|
|
303
306
|
/**
|
|
304
307
|
* Set by `defineField` when the field gave `references` but no `type`, so schema generation resolves
|
|
305
308
|
* the column from the referenced primary key rather than from whatever ended up in `type`. That is
|
|
@@ -317,7 +320,7 @@ export type FieldMeta<V = TsTypeOf<FieldType>> = FieldOptions<V> & {
|
|
|
317
320
|
* and what a default is has to be that value, checked the same way the declared `type` is. `Scalar` by
|
|
318
321
|
* default, for the places that handle a field without knowing which one it is.
|
|
319
322
|
*/
|
|
320
|
-
export type FieldOptions<V = TsTypeOf<FieldType
|
|
323
|
+
export type FieldOptions<V = TsTypeOf<FieldType>, E = unknown> = {
|
|
321
324
|
readonly name?: string;
|
|
322
325
|
readonly isId?: true;
|
|
323
326
|
readonly type?: FieldType;
|
|
@@ -365,9 +368,9 @@ export type FieldOptions<V = TsTypeOf<FieldType>> = {
|
|
|
365
368
|
* expression will do. With `stored`, it becomes a real column - `GENERATED ALWAYS AS (...) STORED` -
|
|
366
369
|
* which the engine keeps up to date, so it can be indexed and read like any other.
|
|
367
370
|
*
|
|
368
|
-
* @example `@Field({ type: String, computed: raw
|
|
371
|
+
* @example `@Field({ type: String, computed: (user) => raw`${user.first} || ' ' || ${user.last}`, stored: true })`
|
|
369
372
|
*/
|
|
370
|
-
readonly computed?:
|
|
373
|
+
readonly computed?: EntitySql<E>;
|
|
371
374
|
/**
|
|
372
375
|
* Whether {@link FieldOptions.computed} is a column the database keeps, rather than an expression
|
|
373
376
|
* spliced into each statement. The dial to flip after profiling: `$select`, `$where` and `$sort`
|
|
@@ -466,9 +469,9 @@ export type TsTypeOf<T> = T extends StringConstructor ? string : T extends Numbe
|
|
|
466
469
|
* `@Field({ references: () => Company })` would silently downgrade a `uuid` key to TEXT on every
|
|
467
470
|
* column pointing at it.
|
|
468
471
|
*/
|
|
469
|
-
export type FieldOptionsFor<V> = (FieldOptions<NonNullable<V
|
|
472
|
+
export type FieldOptionsFor<V, E = unknown> = (FieldOptions<NonNullable<V>, E> & {
|
|
470
473
|
readonly type: TypeFor<V>;
|
|
471
|
-
}) | (FieldOptions<NonNullable<V
|
|
474
|
+
}) | (FieldOptions<NonNullable<V>, E> & {
|
|
472
475
|
readonly references: EntityGetter;
|
|
473
476
|
readonly type?: TypeFor<V>;
|
|
474
477
|
});
|
|
@@ -592,6 +595,48 @@ type RelationOptionsThroughOwner<E, O> = Pick<RelationOptions<E, O>, 'entity' |
|
|
|
592
595
|
export type KeyMap<E> = {
|
|
593
596
|
readonly [K in keyof E]-?: K;
|
|
594
597
|
};
|
|
598
|
+
declare const COLUMN_KEY: unique symbol;
|
|
599
|
+
/**
|
|
600
|
+
* A field of an entity as SQL, read off a {@link RefMap}: interpolated into `raw`, it renders as that
|
|
601
|
+
* field's column. A `QueryRaw` like any other fragment, branded with the field `K` it names.
|
|
602
|
+
*/
|
|
603
|
+
export type ColumnRef<K extends string = string> = QueryRaw & {
|
|
604
|
+
readonly [COLUMN_KEY]?: K;
|
|
605
|
+
};
|
|
606
|
+
/**
|
|
607
|
+
* The fields of `E` as {@link ColumnRef}s, for SQL that names them: `refs(User)` in a statement, the
|
|
608
|
+
* callback's parameter in a definition. Keyed over a type parameter constrained to `keyof E`, as
|
|
609
|
+
* {@link KeyMap} is over `keyof E`, which keeps each ref linked to its field for rename.
|
|
610
|
+
*/
|
|
611
|
+
export type RefMap<E, F extends keyof E = FieldKey<E>> = {
|
|
612
|
+
readonly [K in F]-?: ColumnRef<K & string>;
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* A callback reading an entity's fields off a {@link RefMap} for the SQL it returns. Declared as a
|
|
616
|
+
* method, bivariant in its refs, so one typed for its entity still fits where the entity is erased: the
|
|
617
|
+
* registry, which resolves it.
|
|
618
|
+
*/
|
|
619
|
+
export type SqlCallback<E> = {
|
|
620
|
+
sql(row: RefMap<E>): QueryRaw;
|
|
621
|
+
}['sql'];
|
|
622
|
+
/** SQL a definition writes: `raw`, or a {@link SqlCallback} for SQL that names the entity's fields. */
|
|
623
|
+
export type EntitySql<E> = QueryRaw | SqlCallback<E>;
|
|
624
|
+
/**
|
|
625
|
+
* A predicate DDL carries, over the entity's own fields: a relation, full-text search and a sub-query
|
|
626
|
+
* have nothing a `CHECK` or a partial index can hold. An intersection rather than `Except`, which would
|
|
627
|
+
* remap the keys and lose each one's link to its field.
|
|
628
|
+
*/
|
|
629
|
+
export type EntityPredicate<E> = QueryWhere<E> & {
|
|
630
|
+
readonly [K in RelationKey<E>]?: never;
|
|
631
|
+
} & {
|
|
632
|
+
readonly $text?: never;
|
|
633
|
+
readonly $exists?: never;
|
|
634
|
+
readonly $nexists?: never;
|
|
635
|
+
};
|
|
636
|
+
/** A definition's predicate: an {@link EntityPredicate}, or {@link EntitySql} for what one cannot say. */
|
|
637
|
+
export type EntityWhere<E> = EntityPredicate<E> | EntitySql<E>;
|
|
638
|
+
/** A definition's predicate as metadata keeps it, a callback resolved to its SQL, for the schema build to compile. */
|
|
639
|
+
export type EntityWhereMeta<E> = EntityPredicate<E> | QueryRaw;
|
|
595
640
|
export type RelationReferences = {
|
|
596
641
|
readonly local: string;
|
|
597
642
|
readonly foreign: string;
|
|
@@ -632,13 +677,13 @@ export type IndexTypeOptions = {
|
|
|
632
677
|
distance?: never;
|
|
633
678
|
};
|
|
634
679
|
/**
|
|
635
|
-
* One entry of an index: a column
|
|
636
|
-
* when the entry needs more than
|
|
680
|
+
* One entry of an index: a column by default, a {@link SqlCallback} to index an expression, or an object
|
|
681
|
+
* when the entry needs more than that.
|
|
637
682
|
*
|
|
638
683
|
* @example
|
|
639
684
|
* ```ts
|
|
640
685
|
* @Index((post) => [post.tenantId, { column: post.createdAt, order: 'desc' }]) // keyset pagination
|
|
641
|
-
* @Index(() => [raw`lower(
|
|
686
|
+
* @Index(() => [(post) => raw`lower(${post.email})`], { unique: true }) // case-insensitive uniqueness
|
|
642
687
|
* @Index((post) => [{ column: post.body, length: 64 }]) // MySQL needs a prefix on TEXT
|
|
643
688
|
* @Index((post) => [post.data], { type: 'gin' }) // JSONB containment
|
|
644
689
|
* ```
|
|
@@ -648,7 +693,7 @@ export type IndexTypeOptions = {
|
|
|
648
693
|
* default to the unchecked form for the migration builder's `table.index(...)`, which names raw
|
|
649
694
|
* table columns with no entity in scope.
|
|
650
695
|
*/
|
|
651
|
-
export type IndexColumnInput<C extends string = string, E = unknown> = C |
|
|
696
|
+
export type IndexColumnInput<C extends string = string, E = unknown> = C | SqlCallback<E> | IndexColumnOptions<C, E> | IndexJsonColumnOptions<C, E>;
|
|
652
697
|
/**
|
|
653
698
|
* The JSON entries, whose `path` is checked against the payload of the column the same entry names -
|
|
654
699
|
* a mapped union, one arm per JSON field, so `{ column: 'kind', jsonPath: { path: 'thema.color' } }`
|
|
@@ -663,7 +708,7 @@ export type IndexColumnInput<C extends string = string, E = unknown> = C | Query
|
|
|
663
708
|
* builder. An entity with no JSON field at all offers no arm, which is also the truth.
|
|
664
709
|
*/
|
|
665
710
|
type IndexJsonColumnOptions<C extends string, E> = unknown extends E ? IndexColumnModifiers & {
|
|
666
|
-
readonly column: C
|
|
711
|
+
readonly column: C;
|
|
667
712
|
} : {
|
|
668
713
|
[K in JsonColumnKey<E>]: IndexColumnPlainModifiers & {
|
|
669
714
|
readonly column: K;
|
|
@@ -762,9 +807,9 @@ export type IndexJsonArray = {
|
|
|
762
807
|
};
|
|
763
808
|
/** The modifiers that do not name a JSON path, and so need no entity to be checked against. */
|
|
764
809
|
type IndexColumnPlainModifiers = Except<IndexColumnModifiers, 'jsonPath' | 'jsonArray'>;
|
|
765
|
-
export type IndexColumnOptions<C extends string = string> = IndexColumnPlainModifiers & {
|
|
766
|
-
/** The column to index, or
|
|
767
|
-
readonly column: C |
|
|
810
|
+
export type IndexColumnOptions<C extends string = string, E = unknown> = IndexColumnPlainModifiers & {
|
|
811
|
+
/** The column to index, or a {@link SqlCallback} for an expression. */
|
|
812
|
+
readonly column: C | SqlCallback<E>;
|
|
768
813
|
readonly jsonPath?: never;
|
|
769
814
|
readonly jsonArray?: never;
|
|
770
815
|
};
|
|
@@ -778,18 +823,26 @@ export type IndexColumnSchema = IndexColumnModifiers & {
|
|
|
778
823
|
/** Whether {@link column} is an expression to emit as-is rather than an identifier to quote. */
|
|
779
824
|
readonly expression?: boolean;
|
|
780
825
|
};
|
|
826
|
+
/**
|
|
827
|
+
* One index entry as entity metadata keeps it: a member, or an expression left unrendered until the
|
|
828
|
+
* schema is built, where the dialect and the naming strategy resolve what it references. Rendered, it
|
|
829
|
+
* is an {@link IndexColumnSchema}.
|
|
830
|
+
*/
|
|
831
|
+
export type EntityIndexColumn = IndexColumnModifiers & {
|
|
832
|
+
readonly column: string | QueryRaw;
|
|
833
|
+
};
|
|
781
834
|
/**
|
|
782
835
|
* An index as stored in entity metadata: authored options with the columns normalized.
|
|
783
836
|
*/
|
|
784
|
-
export type EntityIndexMeta = {
|
|
837
|
+
export type EntityIndexMeta<E = object> = {
|
|
785
838
|
/** The indexed columns, in order. */
|
|
786
|
-
columns: readonly
|
|
839
|
+
columns: readonly EntityIndexColumn[];
|
|
787
840
|
/** Custom index name */
|
|
788
841
|
name?: string;
|
|
789
842
|
/** Whether index is unique; omit or `false` for a non-unique index (default). */
|
|
790
843
|
unique?: boolean;
|
|
791
|
-
/** Partial index
|
|
792
|
-
where?:
|
|
844
|
+
/** Partial index predicate, compiled when the schema is built. */
|
|
845
|
+
where?: EntityWhereMeta<E>;
|
|
793
846
|
/**
|
|
794
847
|
* Extra columns stored in the index but not part of its key, so a query reading only these is
|
|
795
848
|
* answered from the index alone. Postgres-wire only (`INCLUDE`).
|
|
@@ -826,9 +879,9 @@ export type EntityMeta<E> = {
|
|
|
826
879
|
[key: string]: RelationMeta | undefined;
|
|
827
880
|
};
|
|
828
881
|
/** Composite indexes defined via @Index decorator */
|
|
829
|
-
indexes?: EntityIndexMeta[];
|
|
830
|
-
/** `CHECK` constraints,
|
|
831
|
-
checks?:
|
|
882
|
+
indexes?: EntityIndexMeta<E>[];
|
|
883
|
+
/** `CHECK` constraints, compiled when the schema is built. */
|
|
884
|
+
checks?: EntityCheckMeta<E>[];
|
|
832
885
|
/** Lifecycle hooks registered via @BeforeInsert, @AfterUpdate, etc. */
|
|
833
886
|
hooks?: Partial<Record<HookEvent, HookRegistration[]>>;
|
|
834
887
|
/**
|
|
@@ -840,13 +893,21 @@ export type EntityMeta<E> = {
|
|
|
840
893
|
processedAt?: number;
|
|
841
894
|
};
|
|
842
895
|
/**
|
|
843
|
-
* A table-level `CHECK
|
|
844
|
-
*
|
|
896
|
+
* A table-level `CHECK`: a predicate over the entity's fields, or SQL reading them off refs. A value in
|
|
897
|
+
* either is written as its literal, since DDL has no placeholder to bind one into.
|
|
898
|
+
*
|
|
899
|
+
* @example `{ where: { balance: { $gte: 0 } } }`
|
|
900
|
+
* @example `{ where: (wallet) => raw`${wallet.spent} <= ${wallet.balance}` }`
|
|
845
901
|
*/
|
|
846
|
-
export type CheckOptions = {
|
|
902
|
+
export type CheckOptions<E = unknown> = {
|
|
847
903
|
/** Derived from the table and the constraint's position when absent. */
|
|
848
904
|
readonly name?: string;
|
|
849
|
-
readonly
|
|
905
|
+
readonly where: EntityWhere<E>;
|
|
906
|
+
};
|
|
907
|
+
/** A `CHECK` as entity metadata keeps it, its callback resolved. */
|
|
908
|
+
export type EntityCheckMeta<E = object> = {
|
|
909
|
+
readonly name?: string;
|
|
910
|
+
readonly where: EntityWhereMeta<E>;
|
|
850
911
|
};
|
|
851
912
|
/**
|
|
852
913
|
* An entity's members as the registry takes them, keyed by plain strings - what a decorator bag, an
|
|
@@ -860,7 +921,7 @@ export type EntityMembers = {
|
|
|
860
921
|
};
|
|
861
922
|
/** An entity's fields as `defineEntity` takes them, keyed like every entity map (see `QuerySelect`). */
|
|
862
923
|
type EntityFieldOptions<E, F extends keyof E = FieldKey<E>> = {
|
|
863
|
-
readonly [K in F]?: FieldOptionsFor<E[K]>;
|
|
924
|
+
readonly [K in F]?: FieldOptionsFor<E[K], E>;
|
|
864
925
|
};
|
|
865
926
|
/**
|
|
866
927
|
* An entity's relations as `defineEntity` takes them. Keyed over every member rather than `RelationKey<E>`:
|
|
@@ -897,7 +958,7 @@ export type EntityOptions<E = unknown> = {
|
|
|
897
958
|
readonly relations?: EntityRelationOptions<E>;
|
|
898
959
|
readonly indexes?: readonly EntityIndexInput<E>[];
|
|
899
960
|
/** Table-level `CHECK` constraints. See {@link CheckOptions}. */
|
|
900
|
-
readonly checks?: readonly CheckOptions[];
|
|
961
|
+
readonly checks?: readonly CheckOptions<E>[];
|
|
901
962
|
/** Each lifecycle event and the methods it runs, read off the key map: `{ beforeInsert: (post) => [post.stamp] }`. */
|
|
902
963
|
readonly hooks?: Partial<Record<HookEvent, (keys: KeyMap<E>) => readonly MethodKey<E>[]>>;
|
|
903
964
|
};
|
|
@@ -909,18 +970,17 @@ export type EntityOptions<E = unknown> = {
|
|
|
909
970
|
export type IndexOptions = Except<EntityIndexMeta, 'columns' | 'include' | 'where'> & {
|
|
910
971
|
/** Non-key columns stored in the index, by column name; a typo builds nothing, the server refusing it. */
|
|
911
972
|
readonly include?: readonly string[];
|
|
912
|
-
/**
|
|
913
|
-
|
|
914
|
-
* there is no placeholder for a bound value. A bare string is the older spelling and still works.
|
|
915
|
-
*/
|
|
916
|
-
readonly where?: string | QueryRaw;
|
|
973
|
+
/** Partial-index predicate, as `raw` with no interpolation: the migration builder has no entity to compile one against. */
|
|
974
|
+
readonly where?: QueryRaw;
|
|
917
975
|
};
|
|
918
976
|
/**
|
|
919
977
|
* {@link IndexOptions} on an entity, whose stored columns are read off its key map, `(post) => [post.slug]`,
|
|
920
978
|
* so they are checked against it and follow a rename. The migration builder names raw columns instead.
|
|
921
979
|
*/
|
|
922
|
-
export type EntityIndexOptions<E> = Except<IndexOptions, 'include'> & {
|
|
980
|
+
export type EntityIndexOptions<E> = Except<IndexOptions, 'include' | 'where'> & {
|
|
923
981
|
readonly include?: (keys: KeyMap<E>) => readonly FieldKey<E>[];
|
|
982
|
+
/** Partial-index predicate. See {@link EntityWhere}. */
|
|
983
|
+
readonly where?: EntityWhere<E>;
|
|
924
984
|
};
|
|
925
985
|
/**
|
|
926
986
|
* An index as authored on an entity, before `defineIndex` reads its columns off the key map. Only the
|
package/dist/type/migration.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { VectorCast } from '../dialect/vectorCast.js';
|
|
2
|
-
import type { FullColumnDefinition, TableDefinition } from '../migrate/builder/types.js';
|
|
2
|
+
import type { FullColumnDefinition, IndexDefinition, TableDefinition } from '../migrate/builder/types.js';
|
|
3
3
|
import type { IndexFacet } from '../schema/indexDifferences.js';
|
|
4
4
|
import type { SchemaAST } from '../schema/schemaAST.js';
|
|
5
5
|
import type { ColumnNode, ForeignKeyAction, IndexNode, IndexType, TableNode } from '../schema/types.js';
|
|
6
|
-
import type { EntityMeta, FieldOptions, IndexColumnSchema, LoggingOptions, Querier, SqlQuerier, Type, VectorIndexOptions } from './index.js';
|
|
6
|
+
import type { EntityMeta, EntityWhereMeta, FieldOptions, IndexColumnSchema, LoggingOptions, Querier, SqlQuerier, Type, VectorIndexOptions } from './index.js';
|
|
7
7
|
/**
|
|
8
8
|
* Defines a migration using a simple object literal. `Q` is `MongoQuerier` for a MongoDB migration.
|
|
9
9
|
*/
|
|
@@ -295,6 +295,11 @@ export interface SchemaGenerator {
|
|
|
295
295
|
* Get the SQL type for a field based on its options
|
|
296
296
|
*/
|
|
297
297
|
getSqlType(fieldOptions: FieldOptions): string;
|
|
298
|
+
/**
|
|
299
|
+
* The text of SQL an entity declares - a check, a stored computed column, an index expression or
|
|
300
|
+
* predicate - rendered for this engine, which is what building an entity's schema needs from it.
|
|
301
|
+
*/
|
|
302
|
+
compileDdl(sql: EntityWhereMeta<object>, entity: Type<object>): string;
|
|
298
303
|
/**
|
|
299
304
|
* Compare an entity with a database table node and return the differences.
|
|
300
305
|
*
|
|
@@ -361,6 +366,8 @@ export interface SqlDdlGenerator extends SchemaGenerator {
|
|
|
361
366
|
generateAddForeignKeySql(tableName: string, foreignKey: ForeignKeySchema): string;
|
|
362
367
|
/** Generate DROP FOREIGN KEY statement */
|
|
363
368
|
generateDropForeignKeySql(tableName: string, constraintName: string): string;
|
|
369
|
+
/** CREATE INDEX from a builder's {@link IndexDefinition}, its SQL rendered for this engine. */
|
|
370
|
+
generateCreateIndexFromDefinition(tableName: string, index: IndexDefinition): string;
|
|
364
371
|
}
|
|
365
372
|
/**
|
|
366
373
|
* Interface for introspecting the current database schema
|
package/dist/type/query.d.ts
CHANGED
|
@@ -90,7 +90,7 @@ export interface UqlContext {
|
|
|
90
90
|
* A filter's `$where` fragment: a plain fragment, or a function of the ambient {@link UqlContext}.
|
|
91
91
|
* Return `undefined` when the condition can't resolve (see {@link FilterOptions.onMissing}).
|
|
92
92
|
*/
|
|
93
|
-
export type
|
|
93
|
+
export type FilterWhere<E> = QueryWhere<E> | ((context: UqlContext | undefined) => QueryWhere<E> | undefined);
|
|
94
94
|
/**
|
|
95
95
|
* What to do when a filter's condition returns `undefined`. `skip` omits it (convenience filters);
|
|
96
96
|
* `throw` fails closed (the default for `security` filters).
|
|
@@ -100,7 +100,7 @@ export type FilterOnMissing = 'skip' | 'throw';
|
|
|
100
100
|
* Authoring shape for `@Entity({ filters })` / `@Filter` / `defineFilter`.
|
|
101
101
|
*/
|
|
102
102
|
export type FilterOptions<E = unknown> = {
|
|
103
|
-
readonly
|
|
103
|
+
readonly where: FilterWhere<E>;
|
|
104
104
|
/** Applied to every query unless bypassed via `QueryOptions.filters`. Defaults to `true`. */
|
|
105
105
|
readonly default?: boolean;
|
|
106
106
|
/**
|
|
@@ -108,7 +108,7 @@ export type FilterOptions<E = unknown> = {
|
|
|
108
108
|
* AND-merged so a client `$where` on the same field can't override it.
|
|
109
109
|
*/
|
|
110
110
|
readonly security?: boolean;
|
|
111
|
-
/** What to do when
|
|
111
|
+
/** What to do when {@link where} returns `undefined`. Defaults to `skip`, or `throw` for `security`. */
|
|
112
112
|
readonly onMissing?: FilterOnMissing;
|
|
113
113
|
};
|
|
114
114
|
/**
|
package/dist/type/queryLock.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
declare const QUERY_LOCK_WAITS: readonly ['
|
|
1
|
+
declare const QUERY_LOCK_WAITS: readonly ['nowait', 'skip'];
|
|
2
2
|
/**
|
|
3
|
-
* What to do about a row someone else already holds. `block` (
|
|
4
|
-
* fails the statement at once; `skip` leaves the row out of the result, which is what makes a
|
|
3
|
+
* What to do about a row someone else already holds. `block` (what `true` asks for) waits for them;
|
|
4
|
+
* `nowait` fails the statement at once; `skip` leaves the row out of the result, which is what makes a
|
|
5
5
|
* work-queue possible: each worker takes rows nobody else has.
|
|
6
6
|
*/
|
|
7
|
-
export type QueryLockWait = (typeof QUERY_LOCK_WAITS)[number];
|
|
7
|
+
export type QueryLockWait = 'block' | (typeof QUERY_LOCK_WAITS)[number];
|
|
8
8
|
/**
|
|
9
|
-
* `true` takes the lock and waits for anyone holding the rows;
|
|
10
|
-
*
|
|
9
|
+
* `true` takes the lock and waits for anyone holding the rows; `$wait` chooses what to do instead of
|
|
10
|
+
* waiting. `false` takes none, so a query built conditionally needs no branch.
|
|
11
11
|
*/
|
|
12
12
|
export type QueryLock = boolean | {
|
|
13
|
-
readonly wait
|
|
13
|
+
readonly $wait: (typeof QUERY_LOCK_WAITS)[number];
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
16
|
* The wait policy this lock resolves to, or `undefined` when there is no lock. An unknown policy
|
package/dist/type/queryLock.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const QUERY_LOCK_WAITS = ['
|
|
1
|
+
const QUERY_LOCK_WAITS = ['nowait', 'skip'];
|
|
2
2
|
function isOneOf(vals, val) {
|
|
3
3
|
return vals.includes(val);
|
|
4
4
|
}
|
|
@@ -8,12 +8,14 @@ function isOneOf(vals, val) {
|
|
|
8
8
|
* the SQL it would have produced.
|
|
9
9
|
*/
|
|
10
10
|
export function parseQueryLock(lock) {
|
|
11
|
-
if (lock
|
|
11
|
+
if (!lock) {
|
|
12
12
|
return undefined;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
throw new TypeError(`unknown $lock wait policy: ${String(wait)}`);
|
|
14
|
+
if (lock === true) {
|
|
15
|
+
return 'block';
|
|
17
16
|
}
|
|
18
|
-
|
|
17
|
+
if (!isOneOf(QUERY_LOCK_WAITS, lock.$wait)) {
|
|
18
|
+
throw new TypeError(`unknown $lock wait policy: ${String(lock.$wait)}`);
|
|
19
|
+
}
|
|
20
|
+
return lock.$wait;
|
|
19
21
|
}
|
package/dist/type/queryRaw.d.ts
CHANGED
|
@@ -1,43 +1,36 @@
|
|
|
1
1
|
import type { QueryContext, QueryDialect } from './dialect.js';
|
|
2
|
-
import type {
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* the prefix.
|
|
14
|
-
*/
|
|
15
|
-
prefix?: string;
|
|
16
|
-
/**
|
|
17
|
-
* the escaped prefix.
|
|
18
|
-
*/
|
|
19
|
-
escapedPrefix?: string;
|
|
2
|
+
import type { Type } from './utility.js';
|
|
3
|
+
/** What a `raw` callback receives. See {@link QueryRawFn}. */
|
|
4
|
+
export type QueryRawRenderOptions = {
|
|
5
|
+
/** The dialect rendering the SQL. */
|
|
6
|
+
dialect: QueryDialect;
|
|
7
|
+
/** The alias of the table in scope, unescaped; empty where there is none. */
|
|
8
|
+
prefix: string;
|
|
9
|
+
/** {@link prefix} escaped, with its trailing dot. */
|
|
10
|
+
escapedPrefix: string;
|
|
11
|
+
/** The query context the SQL is written into. */
|
|
12
|
+
ctx: QueryContext;
|
|
20
13
|
/**
|
|
21
|
-
*
|
|
14
|
+
* The entity being rendered, which a ref read off a definition's map resolves its column against: a
|
|
15
|
+
* computed field's own, or the one whose schema is built. Absent where a statement renders SQL.
|
|
22
16
|
*/
|
|
23
|
-
|
|
17
|
+
entity?: Type<unknown>;
|
|
24
18
|
};
|
|
19
|
+
/** {@link QueryRawRenderOptions} as the callers along the way fill them in, every one still optional. */
|
|
20
|
+
export type QueryRawFnOptions = Partial<QueryRawRenderOptions>;
|
|
25
21
|
/**
|
|
26
22
|
* A `raw` callback: write into `ctx`, or return a string or number to have it appended. Anything else
|
|
27
23
|
* it returns is ignored, which is why the return type is `unknown` rather than `void | Scalar` - the
|
|
28
24
|
* latter rejected `({ ctx }) => ctx.append(...)`, the form every computed field is written in, because
|
|
29
25
|
* TypeScript's "returning a value where void is expected" allowance does not apply to a union.
|
|
30
|
-
*
|
|
31
|
-
* `Required`, and the parameter not optional, because the one place that calls it (`getRawValue`)
|
|
32
|
-
* passes all four every time.
|
|
33
26
|
*/
|
|
34
|
-
export type QueryRawFn = (opts:
|
|
27
|
+
export type QueryRawFn = (opts: QueryRawRenderOptions) => unknown;
|
|
35
28
|
export declare const RAW_VALUE: unique symbol;
|
|
36
29
|
export declare const RAW_ALIAS: unique symbol;
|
|
37
30
|
export declare class QueryRaw {
|
|
38
|
-
readonly [RAW_VALUE]:
|
|
31
|
+
readonly [RAW_VALUE]: QueryRawFn;
|
|
39
32
|
readonly [RAW_ALIAS]?: string;
|
|
40
|
-
constructor(value:
|
|
33
|
+
constructor(value: QueryRawFn, alias?: string);
|
|
41
34
|
/** The same expression under an alias, for a `$select` projection. */
|
|
42
35
|
as(alias: string): QueryRaw;
|
|
43
36
|
/**
|
|
@@ -45,9 +38,8 @@ export declare class QueryRaw {
|
|
|
45
38
|
* business, which is what lets a `raw` tagged template resolve an interpolated fragment without
|
|
46
39
|
* the dialect having to expose a method for it.
|
|
47
40
|
*
|
|
48
|
-
* The alias is not emitted here: it
|
|
49
|
-
*
|
|
50
|
-
* it around this call.
|
|
41
|
+
* The alias is not emitted here: it names a `$select` projection, which writes it after the term,
|
|
42
|
+
* and anywhere else it would land mid-expression.
|
|
51
43
|
*/
|
|
52
|
-
render(opts:
|
|
44
|
+
render(opts: QueryRawRenderOptions): void;
|
|
53
45
|
}
|
package/dist/type/queryRaw.js
CHANGED
|
@@ -16,17 +16,11 @@ export class QueryRaw {
|
|
|
16
16
|
* business, which is what lets a `raw` tagged template resolve an interpolated fragment without
|
|
17
17
|
* the dialect having to expose a method for it.
|
|
18
18
|
*
|
|
19
|
-
* The alias is not emitted here: it
|
|
20
|
-
*
|
|
21
|
-
* it around this call.
|
|
19
|
+
* The alias is not emitted here: it names a `$select` projection, which writes it after the term,
|
|
20
|
+
* and anywhere else it would land mid-expression.
|
|
22
21
|
*/
|
|
23
22
|
render(opts) {
|
|
24
|
-
const
|
|
25
|
-
if (typeof value !== 'function') {
|
|
26
|
-
opts.ctx.append(opts.prefix + String(value));
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const emitted = value(opts);
|
|
23
|
+
const emitted = this[RAW_VALUE](opts);
|
|
30
24
|
if (typeof emitted === 'string' || (typeof emitted === 'number' && !Number.isNaN(emitted))) {
|
|
31
25
|
opts.ctx.append(String(emitted));
|
|
32
26
|
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import { type IndexColumnInput, type IndexColumnSchema, QueryRaw } from '../type/index.js';
|
|
1
|
+
import { type EntityIndexColumn, type IndexColumnInput, type IndexColumnSchema, QueryRaw } from '../type/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* for the callback form and no placeholder a `CREATE` statement could bind a value into.
|
|
6
|
-
*
|
|
7
|
-
* `what` names the thing being declared, so the error says which one the caller got wrong.
|
|
3
|
+
* Reduces an authored index entry to the form metadata keeps, so the shapes users write - a column
|
|
4
|
+
* name, an expression's callback, an options object - reach the schema as one, each callback resolved.
|
|
8
5
|
*/
|
|
9
|
-
export declare function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
export declare function normalizeIndexColumn(entry: IndexColumnInput): IndexColumnSchema;
|
|
6
|
+
export declare function normalizeIndexColumn(entry: IndexColumnInput): EntityIndexColumn;
|
|
7
|
+
/** An index entry as the schema holds it, its expression rendered to text by `render`. */
|
|
8
|
+
export declare function renderIndexColumn(entry: EntityIndexColumn, render: (sql: QueryRaw) => string): IndexColumnSchema;
|
|
9
|
+
/** What an unnamed index's name is built from: each entry's column, or `expr<n>` for an expression, which has none. */
|
|
10
|
+
export declare function indexNameParts(entries: readonly EntityIndexColumn[]): string[];
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
import { QueryRaw
|
|
2
|
-
|
|
3
|
-
if (!(value instanceof QueryRaw)) {
|
|
4
|
-
return value;
|
|
5
|
-
}
|
|
6
|
-
const sql = value[RAW_VALUE];
|
|
7
|
-
if (typeof sql !== 'string') {
|
|
8
|
-
throw new TypeError(`${what} needs raw() with no interpolation, not a function or a bound value`);
|
|
9
|
-
}
|
|
10
|
-
return sql;
|
|
11
|
-
}
|
|
1
|
+
import { QueryRaw } from '../type/index.js';
|
|
2
|
+
import { entitySql } from './raw.js';
|
|
12
3
|
/**
|
|
13
|
-
* Reduces an authored index entry to
|
|
14
|
-
* name, an expression,
|
|
4
|
+
* Reduces an authored index entry to the form metadata keeps, so the shapes users write - a column
|
|
5
|
+
* name, an expression's callback, an options object - reach the schema as one, each callback resolved.
|
|
15
6
|
*/
|
|
16
7
|
export function normalizeIndexColumn(entry) {
|
|
17
8
|
if (typeof entry === 'string') {
|
|
18
9
|
return { column: entry };
|
|
19
10
|
}
|
|
20
|
-
if (entry
|
|
21
|
-
return { column:
|
|
11
|
+
if (typeof entry === 'function') {
|
|
12
|
+
return { column: entitySql(entry) };
|
|
22
13
|
}
|
|
23
|
-
const { column
|
|
24
|
-
return column
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
const { column } = entry;
|
|
15
|
+
return { ...entry, column: typeof column === 'function' ? entitySql(column) : column };
|
|
16
|
+
}
|
|
17
|
+
/** An index entry as the schema holds it, its expression rendered to text by `render`. */
|
|
18
|
+
export function renderIndexColumn(entry, render) {
|
|
19
|
+
const { column } = entry;
|
|
20
|
+
return column instanceof QueryRaw ? { ...entry, column: render(column), expression: true } : { ...entry, column };
|
|
21
|
+
}
|
|
22
|
+
/** What an unnamed index's name is built from: each entry's column, or `expr<n>` for an expression, which has none. */
|
|
23
|
+
export function indexNameParts(entries) {
|
|
24
|
+
return entries.map((entry, at) => (typeof entry.column === 'string' ? entry.column : `expr${at}`));
|
|
27
25
|
}
|
|
@@ -83,7 +83,7 @@ export declare function findVectorSort<E>(sort: QuerySortMap<E> | undefined): {
|
|
|
83
83
|
* The vector index declared on `key`, if any. Answers both "is there an ANN index to tune here" and
|
|
84
84
|
* "which kind", which decide the name Atlas is queried by and the setting Postgres is tuned with.
|
|
85
85
|
*/
|
|
86
|
-
export declare function findVectorIndex<E>(meta: EntityMeta<E>, key: string): EntityIndexMeta | undefined;
|
|
86
|
+
export declare function findVectorIndex<E>(meta: EntityMeta<E>, key: string): EntityIndexMeta<E> | undefined;
|
|
87
87
|
/**
|
|
88
88
|
* Whether a `$where` filters by vector distance anywhere in its tree, `$and`/`$or`/`$not` included.
|
|
89
89
|
* What tells Postgres that an HNSW scan needs to iterate rather than return one candidate list.
|
|
@@ -222,7 +222,7 @@ export function hasVectorNear(where) {
|
|
|
222
222
|
return Object.entries(where).some(([key, value]) => key === '$near' || hasVectorNear(value));
|
|
223
223
|
}
|
|
224
224
|
function indexCoversColumn(index, key) {
|
|
225
|
-
return index.columns.some((entry) =>
|
|
225
|
+
return index.columns.some((entry) => entry.column === key);
|
|
226
226
|
}
|
|
227
227
|
/** `satisfies` ties this to {@link JsonUpdateOp}, so renaming an operator breaks it at compile time. */
|
|
228
228
|
const JSON_UPDATE_OPS = [
|
|
@@ -293,8 +293,7 @@ export function applyFilters(meta, whereMap, opts) {
|
|
|
293
293
|
if (!active) {
|
|
294
294
|
continue;
|
|
295
295
|
}
|
|
296
|
-
const
|
|
297
|
-
const condition = typeof raw === 'function' ? raw(context) : raw;
|
|
296
|
+
const condition = typeof filter.where === 'function' ? filter.where(context) : filter.where;
|
|
298
297
|
if (condition === undefined) {
|
|
299
298
|
const onMissing = filter.onMissing ?? (filter.security ? 'throw' : 'skip');
|
|
300
299
|
if (onMissing === 'throw') {
|
|
@@ -444,7 +443,7 @@ export function textSearchFields(meta, search) {
|
|
|
444
443
|
}
|
|
445
444
|
const fulltext = (meta.indexes ?? []).filter((index) => index.type === 'fulltext');
|
|
446
445
|
if (fulltext.length === 1) {
|
|
447
|
-
return fulltext[0].columns.
|
|
446
|
+
return fulltext[0].columns.flatMap((entry) => (typeof entry.column === 'string' ? [entry.column] : []));
|
|
448
447
|
}
|
|
449
448
|
const name = entityName(meta);
|
|
450
449
|
const declared = fulltext.length
|
|
@@ -32,7 +32,7 @@ export declare function isIntegerColumn(field: Pick<FieldOptions, 'type' | 'colu
|
|
|
32
32
|
* Every read site asks this - the DDL skip, the projection, the `$where` and `ORDER BY` operands -
|
|
33
33
|
* because an inlined field has no column to name, while a stored one is read like any other.
|
|
34
34
|
*/
|
|
35
|
-
export declare function isInlinedExpression(field:
|
|
35
|
+
export declare function isInlinedExpression<F extends FieldOptions>(field: F): field is F & Required<Pick<F, 'computed'>>;
|
|
36
36
|
/**
|
|
37
37
|
* Whether the database supplies this field's value, so no insert or update may write it: a stored
|
|
38
38
|
* computed column *is* a real column, read like one, but writing to it is an error on every engine.
|