@prisma-next/mongo-contract 0.8.0 → 0.9.0-dev.1
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/README.md +3 -3
- package/dist/index.d.mts +657 -96
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +392 -70
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
- package/src/contract-schema.ts +16 -0
- package/src/contract-types.ts +24 -123
- package/src/exports/index.ts +46 -21
- package/src/ir/mongo-change-stream-pre-and-post-images-options.ts +23 -0
- package/src/ir/mongo-clustered-collection-options.ts +32 -0
- package/src/ir/mongo-collation-options.ts +66 -0
- package/src/ir/mongo-collection-options.ts +159 -0
- package/src/ir/mongo-collection.ts +71 -0
- package/src/ir/mongo-index-option-defaults.ts +27 -0
- package/src/ir/mongo-index-options.ts +91 -0
- package/src/ir/mongo-index.ts +64 -0
- package/src/ir/mongo-storage.ts +55 -0
- package/src/ir/mongo-time-series-collection-options.ts +40 -0
- package/src/ir/mongo-validator.ts +42 -0
- package/src/polymorphic-index-scope.ts +16 -6
- package/src/validate-mongo-contract.ts +0 -49
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { MongoIndex, type MongoIndexInput } from './ir/mongo-index';
|
|
2
2
|
|
|
3
3
|
export type PolymorphicIndexScope = {
|
|
4
4
|
readonly discriminatorField: string;
|
|
@@ -6,7 +6,7 @@ export type PolymorphicIndexScope = {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export type ApplyScopeResult =
|
|
9
|
-
| { readonly kind: 'ok'; readonly index:
|
|
9
|
+
| { readonly kind: 'ok'; readonly index: MongoIndex }
|
|
10
10
|
| { readonly kind: 'conflict'; readonly reason: string };
|
|
11
11
|
|
|
12
12
|
function isScalarDiscriminatorValue(value: unknown): value is string | number | boolean {
|
|
@@ -23,7 +23,7 @@ function formatValue(value: unknown): string {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export function applyPolymorphicScopeToMongoIndex(
|
|
26
|
-
index:
|
|
26
|
+
index: MongoIndex,
|
|
27
27
|
scope: PolymorphicIndexScope,
|
|
28
28
|
): ApplyScopeResult {
|
|
29
29
|
if (!isScalarDiscriminatorValue(scope.discriminatorValue)) {
|
|
@@ -50,8 +50,18 @@ export function applyPolymorphicScopeToMongoIndex(
|
|
|
50
50
|
[scope.discriminatorField]: scope.discriminatorValue,
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
index
|
|
53
|
+
const cloned: MongoIndexInput = {
|
|
54
|
+
keys: index.keys,
|
|
55
|
+
...(index.unique !== undefined && { unique: index.unique }),
|
|
56
|
+
...(index.sparse !== undefined && { sparse: index.sparse }),
|
|
57
|
+
...(index.expireAfterSeconds !== undefined && { expireAfterSeconds: index.expireAfterSeconds }),
|
|
58
|
+
partialFilterExpression: merged,
|
|
59
|
+
...(index.wildcardProjection !== undefined && { wildcardProjection: index.wildcardProjection }),
|
|
60
|
+
...(index.collation !== undefined && { collation: index.collation }),
|
|
61
|
+
...(index.weights !== undefined && { weights: index.weights }),
|
|
62
|
+
...(index.default_language !== undefined && { default_language: index.default_language }),
|
|
63
|
+
...(index.language_override !== undefined && { language_override: index.language_override }),
|
|
56
64
|
};
|
|
65
|
+
|
|
66
|
+
return { kind: 'ok', index: new MongoIndex(cloned) };
|
|
57
67
|
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { validateContractDomain } from '@prisma-next/contract/validate-domain';
|
|
2
|
-
import { type as arktypeType } from 'arktype';
|
|
3
|
-
import { MongoContractSchema } from './contract-schema';
|
|
4
|
-
import type { MongoContract } from './contract-types';
|
|
5
|
-
import { validateMongoStorage } from './validate-storage';
|
|
6
|
-
|
|
7
|
-
export interface MongoContractIndices {
|
|
8
|
-
readonly variantToBase: Record<string, string>;
|
|
9
|
-
readonly modelToVariants: Record<string, string[]>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ValidatedMongoContract<TContract extends MongoContract> {
|
|
13
|
-
readonly contract: TContract;
|
|
14
|
-
readonly indices: MongoContractIndices;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function validateMongoContract<TContract extends MongoContract>(
|
|
18
|
-
value: unknown,
|
|
19
|
-
): ValidatedMongoContract<TContract> {
|
|
20
|
-
const parsed = MongoContractSchema(value);
|
|
21
|
-
if (parsed instanceof arktypeType.errors) {
|
|
22
|
-
throw new Error(`Contract structural validation failed: ${parsed.summary}`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const contract = parsed as unknown as TContract;
|
|
26
|
-
|
|
27
|
-
validateContractDomain(contract);
|
|
28
|
-
validateMongoStorage(contract);
|
|
29
|
-
|
|
30
|
-
const indices = buildIndices(contract);
|
|
31
|
-
|
|
32
|
-
return { contract, indices };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function buildIndices(contract: MongoContract): MongoContractIndices {
|
|
36
|
-
const variantToBase: Record<string, string> = {};
|
|
37
|
-
const modelToVariants: Record<string, string[]> = {};
|
|
38
|
-
|
|
39
|
-
for (const [modelName, model] of Object.entries(contract.models)) {
|
|
40
|
-
if (model.base) {
|
|
41
|
-
variantToBase[modelName] = model.base;
|
|
42
|
-
}
|
|
43
|
-
if (model.variants) {
|
|
44
|
-
modelToVariants[modelName] = Object.keys(model.variants);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return { variantToBase, modelToVariants };
|
|
49
|
-
}
|