@prisma-next/adapter-mongo 0.3.0-dev.147 → 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.
@@ -0,0 +1,275 @@
1
+ import type { MigrationOperationClass } from '@prisma-next/framework-components/control';
2
+ import {
3
+ type AnyMongoDdlCommand,
4
+ type AnyMongoInspectionCommand,
5
+ CollModCommand,
6
+ CreateCollectionCommand,
7
+ CreateIndexCommand,
8
+ DropCollectionCommand,
9
+ DropIndexCommand,
10
+ ListCollectionsCommand,
11
+ ListIndexesCommand,
12
+ MongoAndExpr,
13
+ MongoExistsExpr,
14
+ MongoFieldFilter,
15
+ type MongoFilterExpr,
16
+ type MongoMigrationCheck,
17
+ type MongoMigrationPlanOperation,
18
+ type MongoMigrationStep,
19
+ MongoNotExpr,
20
+ MongoOrExpr,
21
+ } from '@prisma-next/mongo-query-ast/control';
22
+ import { type } from 'arktype';
23
+
24
+ const IndexKeyDirection = type('1 | -1 | "text" | "2dsphere" | "2d" | "hashed"');
25
+ const IndexKeyJson = type({ field: 'string', direction: IndexKeyDirection });
26
+
27
+ const CreateIndexJson = type({
28
+ kind: '"createIndex"',
29
+ collection: 'string',
30
+ keys: IndexKeyJson.array().atLeastLength(1),
31
+ 'unique?': 'boolean',
32
+ 'sparse?': 'boolean',
33
+ 'expireAfterSeconds?': 'number',
34
+ 'partialFilterExpression?': 'Record<string, unknown>',
35
+ 'name?': 'string',
36
+ 'wildcardProjection?': 'Record<string, unknown>',
37
+ 'collation?': 'Record<string, unknown>',
38
+ 'weights?': 'Record<string, unknown>',
39
+ 'default_language?': 'string',
40
+ 'language_override?': 'string',
41
+ });
42
+
43
+ const DropIndexJson = type({
44
+ kind: '"dropIndex"',
45
+ collection: 'string',
46
+ name: 'string',
47
+ });
48
+
49
+ const CreateCollectionJson = type({
50
+ kind: '"createCollection"',
51
+ collection: 'string',
52
+ 'validator?': 'Record<string, unknown>',
53
+ 'validationLevel?': '"strict" | "moderate"',
54
+ 'validationAction?': '"error" | "warn"',
55
+ 'capped?': 'boolean',
56
+ 'size?': 'number',
57
+ 'max?': 'number',
58
+ 'timeseries?': 'Record<string, unknown>',
59
+ 'collation?': 'Record<string, unknown>',
60
+ 'changeStreamPreAndPostImages?': 'Record<string, unknown>',
61
+ 'clusteredIndex?': 'Record<string, unknown>',
62
+ });
63
+
64
+ const DropCollectionJson = type({
65
+ kind: '"dropCollection"',
66
+ collection: 'string',
67
+ });
68
+
69
+ const CollModJson = type({
70
+ kind: '"collMod"',
71
+ collection: 'string',
72
+ 'validator?': 'Record<string, unknown>',
73
+ 'validationLevel?': '"strict" | "moderate"',
74
+ 'validationAction?': '"error" | "warn"',
75
+ 'changeStreamPreAndPostImages?': 'Record<string, unknown>',
76
+ });
77
+
78
+ const ListIndexesJson = type({
79
+ kind: '"listIndexes"',
80
+ collection: 'string',
81
+ });
82
+
83
+ const ListCollectionsJson = type({
84
+ kind: '"listCollections"',
85
+ });
86
+
87
+ const FieldFilterJson = type({
88
+ kind: '"field"',
89
+ field: 'string',
90
+ op: 'string',
91
+ value: 'unknown',
92
+ });
93
+
94
+ const ExistsFilterJson = type({
95
+ kind: '"exists"',
96
+ field: 'string',
97
+ exists: 'boolean',
98
+ });
99
+
100
+ const CheckJson = type({
101
+ description: 'string',
102
+ source: 'Record<string, unknown>',
103
+ filter: 'Record<string, unknown>',
104
+ expect: '"exists" | "notExists"',
105
+ });
106
+
107
+ const StepJson = type({
108
+ description: 'string',
109
+ command: 'Record<string, unknown>',
110
+ });
111
+
112
+ const OperationJson = type({
113
+ id: 'string',
114
+ label: 'string',
115
+ operationClass: '"additive" | "widening" | "destructive"',
116
+ precheck: 'Record<string, unknown>[]',
117
+ execute: 'Record<string, unknown>[]',
118
+ postcheck: 'Record<string, unknown>[]',
119
+ });
120
+
121
+ function validate<T>(schema: { assert: (data: unknown) => T }, data: unknown, context: string): T {
122
+ try {
123
+ return schema.assert(data);
124
+ } catch (error) {
125
+ const message = error instanceof Error ? error.message : String(error);
126
+ throw new Error(`Invalid ${context}: ${message}`);
127
+ }
128
+ }
129
+
130
+ function deserializeFilterExpr(json: unknown): MongoFilterExpr {
131
+ const record = json as Record<string, unknown>;
132
+ const kind = record['kind'] as string;
133
+ switch (kind) {
134
+ case 'field': {
135
+ const data = validate(FieldFilterJson, json, 'field filter');
136
+ return MongoFieldFilter.of(data.field, data.op, data.value as never);
137
+ }
138
+ case 'and': {
139
+ const exprs = record['exprs'];
140
+ if (!Array.isArray(exprs)) throw new Error('Invalid and filter: missing exprs array');
141
+ return MongoAndExpr.of(exprs.map(deserializeFilterExpr));
142
+ }
143
+ case 'or': {
144
+ const exprs = record['exprs'];
145
+ if (!Array.isArray(exprs)) throw new Error('Invalid or filter: missing exprs array');
146
+ return MongoOrExpr.of(exprs.map(deserializeFilterExpr));
147
+ }
148
+ case 'not': {
149
+ const expr = record['expr'];
150
+ if (!expr || typeof expr !== 'object') throw new Error('Invalid not filter: missing expr');
151
+ return new MongoNotExpr(deserializeFilterExpr(expr));
152
+ }
153
+ case 'exists': {
154
+ const data = validate(ExistsFilterJson, json, 'exists filter');
155
+ return new MongoExistsExpr(data.field, data.exists);
156
+ }
157
+ default:
158
+ throw new Error(`Unknown filter expression kind: ${kind}`);
159
+ }
160
+ }
161
+
162
+ function deserializeDdlCommand(json: unknown): AnyMongoDdlCommand {
163
+ const record = json as Record<string, unknown>;
164
+ const kind = record['kind'] as string;
165
+ switch (kind) {
166
+ case 'createIndex': {
167
+ const data = validate(CreateIndexJson, json, 'createIndex command');
168
+ return new CreateIndexCommand(data.collection, data.keys, {
169
+ unique: data.unique,
170
+ sparse: data.sparse,
171
+ expireAfterSeconds: data.expireAfterSeconds,
172
+ partialFilterExpression: data.partialFilterExpression,
173
+ name: data.name,
174
+ wildcardProjection: data.wildcardProjection as Record<string, 0 | 1> | undefined,
175
+ collation: data.collation,
176
+ weights: data.weights as Record<string, number> | undefined,
177
+ default_language: data.default_language,
178
+ language_override: data.language_override,
179
+ });
180
+ }
181
+ case 'dropIndex': {
182
+ const data = validate(DropIndexJson, json, 'dropIndex command');
183
+ return new DropIndexCommand(data.collection, data.name);
184
+ }
185
+ case 'createCollection': {
186
+ const data = validate(CreateCollectionJson, json, 'createCollection command');
187
+ return new CreateCollectionCommand(data.collection, {
188
+ validator: data.validator,
189
+ validationLevel: data.validationLevel,
190
+ validationAction: data.validationAction,
191
+ capped: data.capped,
192
+ size: data.size,
193
+ max: data.max,
194
+ timeseries: data.timeseries as CreateCollectionCommand['timeseries'],
195
+ collation: data.collation,
196
+ changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as
197
+ | { enabled: boolean }
198
+ | undefined,
199
+ clusteredIndex: data.clusteredIndex as CreateCollectionCommand['clusteredIndex'],
200
+ });
201
+ }
202
+ case 'dropCollection': {
203
+ const data = validate(DropCollectionJson, json, 'dropCollection command');
204
+ return new DropCollectionCommand(data.collection);
205
+ }
206
+ case 'collMod': {
207
+ const data = validate(CollModJson, json, 'collMod command');
208
+ return new CollModCommand(data.collection, {
209
+ validator: data.validator,
210
+ validationLevel: data.validationLevel,
211
+ validationAction: data.validationAction,
212
+ changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as
213
+ | { enabled: boolean }
214
+ | undefined,
215
+ });
216
+ }
217
+ default:
218
+ throw new Error(`Unknown DDL command kind: ${kind}`);
219
+ }
220
+ }
221
+
222
+ function deserializeInspectionCommand(json: unknown): AnyMongoInspectionCommand {
223
+ const record = json as Record<string, unknown>;
224
+ const kind = record['kind'] as string;
225
+ switch (kind) {
226
+ case 'listIndexes': {
227
+ const data = validate(ListIndexesJson, json, 'listIndexes command');
228
+ return new ListIndexesCommand(data.collection);
229
+ }
230
+ case 'listCollections': {
231
+ validate(ListCollectionsJson, json, 'listCollections command');
232
+ return new ListCollectionsCommand();
233
+ }
234
+ default:
235
+ throw new Error(`Unknown inspection command kind: ${kind}`);
236
+ }
237
+ }
238
+
239
+ function deserializeCheck(json: unknown): MongoMigrationCheck {
240
+ const data = validate(CheckJson, json, 'migration check');
241
+ return {
242
+ description: data.description,
243
+ source: deserializeInspectionCommand(data.source),
244
+ filter: deserializeFilterExpr(data.filter),
245
+ expect: data.expect,
246
+ };
247
+ }
248
+
249
+ function deserializeStep(json: unknown): MongoMigrationStep {
250
+ const data = validate(StepJson, json, 'migration step');
251
+ return {
252
+ description: data.description,
253
+ command: deserializeDdlCommand(data.command),
254
+ };
255
+ }
256
+
257
+ export function deserializeMongoOp(json: unknown): MongoMigrationPlanOperation {
258
+ const data = validate(OperationJson, json, 'migration operation');
259
+ return {
260
+ id: data.id,
261
+ label: data.label,
262
+ operationClass: data.operationClass as MigrationOperationClass,
263
+ precheck: data.precheck.map(deserializeCheck),
264
+ execute: data.execute.map(deserializeStep),
265
+ postcheck: data.postcheck.map(deserializeCheck),
266
+ };
267
+ }
268
+
269
+ export function deserializeMongoOps(json: readonly unknown[]): MongoMigrationPlanOperation[] {
270
+ return json.map(deserializeMongoOp);
271
+ }
272
+
273
+ export function serializeMongoOps(ops: readonly MongoMigrationPlanOperation[]): string {
274
+ return JSON.stringify(ops, null, 2);
275
+ }