@ez4/database 0.26.0 → 0.28.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.
@@ -1,6 +1,6 @@
1
- import type { DecomposeIndexName } from './indexes.js';
2
- import type { TableMetadata } from './table.js';
3
- import type { Database } from './database.js';
1
+ import type { DecomposeIndexName } from './indexes';
2
+ import type { TableMetadata } from './table';
3
+ import type { Database } from './database';
4
4
  /**
5
5
  * Query order types.
6
6
  */
@@ -16,9 +16,9 @@ export declare const enum OrderMode {
16
16
  AnyColumns = "any"
17
17
  }
18
18
  /**
19
- * Order utils.
19
+ * Order mode utils.
20
20
  */
21
- export declare namespace OrderUtils {
21
+ export declare namespace OrderModeUtils {
22
22
  /**
23
23
  * Order input for index columns.
24
24
  */
@@ -1,4 +1,4 @@
1
- import type { DatabaseEngine, EngineUtils } from './engine.js';
1
+ import type { DatabaseEngine, EngineUtils } from './engine';
2
2
  /**
3
3
  * Pagination mode.
4
4
  */
@@ -7,9 +7,9 @@ export declare const enum PaginationMode {
7
7
  Offset = "offset"
8
8
  }
9
9
  /**
10
- * Pagination utils.
10
+ * Pagination mode utils.
11
11
  */
12
- export declare namespace PaginationUtils {
12
+ export declare namespace PaginationModeUtils {
13
13
  /**
14
14
  * Get the pagination range based on the given database engine.
15
15
  */
@@ -1,5 +1,5 @@
1
- import type { EngineUtils } from './engine.js';
2
- import type { Database } from './database.js';
1
+ import type { EngineUtils } from './engine';
2
+ import type { Database } from './database';
3
3
  /**
4
4
  * Parameters mode.
5
5
  */
@@ -8,9 +8,9 @@ export declare const enum ParametersMode {
8
8
  OnlyIndex = "index"
9
9
  }
10
10
  /**
11
- * Parameters utils.
11
+ * Parameters mode utils.
12
12
  */
13
- export declare namespace ParametersUtils {
13
+ export declare namespace ParametersModeUtils {
14
14
  /**
15
15
  * Get the parameters type based on the given database service.
16
16
  */
@@ -1,12 +1,13 @@
1
- import type { DecomposeIndexName, PrimaryIndexes, UniqueIndexes } from './indexes.js';
2
- import type { DatabaseEngine } from './engine.js';
3
- import type { RelationMetadata } from './relations.js';
4
- import type { InsensitiveUtils } from './insensitive.js';
5
- import type { PaginationUtils } from './pagination.js';
6
- import type { TableMetadata } from './table.js';
7
- import type { OrderUtils } from './order.js';
8
- import type { Database } from './database.js';
9
- import type { AnyObject, PartialProperties, PartialObject, FlatObject, OptionalObject, StrictObject, IsNullable, IsObjectEmpty, IsObject, IsArray, Prettify } from '@ez4/utils';
1
+ import type { DecomposeIndexName, PrimaryIndexes, UniqueIndexes } from './indexes';
2
+ import type { DatabaseEngine } from './engine';
3
+ import type { RelationMetadata } from './relations';
4
+ import type { InsensitiveModeUtils } from './insensitive';
5
+ import type { PaginationModeUtils } from './pagination';
6
+ import type { OrderModeUtils } from './order';
7
+ import type { LockModeUtils } from './lock';
8
+ import type { TableMetadata } from './table';
9
+ import type { Database } from './database';
10
+ import type { AnyObject, PartialProperties, PartialObject, FlatObject, OptionalObject, StrictObject, IsNullable, IsUndefined, IsObjectEmpty, IsObject, IsArray, Prettify } from '@ez4/utils';
10
11
  /**
11
12
  * Query builder types.
12
13
  */
@@ -16,17 +17,20 @@ export declare namespace Query {
16
17
  data: Prettify<InsertDataInput<T>>;
17
18
  };
18
19
  export type UpdateOneInput<S extends AnyObject, T extends TableMetadata> = {
20
+ lock?: LockModeUtils.Input<T>;
19
21
  select?: StrictSelectInput<S, T>;
20
22
  include?: StrictIncludeInput<S, T>;
21
23
  data: Prettify<OptionalObject<UpdateDataInput<T>>>;
22
24
  where: WhereInput<T, true>;
23
25
  };
24
26
  export type FindOneInput<S extends AnyObject, T extends TableMetadata> = {
27
+ lock?: LockModeUtils.Input<T>;
25
28
  select: StrictSelectInput<S, T>;
26
29
  include?: StrictIncludeInput<S, T>;
27
30
  where: WhereInput<T, true>;
28
31
  };
29
32
  export type UpsertOneInput<S extends AnyObject, T extends TableMetadata> = {
33
+ lock?: LockModeUtils.Input<T>;
30
34
  select?: StrictSelectInput<S, T>;
31
35
  include?: StrictIncludeInput<S, T>;
32
36
  update: Prettify<OptionalObject<UpdateDataInput<T>>>;
@@ -41,20 +45,22 @@ export declare namespace Query {
41
45
  export type InsertManyInput<T extends TableMetadata> = {
42
46
  data: Prettify<T['schema']>[];
43
47
  };
44
- export type UpdateManyInput<S extends AnyObject, T extends TableMetadata> = PaginationUtils.End<T['engine']> & {
48
+ export type UpdateManyInput<S extends AnyObject, T extends TableMetadata> = PaginationModeUtils.End<T['engine']> & {
49
+ lock?: LockModeUtils.Input<T>;
45
50
  select?: StrictSelectInput<S, T>;
46
51
  include?: StrictIncludeInput<S, T>;
47
52
  data: Prettify<OptionalObject<UpdateDataInput<T>>>;
48
53
  where?: WhereInput<T>;
49
54
  };
50
- export type FindManyInput<S extends AnyObject, C extends boolean, T extends TableMetadata> = PaginationUtils.Range<T['engine']> & {
55
+ export type FindManyInput<S extends AnyObject, C extends boolean, T extends TableMetadata> = PaginationModeUtils.Range<T['engine']> & {
56
+ lock?: LockModeUtils.Input<T>;
51
57
  select: StrictSelectInput<S, T>;
52
58
  include?: StrictIncludeInput<S, T>;
53
59
  where?: WhereInput<T>;
54
60
  order?: OrderInput<T>;
55
61
  count?: C;
56
62
  };
57
- export type DeleteManyInput<S extends AnyObject, T extends TableMetadata> = PaginationUtils.End<T['engine']> & {
63
+ export type DeleteManyInput<S extends AnyObject, T extends TableMetadata> = PaginationModeUtils.End<T['engine']> & {
58
64
  select?: StrictSelectInput<S, T>;
59
65
  include?: StrictIncludeInput<S, T>;
60
66
  where?: WhereInput<T>;
@@ -62,30 +68,30 @@ export declare namespace Query {
62
68
  export type CountInput<T extends TableMetadata> = {
63
69
  where?: WhereInput<T>;
64
70
  };
65
- export type InsertOneResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T>;
66
- export type UpdateOneResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T> | undefined;
67
- export type FindOneResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T> | undefined;
68
- export type UpsertOneResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T>;
69
- export type DeleteOneResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T> | undefined;
70
- export type UpdateManyResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T>[];
71
+ export type InsertOneResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T>;
72
+ export type UpdateOneResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T> | undefined;
73
+ export type FindOneResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T> | undefined;
74
+ export type UpsertOneResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T>;
75
+ export type DeleteOneResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T> | undefined;
76
+ export type UpdateManyResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T>[];
71
77
  export type InsertManyResult = void;
72
- export type FindManyResult<S extends AnyObject, C extends boolean, T extends TableMetadata> = PaginationUtils.Result<T['engine']> & {
73
- records: Query.SelectInput<T> extends S ? void : Record<S, T>[];
78
+ export type FindManyResult<S extends AnyObject, C extends boolean, T extends TableMetadata> = PaginationModeUtils.Result<T['engine']> & {
79
+ records: SelectInput<T> extends S ? void : Record<S, T>[];
74
80
  } & (false extends C ? {} : {
75
81
  total: number;
76
82
  });
77
- export type DeleteManyResult<S extends AnyObject, T extends TableMetadata> = Query.SelectInput<T> extends S ? void : Record<S, T>[];
78
- export type Record<S extends AnyObject, T extends TableMetadata> = PartialObject<SelectFields<T['schema'], T['relations']>, S>;
79
- export type SelectInput<T extends TableMetadata> = PartialProperties<SelectFields<T['schema'], T['relations']>>;
80
- export type StrictSelectInput<S extends AnyObject, T extends TableMetadata> = StrictObject<S, FlatObject<SelectFields<T['schema'], T['relations']>>>;
81
- export type InsertDataInput<T extends TableMetadata> = IsObjectEmpty<T['relations']['changes']> extends false ? Omit<T['schema'] & T['relations']['changes'], T['relations']['indexes']> : T['schema'];
82
- export type UpdateDataInput<T extends TableMetadata> = AtomicDataInput<IsObjectEmpty<T['relations']['changes']> extends false ? Omit<T['schema'] & FlatObject<T['relations']['changes']>, T['relations']['indexes']> : T['schema']>;
83
- export type OrderInput<T extends TableMetadata> = OrderUtils.Input<T>;
83
+ export type DeleteManyResult<S extends AnyObject, T extends TableMetadata> = SelectInput<T> extends S ? void : Record<S, T>[];
84
+ export type Record<S extends AnyObject, T extends TableMetadata> = PartialObject<SelectOutputFields<T['schema'], T['relations']>, S>;
85
+ export type SelectInput<T extends TableMetadata> = PartialProperties<SelectInputFields<T['schema'], T['relations']>>;
86
+ export type StrictSelectInput<S extends AnyObject, T extends TableMetadata> = StrictObject<S, SelectInputFields<T['schema'], T['relations']>>;
87
+ export type InsertDataInput<T extends TableMetadata> = IsObjectEmpty<T['relations']['inserts']> extends false ? Omit<T['schema'], T['relations']['indexes']> & T['relations']['inserts'] : T['schema'];
88
+ export type UpdateDataInput<T extends TableMetadata> = IsObjectEmpty<T['relations']['updates']> extends false ? AtomicDataInput<Omit<T['schema'], T['relations']['indexes']> & FlatObject<T['relations']['updates']>> : AtomicDataInput<T['schema']>;
89
+ export type OrderInput<T extends TableMetadata> = OrderModeUtils.Input<T>;
84
90
  export type StrictIncludeInput<S extends AnyObject, T extends TableMetadata> = IsObjectEmpty<T['relations']['filters']> extends true ? never : {
85
- [P in keyof T['relations']['filters']]?: P extends keyof S ? StrictIncludeRelation<T['relations']['filters'][P], T['engine']> : never;
91
+ [P in keyof T['relations']['filters']]?: P extends keyof S ? StrictIncludeRelation<NonNullable<T['relations']['filters'][P]>, T['engine']> : never;
86
92
  };
87
- export type StrictIncludeOrder<V extends AnyObject> = OrderUtils.AnyInput<V>;
88
- export type StrictIncludeRelation<V extends AnyObject, E extends DatabaseEngine> = PaginationUtils.Range<E> & {
93
+ export type StrictIncludeOrder<V extends AnyObject> = OrderModeUtils.AnyInput<V>;
94
+ export type StrictIncludeRelation<V extends AnyObject, E extends DatabaseEngine> = PaginationModeUtils.Range<E> & {
89
95
  where?: WhereRelationField<V, E>;
90
96
  order?: StrictIncludeOrder<V>;
91
97
  };
@@ -94,7 +100,8 @@ export declare namespace Query {
94
100
  AND?: WhereInput<T>[];
95
101
  OR?: WhereInput<T>[];
96
102
  }>;
97
- type SelectFields<T extends Database.Schema, R extends RelationMetadata> = IsObjectEmpty<R['selects']> extends true ? T : T & R['selects'];
103
+ type SelectInputFields<T extends Database.Schema, R extends RelationMetadata> = IsObjectEmpty<R['selects']> extends true ? T : T & R['selects'];
104
+ type SelectOutputFields<T extends Database.Schema, R extends RelationMetadata> = IsObjectEmpty<R['records']> extends true ? T : T & R['records'];
98
105
  type WhereOperations<V, E extends DatabaseEngine> = WhereNegate<V, E> | WhereEqual<V, E> | WhereGreaterThan<V> | WhereGreaterThanOrEqual<V> | WhereLessThan<V> | WhereLessThanOrEqual<V> | WhereIn<V> | WhereBetween<V> | WhereIsMissing | WhereIsNull | WhereStartsWith<E> | WhereContains<V, E>;
99
106
  type WhereField<V, E extends DatabaseEngine> = IsObject<V> extends false ? V | WhereOperations<V, E> : IsNullable<V> extends true ? null | WhereObjectField<NonNullable<V>, E> : WhereObjectField<NonNullable<V>, E>;
100
107
  type WhereObjectField<V extends AnyObject, E extends DatabaseEngine> = {
@@ -120,10 +127,10 @@ export declare namespace Query {
120
127
  type WhereCommonFilters<V extends AnyObject, T extends TableMetadata, I extends Database.Indexes> = IsObjectEmpty<I> extends true ? WhereObjectField<V, T['engine']> : WhereRequiredFilters<V, I> & WhereOptionalFilters<V, T, I>;
121
128
  type WhereInputFilters<T extends TableMetadata, I extends Database.Indexes> = WhereCommonFilters<T['schema'], T, I> & (IsObjectEmpty<T['relations']['filters']> extends false ? WhereRelationFilters<T['relations']['filters'], T['engine']> : {});
122
129
  export type WhereOperators = keyof (WhereNegate<any, never> & WhereEqual<any, never> & WhereGreaterThan<any> & WhereGreaterThanOrEqual<any> & WhereLessThan<any> & WhereLessThanOrEqual<any> & WhereIn<any> & WhereBetween<any> & WhereIsMissing & WhereIsNull & WhereStartsWith<never> & WhereContains<any, never>);
123
- type WhereNegate<V, E extends DatabaseEngine> = (V extends string ? InsensitiveUtils.Input<E> : {}) & {
130
+ type WhereNegate<V, E extends DatabaseEngine> = (V extends string ? InsensitiveModeUtils.Input<E> : {}) & {
124
131
  not: V;
125
132
  };
126
- type WhereEqual<V, E extends DatabaseEngine> = (V extends string ? InsensitiveUtils.Input<E> : {}) & {
133
+ type WhereEqual<V, E extends DatabaseEngine> = (V extends string ? InsensitiveModeUtils.Input<E> : {}) & {
127
134
  equal: V;
128
135
  };
129
136
  type WhereGreaterThan<V> = {
@@ -150,20 +157,20 @@ export declare namespace Query {
150
157
  type WhereIsNull = {
151
158
  isNull: boolean;
152
159
  };
153
- type WhereStartsWith<E extends DatabaseEngine> = InsensitiveUtils.Input<E> & {
160
+ type WhereStartsWith<E extends DatabaseEngine> = InsensitiveModeUtils.Input<E> & {
154
161
  startsWith: string;
155
162
  };
156
- type WhereContains<V, E extends DatabaseEngine> = (V extends string ? InsensitiveUtils.Input<E> : {}) & {
163
+ type WhereContains<V, E extends DatabaseEngine> = (V extends string ? InsensitiveModeUtils.Input<E> : {}) & {
157
164
  contains: IsObject<V> extends true ? Partial<V> : V;
158
165
  };
159
166
  export type AtomicOperators = keyof (AtomicIncrement & AtomicDecrement & AtomicMultiply & AtomicDivide);
160
167
  type AtomicDataInput<T extends AnyObject> = AtomicRequiredFields<T> & AtomicOptionalFields<T>;
161
168
  type AtomicDataField<T> = T extends number ? AtomicOperation | T : T extends AnyObject ? AtomicDataInput<T> : NonNullable<T> extends AnyObject ? null | AtomicDataInput<NonNullable<T>> : T;
162
169
  type AtomicRequiredFields<T extends AnyObject> = {
163
- [P in keyof T as T[P] extends undefined ? never : P]: AtomicDataField<T[P]>;
170
+ [P in keyof T as IsUndefined<T[P]> extends true ? never : P]: AtomicDataField<T[P]>;
164
171
  };
165
172
  type AtomicOptionalFields<T extends AnyObject> = {
166
- [P in keyof T as T[P] extends undefined ? P : never]?: AtomicDataField<T[P]>;
173
+ [P in keyof T as IsUndefined<T[P]> extends true ? P : never]?: AtomicDataField<T[P]>;
167
174
  };
168
175
  type AtomicOperation = AtomicIncrement | AtomicDecrement | AtomicMultiply | AtomicDivide;
169
176
  type AtomicIncrement = {
@@ -1,14 +1,16 @@
1
- import type { AnyObject, ArrayRest, PropertyType, ExclusiveType, IsArrayEmpty, IsUndefined } from '@ez4/utils';
2
- import type { IndexedTables, PrimaryIndexes, UniqueIndexes } from './indexes.js';
3
- import type { Database, DatabaseTables } from './database.js';
4
- import type { TableSchemas } from './schemas.js';
1
+ import type { AnyObject, ArrayRest, PropertyType, ExclusiveType, IsArrayEmpty, IsUndefined, MergeObject, Prettify } from '@ez4/utils';
2
+ import type { IndexedTables, PrimaryIndexes, UniqueIndexes } from './indexes';
3
+ import type { Database, DatabaseTables } from './database';
4
+ import type { TableSchemas } from './schemas';
5
5
  /**
6
6
  * Internal relation type.
7
7
  */
8
8
  export type RelationMetadata = {
9
- filters: Record<string, {}>;
10
- selects: Record<string, {}>;
11
- changes: Record<string, {}>;
9
+ filters: Record<string, AnyObject | undefined>;
10
+ updates: Record<string, AnyObject | undefined>;
11
+ inserts: Record<string, AnyObject | undefined>;
12
+ selects: Record<string, AnyObject | undefined>;
13
+ records: Record<string, AnyObject | undefined>;
12
14
  indexes: string;
13
15
  };
14
16
  /**
@@ -30,70 +32,138 @@ export type RelationTargetAlias<T> = T extends `${string}@${infer U}` ? U : neve
30
32
  /**
31
33
  * Given a database service `T`, it produces an object with all relation tables.
32
34
  */
33
- export type RelationTables<T extends Database.Service> = MergeRelations<DatabaseTables<T>, TableSchemas<T>, IndexedTables<T>>;
35
+ export type RelationTables<T extends Database.Service> = MergeRelations<DatabaseTables<T>, DatabaseTables<T>, TableSchemas<T>, IndexedTables<T>>;
34
36
  /**
35
37
  * Given a list of tables with relations `T`, it produces an object containing all the
36
38
  * relation tables.
37
39
  */
38
- type MergeRelations<T extends Database.Table[], S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = IsArrayEmpty<T> extends false ? TableRelation<T[0], S, I> & MergeRelations<ArrayRest<T>, S, I> : {};
40
+ type MergeRelations<T extends Database.Table[], C extends Database.Table[], S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = IsArrayEmpty<C> extends false ? TableRelation<T, C[0], S, I> & MergeRelations<T, ArrayRest<C>, S, I> : {};
39
41
  /**
40
42
  * Given a database table `T`, it produces an object containing all its relations.
41
43
  */
42
- type TableRelation<T, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = T extends {
44
+ type TableRelation<T extends Database.Table[], C extends Database.Table, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = C extends {
43
45
  name: infer N;
44
46
  relations: infer R;
45
47
  } ? N extends string ? R extends AnyObject ? {
46
48
  [P in N]: {
47
- indexes: RelationIndexes<PropertyType<N, I>, R>;
49
+ indexes: RelationIndexes<N, I, R>;
48
50
  filters: FilterableRelationSchemas<S, R>;
49
- changes: RequiredRelationSchemas<PropertyType<N, S>, S, I, R, true> & OptionalRelationSchemas<PropertyType<N, S>, S, I, R, true>;
50
- selects: RequiredRelationSchemas<PropertyType<N, S>, S, I, R, false> & OptionalRelationSchemas<PropertyType<N, S>, S, I, R, false>;
51
+ updates: UpdateRelationSchemas<N, S, I, R>;
52
+ inserts: InsertRelationSchemas<N, PropertyType<N, S>, S, I, R>;
53
+ selects: SelectRelationSchemas<S, I, R> & NestedSelectRelationSchemas<T, S, I, R>;
54
+ records: MergeObject<RecordsRelationSchemas<PropertyType<N, S>, S, I, R>, NestedRecordsRelationSchemas<T, S, I, R>>;
51
55
  };
52
56
  } : {} : {} : {};
57
+ /**
58
+ * Check whether the given column is a primary index.
59
+ */
60
+ type IsPrimarySourceIndex<C, I extends Record<string, Database.Indexes>> = RelationSourceColumn<C> extends keyof PrimaryIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : false;
61
+ /**
62
+ * Check whether the given column is a unique index.
63
+ */
64
+ type IsUniqueSourceIndex<C, I extends Record<string, Database.Indexes>> = RelationSourceColumn<C> extends keyof UniqueIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : false;
65
+ /**
66
+ * Check whether the given column is a primary target index.
67
+ */
68
+ type IsPrimaryTargetIndex<V, I extends Database.Indexes> = RelationTargetColumn<V> extends keyof PrimaryIndexes<I> ? true : false;
69
+ /**
70
+ * Check whether the given column is a unique target index.
71
+ */
72
+ export type IsUniqueTargetIndex<V, I extends Database.Indexes> = RelationTargetColumn<V> extends keyof UniqueIndexes<I> ? true : false;
73
+ /**
74
+ * Check whether the given column is a secondary target index.
75
+ */
76
+ type IsSecondaryTargetIndex<V, I extends Database.Indexes> = RelationTargetColumn<V> extends keyof (PrimaryIndexes<I> & UniqueIndexes<I>) ? false : true;
77
+ /**
78
+ * Check whether a relation is optional or not.
79
+ */
80
+ type IsOptionalRelation<C, V, T extends Database.Schema, I extends Record<string, Database.Indexes>, E extends boolean> = IsPrimarySourceIndex<C, I> extends true ? IsUndefined<PropertyType<RelationTargetColumn<V>, T>> : IsUniqueSourceIndex<C, I> extends true ? true : E;
53
81
  /**
54
82
  * Produce an object containing all relation indexes.
55
83
  */
56
- type RelationIndexes<I extends Database.Indexes, R extends AnyObject> = keyof {
57
- [C in keyof R as RelationTargetColumn<C> extends keyof PrimaryIndexes<I> ? never : RelationTargetColumn<C>]: never;
84
+ type RelationIndexes<N, I extends Record<string, Database.Indexes>, R extends AnyObject> = keyof {
85
+ [P in keyof R as IsRelationIndex<N, R[P], P, I> extends true ? RelationTargetColumn<P> : never]: never;
58
86
  };
59
87
  /**
60
- * Produce an object containing all filterable relation schemas.
88
+ * Check whether the given source and target columns are used to index the relation.
89
+ */
90
+ type IsRelationIndex<N, C, V, I extends Record<string, Database.Indexes>> = IsPrimarySourceIndex<C, I> extends false ? IsSecondaryTargetIndex<V, PropertyType<N, I>> extends false ? IsUniqueTargetIndex<V, PropertyType<N, I>> extends true ? IsUniqueSourceIndex<C, I> : false : true : true;
91
+ /**
92
+ * Produce an object containing relation schemas for filters.
61
93
  */
62
94
  type FilterableRelationSchemas<S extends Record<string, Database.Schema>, R extends AnyObject> = {
63
- [C in keyof R as RelationTargetAlias<C>]: Omit<PropertyType<RelationSourceTable<R[C]>, S>, RelationSourceColumn<R[C]>>;
95
+ [P in keyof R as RelationTargetAlias<P>]: Omit<PropertyType<RelationSourceTable<R[P]>, S>, RelationSourceColumn<R[P]>>;
64
96
  };
65
97
  /**
66
- * Produce an object containing only required relation schemas.
98
+ * Produce an object containing relation schemas for updates.
67
99
  */
68
- type RequiredRelationSchemas<T extends Database.Schema, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject, E extends boolean> = {
69
- [C in keyof R as IsOptionalRelation<R[C], C, T, I, E> extends false ? RelationTargetAlias<C> : never]: RelationSchema<R[C], C, T, S, I, E>;
100
+ type UpdateRelationSchemas<N, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
101
+ [P in keyof R as RelationTargetAlias<P>]?: ChangeRelationSchema<N, R[P], P, S, I>;
70
102
  };
71
103
  /**
72
- * Produce an object containing only optional relation schemas.
104
+ * Produce an object containing relation schemas for inserts.
73
105
  */
74
- type OptionalRelationSchemas<T extends Database.Schema, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject, E extends boolean> = {
75
- [C in keyof R as IsOptionalRelation<R[C], C, T, I, E> extends true ? RelationTargetAlias<C> : never]?: RelationSchema<R[C], C, T, S, I, E>;
106
+ type InsertRelationSchemas<N, T extends Database.Schema, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
107
+ [P in keyof R as IsOptionalRelation<R[P], P, T, I, true> extends true ? RelationTargetAlias<P> : never]?: ChangeRelationSchema<N, R[P], P, S, I>;
108
+ } & {
109
+ [P in keyof R as IsOptionalRelation<R[P], P, T, I, true> extends false ? RelationTargetAlias<P> : never]: ChangeRelationSchema<N, R[P], P, S, I>;
76
110
  };
77
111
  /**
78
- * Check whether a relation is optional or not.
112
+ * Produce an object containing relation schemas for selects.
113
+ */
114
+ type SelectRelationSchemas<S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
115
+ [P in keyof R as RelationTargetAlias<P>]?: SelectRelationSchema<R[P], S, I>;
116
+ };
117
+ /**
118
+ * Produce an object containing relation schemas for records.
119
+ */
120
+ type RecordsRelationSchemas<T extends Database.Schema, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
121
+ [P in keyof R as IsOptionalRelation<R[P], P, T, I, false> extends true ? RelationTargetAlias<P> : never]?: RecordRelationSchema<R[P], S, I>;
122
+ } & {
123
+ [P in keyof R as IsOptionalRelation<R[P], P, T, I, false> extends false ? RelationTargetAlias<P> : never]: RecordRelationSchema<R[P], S, I>;
124
+ };
125
+ /**
126
+ * Produce an object containing all nested relation schemas for select operations.
127
+ */
128
+ type NestedSelectRelationSchemas<T extends Database.Table[], S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
129
+ [P in keyof R as RelationTargetAlias<P>]?: RelationSourceTable<R[P]> extends keyof MergeRelations<T, T, S, I> ? MergeRelations<T, T, S, I>[RelationSourceTable<R[P]>] extends {
130
+ selects: infer N;
131
+ } ? N : never : never;
132
+ };
133
+ /**
134
+ * Produce an object containing all nested relation schemas for records.
135
+ */
136
+ type NestedRecordsRelationSchemas<T extends Database.Table[], S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
137
+ [P in keyof R as RelationTargetAlias<P>]?: RelationSourceTable<R[P]> extends keyof MergeRelations<T, T, S, I> ? MergeRelations<T, T, S, I>[RelationSourceTable<R[P]>] extends {
138
+ records: infer N;
139
+ } ? N : never : never;
140
+ };
141
+ /**
142
+ * Produce a type corresponding to the source column schema.
143
+ */
144
+ type SourceColumnSchema<C, S extends Record<string, Database.Schema>> = PropertyType<RelationSourceTable<C>, S>;
145
+ /**
146
+ * Produce a change relation schema according to its indexation.
79
147
  */
80
- type IsOptionalRelation<C, V, T extends Database.Schema, I extends Record<string, Database.Indexes>, E extends boolean> = RelationSourceColumn<C> extends keyof PrimaryIndexes<PropertyType<RelationSourceTable<C>, I>> ? IsUndefined<PropertyType<RelationTargetColumn<V>, T>> : RelationSourceColumn<C> extends keyof UniqueIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : E;
148
+ type ChangeRelationSchema<N, C, V, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = IsPrimarySourceIndex<C, I> extends true ? ExclusiveType<SourceColumnSchema<C, S>, PrimaryKeyConnectionSchema<C, S, I>> : IsUniqueSourceIndex<C, I> extends true ? IsPrimaryTargetIndex<V, PropertyType<N, I>> extends true ? ExclusiveType<Omit<SourceColumnSchema<C, S>, RelationSourceColumn<C>>, UniqueKeyConnectionSchema<C, S, I>> : ExclusiveType<SourceColumnSchema<C, S>, UniqueKeyConnectionSchema<C, S, I>> : ExclusiveType<Omit<SourceColumnSchema<C, S>, RelationSourceColumn<C>>, PrimaryKeyConnectionSchema<C, S, I>>[];
81
149
  /**
82
- * Check whether a column is primary.
150
+ * Produce a select relation schema according to its indexation.
83
151
  */
84
- type IsPrimaryIndex<C, I extends Record<string, Database.Indexes>> = RelationSourceColumn<C> extends keyof PrimaryIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : false;
152
+ type SelectRelationSchema<C, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = IsPrimarySourceIndex<C, I> extends true ? SourceColumnSchema<C, S> : IsUniqueSourceIndex<C, I> extends true ? SourceColumnSchema<C, S> : SourceColumnSchema<C, S>;
85
153
  /**
86
- * Check whether a column is unique.
154
+ * Produce a record relation schema according to its indexation.
87
155
  */
88
- type IsUniqueIndex<C, I extends Record<string, Database.Indexes>> = RelationSourceColumn<C> extends keyof UniqueIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : false;
156
+ type RecordRelationSchema<C, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = IsPrimarySourceIndex<C, I> extends true ? SourceColumnSchema<C, S> : IsUniqueSourceIndex<C, I> extends true ? SourceColumnSchema<C, S> : SourceColumnSchema<C, S>[];
89
157
  /**
90
- * Produce a type corresponding to the source index column type.
158
+ * Produce a relation schema for connections using primary keys.
91
159
  */
92
- type ExtractSourceIndexType<C, S extends Record<string, Database.Schema>> = PropertyType<RelationSourceTable<C>, S>;
160
+ type PrimaryKeyConnectionSchema<C, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = Prettify<{
161
+ [P in keyof PrimaryIndexes<PropertyType<RelationSourceTable<C>, I>>]: PropertyType<P, PropertyType<RelationSourceTable<C>, S>> | undefined | null;
162
+ }>;
93
163
  /**
94
- * Produce a relation schema according to its indexation.
164
+ * Produce a relation schema for connections using unique keys.
95
165
  */
96
- type RelationSchema<C, V, T extends Database.Schema, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, E extends boolean> = IsPrimaryIndex<C, I> extends true ? E extends false ? ExtractSourceIndexType<C, S> : ExclusiveType<ExtractSourceIndexType<C, S>, {
97
- [P in RelationTargetColumn<V>]: PropertyType<RelationTargetColumn<V>, T>;
98
- }> : IsUniqueIndex<C, I> extends true ? E extends false ? ExtractSourceIndexType<C, S> : Omit<ExtractSourceIndexType<C, S>, RelationSourceColumn<C>> : Omit<ExtractSourceIndexType<C, S>, RelationSourceColumn<C>>[];
166
+ type UniqueKeyConnectionSchema<C, S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>> = Prettify<{
167
+ [P in keyof UniqueIndexes<PropertyType<RelationSourceTable<C>, I>> as P extends RelationSourceColumn<C> ? P : never]: PropertyType<P, PropertyType<RelationSourceTable<C>, S>> | undefined | null;
168
+ }>;
99
169
  export {};
@@ -1,5 +1,5 @@
1
1
  import type { ArrayRest, IsArrayEmpty } from '@ez4/utils';
2
- import type { Database, DatabaseTables } from './database.js';
2
+ import type { Database, DatabaseTables } from './database';
3
3
  /**
4
4
  * Given a database service `T`, it produces an object containing all tables with schemas.
5
5
  */
@@ -1,10 +1,10 @@
1
1
  import type { AnyObject, PropertyExists } from '@ez4/utils';
2
- import type { RelationMetadata, RelationTables } from './relations.js';
3
- import type { IndexedTables } from './indexes.js';
4
- import type { TableSchemas } from './schemas.js';
5
- import type { Database } from './database.js';
6
- import type { DatabaseEngine } from './engine.js';
7
- import type { Query } from './query.js';
2
+ import type { RelationMetadata, RelationTables } from './relations';
3
+ import type { IndexedTables } from './indexes';
4
+ import type { TableSchemas } from './schemas';
5
+ import type { Database } from './database';
6
+ import type { DatabaseEngine } from './engine';
7
+ import type { Query } from './query';
8
8
  /**
9
9
  * Given an indexed table `T` and a property `P`, it returns all the indexes corresponding
10
10
  * to the given property.
@@ -1,11 +1,11 @@
1
- import type { TableIndex, TableMetadata, TableRelation } from './table.js';
2
- import type { RelationTables } from './relations.js';
3
- import type { IndexedTables } from './indexes.js';
4
- import type { EngineUtils } from './engine.js';
5
- import type { TableSchemas } from './schemas.js';
6
- import type { Database } from './database.js';
7
- import type { Client } from './client.js';
8
- import type { Query } from './query.js';
1
+ import type { TableIndex, TableMetadata, TableRelation } from './table';
2
+ import type { RelationTables } from './relations';
3
+ import type { IndexedTables } from './indexes';
4
+ import type { EngineUtils } from './engine';
5
+ import type { TableSchemas } from './schemas';
6
+ import type { Database } from './database';
7
+ import type { Client } from './client';
8
+ import type { Query } from './query';
9
9
  /**
10
10
  * Transaction mode.
11
11
  */
@@ -14,9 +14,9 @@ export declare const enum TransactionMode {
14
14
  Static = "static"
15
15
  }
16
16
  /**
17
- * Transaction utils.
17
+ * Transaction mode utils.
18
18
  */
19
- export declare namespace TransactionUtils {
19
+ export declare namespace TransactionModeUtils {
20
20
  /**
21
21
  * Extract the operation result from an interactive transaction.
22
22
  */
@@ -1,13 +1,15 @@
1
- import type { ParametersMode } from '../services/parameters.js';
2
- import type { TransactionMode } from '../services/transaction.js';
3
- import type { InsensitiveMode } from '../services/insensitive.js';
4
- import type { PaginationMode } from '../services/pagination.js';
5
- import type { OrderMode } from '../services/order.js';
1
+ import type { ParametersMode } from '../services/parameters';
2
+ import type { TransactionMode } from '../services/transaction';
3
+ import type { InsensitiveMode } from '../services/insensitive';
4
+ import type { PaginationMode } from '../services/pagination';
5
+ import type { OrderMode } from '../services/order';
6
+ import type { LockMode } from '../services/lock';
6
7
  export type DatabaseEngine = {
7
8
  parametersMode: ParametersMode;
8
9
  transactionMode: TransactionMode;
9
10
  insensitiveMode: InsensitiveMode;
10
11
  paginationMode: PaginationMode;
11
12
  orderMode: OrderMode;
13
+ lockMode: LockMode;
12
14
  name: string;
13
15
  };
@@ -1,4 +1,4 @@
1
- import type { Index } from '../services/indexes.js';
1
+ import type { Index } from '../services/indexes';
2
2
  export type TableIndex = {
3
3
  name: string;
4
4
  columns: string[];
@@ -1,4 +1,4 @@
1
- import type { Index } from '../services/indexes.js';
1
+ import type { Index } from '../services/indexes';
2
2
  export type TableRelation = {
3
3
  sourceTable: string;
4
4
  sourceColumn: string;
@@ -1,7 +1,7 @@
1
1
  import type { ServiceMetadata } from '@ez4/project/library';
2
- import type { DatabaseScalability } from './scalability.js';
3
- import type { DatabaseEngine } from './engine.js';
4
- import type { DatabaseTable } from './table.js';
2
+ import type { DatabaseScalability } from './scalability';
3
+ import type { DatabaseEngine } from './engine';
4
+ import type { DatabaseTable } from './table';
5
5
  export declare const ServiceType = "@ez4/database";
6
6
  export type DatabaseService = ServiceMetadata & {
7
7
  type: typeof ServiceType;
@@ -1,6 +1,6 @@
1
1
  import type { LinkedVariables } from '@ez4/project/library';
2
2
  import type { ServiceListener } from '@ez4/common/library';
3
- import type { StreamHandler } from './handler.js';
3
+ import type { StreamHandler } from './handler';
4
4
  export type TableStream = {
5
5
  listener?: ServiceListener;
6
6
  handler: StreamHandler;
@@ -1,7 +1,7 @@
1
- import type { TableRelation } from './relations.js';
2
- import type { TableIndex } from './indexes.js';
3
- import type { TableSchema } from './schema.js';
4
- import type { TableStream } from './stream.js';
1
+ import type { TableRelation } from './relations';
2
+ import type { TableIndex } from './indexes';
3
+ import type { TableSchema } from './schema';
4
+ import type { TableStream } from './stream';
5
5
  export type DatabaseTable = {
6
6
  name: string;
7
7
  schema: TableSchema;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ez4/database",
3
3
  "description": "EZ4: Components to build database services",
4
- "version": "0.26.0",
4
+ "version": "0.28.0",
5
5
  "author": "Silas B.",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "git+https://github.com/sbalmt/ez4.git",
14
- "directory": "packages/database"
14
+ "directory": "contracts/database"
15
15
  },
16
16
  "engines": {
17
17
  "node": ">=22.7"
@@ -29,24 +29,23 @@
29
29
  }
30
30
  },
31
31
  "workspaces": [
32
- "packages/*"
32
+ "foundation/*",
33
+ "contracts/*"
33
34
  ],
34
35
  "scripts": {
35
36
  "lint": "eslint --cache",
36
37
  "clean": "rm -f *.tsbuildinfo && rm -rf dist/*",
37
- "build": "tsc -p tsconfig.json && node tools/bundler.mjs",
38
- "test": "npm run test:types && node --test --import ../../tools/tsnode.mjs test/*.spec.ts",
39
- "test:only": "npm run test:types && node --test-only --test --import ../../tools/tsnode.mjs test/*.spec.ts",
40
- "test:types": "npm run build && tsc -p tsconfig.test.json",
38
+ "build": "tsc && node tools/bundler.mjs",
39
+ "test": "npm run build && tsc -p tsconfig.test.json && ez4 test",
41
40
  "local:publish": "npm run build && npm run clean:registry && npm publish --registry http://localhost:4873",
42
41
  "clean:registry": "rm -rf ../../.registry/@ez4/database",
43
42
  "live:publish": "npm run build && npm publish --access public"
44
43
  },
45
44
  "dependencies": {
46
- "@ez4/common": "^0.26.0",
47
- "@ez4/project": "^0.26.0",
48
- "@ez4/reflection": "^0.26.0",
49
- "@ez4/schema": "^0.26.0",
50
- "@ez4/utils": "^0.26.0"
45
+ "@ez4/common": "^0.28.0",
46
+ "@ez4/project": "^0.28.0",
47
+ "@ez4/reflection": "^0.28.0",
48
+ "@ez4/schema": "^0.28.0",
49
+ "@ez4/utils": "^0.28.0"
51
50
  }
52
51
  }