@prisma-next/mongo-orm 0.11.0 → 0.12.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/index.d.mts +87 -51
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +21 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +28 -17
- package/src/collection.ts +37 -27
- package/src/field-accessor.ts +92 -35
- package/src/mongo-orm.ts +11 -6
- package/src/mongo-raw.ts +6 -5
- package/src/types.ts +56 -45
package/src/field-accessor.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ContractField,
|
|
3
|
+
ContractValueObject,
|
|
4
|
+
ContractValueObjectsMap,
|
|
5
|
+
} from '@prisma-next/contract/types';
|
|
2
6
|
import type {
|
|
3
7
|
ExtractMongoCodecTypes,
|
|
4
8
|
ExtractMongoFieldOutputTypes,
|
|
5
9
|
InferModelRow,
|
|
6
10
|
MongoContract,
|
|
7
11
|
MongoContractWithTypeMaps,
|
|
12
|
+
MongoModelsMap,
|
|
8
13
|
MongoTypeMaps,
|
|
9
14
|
} from '@prisma-next/mongo-contract';
|
|
10
15
|
import type { MongoValue } from '@prisma-next/mongo-value';
|
|
@@ -32,46 +37,85 @@ export interface FieldOperation {
|
|
|
32
37
|
|
|
33
38
|
type ScalarFieldKeys<
|
|
34
39
|
TContract extends MongoContract,
|
|
35
|
-
ModelName extends string & keyof TContract
|
|
40
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
36
41
|
> = {
|
|
37
|
-
[K in keyof TContract[
|
|
38
|
-
string]: TContract[
|
|
42
|
+
[K in keyof MongoModelsMap<TContract>[ModelName]['fields'] &
|
|
43
|
+
string]: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
39
44
|
readonly type: { readonly kind: 'scalar' };
|
|
40
45
|
}
|
|
41
46
|
? K
|
|
42
47
|
: never;
|
|
43
|
-
}[keyof TContract[
|
|
48
|
+
}[keyof MongoModelsMap<TContract>[ModelName]['fields'] & string];
|
|
44
49
|
|
|
45
50
|
type ValueObjectFieldKeys<
|
|
46
51
|
TContract extends MongoContract,
|
|
47
|
-
ModelName extends string & keyof TContract
|
|
52
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
48
53
|
> = {
|
|
49
|
-
[K in keyof TContract[
|
|
50
|
-
string]: TContract[
|
|
54
|
+
[K in keyof MongoModelsMap<TContract>[ModelName]['fields'] &
|
|
55
|
+
string]: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
51
56
|
readonly type: { readonly kind: 'valueObject'; readonly name: string };
|
|
52
57
|
}
|
|
53
58
|
? K
|
|
54
59
|
: never;
|
|
55
|
-
}[keyof TContract[
|
|
60
|
+
}[keyof MongoModelsMap<TContract>[ModelName]['fields'] & string];
|
|
56
61
|
|
|
57
62
|
type ResolvedModelRow<
|
|
58
63
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
59
|
-
ModelName extends string & keyof TContract
|
|
64
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
65
|
+
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
60
66
|
> = string extends keyof ExtractMongoFieldOutputTypes<TContract>
|
|
61
|
-
? InferModelRow<TContract, ModelName>
|
|
67
|
+
? InferModelRow<TContract, ModelName, MongoModelsMap<TContract>[ModelName]['fields'], TCodecTypes>
|
|
62
68
|
: ModelName extends keyof ExtractMongoFieldOutputTypes<TContract>
|
|
63
69
|
? {
|
|
64
70
|
-readonly [K in keyof ExtractMongoFieldOutputTypes<TContract>[ModelName]]: ExtractMongoFieldOutputTypes<TContract>[ModelName][K];
|
|
65
71
|
}
|
|
66
|
-
: InferModelRow<
|
|
72
|
+
: InferModelRow<
|
|
73
|
+
TContract,
|
|
74
|
+
ModelName,
|
|
75
|
+
MongoModelsMap<TContract>[ModelName]['fields'],
|
|
76
|
+
TCodecTypes
|
|
77
|
+
>;
|
|
67
78
|
|
|
68
79
|
type ResolveFieldType<
|
|
69
80
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
70
|
-
ModelName extends string & keyof TContract
|
|
71
|
-
K extends keyof TContract[
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
:
|
|
81
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
82
|
+
K extends keyof MongoModelsMap<TContract>[ModelName]['fields'] & string,
|
|
83
|
+
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
84
|
+
> = MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
85
|
+
readonly type: {
|
|
86
|
+
readonly kind: 'scalar';
|
|
87
|
+
readonly codecId: infer CId extends string & keyof TCodecTypes;
|
|
88
|
+
};
|
|
89
|
+
readonly many: true;
|
|
90
|
+
readonly nullable: true;
|
|
91
|
+
}
|
|
92
|
+
? readonly TCodecTypes[CId]['output'][] | null
|
|
93
|
+
: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
94
|
+
readonly type: {
|
|
95
|
+
readonly kind: 'scalar';
|
|
96
|
+
readonly codecId: infer CId extends string & keyof TCodecTypes;
|
|
97
|
+
};
|
|
98
|
+
readonly many: true;
|
|
99
|
+
}
|
|
100
|
+
? readonly TCodecTypes[CId]['output'][]
|
|
101
|
+
: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
102
|
+
readonly type: {
|
|
103
|
+
readonly kind: 'scalar';
|
|
104
|
+
readonly codecId: infer CId extends string & keyof TCodecTypes;
|
|
105
|
+
};
|
|
106
|
+
readonly nullable: true;
|
|
107
|
+
}
|
|
108
|
+
? TCodecTypes[CId]['output'] | null
|
|
109
|
+
: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
110
|
+
readonly type: {
|
|
111
|
+
readonly kind: 'scalar';
|
|
112
|
+
readonly codecId: infer CId extends string & keyof TCodecTypes;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
? TCodecTypes[CId]['output']
|
|
116
|
+
: K extends keyof ResolvedModelRow<TContract, ModelName, TCodecTypes>
|
|
117
|
+
? ResolvedModelRow<TContract, ModelName, TCodecTypes>[K]
|
|
118
|
+
: unknown;
|
|
75
119
|
|
|
76
120
|
type NumericOps = {
|
|
77
121
|
inc(value: number): FieldOperation;
|
|
@@ -87,13 +131,24 @@ export type FieldExpression<T = unknown> = {
|
|
|
87
131
|
pop(end: 1 | -1): FieldOperation;
|
|
88
132
|
} & (T extends number ? NumericOps : unknown);
|
|
89
133
|
|
|
90
|
-
type HasValueObjects =
|
|
134
|
+
type HasValueObjects = MongoContract;
|
|
91
135
|
|
|
92
|
-
type
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
136
|
+
type MergedContractValueObjects<TContract extends HasValueObjects> =
|
|
137
|
+
ContractValueObjectsMap<TContract> &
|
|
138
|
+
(TContract extends { readonly valueObjects?: infer VOs }
|
|
139
|
+
? VOs extends Record<string, ContractValueObject>
|
|
140
|
+
? VOs
|
|
141
|
+
: Record<string, never>
|
|
142
|
+
: Record<string, never>);
|
|
143
|
+
|
|
144
|
+
type VOFields<
|
|
145
|
+
TContract extends HasValueObjects,
|
|
146
|
+
VOName extends string,
|
|
147
|
+
> = VOName extends keyof MergedContractValueObjects<TContract>
|
|
148
|
+
? MergedContractValueObjects<TContract>[VOName] extends {
|
|
149
|
+
readonly fields: infer F extends Record<string, ContractField>;
|
|
150
|
+
}
|
|
151
|
+
? F
|
|
97
152
|
: never
|
|
98
153
|
: never;
|
|
99
154
|
|
|
@@ -127,12 +182,12 @@ type VODotPaths<
|
|
|
127
182
|
|
|
128
183
|
export type DotPath<
|
|
129
184
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
130
|
-
ModelName extends string & keyof TContract
|
|
185
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
131
186
|
> = {
|
|
132
187
|
[K in ValueObjectFieldKeys<
|
|
133
188
|
TContract,
|
|
134
189
|
ModelName
|
|
135
|
-
>]: TContract[
|
|
190
|
+
>]: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
136
191
|
readonly type: { readonly kind: 'valueObject'; readonly name: infer N extends string };
|
|
137
192
|
}
|
|
138
193
|
? VODotPaths<TContract, VOFields<TContract, N>, `${K}.`>
|
|
@@ -165,12 +220,12 @@ type ResolveDotPathInFields<
|
|
|
165
220
|
|
|
166
221
|
export type ResolveDotPathType<
|
|
167
222
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
168
|
-
ModelName extends string & keyof TContract
|
|
223
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
169
224
|
Path extends string,
|
|
170
225
|
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
171
226
|
> = Path extends `${infer Head}.${infer Rest}`
|
|
172
|
-
? Head extends keyof TContract[
|
|
173
|
-
? TContract[
|
|
227
|
+
? Head extends keyof MongoModelsMap<TContract>[ModelName]['fields'] & string
|
|
228
|
+
? MongoModelsMap<TContract>[ModelName]['fields'][Head] extends {
|
|
174
229
|
readonly type: { readonly kind: 'valueObject'; readonly name: infer N extends string };
|
|
175
230
|
}
|
|
176
231
|
? ResolveDotPathInFields<TContract, VOFields<TContract, N>, Rest, TCodecTypes>
|
|
@@ -180,18 +235,19 @@ export type ResolveDotPathType<
|
|
|
180
235
|
|
|
181
236
|
export type FieldAccessor<
|
|
182
237
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
183
|
-
ModelName extends string & keyof TContract
|
|
238
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
239
|
+
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
184
240
|
> = {
|
|
185
241
|
readonly [K in ScalarFieldKeys<TContract, ModelName>]: FieldExpression<
|
|
186
|
-
ResolveFieldType<TContract, ModelName, K>
|
|
242
|
+
ResolveFieldType<TContract, ModelName, K, TCodecTypes>
|
|
187
243
|
>;
|
|
188
244
|
} & {
|
|
189
245
|
readonly [K in ValueObjectFieldKeys<TContract, ModelName>]: FieldExpression<
|
|
190
|
-
ResolveFieldType<TContract, ModelName, K>
|
|
246
|
+
ResolveFieldType<TContract, ModelName, K, TCodecTypes>
|
|
191
247
|
>;
|
|
192
248
|
} & (<P extends DotPath<TContract, ModelName>>(
|
|
193
249
|
path: P,
|
|
194
|
-
) => FieldExpression<ResolveDotPathType<TContract, ModelName, P>>);
|
|
250
|
+
) => FieldExpression<ResolveDotPathType<TContract, ModelName, P, TCodecTypes>>);
|
|
195
251
|
|
|
196
252
|
// ── Runtime implementation ───────────────────────────────────────────────────
|
|
197
253
|
|
|
@@ -236,9 +292,10 @@ function createFieldExpression(fieldPath: string): RuntimeFieldExpression {
|
|
|
236
292
|
|
|
237
293
|
export function createFieldAccessor<
|
|
238
294
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
239
|
-
ModelName extends string & keyof TContract
|
|
240
|
-
|
|
241
|
-
|
|
295
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
296
|
+
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
297
|
+
>(): FieldAccessor<TContract, ModelName, TCodecTypes> {
|
|
298
|
+
return new Proxy((() => {}) as unknown as FieldAccessor<TContract, ModelName, TCodecTypes>, {
|
|
242
299
|
get(_target, prop: string): RuntimeFieldExpression {
|
|
243
300
|
return createFieldExpression(prop);
|
|
244
301
|
},
|
package/src/mongo-orm.ts
CHANGED
|
@@ -2,7 +2,9 @@ import type {
|
|
|
2
2
|
MongoContract,
|
|
3
3
|
MongoContractWithTypeMaps,
|
|
4
4
|
MongoTypeMaps,
|
|
5
|
+
RootModelName,
|
|
5
6
|
} from '@prisma-next/mongo-contract';
|
|
7
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
6
8
|
import type { MongoCollection } from './collection';
|
|
7
9
|
import { createMongoCollection } from './collection';
|
|
8
10
|
import type { MongoQueryExecutor } from './executor';
|
|
@@ -15,10 +17,10 @@ export interface MongoOrmOptions<TContract extends MongoContract> {
|
|
|
15
17
|
export type MongoOrmClient<
|
|
16
18
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
17
19
|
> = {
|
|
18
|
-
readonly [K in keyof TContract['roots']
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
readonly [K in keyof TContract['roots'] & string]: MongoCollection<
|
|
21
|
+
TContract,
|
|
22
|
+
RootModelName<TContract, K>
|
|
23
|
+
>;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
export function mongoOrm<TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>>(
|
|
@@ -27,10 +29,13 @@ export function mongoOrm<TContract extends MongoContractWithTypeMaps<MongoContra
|
|
|
27
29
|
const { contract, executor } = options;
|
|
28
30
|
const client: Record<string, unknown> = {};
|
|
29
31
|
|
|
30
|
-
for (const [rootName,
|
|
32
|
+
for (const [rootName, rootRef] of Object.entries(contract.roots)) {
|
|
31
33
|
client[rootName] = createMongoCollection(
|
|
32
34
|
contract,
|
|
33
|
-
|
|
35
|
+
blindCast<
|
|
36
|
+
RootModelName<TContract, typeof rootName & keyof TContract['roots'] & string>,
|
|
37
|
+
'roots entries are CrossReferences; rootRef.model is a valid RootModelName for this contract'
|
|
38
|
+
>(rootRef.model),
|
|
34
39
|
executor,
|
|
35
40
|
);
|
|
36
41
|
}
|
package/src/mongo-raw.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { contractModels, type PlanMeta } from '@prisma-next/contract/types';
|
|
2
2
|
import type { MongoContract, MongoModelDefinition } from '@prisma-next/mongo-contract';
|
|
3
3
|
import { createRawMongoCollection, type RawMongoCollection } from './raw-collection';
|
|
4
4
|
|
|
@@ -13,11 +13,12 @@ export function mongoRaw<TContract extends MongoContract>(options: {
|
|
|
13
13
|
|
|
14
14
|
return {
|
|
15
15
|
collection<K extends keyof TContract['roots'] & string>(rootName: K): RawMongoCollection {
|
|
16
|
-
const modelName = contract.roots[rootName]
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const modelName = contract.roots[rootName]?.model;
|
|
17
|
+
const models = contractModels(contract);
|
|
18
|
+
if (!modelName || !Object.hasOwn(models, modelName)) {
|
|
19
|
+
throw new Error(`Unknown model "${modelName ?? ''}" for root "${rootName}"`);
|
|
19
20
|
}
|
|
20
|
-
const model =
|
|
21
|
+
const model = models[modelName] as MongoModelDefinition;
|
|
21
22
|
const collectionName = model.storage.collection ?? modelName;
|
|
22
23
|
|
|
23
24
|
const meta: PlanMeta = {
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
InferModelRow,
|
|
7
7
|
MongoContract,
|
|
8
8
|
MongoContractWithTypeMaps,
|
|
9
|
+
MongoModelsMap,
|
|
9
10
|
MongoTypeMaps,
|
|
10
11
|
} from '@prisma-next/mongo-contract';
|
|
11
12
|
|
|
@@ -13,12 +14,12 @@ type Simplify<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
|
|
|
13
14
|
|
|
14
15
|
type ModelRelations<
|
|
15
16
|
TContract extends MongoContract,
|
|
16
|
-
ModelName extends string & keyof TContract
|
|
17
|
-
> = NonNullable<TContract[
|
|
17
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
18
|
+
> = NonNullable<MongoModelsMap<TContract>[ModelName]['relations']>;
|
|
18
19
|
|
|
19
20
|
export type ReferenceRelationKeys<
|
|
20
21
|
TContract extends MongoContract,
|
|
21
|
-
ModelName extends string & keyof TContract
|
|
22
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
22
23
|
> = {
|
|
23
24
|
[K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<
|
|
24
25
|
TContract,
|
|
@@ -30,7 +31,7 @@ export type ReferenceRelationKeys<
|
|
|
30
31
|
|
|
31
32
|
export type EmbedRelationKeys<
|
|
32
33
|
TContract extends MongoContract,
|
|
33
|
-
ModelName extends string & keyof TContract
|
|
34
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
34
35
|
> = {
|
|
35
36
|
[K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<
|
|
36
37
|
TContract,
|
|
@@ -42,7 +43,7 @@ export type EmbedRelationKeys<
|
|
|
42
43
|
|
|
43
44
|
type ResolvedOutputRow<
|
|
44
45
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
45
|
-
ModelName extends string & keyof TContract
|
|
46
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
46
47
|
> = string extends keyof ExtractMongoFieldOutputTypes<TContract>
|
|
47
48
|
? InferModelRow<TContract, ModelName>
|
|
48
49
|
: ModelName extends keyof ExtractMongoFieldOutputTypes<TContract>
|
|
@@ -53,7 +54,7 @@ type ResolvedOutputRow<
|
|
|
53
54
|
|
|
54
55
|
type ResolvedInputRow<
|
|
55
56
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
56
|
-
ModelName extends string & keyof TContract
|
|
57
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
57
58
|
> = string extends keyof ExtractMongoFieldInputTypes<TContract>
|
|
58
59
|
? InferModelRow<TContract, ModelName>
|
|
59
60
|
: ModelName extends keyof ExtractMongoFieldInputTypes<TContract>
|
|
@@ -62,24 +63,32 @@ type ResolvedInputRow<
|
|
|
62
63
|
}
|
|
63
64
|
: InferModelRow<TContract, ModelName>;
|
|
64
65
|
|
|
66
|
+
type RelationTargetModel<TContract extends MongoContract, R> = R extends {
|
|
67
|
+
readonly to: { readonly model: infer M extends string & keyof MongoModelsMap<TContract> };
|
|
68
|
+
}
|
|
69
|
+
? M
|
|
70
|
+
: never;
|
|
71
|
+
|
|
65
72
|
type EmbedRelationRowType<
|
|
66
73
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
67
|
-
ModelName extends string & keyof TContract
|
|
74
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
68
75
|
RelKey extends keyof ModelRelations<TContract, ModelName>,
|
|
69
|
-
> = ModelRelations<TContract, ModelName>[RelKey] extends
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
> = ModelRelations<TContract, ModelName>[RelKey] extends infer R
|
|
77
|
+
? R extends { readonly cardinality: infer C }
|
|
78
|
+
? ModelRelations<TContract, ModelName>[RelKey] extends ContractReferenceRelation
|
|
79
|
+
? never
|
|
80
|
+
: RelationTargetModel<TContract, R> extends infer To extends string &
|
|
81
|
+
keyof MongoModelsMap<TContract>
|
|
82
|
+
? C extends '1:N'
|
|
83
|
+
? ResolvedOutputRow<TContract, To>[]
|
|
84
|
+
: ResolvedOutputRow<TContract, To>
|
|
85
|
+
: never
|
|
86
|
+
: never
|
|
78
87
|
: never;
|
|
79
88
|
|
|
80
89
|
export type InferFullRow<
|
|
81
90
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
82
|
-
ModelName extends string & keyof TContract
|
|
91
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
83
92
|
> =
|
|
84
93
|
EmbedRelationKeys<TContract, ModelName> extends never
|
|
85
94
|
? ResolvedOutputRow<TContract, ModelName>
|
|
@@ -94,14 +103,14 @@ export type InferFullRow<
|
|
|
94
103
|
|
|
95
104
|
type VariantRow<
|
|
96
105
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
97
|
-
ModelName extends string & keyof TContract
|
|
98
|
-
> = TContract[
|
|
106
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
107
|
+
> = MongoModelsMap<TContract>[ModelName] extends {
|
|
99
108
|
readonly discriminator: { readonly field: infer DiscField extends string };
|
|
100
109
|
readonly variants: infer V;
|
|
101
110
|
}
|
|
102
111
|
? V extends Record<string, { readonly value: string }>
|
|
103
112
|
? {
|
|
104
|
-
[VK in keyof V]: VK extends string & keyof TContract
|
|
113
|
+
[VK in keyof V]: VK extends string & keyof MongoModelsMap<TContract>
|
|
105
114
|
? Simplify<
|
|
106
115
|
Omit<InferFullRow<TContract, ModelName>, DiscField> &
|
|
107
116
|
InferFullRow<TContract, VK> &
|
|
@@ -114,13 +123,13 @@ type VariantRow<
|
|
|
114
123
|
|
|
115
124
|
export type InferRootRow<
|
|
116
125
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
117
|
-
ModelName extends string & keyof TContract
|
|
126
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
118
127
|
> = VariantRow<TContract, ModelName>;
|
|
119
128
|
|
|
120
129
|
export type VariantNames<
|
|
121
130
|
TContract extends MongoContract,
|
|
122
|
-
ModelName extends string & keyof TContract
|
|
123
|
-
> = TContract[
|
|
131
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
132
|
+
> = MongoModelsMap<TContract>[ModelName] extends {
|
|
124
133
|
readonly variants: infer V extends Record<string, unknown>;
|
|
125
134
|
}
|
|
126
135
|
? keyof V & string
|
|
@@ -128,14 +137,14 @@ export type VariantNames<
|
|
|
128
137
|
|
|
129
138
|
export type VariantModelRow<
|
|
130
139
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
131
|
-
ModelName extends string & keyof TContract
|
|
140
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
132
141
|
VariantName extends string,
|
|
133
|
-
> = TContract[
|
|
142
|
+
> = MongoModelsMap<TContract>[ModelName] extends {
|
|
134
143
|
readonly discriminator: { readonly field: infer DiscField extends string };
|
|
135
144
|
readonly variants: infer V;
|
|
136
145
|
}
|
|
137
146
|
? V extends Record<string, { readonly value: string }>
|
|
138
|
-
? VariantName extends keyof V & string & keyof TContract
|
|
147
|
+
? VariantName extends keyof V & string & keyof MongoModelsMap<TContract>
|
|
139
148
|
? Simplify<
|
|
140
149
|
Omit<InferFullRow<TContract, ModelName>, DiscField> &
|
|
141
150
|
InferFullRow<TContract, VariantName> &
|
|
@@ -147,22 +156,24 @@ export type VariantModelRow<
|
|
|
147
156
|
|
|
148
157
|
type IncludeRelationRowType<
|
|
149
158
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
150
|
-
ModelName extends string & keyof TContract
|
|
159
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
151
160
|
RelKey extends keyof ModelRelations<TContract, ModelName>,
|
|
152
161
|
> = ModelRelations<TContract, ModelName>[RelKey] extends ContractReferenceRelation
|
|
153
|
-
? ModelRelations<TContract, ModelName>[RelKey] extends
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
162
|
+
? ModelRelations<TContract, ModelName>[RelKey] extends infer R
|
|
163
|
+
? R extends { readonly cardinality: infer C }
|
|
164
|
+
? RelationTargetModel<TContract, R> extends infer To extends string &
|
|
165
|
+
keyof MongoModelsMap<TContract>
|
|
166
|
+
? C extends 'N:1' | '1:1'
|
|
167
|
+
? InferFullRow<TContract, To> | null
|
|
168
|
+
: InferFullRow<TContract, To>[]
|
|
169
|
+
: never
|
|
170
|
+
: never
|
|
160
171
|
: never
|
|
161
172
|
: never;
|
|
162
173
|
|
|
163
174
|
export type IncludeResultFields<
|
|
164
175
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
165
|
-
ModelName extends string & keyof TContract
|
|
176
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
166
177
|
TInclude extends MongoIncludeSpec<TContract, ModelName>,
|
|
167
178
|
> = {
|
|
168
179
|
-readonly [K in keyof TInclude & string as TInclude[K] extends true
|
|
@@ -174,10 +185,10 @@ export type IncludeResultFields<
|
|
|
174
185
|
|
|
175
186
|
export type MongoWhereFilter<
|
|
176
187
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
177
|
-
ModelName extends string & keyof TContract
|
|
188
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
178
189
|
TCodecTypes extends Record<string, { output: unknown }> = ExtractMongoCodecTypes<TContract>,
|
|
179
190
|
> = {
|
|
180
|
-
readonly [K in keyof TContract[
|
|
191
|
+
readonly [K in keyof MongoModelsMap<TContract>[ModelName]['fields']]?: MongoModelsMap<TContract>[ModelName]['fields'][K] extends {
|
|
181
192
|
readonly type: {
|
|
182
193
|
readonly kind: 'scalar';
|
|
183
194
|
readonly codecId: infer CId extends string & keyof TCodecTypes;
|
|
@@ -189,7 +200,7 @@ export type MongoWhereFilter<
|
|
|
189
200
|
|
|
190
201
|
export type MongoIncludeSpec<
|
|
191
202
|
TContract extends MongoContract,
|
|
192
|
-
ModelName extends string & keyof TContract
|
|
203
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
193
204
|
> = {
|
|
194
205
|
readonly [K in ReferenceRelationKeys<TContract, ModelName>]?: true;
|
|
195
206
|
};
|
|
@@ -198,18 +209,18 @@ export type NoIncludes = Pick<Record<string, boolean>, never>;
|
|
|
198
209
|
|
|
199
210
|
export type IncludedRow<
|
|
200
211
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
201
|
-
ModelName extends string & keyof TContract
|
|
212
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
202
213
|
TIncludes extends MongoIncludeSpec<TContract, ModelName> = NoIncludes,
|
|
203
214
|
> = InferRootRow<TContract, ModelName> & IncludeResultFields<TContract, ModelName, TIncludes>;
|
|
204
215
|
|
|
205
216
|
export type DefaultModelRow<
|
|
206
217
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
207
|
-
ModelName extends string & keyof TContract
|
|
218
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
208
219
|
> = ResolvedOutputRow<TContract, ModelName>;
|
|
209
220
|
|
|
210
221
|
export type CreateInput<
|
|
211
222
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
212
|
-
ModelName extends string & keyof TContract
|
|
223
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
213
224
|
> = Omit<ResolvedInputRow<TContract, ModelName>, '_id'> &
|
|
214
225
|
Partial<
|
|
215
226
|
Pick<
|
|
@@ -220,8 +231,8 @@ export type CreateInput<
|
|
|
220
231
|
|
|
221
232
|
type DiscriminatorField<
|
|
222
233
|
TContract extends MongoContract,
|
|
223
|
-
ModelName extends string & keyof TContract
|
|
224
|
-
> = TContract[
|
|
234
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
235
|
+
> = MongoModelsMap<TContract>[ModelName] extends {
|
|
225
236
|
readonly discriminator: { readonly field: infer F extends string };
|
|
226
237
|
}
|
|
227
238
|
? F
|
|
@@ -233,7 +244,7 @@ type DiscriminatorField<
|
|
|
233
244
|
// input-side VariantModelRow.
|
|
234
245
|
export type VariantCreateInput<
|
|
235
246
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
236
|
-
ModelName extends string & keyof TContract
|
|
247
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
237
248
|
VariantName extends string,
|
|
238
249
|
> = Omit<
|
|
239
250
|
VariantModelRow<TContract, ModelName, VariantName>,
|
|
@@ -248,7 +259,7 @@ export type VariantCreateInput<
|
|
|
248
259
|
|
|
249
260
|
export type ResolvedCreateInput<
|
|
250
261
|
TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>,
|
|
251
|
-
ModelName extends string & keyof TContract
|
|
262
|
+
ModelName extends string & keyof MongoModelsMap<TContract>,
|
|
252
263
|
TVariant extends string,
|
|
253
264
|
> = [TVariant] extends [never]
|
|
254
265
|
? CreateInput<TContract, ModelName>
|