@ez4/database 0.10.0 → 0.11.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/main.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './services/streams.js';
2
2
  export { Client } from './services/client.js';
3
- export { Database } from './services/database.js';
3
+ export { RelationMetadata } from './services/relations.js';
4
4
  export { Transaction } from './services/transaction.js';
5
- export { Relations } from './services/relations.js';
5
+ export { Database } from './services/database.js';
6
6
  export { Index } from './services/indexes.js';
7
7
  export { Table } from './services/table.js';
8
8
  export { Query } from './services/query.js';
@@ -1,93 +1,115 @@
1
- import type { Relations } from './relations.js';
1
+ import type { RelationMetadata } from './relations.js';
2
2
  import type { Database } from './database.js';
3
3
  import type { Order } from './order.js';
4
4
  import type { DecomposeIndexName, DecomposePrimaryIndexNames, DecomposeUniqueIndexNames } from './indexes.js';
5
- import type { AnyObject, PartialProperties, PartialObject, DeepPartial, FlatObject, IsObject, StrictType } from '@ez4/utils';
5
+ import type { AnyObject, PartialProperties, PartialObject, FlatObject, OptionalObject, StrictObject, IsNullable, IsObjectEmpty, IsObject } from '@ez4/utils';
6
6
  /**
7
7
  * Query builder types.
8
8
  */
