@prisma-next/mongo-orm 0.3.0-dev.135 → 0.3.0-dev.162

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/src/types.ts CHANGED
@@ -1,25 +1,26 @@
1
- import type { DomainReferenceRelation } from '@prisma-next/contract/types';
1
+ import type { ContractReferenceRelation } from '@prisma-next/contract/types';
2
2
  import type {
3
3
  ExtractMongoCodecTypes,
4
+ ExtractMongoFieldInputTypes,
5
+ ExtractMongoFieldOutputTypes,
4
6
  InferModelRow,
5
7
  MongoContract,
6
8
  MongoContractWithTypeMaps,
7
- MongoQueryPlan,
8
9
  MongoTypeMaps,
9
- } from '@prisma-next/mongo-core';
10
- import type { AsyncIterableResult } from '@prisma-next/runtime-executor';
11
-
12
- export interface MongoQueryExecutor {
13
- execute<Row>(plan: MongoQueryPlan<Row>): AsyncIterableResult<Row>;
14
- }
15
-
16
- export interface MongoOrmOptions<TContract extends MongoContract> {
17
- readonly contract: TContract;
18
- readonly executor: MongoQueryExecutor;
19
- }
10
+ } from '@prisma-next/mongo-contract';
20
11
 
21
12
  type Simplify<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
22
13
 
14
+ export type SimplifyDeep<T> = T extends readonly (infer E)[]
15
+ ? SimplifyDeep<E>[]
16
+ : T extends Date | RegExp | ((...args: never[]) => unknown)
17
+ ? T
18
+ : T extends object
19
+ ? T extends unknown
20
+ ? { [K in keyof T]: SimplifyDeep<T[K]> }
21
+ : never
22
+ : T;
23
+
23
24
  type ModelRelations<
24
25
  TContract extends MongoContract,
25
26
  ModelName extends string & keyof TContract['models'],
@@ -32,7 +33,7 @@ export type ReferenceRelationKeys<
32
33
  [K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<
33
34
  TContract,
34
35
  ModelName
35
- >[K] extends DomainReferenceRelation
36
+ >[K] extends ContractReferenceRelation
36
37
  ? K
37
38
  : never;
38
39
  }[keyof ModelRelations<TContract, ModelName>];
@@ -44,11 +45,33 @@ export type EmbedRelationKeys<
44
45
  [K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<
45
46
  TContract,
46
47
  ModelName
47
- >[K] extends DomainReferenceRelation
48
+ >[K] extends ContractReferenceRelation
48
49
  ? never
49
50
  : K;
50
51
  }[keyof ModelRelations<TContract, ModelName>];
51
52
 
53
+ type ResolvedOutputRow<
54
+ TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
55
+ ModelName extends string & keyof TContract['models'],
56
+ > = string extends keyof ExtractMongoFieldOutputTypes<TContract>
57
+ ? InferModelRow<TContract, ModelName>
58
+ : ModelName extends keyof ExtractMongoFieldOutputTypes<TContract>
59
+ ? {
60
+ -readonly [K in keyof ExtractMongoFieldOutputTypes<TContract>[ModelName]]: ExtractMongoFieldOutputTypes<TContract>[ModelName][K];
61
+ }
62
+ : InferModelRow<TContract, ModelName>;
63
+
64
+ type ResolvedInputRow<
65
+ TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
66
+ ModelName extends string & keyof TContract['models'],
67
+ > = string extends keyof ExtractMongoFieldInputTypes<TContract>
68
+ ? InferModelRow<TContract, ModelName>
69
+ : ModelName extends keyof ExtractMongoFieldInputTypes<TContract>
70
+ ? {
71
+ -readonly [K in keyof ExtractMongoFieldInputTypes<TContract>[ModelName]]: ExtractMongoFieldInputTypes<TContract>[ModelName][K];
72
+ }
73
+ : InferModelRow<TContract, ModelName>;
74
+
52
75
  type EmbedRelationRowType<
