@prisma/orm-sqlite 8.0.0-rc.23-dev.14 → 8.0.0-rc.24-dev.2
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/runtime.d.mts +3 -2
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +30 -4
- package/dist/runtime.mjs.map +1 -1
- package/dist/{sqlite-DrpYKUC0-wkBQ7gVM.d.mts → sqlite-DtcJe6kq-Qi6-p5Cy.d.mts} +14 -5
- package/dist/sqlite-DtcJe6kq-Qi6-p5Cy.d.mts.map +1 -0
- package/dist/{sqlite-static-CUbtFUTw-CIXVd8ew.mjs → sqlite-static-hboT7-Nq-D5068C14.mjs} +4 -4
- package/dist/sqlite-static-hboT7-Nq-D5068C14.mjs.map +1 -0
- package/dist/static.d.mts +1 -1
- package/dist/static.mjs +1 -1
- package/package.json +9 -8
- package/skills/prisma-8/SKILL.md +1 -1
- package/skills/prisma-8/references/contract.md +3 -1
- package/skills/prisma-8/references/migrations.md +1 -1
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +319 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
- package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +304 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
- package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
- package/dist/sqlite-DrpYKUC0-wkBQ7gVM.d.mts.map +0 -1
- package/dist/sqlite-static-CUbtFUTw-CIXVd8ew.mjs.map +0 -1
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites the removed MongoDB field helpers of TypeScript contracts to `field.scalar(type.X())`: `field.objectId()` becomes `field.scalar(type.ObjectId())`, `field.scalar('mongo/string@1')` becomes `field.scalar(type.String())`, and `field.vector({ typeParams: { length: 3 } })` becomes `field.scalar(type.Vector(3))`.
|
|
4
|
+
*
|
|
5
|
+
* It rewrites a call only where its `field` is the MongoDB builder's: imported or required from a MongoDB contract builder entry under any name, destructured from the helpers of a `defineContract` imported from one, or read as `helpers.field` from them, which becomes `helpers.field.scalar(helpers.type.Int32())`. Where a rewritten call reads `type` by itself, the script adds `type` to the import or destructuring that gives the call its `field`. A `field` of another builder, such as a SQL `defineContract`'s, is left alone.
|
|
6
|
+
*
|
|
7
|
+
* Usage: node rewrite-mongo-field-types.ts [file or directory ...]
|
|
8
|
+
*
|
|
9
|
+
* The paths default to the working directory. In a directory the script reads every TypeScript and JavaScript file outside `node_modules`, `.git`, `dist` and `build`. It rewrites only code: comments and strings stay as they are, and the contract each file builds does not change. It reads the files with the `typescript` package of the project, or of the folder the script is in. It prints one line per file it changes, and one line per call it leaves for a person: a call it cannot carry over, such as a codec id with no type constructor; a call where `type` already names another value; and a removed helper called on a `field` it cannot tell is the MongoDB builder's, such as one imported from a module of the project.
|
|
10
|
+
*/
|
|
11
|
+
import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { join, relative } from 'node:path';
|
|
14
|
+
import type * as TypeScript from 'typescript';
|
|
15
|
+
|
|
16
|
+
const SKIPPED_DIRECTORIES = new Set(['node_modules', '.git', 'dist', 'build']);
|
|
17
|
+
const SOURCE_FILE = /\.(?:[cm]?ts|tsx|[cm]?js|jsx)$/;
|
|
18
|
+
const BUILDER_MODULE =
|
|
19
|
+
/^(?:@prisma\/orm-mongo|@prisma\/orm-family-mongo\/contract-ts|@internal\/mongo|@internal\/mongo-contract-ts)\/contract-builder$/;
|
|
20
|
+
const TYPE_EXPORTING_MODULE = /^(?:@prisma\/orm-mongo|@internal\/mongo)\/contract-builder$/;
|
|
21
|
+
const WRITE_BY_HAND = 'write it as field.scalar(type.X(...))';
|
|
22
|
+
const WRITE_BY_HAND_IF_MONGO =
|
|
23
|
+
'if it is, write it as field.scalar(type.X(...)), and if not, leave it';
|
|
24
|
+
const UNKNOWN: Origin = {
|
|
25
|
+
kind: 'unknown',
|
|
26
|
+
reason: "the script cannot tell this field is the MongoDB builder's",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const TYPE_OF_HELPER: Readonly<Record<string, string>> = {
|
|
30
|
+
objectId: 'ObjectId',
|
|
31
|
+
string: 'String',
|
|
32
|
+
double: 'Double',
|
|
33
|
+
int32: 'Int32',
|
|
34
|
+
bool: 'Bool',
|
|
35
|
+
date: 'DateTime',
|
|
36
|
+
int64: 'Int64',
|
|
37
|
+
int64Number: 'Int64Number',
|
|
38
|
+
decimal128: 'Decimal128',
|
|
39
|
+
binary: 'Binary',
|
|
40
|
+
json: 'Json',
|
|
41
|
+
bson: 'Bson',
|
|
42
|
+
vector: 'Vector',
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const TYPE_OF_CODEC: Readonly<Record<string, string>> = Object.fromEntries(
|
|
46
|
+
Object.entries(TYPE_OF_HELPER).map(([helper, typeName]) => [`mongo/${helper}@1`, typeName]),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
function loadTypeScript(): typeof TypeScript {
|
|
50
|
+
for (const from of [join(process.cwd(), 'package.json'), import.meta.url]) {
|
|
51
|
+
try {
|
|
52
|
+
return createRequire(from)('typescript');
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
console.error(
|
|
56
|
+
'The script reads files with the "typescript" package, which this project does not have. Install it, for example with "pnpm add -D typescript", and run the script again.',
|
|
57
|
+
);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const ts = loadTypeScript();
|
|
62
|
+
|
|
63
|
+
interface Edit {
|
|
64
|
+
readonly start: number;
|
|
65
|
+
readonly end: number;
|
|
66
|
+
readonly text: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What a name binds `field` to: the MongoDB builder's, another builder's, or something the script cannot tell. */
|
|
70
|
+
type Origin =
|
|
71
|
+
| {
|
|
72
|
+
readonly kind: 'mongo';
|
|
73
|
+
/** The `type` the rewritten call reads, such as `type` or `helpers.type`. */
|
|
74
|
+
readonly typeText: string;
|
|
75
|
+
/** Adds `type` beside the `field` binding, once; undefined when the rewrite reads a `type` already there. */
|
|
76
|
+
readonly bind: (() => void) | undefined;
|
|
77
|
+
/** The declaration of the `type` this origin reads or adds, which is not a conflict. */
|
|
78
|
+
readonly ownType: TypeScript.Node | undefined;
|
|
79
|
+
}
|
|
80
|
+
| { readonly kind: 'other' }
|
|
81
|
+
| { readonly kind: 'unknown'; readonly reason: string };
|
|
82
|
+
|
|
83
|
+
/** A binding of a name and the code it reaches. */
|
|
84
|
+
interface Scoped<Value> {
|
|
85
|
+
readonly value: Value;
|
|
86
|
+
readonly scope: TypeScript.Node;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface FileResult {
|
|
90
|
+
readonly text: string;
|
|
91
|
+
readonly rewritten: number;
|
|
92
|
+
readonly reports: readonly string[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function moduleOf(node: TypeScript.Node): string | undefined {
|
|
96
|
+
if (ts.isImportDeclaration(node)) {
|
|
97
|
+
return ts.isStringLiteral(node.moduleSpecifier) ? node.moduleSpecifier.text : undefined;
|
|
98
|
+
}
|
|
99
|
+
if (
|
|
100
|
+
ts.isCallExpression(node) &&
|
|
101
|
+
ts.isIdentifier(node.expression) &&
|
|
102
|
+
node.expression.text === 'require' &&
|
|
103
|
+
node.arguments.length === 1
|
|
104
|
+
) {
|
|
105
|
+
const [specifier] = node.arguments;
|
|
106
|
+
return specifier !== undefined && ts.isStringLiteralLike(specifier)
|
|
107
|
+
? specifier.text
|
|
108
|
+
: undefined;
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function propertyNameOf(element: TypeScript.BindingElement | TypeScript.ImportSpecifier) {
|
|
114
|
+
const name = element.propertyName ?? element.name;
|
|
115
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The code a declaration reaches: a parameter's function, an import's file, and otherwise the nearest block. */
|
|
119
|
+
function scopeOf(declaration: TypeScript.Node): TypeScript.Node {
|
|
120
|
+
let node: TypeScript.Node = declaration;
|
|
121
|
+
while (
|
|
122
|
+
ts.isBindingElement(node) ||
|
|
123
|
+
ts.isObjectBindingPattern(node) ||
|
|
124
|
+
ts.isArrayBindingPattern(node)
|
|
125
|
+
) {
|
|
126
|
+
node = node.parent;
|
|
127
|
+
}
|
|
128
|
+
if (ts.isParameter(node)) return node.parent;
|
|
129
|
+
if (
|
|
130
|
+
(ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) &&
|
|
131
|
+
node.parent !== undefined
|
|
132
|
+
) {
|
|
133
|
+
node = node.parent;
|
|
134
|
+
}
|
|
135
|
+
while (
|
|
136
|
+
!ts.isBlock(node) &&
|
|
137
|
+
!ts.isSourceFile(node) &&
|
|
138
|
+
!ts.isCaseClause(node) &&
|
|
139
|
+
!ts.isFunctionLike(node) &&
|
|
140
|
+
node.parent !== undefined
|
|
141
|
+
) {
|
|
142
|
+
node = node.parent;
|
|
143
|
+
}
|
|
144
|
+
return node;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function rewrite(file: TypeScript.SourceFile): FileResult {
|
|
148
|
+
const edits: Edit[] = [];
|
|
149
|
+
const reports: string[] = [];
|
|
150
|
+
const fields = new Map<string, Scoped<Origin>[]>();
|
|
151
|
+
const helpersObjects = new Map<string, Scoped<'mongo' | 'other'>[]>();
|
|
152
|
+
const typeDeclarations: Scoped<TypeScript.Node>[] = [];
|
|
153
|
+
const mongoDefines = new Set<string>();
|
|
154
|
+
const otherDefines = new Set<string>();
|
|
155
|
+
const mongoNamespaces = new Map<string, string>();
|
|
156
|
+
const knownPatterns = new Set<TypeScript.Node>();
|
|
157
|
+
const constantStrings = new Map<string, Scoped<string>[]>();
|
|
158
|
+
const namesBuilder =
|
|
159
|
+
file.statements.some((statement) => {
|
|
160
|
+
const module = ts.isImportDeclaration(statement) ? moduleOf(statement) : undefined;
|
|
161
|
+
return module !== undefined && BUILDER_MODULE.test(module);
|
|
162
|
+
}) ||
|
|
163
|
+
/require\(\s*['"](?:@prisma\/orm-mongo|@prisma\/orm-family-mongo\/contract-ts|@internal\/mongo|@internal\/mongo-contract-ts)\/contract-builder['"]\s*\)/.test(
|
|
164
|
+
file.text,
|
|
165
|
+
);
|
|
166
|
+
let rewritten = 0;
|
|
167
|
+
|
|
168
|
+
const report = (node: TypeScript.Node, why: string, advice = WRITE_BY_HAND) => {
|
|
169
|
+
const line = file.getLineAndCharacterOfPosition(node.getStart(file)).line + 1;
|
|
170
|
+
const call = node.getText(file).replace(/\s+/g, ' ');
|
|
171
|
+
reports.push(
|
|
172
|
+
`line ${line}: ${call.length > 100 ? `${call.slice(0, 97)}...` : call}: not rewritten, as ${why}; ${advice}.`,
|
|
173
|
+
);
|
|
174
|
+
};
|
|
175
|
+
const contains = (scope: TypeScript.Node, node: TypeScript.Node) =>
|
|
176
|
+
scope.getStart(file) <= node.getStart(file) && node.end <= scope.end;
|
|
177
|
+
function innermost<Value>(
|
|
178
|
+
bindings: readonly Scoped<Value>[] | undefined,
|
|
179
|
+
node: TypeScript.Node,
|
|
180
|
+
): Value | undefined {
|
|
181
|
+
const reaching = (bindings ?? []).filter(({ scope }) => contains(scope, node));
|
|
182
|
+
reaching.sort(
|
|
183
|
+
(a, b) => a.scope.end - a.scope.getStart(file) - (b.scope.end - b.scope.getStart(file)),
|
|
184
|
+
);
|
|
185
|
+
return reaching[0]?.value;
|
|
186
|
+
}
|
|
187
|
+
const add = <Value>(map: Map<string, Scoped<Value>[]>, name: string, binding: Scoped<Value>) => {
|
|
188
|
+
map.set(name, [...(map.get(name) ?? []), binding]);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** The origin of `field` bound in `elements`, beside which `type` is added when needed. */
|
|
192
|
+
function mongoOrigin(
|
|
193
|
+
elements: TypeScript.NodeArray<TypeScript.BindingElement | TypeScript.ImportSpecifier>,
|
|
194
|
+
exportsType: boolean,
|
|
195
|
+
): Origin {
|
|
196
|
+
const existing = elements.find((element) => propertyNameOf(element) === 'type');
|
|
197
|
+
if (existing !== undefined && ts.isIdentifier(existing.name)) {
|
|
198
|
+
return { kind: 'mongo', typeText: existing.name.text, bind: undefined, ownType: existing };
|
|
199
|
+
}
|
|
200
|
+
const last = elements[elements.length - 1];
|
|
201
|
+
if (!exportsType || last === undefined) {
|
|
202
|
+
return {
|
|
203
|
+
kind: 'unknown',
|
|
204
|
+
reason:
|
|
205
|
+
'this module exports no type, so import type from @prisma/orm-mongo/contract-builder',
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
let bound = false;
|
|
209
|
+
return {
|
|
210
|
+
kind: 'mongo',
|
|
211
|
+
typeText: 'type',
|
|
212
|
+
ownType: undefined,
|
|
213
|
+
bind: () => {
|
|
214
|
+
if (bound) return;
|
|
215
|
+
bound = true;
|
|
216
|
+
if (ts.isBindingElement(last) && last.dotDotDotToken !== undefined) {
|
|
217
|
+
edits.push({ start: last.getStart(file), end: last.getStart(file), text: 'type, ' });
|
|
218
|
+
} else {
|
|
219
|
+
edits.push({ start: last.end, end: last.end, text: ', type' });
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Records what an import or `require` of a builder entry binds. */
|
|
226
|
+
function bindModule(
|
|
227
|
+
module: string,
|
|
228
|
+
names:
|
|
229
|
+
| TypeScript.NamedImports
|
|
230
|
+
| TypeScript.ObjectBindingPattern
|
|
231
|
+
| TypeScript.Identifier
|
|
232
|
+
| undefined,
|
|
233
|
+
): void {
|
|
234
|
+
if (names === undefined) return;
|
|
235
|
+
const isMongo = BUILDER_MODULE.test(module);
|
|
236
|
+
const isOtherBuilder = !isMongo && /\/contract-builder$/.test(module);
|
|
237
|
+
if (ts.isObjectBindingPattern(names)) knownPatterns.add(names);
|
|
238
|
+
if (ts.isIdentifier(names)) {
|
|
239
|
+
if (isMongo) mongoNamespaces.set(names.text, module);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
for (const element of names.elements) {
|
|
243
|
+
if (!ts.isIdentifier(element.name)) continue;
|
|
244
|
+
const imported = propertyNameOf(element);
|
|
245
|
+
if (imported === 'defineContract') {
|
|
246
|
+
(isMongo ? mongoDefines : otherDefines).add(element.name.text);
|
|
247
|
+
}
|
|
248
|
+
if (imported === 'field') {
|
|
249
|
+
add(fields, element.name.text, {
|
|
250
|
+
value: isMongo
|
|
251
|
+
? mongoOrigin(names.elements, TYPE_EXPORTING_MODULE.test(module))
|
|
252
|
+
: isOtherBuilder
|
|
253
|
+
? { kind: 'other' }
|
|
254
|
+
: UNKNOWN,
|
|
255
|
+
scope: file,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function collectModules(node: TypeScript.Node): void {
|
|
262
|
+
if (ts.isImportDeclaration(node)) {
|
|
263
|
+
const module = moduleOf(node);
|
|
264
|
+
const bindings = node.importClause?.namedBindings;
|
|
265
|
+
if (module !== undefined && node.importClause?.isTypeOnly !== true) {
|
|
266
|
+
if (bindings !== undefined && ts.isNamedImports(bindings)) bindModule(module, bindings);
|
|
267
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings)) {
|
|
268
|
+
bindModule(module, bindings.name);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (ts.isVariableDeclaration(node) && node.initializer !== undefined) {
|
|
273
|
+
const module = moduleOf(node.initializer);
|
|
274
|
+
if (
|
|
275
|
+
module !== undefined &&
|
|
276
|
+
(ts.isObjectBindingPattern(node.name) || ts.isIdentifier(node.name))
|
|
277
|
+
) {
|
|
278
|
+
bindModule(module, node.name);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
ts.forEachChild(node, collectModules);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Whether `callee` is a `defineContract` of the MongoDB builder, of another builder, or neither. */
|
|
285
|
+
function defineOf(callee: TypeScript.Expression): 'mongo' | 'other' | undefined {
|
|
286
|
+
if (ts.isIdentifier(callee)) {
|
|
287
|
+
if (mongoDefines.has(callee.text)) return 'mongo';
|
|
288
|
+
return otherDefines.has(callee.text) ? 'other' : undefined;
|
|
289
|
+
}
|
|
290
|
+
if (
|
|
291
|
+
ts.isPropertyAccessExpression(callee) &&
|
|
292
|
+
callee.name.text === 'defineContract' &&
|
|
293
|
+
ts.isIdentifier(callee.expression)
|
|
294
|
+
) {
|
|
295
|
+
return mongoNamespaces.has(callee.expression.text) ? 'mongo' : undefined;
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Records the bindings of `field`, of a helpers object, and of other values named `type`. */
|
|
301
|
+
function collectBindings(node: TypeScript.Node): void {
|
|
302
|
+
if (ts.isCallExpression(node)) {
|
|
303
|
+
const define = defineOf(node.expression);
|
|
304
|
+
if (define !== undefined) {
|
|
305
|
+
for (const argument of node.arguments) {
|
|
306
|
+
if (!ts.isArrowFunction(argument) && !ts.isFunctionExpression(argument)) continue;
|
|
307
|
+
const [helpers] = argument.parameters;
|
|
308
|
+
if (helpers === undefined) continue;
|
|
309
|
+
if (ts.isIdentifier(helpers.name)) {
|
|
310
|
+
add(helpersObjects, helpers.name.text, { value: define, scope: argument });
|
|
311
|
+
}
|
|
312
|
+
if (ts.isObjectBindingPattern(helpers.name)) bindHelpers(helpers.name, define, argument);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (
|
|
317
|
+
ts.isVariableDeclaration(node) &&
|
|
318
|
+
ts.isObjectBindingPattern(node.name) &&
|
|
319
|
+
node.initializer !== undefined &&
|
|
320
|
+
ts.isIdentifier(node.initializer)
|
|
321
|
+
) {
|
|
322
|
+
const origin = innermost(helpersObjects.get(node.initializer.text), node);
|
|
323
|
+
if (origin !== undefined) bindHelpers(node.name, origin, scopeOf(node));
|
|
324
|
+
}
|
|
325
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
326
|
+
const origin = node.initializer === undefined ? undefined : propertyOrigin(node.initializer);
|
|
327
|
+
if (origin !== undefined) {
|
|
328
|
+
add(fields, node.name.text, { value: origin, scope: scopeOf(node) });
|
|
329
|
+
} else if (node.name.text === 'field' && namesBuilder) {
|
|
330
|
+
add(fields, 'field', { value: UNKNOWN, scope: scopeOf(node) });
|
|
331
|
+
}
|
|
332
|
+
if (
|
|
333
|
+
node.initializer !== undefined &&
|
|
334
|
+
ts.isStringLiteralLike(node.initializer) &&
|
|
335
|
+
ts.isVariableDeclarationList(node.parent) &&
|
|
336
|
+
(node.parent.flags & ts.NodeFlags.Const) !== 0
|
|
337
|
+
) {
|
|
338
|
+
add(constantStrings, node.name.text, {
|
|
339
|
+
value: node.initializer.text,
|
|
340
|
+
scope: scopeOf(node),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (
|
|
345
|
+
(ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) &&
|
|
346
|
+
node.name?.text === 'field' &&
|
|
347
|
+
namesBuilder
|
|
348
|
+
) {
|
|
349
|
+
add(fields, 'field', { value: UNKNOWN, scope: scopeOf(node) });
|
|
350
|
+
}
|
|
351
|
+
if (ts.isParameter(node) && ts.isIdentifier(node.name) && node.name.text === 'field') {
|
|
352
|
+
if (!isHelpersParameter(node)) add(fields, 'field', { value: UNKNOWN, scope: node.parent });
|
|
353
|
+
}
|
|
354
|
+
if (
|
|
355
|
+
ts.isBindingElement(node) &&
|
|
356
|
+
ts.isIdentifier(node.name) &&
|
|
357
|
+
propertyNameOf(node) === 'field' &&
|
|
358
|
+
!isKnownPattern(node.parent)
|
|
359
|
+
) {
|
|
360
|
+
add(fields, node.name.text, { value: UNKNOWN, scope: scopeOf(node) });
|
|
361
|
+
}
|
|
362
|
+
const declared = declaredName(node);
|
|
363
|
+
if (declared === 'type') typeDeclarations.push({ value: node, scope: scopeOf(node) });
|
|
364
|
+
ts.forEachChild(node, collectBindings);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function isKnownPattern(pattern: TypeScript.Node): boolean {
|
|
368
|
+
return knownPatterns.has(pattern);
|
|
369
|
+
}
|
|
370
|
+
function isHelpersParameter(parameter: TypeScript.ParameterDeclaration): boolean {
|
|
371
|
+
const owner = parameter.parent;
|
|
372
|
+
return ts.isCallExpression(owner.parent) && defineOf(owner.parent.expression) !== undefined;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function bindHelpers(
|
|
376
|
+
pattern: TypeScript.ObjectBindingPattern,
|
|
377
|
+
define: 'mongo' | 'other',
|
|
378
|
+
scope: TypeScript.Node,
|
|
379
|
+
): void {
|
|
380
|
+
knownPatterns.add(pattern);
|
|
381
|
+
for (const element of pattern.elements) {
|
|
382
|
+
if (propertyNameOf(element) !== 'field' || !ts.isIdentifier(element.name)) continue;
|
|
383
|
+
add(fields, element.name.text, {
|
|
384
|
+
value: define === 'mongo' ? mongoOrigin(pattern.elements, true) : { kind: 'other' },
|
|
385
|
+
scope,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** The name a declaration binds, when it is one. */
|
|
391
|
+
function declaredName(node: TypeScript.Node): string | undefined {
|
|
392
|
+
if (
|
|
393
|
+
ts.isVariableDeclaration(node) ||
|
|
394
|
+
ts.isParameter(node) ||
|
|
395
|
+
ts.isBindingElement(node) ||
|
|
396
|
+
ts.isFunctionDeclaration(node) ||
|
|
397
|
+
ts.isClassDeclaration(node) ||
|
|
398
|
+
ts.isImportSpecifier(node) ||
|
|
399
|
+
ts.isNamespaceImport(node) ||
|
|
400
|
+
ts.isImportClause(node)
|
|
401
|
+
) {
|
|
402
|
+
return node.name !== undefined && ts.isIdentifier(node.name) ? node.name.text : undefined;
|
|
403
|
+
}
|
|
404
|
+
return undefined;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** The origin of `<object>.field`: a helpers object's, a namespace import's, or `require(<builder>).field`; undefined for any other expression. */
|
|
408
|
+
function propertyOrigin(expression: TypeScript.Expression): Origin | undefined {
|
|
409
|
+
if (!ts.isPropertyAccessExpression(expression) || expression.name.text !== 'field') {
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
const module = moduleOf(expression.expression);
|
|
413
|
+
if (module !== undefined) {
|
|
414
|
+
return BUILDER_MODULE.test(module)
|
|
415
|
+
? {
|
|
416
|
+
kind: 'unknown',
|
|
417
|
+
reason:
|
|
418
|
+
'no type is bound beside this field, so import type from @prisma/orm-mongo/contract-builder',
|
|
419
|
+
}
|
|
420
|
+
: UNKNOWN;
|
|
421
|
+
}
|
|
422
|
+
if (!ts.isIdentifier(expression.expression)) return UNKNOWN;
|
|
423
|
+
const object = expression.expression.text;
|
|
424
|
+
const helpers = innermost(helpersObjects.get(object), expression);
|
|
425
|
+
const namespace = mongoNamespaces.get(object);
|
|
426
|
+
if (helpers === 'other') return { kind: 'other' };
|
|
427
|
+
if (helpers === 'mongo' || (namespace !== undefined && TYPE_EXPORTING_MODULE.test(namespace))) {
|
|
428
|
+
return { kind: 'mongo', typeText: `${object}.type`, bind: undefined, ownType: undefined };
|
|
429
|
+
}
|
|
430
|
+
return UNKNOWN;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** The origin of the `field` a call reads through `receiver`. */
|
|
434
|
+
function originOf(receiver: TypeScript.Expression): Origin | undefined {
|
|
435
|
+
if (ts.isIdentifier(receiver)) return innermost(fields.get(receiver.text), receiver);
|
|
436
|
+
return propertyOrigin(receiver);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** The length a `{ typeParams: { length } }` object gives, as written, or undefined when it holds anything else. */
|
|
440
|
+
function lengthOf(argument: TypeScript.Expression): string | undefined {
|
|
441
|
+
if (!ts.isObjectLiteralExpression(argument) || argument.properties.length !== 1)
|
|
442
|
+
return undefined;
|
|
443
|
+
const [typeParams] = argument.properties;
|
|
444
|
+
if (
|
|
445
|
+
typeParams === undefined ||
|
|
446
|
+
!ts.isPropertyAssignment(typeParams) ||
|
|
447
|
+
typeParams.name.getText(file) !== 'typeParams' ||
|
|
448
|
+
!ts.isObjectLiteralExpression(typeParams.initializer) ||
|
|
449
|
+
typeParams.initializer.properties.length !== 1
|
|
450
|
+
) {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
const [length] = typeParams.initializer.properties;
|
|
454
|
+
if (length === undefined || length.name?.getText(file) !== 'length') return undefined;
|
|
455
|
+
if (ts.isShorthandPropertyAssignment(length)) return 'length';
|
|
456
|
+
return ts.isPropertyAssignment(length) ? length.initializer.getText(file) : undefined;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** The type call a removed-helper call becomes, the reason it cannot be rewritten, or `keep` for a call that is not a removed helper. */
|
|
460
|
+
function typeCallOf(
|
|
461
|
+
helper: string,
|
|
462
|
+
args: readonly TypeScript.Expression[],
|
|
463
|
+
): { readonly typeName: string; readonly argument: string } | string {
|
|
464
|
+
if (helper === 'scalar') {
|
|
465
|
+
const [codec, options] = args;
|
|
466
|
+
if (
|
|
467
|
+
codec === undefined ||
|
|
468
|
+
ts.isCallExpression(codec) ||
|
|
469
|
+
ts.isObjectLiteralExpression(codec)
|
|
470
|
+
) {
|
|
471
|
+
return 'keep';
|
|
472
|
+
}
|
|
473
|
+
if (!ts.isStringLiteralLike(codec)) {
|
|
474
|
+
const codecId = ts.isIdentifier(codec) ? constantStringOf(codec) : undefined;
|
|
475
|
+
return codecId === undefined ? 'keep' : `its argument is the codec id "${codecId}"`;
|
|
476
|
+
}
|
|
477
|
+
const typeName = TYPE_OF_CODEC[codec.text];
|
|
478
|
+
if (typeName === undefined) return `codec "${codec.text}" has no type constructor here`;
|
|
479
|
+
if (args.length === 1) return { typeName, argument: '' };
|
|
480
|
+
const length = typeName === 'Vector' && options !== undefined ? lengthOf(options) : undefined;
|
|
481
|
+
if (args.length === 2 && length !== undefined) return { typeName, argument: length };
|
|
482
|
+
return 'its options are not { typeParams: { length } }';
|
|
483
|
+
}
|
|
484
|
+
const typeName = TYPE_OF_HELPER[helper];
|
|
485
|
+
if (typeName === undefined) return 'keep';
|
|
486
|
+
if (args.length === 0) return { typeName, argument: '' };
|
|
487
|
+
const [options] = args;
|
|
488
|
+
const length = helper === 'vector' && options !== undefined ? lengthOf(options) : undefined;
|
|
489
|
+
if (args.length === 1 && length !== undefined) return { typeName, argument: length };
|
|
490
|
+
return 'its arguments are not { typeParams: { length } }';
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Whether a `type` other than the builder's reaches `node`. */
|
|
494
|
+
function typeIsTaken(
|
|
495
|
+
node: TypeScript.Node,
|
|
496
|
+
origin: Origin & { readonly kind: 'mongo' },
|
|
497
|
+
): boolean {
|
|
498
|
+
return typeDeclarations.some(
|
|
499
|
+
({ value, scope }) => value !== origin.ownType && contains(scope, node),
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function visit(node: TypeScript.Node): void {
|
|
504
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
|
|
505
|
+
const callee = node.expression;
|
|
506
|
+
const origin = originOf(callee.expression);
|
|
507
|
+
const typeCall = origin === undefined ? 'keep' : typeCallOf(callee.name.text, node.arguments);
|
|
508
|
+
if (origin !== undefined && typeCall !== 'keep' && origin.kind !== 'other') {
|
|
509
|
+
if (origin.kind === 'unknown') {
|
|
510
|
+
report(node, origin.reason, origin === UNKNOWN ? WRITE_BY_HAND_IF_MONGO : WRITE_BY_HAND);
|
|
511
|
+
} else if (typeof typeCall === 'string') {
|
|
512
|
+
report(node, typeCall);
|
|
513
|
+
} else if (holdsComment(callee.expression.end, node.end)) {
|
|
514
|
+
report(node, 'a comment inside it would be lost');
|
|
515
|
+
} else if (origin.typeText === 'type' && typeIsTaken(node, origin)) {
|
|
516
|
+
report(node, 'type names another value here');
|
|
517
|
+
} else {
|
|
518
|
+
origin.bind?.();
|
|
519
|
+
rewritten += 1;
|
|
520
|
+
edits.push({
|
|
521
|
+
start: node.getStart(file),
|
|
522
|
+
end: node.end,
|
|
523
|
+
text: `${callee.expression.getText(file)}.scalar(${origin.typeText}.${typeCall.typeName}(${typeCall.argument}))`,
|
|
524
|
+
});
|
|
525
|
+
for (const argument of node.arguments) ts.forEachChild(argument, visit);
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
ts.forEachChild(node, visit);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Whether the text from `start` to `end` holds a comment. */
|
|
534
|
+
function holdsComment(start: number, end: number): boolean {
|
|
535
|
+
const scanner = ts.createScanner(
|
|
536
|
+
ts.ScriptTarget.Latest,
|
|
537
|
+
false,
|
|
538
|
+
file.languageVariant,
|
|
539
|
+
file.text,
|
|
540
|
+
undefined,
|
|
541
|
+
start,
|
|
542
|
+
end - start,
|
|
543
|
+
);
|
|
544
|
+
for (
|
|
545
|
+
let token = scanner.scan();
|
|
546
|
+
token !== ts.SyntaxKind.EndOfFileToken;
|
|
547
|
+
token = scanner.scan()
|
|
548
|
+
) {
|
|
549
|
+
if (
|
|
550
|
+
token === ts.SyntaxKind.SingleLineCommentTrivia ||
|
|
551
|
+
token === ts.SyntaxKind.MultiLineCommentTrivia
|
|
552
|
+
) {
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** The string a `const` that reaches `name` holds, when it is a string literal. */
|
|
560
|
+
function constantStringOf(name: TypeScript.Identifier): string | undefined {
|
|
561
|
+
return innermost(constantStrings.get(name.text), name);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
collectModules(file);
|
|
565
|
+
collectBindings(file);
|
|
566
|
+
visit(file);
|
|
567
|
+
const text = edits
|
|
568
|
+
.sort((a, b) => b.start - a.start)
|
|
569
|
+
.reduce(
|
|
570
|
+
(result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end),
|
|
571
|
+
file.text,
|
|
572
|
+
);
|
|
573
|
+
return { text, rewritten, reports };
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function sourceFilesIn(directory: string): readonly string[] {
|
|
577
|
+
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
578
|
+
if (entry.isDirectory()) {
|
|
579
|
+
return SKIPPED_DIRECTORIES.has(entry.name) ? [] : sourceFilesIn(join(directory, entry.name));
|
|
580
|
+
}
|
|
581
|
+
return entry.isFile() && SOURCE_FILE.test(entry.name) ? [join(directory, entry.name)] : [];
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function scriptKindOf(file: string): TypeScript.ScriptKind {
|
|
586
|
+
if (file.endsWith('.tsx')) return ts.ScriptKind.TSX;
|
|
587
|
+
if (file.endsWith('.jsx')) return ts.ScriptKind.JSX;
|
|
588
|
+
return /\.[cm]?js$/.test(file) ? ts.ScriptKind.JS : ts.ScriptKind.TS;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function main(args: readonly string[]): void {
|
|
592
|
+
const root = process.cwd();
|
|
593
|
+
let changed = 0;
|
|
594
|
+
const paths = args.length === 0 ? ['.'] : args;
|
|
595
|
+
const files = paths.flatMap((path) =>
|
|
596
|
+
statSync(path).isDirectory() ? sourceFilesIn(path) : [path],
|
|
597
|
+
);
|
|
598
|
+
for (const file of files) {
|
|
599
|
+
const name = relative(root, file) || file;
|
|
600
|
+
const text = readFileSync(file, 'utf8');
|
|
601
|
+
const source = ts.createSourceFile(
|
|
602
|
+
file,
|
|
603
|
+
text,
|
|
604
|
+
ts.ScriptTarget.Latest,
|
|
605
|
+
true,
|
|
606
|
+
scriptKindOf(file),
|
|
607
|
+
);
|
|
608
|
+
const result = rewrite(source);
|
|
609
|
+
if (result.text !== text) {
|
|
610
|
+
writeFileSync(file, result.text);
|
|
611
|
+
changed += 1;
|
|
612
|
+
console.log(
|
|
613
|
+
`${name}: rewrote ${result.rewritten} field helper call${result.rewritten === 1 ? '' : 's'}.`,
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
for (const line of result.reports) console.log(`${name}: ${line}`);
|
|
617
|
+
}
|
|
618
|
+
console.log(`Changed ${changed} file${changed === 1 ? '' : 's'}.`);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
main(process.argv.slice(2));
|