9
9
  export declare namespace Query {
10
- export type InsertOneInput<T extends Database.Schema, R extends Relations> = {
11
- data: Omit<T, R['indexes']> & R['changes'];
12
- };
13
- export type UpdateOneInput<T extends Database.Schema, S extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
14
- select?: StrictType<SelectInput<T, R>, S>;
15
- data: DeepPartial<Omit<T, R['indexes']> & FlatObject<R['changes']>>;
16
- where: WhereInput<T, I>;
17
- };
18
- export type FindOneInput<T extends Database.Schema, S extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
19
- select: StrictType<SelectInput<T, R>, S>;
20
- where: WhereInput<T, I>;
21
- };
22
- export type UpsertOneInput<T extends Database.Schema, S extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
23
- select?: StrictType<SelectInput<T, R>, S>;
24
- insert: Omit<T, R['indexes']> & R['changes'];
25
- update: DeepPartial<Omit<T, R['indexes']> & FlatObject<R['changes']>>;
26
- where: WhereInput<T, I>;
27
- };
28
- export type DeleteOneInput<T extends Database.Schema, S extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
29
- select?: StrictType<SelectInput<T, R>, S>;
30
- where: WhereInput<T, I>;
31
- };
32
- export type InsertManyInput<T extends Database.Schema> = {
10
+ export type InsertOneInput<T extends Database.Schema, R extends RelationMetadata> = {
11
+ data: InsertDataInput<T, R>;
12
+ };
13
+ export type UpdateOneInput<T extends Database.Schema, S extends AnyObject, I extends Database.Indexes<T>, R extends RelationMetadata> = {
14
+ select?: StrictSelectInput<T, S, R>;
15
+ include?: StrictIncludeInput<S, R>;
16
+ data: OptionalObject<UpdateDataInput<T, R>>;
17
+ where: WhereInput<T, I, R>;
18
+ };
19
+ export type FindOneInput<T extends Database.Schema, S extends AnyObject, I extends Database.Indexes<T>, R extends RelationMetadata> = {
20
+ select: StrictSelectInput<T, S, R>;
21
+ include?: StrictIncludeInput<S, R>;
22
+ where: WhereInput<T, I, R>;
23
+ };
24
+ export type UpsertOneInput<T extends Database.Schema, S extends AnyObject, I extends Database.Indexes<T>, R extends RelationMetadata> = {
25
+ select?: StrictSelectInput<T, S, R>;
26
+ include?: StrictIncludeInput<S, R>;
27
+ update: OptionalObject<UpdateDataInput<T, R>>;
28
+ insert: InsertDataInput<T, R>;
29
+ where: WhereInput<T, I, R>;
30
+ };
31
+ export type DeleteOneInput<T extends Database.Schema, S extends AnyObject, I extends Database.Indexes<T>, R extends RelationMetadata> = {
32
+ select?: StrictSelectInput<T, S, R>;
33
+ include?: StrictIncludeInput<S, R>;
34
+ where: WhereInput<T, I, R>;
35
+ };
36
+ export type InsertManyInput<T extends Database.Schema = {}> = {
33
37
  data: T[];
34
38
  };
35
- export type UpdateManyInput<T extends Database.Schema, S extends Database.Schema, R extends Relations> = {
36
- select?: StrictType<SelectInput<T, R>, S>;
37
- data: DeepPartial<Omit<T, R['indexes']> & FlatObject<R['changes']>>;
38
- where?: WhereInput<T>;
39
+ export type UpdateManyInput<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = {
40
+ select?: StrictSelectInput<T, S, R>;
41
+ include?: StrictIncludeInput<S, R>;
42
+ data: OptionalObject<UpdateDataInput<T, R>>;
43
+ where?: WhereInput<T, {}, R>;
39
44
  limit?: number;
40
45
  };
41
- export type FindManyInput<T extends Database.Schema, S extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
42
- select: StrictType<SelectInput<T, R>, S>;
43
- where?: WhereInput<T>;
46
+ export type FindManyInput<T extends Database.Schema, S extends AnyObject, I extends Database.Indexes<T>, R extends RelationMetadata> = {
47
+ select: StrictSelectInput<T, S, R>;
48
+ include?: StrictIncludeInput<S, R>;
49
+ where?: WhereInput<T, {}, R>;
44
50
  order?: OrderInput<I>;
45
51
  cursor?: number | string;
46
52
  limit?: number;
47
53
  };
48
- export type DeleteManyInput<T extends Database.Schema, S extends Database.Schema, R extends Relations> = {
49
- select?: StrictType<SelectInput<T, R>, S>;
50
- where?: WhereInput<T>;
54
+ export type DeleteManyInput<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = {
55
+ select?: StrictSelectInput<T, S, R>;
56
+ include?: StrictIncludeInput<S, R>;
57
+ where?: WhereInput<T, {}, R>;
51
58
  limit?: number;
52
59
  };
53
60
  export type InsertOneResult = void;
54
- export type UpdateOneResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R> | undefined;
55
- export type FindOneResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R> | undefined;
56
- export type UpsertOneResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R> | undefined;
57
- export type DeleteOneResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R> | undefined;
61
+ export type UpdateOneResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R> | undefined;
62
+ export type FindOneResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R> | undefined;
63
+ export type UpsertOneResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R> | undefined;
64
+ export type DeleteOneResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R> | undefined;
58
65
  export type InsertManyResult = void;
59
- export type UpdateManyResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R>[];
60
- export type FindManyResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = {
66
+ export type UpdateManyResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R>[];
67
+ export type FindManyResult<T extends Database.Schema, S extends Database.Schema, R extends RelationMetadata> = {
61
68
  records: Record<T, S, R>[];
62
69
  cursor?: number | string;
63
70
  };
64
- export type DeleteManyResult<T extends Database.Schema, S extends AnyObject, R extends Relations> = Record<T, S, R>[];
65
- export type Record<T extends Database.Schema, S extends AnyObject, R extends Relations> = PartialObject<T & R['selects'], S, false>;
66
- export type SelectInput<T extends Database.Schema, R extends Relations> = PartialProperties<T & R['selects']>;
71
+ export type DeleteManyResult<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = Record<T, S, R>[];
72
+ export type Record<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = PartialObject<SelectFields<T, R>, S, false>;
73
+ export type SelectInput<T extends Database.Schema, R extends RelationMetadata> = PartialProperties<SelectFields<T, R>>;
74
+ export type StrictSelectInput<T extends Database.Schema, S extends AnyObject, R extends RelationMetadata> = StrictObject<S, FlatObject<SelectFields<T, R>>>;
75
+ export type InsertDataInput<T extends Database.Schema, R extends RelationMetadata> = Omit<IsObjectEmpty<R['changes']> extends true ? T : T & R['changes'], IndexFields<R>>;
76
+ export type UpdateDataInput<T extends Database.Schema, R extends RelationMetadata> = Omit<IsObjectEmpty<R['changes']> extends true ? T : T & FlatObject<R['changes']>, IndexFields<R>>;
77
+ export type StrictIncludeInput<S extends AnyObject, R extends RelationMetadata> = IsObjectEmpty<R['filters']> extends true ? never : IncludeFilters<R['filters'], S>;
67
78
  export type OrderInput<I extends Database.Indexes> = {
68
79
  [P in DecomposeIndexName<keyof I>]?: Order;
69
80
  };
70
- export type WhereInput<T extends Database.Schema, I extends Database.Indexes<T> = {}> = WhereFields<T, I> & WhereNot<T, I> & WhereAnd<T, I> & WhereOr<T, I>;
81
+ export type WhereInput<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> = WhereInputFilters<T, I, R> & WhereNot<WhereInputFilters<T, I, R>> & WhereAnd<WhereInputFilters<T, I, R>> & WhereOr<WhereInputFilters<T, I, R>>;
71
82
  export type WhereOperators = keyof (WhereNegate<any> & WhereEqual<any> & WhereGreaterThan<any> & WhereGreaterThanOrEqual<any> & WhereLessThan<any> & WhereLessThanOrEqual<any> & WhereIn<any> & WhereBetween<any> & WhereIsMissing & WhereIsNull & WhereStartsWith & WhereContains);
83
+ type IndexFields<R extends RelationMetadata> = string extends R['indexes'] ? never : R['indexes'];
84
+ type SelectFields<T extends Database.Schema, R extends RelationMetadata> = IsObjectEmpty<R['selects']> extends true ? T : T & R['selects'];
85
+ type IncludeFilters<T extends AnyObject, S extends AnyObject> = {
86
+ [P in keyof T]?: P extends keyof S ? IsObject<T[P]> extends true ? null | WhereRelationField<NonNullable<T[P]>> : never : never;
87
+ };
72
88
  type WhereOperations<T> = WhereNegate<T> | WhereEqual<T> | WhereGreaterThan<T> | WhereGreaterThanOrEqual<T> | WhereLessThan<T> | WhereLessThanOrEqual<T> | WhereIn<T> | WhereBetween<T> | WhereIsMissing | WhereIsNull | WhereStartsWith | WhereContains;
73
- type WherePrimaryFields<T extends Database.Schema, I extends Database.Indexes<T>> = {
74
- [P in DecomposePrimaryIndexNames<I>]: P extends keyof T ? IsObject<T[P]> extends true ? WhereFields<NonNullable<T[P]>, I> : T[P] | WhereOperations<T[P]> : never;
89
+ type WhereField<T> = IsObject<T> extends false ? T | WhereOperations<T> : IsNullable<T> extends true ? null | WhereObjectField<NonNullable<T>> : WhereObjectField<NonNullable<T>>;
90
+ type WhereObjectField<T extends AnyObject> = {
91
+ [P in keyof T]?: WhereField<T[P]>;
92
+ };
93
+ type WhereRelationField<T extends AnyObject> = WhereObjectField<T> & WhereNot<WhereObjectField<T>> & WhereAnd<WhereObjectField<T>> & WhereOr<WhereObjectField<T>>;
94
+ type WhereRelationFilters<T extends AnyObject> = {
95
+ [P in keyof T]?: IsObject<T[P]> extends true ? IsObjectEmpty<T[P]> extends false ? null | WhereRelationField<NonNullable<T[P]>> : null | {} : never;
75
96
  };
76
- type WhereUniqueFields<T extends Database.Schema, I extends Database.Indexes<T>> = {
77
- [P in DecomposeUniqueIndexNames<I>]: P extends keyof T ? IsObject<T[P]> extends true ? WhereFields<NonNullable<T[P]>, I> : T[P] | WhereOperations<T[P]> : never;
97
+ type WhereRequiredFilters<T extends AnyObject, N extends string> = {
98
+ [P in N as P extends keyof T ? P : never]: P extends keyof T ? WhereField<T[P]> : never;
78
99
  };
79
- type WhereOptionalFields<T extends Database.Schema, I extends Database.Indexes<T>, N> = {
80
- [P in Exclude<keyof T, N>]?: IsObject<T[P]> extends true ? WhereFields<NonNullable<T[P]>, I> : T[P] | WhereOperations<T[P]>;
100
+ type WhereOptionalFilters<T extends AnyObject, N extends string> = {
101
+ [P in Exclude<keyof T, N>]?: WhereField<T[P]>;
81
102
  };
82
- type WhereFields<T extends Database.Schema, I extends Database.Indexes<T>> = (WherePrimaryFields<T, I> & WhereOptionalFields<T, I, DecomposePrimaryIndexNames<I>>) | (WhereUniqueFields<T, I> & WhereOptionalFields<T, I, DecomposeUniqueIndexNames<I>>);
83
- type WhereNot<T extends Database.Schema, I extends Database.Indexes<T>> = {
84
- NOT?: WhereInput<T, I> | WhereAnd<T, I> | WhereOr<T, I>;
103
+ type WhereCommonFilters<T extends AnyObject, I extends Database.Indexes<T>> = (WhereRequiredFilters<T, DecomposePrimaryIndexNames<I>> & WhereOptionalFilters<T, DecomposePrimaryIndexNames<I>>) | (WhereRequiredFilters<T, DecomposeUniqueIndexNames<I>> & WhereOptionalFilters<T, DecomposeUniqueIndexNames<I>>);
104
+ type WhereInputFilters<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> = WhereCommonFilters<T, I> & WhereRelationFilters<R['filters']>;
105
+ type WhereNot<T extends AnyObject> = {
106
+ NOT?: T | WhereAnd<T> | WhereOr<T>;
85
107
  };
86
- type WhereAnd<T extends Database.Schema, I extends Database.Indexes<T>> = {
87
- AND?: (WhereInput<T, I> | WhereOr<T, I> | WhereNot<T, I>)[];
108
+ type WhereAnd<T extends AnyObject> = {
109
+ AND?: (T | WhereNot<T> | WhereAnd<T> | WhereOr<T>)[];
88
110
  };
89
- type WhereOr<T extends Database.Schema, I extends Database.Indexes<T>> = {
90
- OR?: (WhereInput<T, I> | WhereAnd<T, I> | WhereNot<T, I>)[];
111
+ type WhereOr<T extends AnyObject> = {
112
+ OR?: (T | WhereNot<T> | WhereAnd<T> | WhereOr<T>)[];
91
113
  };
92
114
  type WhereNegate<T> = {
93
115
  not: T | WhereOperations<T>;
@@ -1,14 +1,15 @@
1
- import type { AnyObject, ArrayRest, IsArrayEmpty, PropertyType, ExclusiveType } from '@ez4/utils';
2
1
  import type { IndexedTables, PrimaryIndexes, UniqueIndexes } from './indexes.js';
3
2
  import type { Database, DatabaseTables } from './database.js';
4
3
  import type { TableSchemas } from './schemas.js';
4
+ import type { AnyObject, ArrayRest, PropertyType, ExclusiveType, IsArrayEmpty, IsUndefined } from '@ez4/utils';
5
5
  /**
6
6
  * Internal relation type.
7
7
  */
8
- export type Relations = {
8
+ export type RelationMetadata = {
9
9
  indexes: string;
10
- selects: Record<string, any>;
11
- changes: Record<string, any>;
10
+ filters: Record<string, unknown>;
11
+ selects: Record<string, unknown>;
12
+ changes: Record<string, unknown>;
12
13
  };
13
14
  /**
14
15
  * Given a relation source name `T`, it produces the source table name.
@@ -43,33 +44,40 @@ type TableRelation<T, S extends Record<string, Database.Schema>, I extends Recor
43
44
  relations: infer R;
44
45
  } ? N extends string ? R extends AnyObject ? {
45
46
  [P in N]: {
46
- indexes: keyof RelationIndexes<PropertyType<N, S>, I, R>;
47
+ indexes: RequiredRelationIndexes<PropertyType<N, S>, I, R>;
48
+ filters: FilterableRelationSchemas<S, I, R>;
47
49
  changes: RequiredRelationSchemas<PropertyType<N, S>, S, I, R, true> & OptionalRelationSchemas<PropertyType<N, S>, S, I, R, true>;
48
50
  selects: RequiredRelationSchemas<PropertyType<N, S>, S, I, R, false> & OptionalRelationSchemas<PropertyType<N, S>, S, I, R, false>;
49
51
  };
50
52
  } : {} : {} : {};
53
+ /**
54
+ * Produce an object containing all required relation indexes.
55
+ */
56
+ type RequiredRelationIndexes<T extends Database.Schema, I extends Record<string, Database.Indexes>, R extends AnyObject> = keyof {
57
+ [C in keyof R as IsOptionalRelation<C, R[C], T, I, true> extends true ? never : RelationTargetColumn<R[C]>]: never;
58
+ };
59
+ /**
60
+ * Produce an object containing all filterable relation schemas.
61
+ */
62
+ type FilterableRelationSchemas<S extends Record<string, Database.Schema>, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
63
+ [C in keyof R as RelationTargetAlias<R[C]>]: IsPrimaryIndex<C, I> extends false ? Omit<PropertyType<RelationSourceTable<C>, S>, RelationSourceColumn<C>> : PropertyType<RelationSourceTable<C>, S>;
64
+ };
51
65
  /**
52
66
  * Produce an object containing only required relation schemas.
53
67
  */
54
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> = {
55
- [C in keyof R as IsOptionalRelation<C, R[C], T, I> extends true ? never : RelationTargetAlias<R[C]>]: RelationSchema<C, R[C], T, S, I, E>;
69
+ [C in keyof R as IsOptionalRelation<C, R[C], T, I, E> extends false ? RelationTargetAlias<R[C]> : never]: RelationSchema<C, R[C], T, S, I, E>;
56
70
  };
57
71
  /**
58
72
  * Produce an object containing only optional relation schemas.
59
73
  */
60
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> = {
61
- [C in keyof R as IsOptionalRelation<C, R[C], T, I> extends true ? RelationTargetAlias<R[C]> : never]?: RelationSchema<C, R[C], T, S, I, E>;
62
- };
63
- /**
64
- * Produce an object containing all relation indexes.
65
- */
66
- type RelationIndexes<T extends Database.Schema, I extends Record<string, Database.Indexes>, R extends AnyObject> = {
67
- [C in keyof R as IsOptionalRelation<C, R[C], T, I> extends true ? never : RelationTargetColumn<R[C]>]: never;
75
+ [C in keyof R as IsOptionalRelation<C, R[C], T, I, E> extends true ? RelationTargetAlias<R[C]> : never]?: RelationSchema<C, R[C], T, S, I, E>;
68
76
  };
69
77
  /**
70
78
  * Check whether a relation is optional or not.
71
79
  */
72
- type IsOptionalRelation<C, V, T extends Database.Schema, I extends Record<string, Database.Indexes>> = RelationSourceColumn<C> extends keyof PrimaryIndexes<PropertyType<RelationSourceTable<C>, I>> ? undefined extends PropertyType<RelationTargetColumn<V>, T> ? true : false : true;
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>> extends true ? true : false : RelationSourceColumn<C> extends keyof UniqueIndexes<PropertyType<RelationSourceTable<C>, I>> ? true : E;
73
81
  /**
74
82
  * Check whether a column is primary.
75
83
  */
@@ -1,5 +1,5 @@
1
1
  import type { AnyObject, ArrayRest, IsArrayEmpty, IsAny, PropertyExists } from '@ez4/utils';
2
- import type { Relations, RelationTables } from './relations.js';
2
+ import type { RelationMetadata, RelationTables } from './relations.js';
3
3
  import type { IndexedTables } from './indexes.js';
4
4
  import type { TableSchemas } from './schemas.js';
5
5
  import type { Database } from './database.js';
@@ -14,18 +14,10 @@ export type TableTypes<T extends Database.Schema[]> = IsAny<T> extends true ? an
14
14
  */
15
15
  export type TableIndex<P, T extends AnyObject> = PropertyExists<P, T> extends true ? (T[P] extends Database.Indexes ? T[P] : {}) : {};
16
16
  /**
17
- * Given a table `T` and a property `P`, it returns all the relations corresponding
18
- * to the given property.
17
+ * Given a table `T` and a property `P`, it returns all the relations corresponding to the
18
+ * given property.
19
19
  */
20
- export type TableRelation<P, T extends AnyObject> = PropertyExists<P, T> extends true ? T[P] extends Relations ? T[P] : {
21
- indexes: never;
22
- selects: {};
23
- changes: {};
24
- } : {
25
- indexes: never;
26
- selects: {};
27
- changes: {};
28
- };
20
+ export type TableRelation<P, T extends AnyObject> = PropertyExists<P, T> extends true ? T[P] extends RelationMetadata ? T[P] : RelationMetadata : RelationMetadata;
29
21
  /**
30
22
  * Given a database service `T`, it returns all table clients.
31
23
  */
@@ -35,7 +27,7 @@ export type TableClients<T extends Database.Service<any>> = {
35
27
  /**
36
28
  * Table client.
37
29
  */
38
- export interface Table<T extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> {
30
+ export interface Table<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> {
39
31
  /**
40
32
  * Insert one record into the database.
41
33
  *
@@ -1,4 +1,5 @@
1
- import type { Relations, RelationTables } from './relations.js';
1
+ import type { AnyObject } from '@ez4/utils';
2
+ import type { RelationMetadata, RelationTables } from './relations.js';
2
3
  import type { TableIndex, TableRelation } from './table.js';
3
4
  import type { IndexedTables } from './indexes.js';
4
5
  import type { TableSchemas } from './schemas.js';
@@ -12,18 +13,17 @@ export declare namespace Transaction {
12
13
  * Write operations.
13
14
  */
14
15
  export type WriteOperations<T extends Database.Service<any>> = {
15
- [P in keyof TableSchemas<T>]?: TableSchemas<T>[P] extends Database.Schema ? {
16
- [alias: string]: InsertOperation<TableSchemas<T>[P], TableRelation<P, RelationTables<T>>> | UpdateOperation<TableSchemas<T>[P], TableIndex<P, IndexedTables<T>>, TableRelation<P, RelationTables<T>>> | DeleteOperation<TableSchemas<T>[P], TableIndex<P, IndexedTables<T>>, TableRelation<P, RelationTables<T>>>;
17
- } : never;
16
+ [P in keyof TableSchemas<T>]?: (TableSchemas<T>[P] extends Database.Schema ? AnyOperation<TableSchemas<T>[P], TableIndex<P, IndexedTables<T>>, TableRelation<P, RelationTables<T>>> : AnyObject)[];
18
17
  };
19
- type InsertOperation<T extends Database.Schema, R extends Relations> = {
18
+ type AnyOperation<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> = InsertOperation<T, R> | UpdateOperation<T, I, R> | DeleteOperation<T, I, R>;
19
+ type InsertOperation<T extends Database.Schema, R extends RelationMetadata> = {
20
20
  insert: Query.InsertOneInput<T, R>;
21
21
  };
22
- type UpdateOperation<T extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
23
- update: Omit<Query.UpdateOneInput<T, Query.SelectInput<T, R>, I, R>, 'select'>;
22
+ type UpdateOperation<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> = {
23
+ update: Omit<Query.UpdateOneInput<T, Query.SelectInput<T, R>, I, R>, 'select' | 'include'>;
24
24
  };
25
- type DeleteOperation<T extends Database.Schema, I extends Database.Indexes<T>, R extends Relations> = {
26
- delete: Omit<Query.DeleteOneInput<T, Query.SelectInput<T, R>, I, R>, 'select'>;
25
+ type DeleteOperation<T extends Database.Schema, I extends Database.Indexes<T>, R extends RelationMetadata> = {
26
+ delete: Omit<Query.DeleteOneInput<T, Query.SelectInput<T, R>, I, R>, 'select' | 'include'>;
27
27
  };
28
28
  export {};
29
29
  }
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.10.0",
4
+ "version": "0.11.0",
5
5
  "author": "Silas B.",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -42,12 +42,12 @@
42
42
  "live:publish": "npm run test && npm publish --access public"
43
43
  },
44
44
  "peerDependencies": {
45
- "@ez4/project": "^0.10.0"
45
+ "@ez4/project": "^0.11.0"
46
46
  },
47
47
  "dependencies": {
48
- "@ez4/common": "^0.10.0",
49
- "@ez4/reflection": "^0.10.0",
50
- "@ez4/schema": "^0.10.0",
51
- "@ez4/utils": "^0.10.0"
48
+ "@ez4/common": "^0.11.0",
49
+ "@ez4/reflection": "^0.11.0",
50
+ "@ez4/schema": "^0.11.0",
51
+ "@ez4/utils": "^0.11.0"
52
52
  }
53
53
  }
@@ -1,10 +0,0 @@
1
- export declare namespace Schema {
2
- /**
3
- * Set property to be a primary index.
4
- */
5
- type Primary<T> = T;
6
- /**
7
- * Set property to be a regular index.
8
- */
9
- type Index<T> = T;
10
- }
@@ -1,104 +0,0 @@
1
- import type { EveryType, TypeObject } from '@ez4/reflection';
2
- export type RichTypes = {
3
- index?: string;
4
- type: EveryType;
5
- };
6
- export declare const getRichTypes: (type: TypeObject) => RichTypes | null;
7
- export declare const createRichType: (richType: RichTypes) => {
8
- extra: {
9
- index: "primary" | "regular";
10
- };
11
- type: import("@ez4/reflection").TypeName.Object;
12
- file?: string;
13
- members?: import("@ez4/reflection").EveryMemberType[];
14
- } | {
15
- extra: {
16
- index: "primary" | "regular";
17
- };
18
- type: import("@ez4/reflection").TypeName.Any;
19
- } | {
20
- extra: {
21
- index: "primary" | "regular";
22
- };
23
- type: import("@ez4/reflection").TypeName.Void;
24
- } | {
25
- extra: {
26
- index: "primary" | "regular";
27
- };
28
- type: import("@ez4/reflection").TypeName.Never;
29
- } | {
30
- extra: {
31
- index: "primary" | "regular";
32
- };
33
- type: import("@ez4/reflection").TypeName.Unknown;
34
- } | {
35
- extra: {
36
- index: "primary" | "regular";
37
- };
38
- type: import("@ez4/reflection").TypeName.Undefined;
39
- } | {
40
- extra: {
41
- index: "primary" | "regular";
42
- };
43
- type: import("@ez4/reflection").TypeName.Null;
44
- literal?: boolean;
45
- default?: boolean;
46
- } | {
47
- extra: {
48
- index: "primary" | "regular";
49
- };
50
- type: import("@ez4/reflection").TypeName.Boolean;
51
- literal?: boolean;
52
- default?: boolean;
53
- } | {
54
- extra: {
55
- index: "primary" | "regular";
56
- };
57
- type: import("@ez4/reflection").TypeName.Number;
58
- literal?: number;
59
- default?: number;
60
- } | {
61
- extra: {
62
- index: "primary" | "regular";
63
- };
64
- type: import("@ez4/reflection").TypeName.String;
65
- literal?: string;
66
- default?: string;
67
- } | {
68
- extra: {
69
- index: "primary" | "regular";
70
- };
71
- type: import("@ez4/reflection").TypeName.Reference;
72
- namespace?: string;
73
- internal?: boolean;
74
- path: string;
75
- } | {
76
- extra: {
77
- index: "primary" | "regular";
78
- };
79
- type: import("@ez4/reflection").TypeName.Union;
80
- elements: EveryType[];
81
- } | {
82
- extra: {
83
- index: "primary" | "regular";
84
- };
85
- type: import("@ez4/reflection").TypeName.Array;
86
- element: EveryType;
87
- spread?: boolean;
88
- } | {
89
- extra: {
90
- index: "primary" | "regular";
91
- };
92
- type: import("@ez4/reflection").TypeName.Tuple;
93
- elements: EveryType[];
94
- } | {
95
- extra: {
96
- index: "primary" | "regular";
97
- };
98
- type: import("@ez4/reflection").TypeName.Callback;
99
- name?: string;
100
- file?: string;
101
- description?: string;
102
- parameters?: import("@ez4/reflection").TypeParameter[];
103
- return?: EveryType;
104
- } | null;
@@ -1,12 +0,0 @@
1
- import type { ArrayRest, IsArrayEmpty, IsAny } from '@ez4/utils';
2
- import type { Database } from './database.js';
3
- /**
4
- * Given an array of schemas `T`, it returns an union of `Database.Table` for each schema.
5
- */
6
- export type TableTypes<T extends Database.Schema[]> = IsAny<T> extends true ? any : IsArrayEmpty<T> extends true ? Database.Table<Database.Schema> : Database.Table<T[0]> | TableTypes<ArrayRest<T>>;
7
- /**
8
- * Given a database service `T`, it returns all its table types.
9
- */
10
- export type DatabaseTables<T> = T extends {
11
- tables: infer U;
12
- } ? U : [];
@@ -1,34 +0,0 @@
1
- /**
2
- * Stream change for `insert`, `update` or `delete` operations.
3
- */
4
- export type StreamChange<T> = StreamInsertChange<T> | StreamUpdateChange<T> | StreamDeleteChange<T>;
5
- /**
6
- * Stream change for an `insert` operation.
7
- */
8
- export type StreamInsertChange<T> = {
9
- type: StreamType.Insert;
10
- record: T;
11
- };
12
- /**
13
- * Stream change for an `update` operation.
14
- */
15
- export type StreamUpdateChange<T> = {
16
- type: StreamType.Update;
17
- oldRecord: T;
18
- newRecord: T;
19
- };
20
- /**
21
- * Stream change for an `delete` operation.
22
- */
23
- export type StreamDeleteChange<T> = {
24
- type: StreamType.Delete;
25
- record: T;
26
- };
27
- /**
28
- * Stream change types.
29
- */
30
- export declare const enum StreamType {
31
- Insert = "insert",
32
- Update = "update",
33
- Delete = "delete"
34
- }
@@ -1,100 +0,0 @@
1
- import type { TypeObject } from '@ez4/reflection';
2
- export declare const applyRichTypePath: (file: string) => string | null;
3
- export declare const applyRichTypeObject: (type: TypeObject) => {
4
- extra: {
5
- index: "primary" | "regular";
6
- };
7
- type: import("@ez4/reflection").TypeName.Object;
8
- file?: string;
9
- members?: import("@ez4/reflection").EveryMemberType[];
10
- } | {
11
- extra: {
12
- index: "primary" | "regular";
13
- };
14
- type: import("@ez4/reflection").TypeName.Any;
15
- } | {
16
- extra: {
17
- index: "primary" | "regular";
18
- };
19
- type: import("@ez4/reflection").TypeName.Void;
20
- } | {
21
- extra: {
22
- index: "primary" | "regular";
23
- };
24
- type: import("@ez4/reflection").TypeName.Never;
25
- } | {
26
- extra: {
27
- index: "primary" | "regular";
28
- };
29
- type: import("@ez4/reflection").TypeName.Unknown;
30
- } | {
31
- extra: {
32
- index: "primary" | "regular";
33
- };
34
- type: import("@ez4/reflection").TypeName.Undefined;
35
- } | {
36
- extra: {
37
- index: "primary" | "regular";
38
- };
39
- type: import("@ez4/reflection").TypeName.Null;
40
- literal?: boolean;
41
- default?: boolean;
42
- } | {
43
- extra: {
44
- index: "primary" | "regular";
45
- };
46
- type: import("@ez4/reflection").TypeName.Boolean;
47
- literal?: boolean;
48
- default?: boolean;
49
- } | {
50
- extra: {
51
- index: "primary" | "regular";
52
- };
53
- type: import("@ez4/reflection").TypeName.Number;
54
- literal?: number;
55
- default?: number;
56
- } | {
57
- extra: {
58
- index: "primary" | "regular";
59
- };
60
- type: import("@ez4/reflection").TypeName.String;
61
- literal?: string;
62
- default?: string;
63
- } | {
64
- extra: {
65
- index: "primary" | "regular";
66
- };
67
- type: import("@ez4/reflection").TypeName.Reference;
68
- namespace?: string;
69
- internal?: boolean;
70
- path: string;
71
- } | {
72
- extra: {
73
- index: "primary" | "regular";
74
- };
75
- type: import("@ez4/reflection").TypeName.Union;
76
- elements: import("@ez4/reflection").EveryType[];
77
- } | {
78
- extra: {
79
- index: "primary" | "regular";
80
- };
81
- type: import("@ez4/reflection").TypeName.Array;
82
- element: import("@ez4/reflection").EveryType;
83
- spread?: boolean;
84
- } | {
85
- extra: {
86
- index: "primary" | "regular";
87
- };
88
- type: import("@ez4/reflection").TypeName.Tuple;
89
- elements: import("@ez4/reflection").EveryType[];
90
- } | {
91
- extra: {
92
- index: "primary" | "regular";
93
- };
94
- type: import("@ez4/reflection").TypeName.Callback;
95
- name?: string;
96
- file?: string;
97
- description?: string;
98
- parameters?: import("@ez4/reflection").TypeParameter[];
99
- return?: import("@ez4/reflection").EveryType;
100
- } | null;