53
76
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
54
77
  ModelName extends string & keyof TContract['models'],
@@ -57,19 +80,19 @@ type EmbedRelationRowType<
57
80
  readonly to: infer To extends string & keyof TContract['models'];
58
81
  readonly cardinality: infer C;
59
82
  }
60
- ? ModelRelations<TContract, ModelName>[RelKey] extends DomainReferenceRelation
83
+ ? ModelRelations<TContract, ModelName>[RelKey] extends ContractReferenceRelation
61
84
  ? never
62
85
  : C extends '1:N'
63
- ? InferModelRow<TContract, To>[]
64
- : InferModelRow<TContract, To>
86
+ ? ResolvedOutputRow<TContract, To>[]
87
+ : ResolvedOutputRow<TContract, To>
65
88
  : never;
66
89
 
67
90
  export type InferFullRow<
68
91
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
69
92
  ModelName extends string & keyof TContract['models'],
70
93
  > = EmbedRelationKeys<TContract, ModelName> extends never
71
- ? InferModelRow<TContract, ModelName>
72
- : InferModelRow<TContract, ModelName> & {
94
+ ? ResolvedOutputRow<TContract, ModelName>
95
+ : ResolvedOutputRow<TContract, ModelName> & {
73
96
  -readonly [K in EmbedRelationKeys<TContract, ModelName> &
74
97
  keyof ModelRelations<TContract, ModelName>]: EmbedRelationRowType<TContract, ModelName, K>;
75
98
  };
@@ -99,11 +122,39 @@ export type InferRootRow<
99
122
  ModelName extends string & keyof TContract['models'],
100
123
  > = VariantRow<TContract, ModelName>;
101
124
 
125
+ export type VariantNames<
126
+ TContract extends MongoContract,
127
+ ModelName extends string & keyof TContract['models'],
128
+ > = TContract['models'][ModelName] extends {
129
+ readonly variants: infer V extends Record<string, unknown>;
130
+ }
131
+ ? keyof V & string
132
+ : never;
133
+
134
+ export type VariantModelRow<
135
+ TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
136
+ ModelName extends string & keyof TContract['models'],
137
+ VariantName extends string,
138
+ > = TContract['models'][ModelName] extends {
139
+ readonly discriminator: { readonly field: infer DiscField extends string };
140
+ readonly variants: infer V;
141
+ }
142
+ ? V extends Record<string, { readonly value: string }>
143
+ ? VariantName extends keyof V & string & keyof TContract['models']
144
+ ? Simplify<
145
+ Omit<InferFullRow<TContract, ModelName>, DiscField> &
146
+ InferFullRow<TContract, VariantName> &
147
+ Record<DiscField, V[VariantName]['value']>
148
+ >
149
+ : InferFullRow<TContract, ModelName>
150
+ : InferFullRow<TContract, ModelName>
151
+ : InferFullRow<TContract, ModelName>;
152
+
102
153
  type IncludeRelationRowType<
103
154
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
104
155
  ModelName extends string & keyof TContract['models'],
105
156
  RelKey extends keyof ModelRelations<TContract, ModelName>,
106
- > = ModelRelations<TContract, ModelName>[RelKey] extends DomainReferenceRelation
157
+ > = ModelRelations<TContract, ModelName>[RelKey] extends ContractReferenceRelation
107
158
  ? ModelRelations<TContract, ModelName>[RelKey] extends {
108
159
  readonly to: infer To extends string & keyof TContract['models'];
109
160
  readonly cardinality: infer C;
@@ -132,7 +183,10 @@ export type MongoWhereFilter<
132
183
  TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
133
184
  > = {
134
185
  readonly [K in keyof TContract['models'][ModelName]['fields']]?: TContract['models'][ModelName]['fields'][K] extends {
135
- readonly codecId: infer CId extends string & keyof TCodecTypes;
186
+ readonly type: {
187
+ readonly kind: 'scalar';
188
+ readonly codecId: infer CId extends string & keyof TCodecTypes;
189
+ };
136
190
  }
137
191
  ? TCodecTypes[CId]['output']
138
192
  : unknown;
@@ -145,31 +199,62 @@ export type MongoIncludeSpec<
145
199
  readonly [K in ReferenceRelationKeys<TContract, ModelName>]?: true;
146
200
  };
147
201
 
148
- export interface MongoFindManyOptions<
202
+ export type NoIncludes = Pick<Record<string, boolean>, never>;
203
+
204
+ export type IncludedRow<
149
205
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
150
206
  ModelName extends string & keyof TContract['models'],
151
- TInclude extends MongoIncludeSpec<TContract, ModelName> = Record<string, never>,
152
- > {
153
- readonly where?: MongoWhereFilter<TContract, ModelName>;
154
- readonly include?: TInclude;
155
- }
207
+ TIncludes extends MongoIncludeSpec<TContract, ModelName> = NoIncludes,
208
+ > = InferRootRow<TContract, ModelName> & IncludeResultFields<TContract, ModelName, TIncludes>;
156
209
 
157
- export type MongoOrmClient<
210
+ export type DefaultModelRow<
158
211
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
159
- > = {
160
- readonly [K in keyof TContract['roots']]: TContract['roots'][K] extends string &
161
- keyof TContract['models']
162
- ? MongoCollection<TContract, TContract['roots'][K]>
163
- : never;
164
- };
212
+ ModelName extends string & keyof TContract['models'],
213
+ > = ResolvedOutputRow<TContract, ModelName>;
165
214
 
166
- export interface MongoCollection<
215
+ export type CreateInput<
167
216
  TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
168
217
  ModelName extends string & keyof TContract['models'],
169
- > {
170
- findMany<TInclude extends MongoIncludeSpec<TContract, ModelName> = Record<string, never>>(
171
- options?: MongoFindManyOptions<TContract, ModelName, TInclude>,
172
- ): AsyncIterableResult<
173
- InferRootRow<TContract, ModelName> & IncludeResultFields<TContract, ModelName, TInclude>
218
+ > = Omit<ResolvedInputRow<TContract, ModelName>, '_id'> &
219
+ Partial<
220
+ Pick<
221
+ ResolvedInputRow<TContract, ModelName>,
222
+ '_id' & keyof ResolvedInputRow<TContract, ModelName>
223
+ >
174
224
  >;
225
+
226
+ type DiscriminatorField<
227
+ TContract extends MongoContract,
228
+ ModelName extends string & keyof TContract['models'],
229
+ > = TContract['models'][ModelName] extends {
230
+ readonly discriminator: { readonly field: infer F extends string };
175
231
  }
232
+ ? F
233
+ : never;
234
+
235
+ // TODO(TML-2229): VariantModelRow flows through ResolvedOutputRow, so variant
236
+ // domain fields use output types. Only the _id pick uses ResolvedInputRow.
237
+ // When input/output types diverge (parameterized codecs), this needs an
238
+ // input-side VariantModelRow.
239
+ export type VariantCreateInput<
240
+ TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
241
+ ModelName extends string & keyof TContract['models'],
242
+ VariantName extends string,
243
+ > = Omit<
244
+ VariantModelRow<TContract, ModelName, VariantName>,
245
+ '_id' | DiscriminatorField<TContract, ModelName>
246
+ > &
247
+ Partial<
248
+ Pick<
249
+ ResolvedInputRow<TContract, ModelName>,
250
+ '_id' & keyof ResolvedInputRow<TContract, ModelName>
251
+ >
252
+ >;
253
+
254
+ export type ResolvedCreateInput<
255
+ TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
256
+ ModelName extends string & keyof TContract['models'],
257
+ TVariant extends string,
258
+ > = [TVariant] extends [never]
259
+ ? CreateInput<TContract, ModelName>
260
+ : VariantCreateInput<TContract, ModelName, TVariant>;