@lunora/codegen 0.0.0 → 1.0.0-alpha.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/LICENSE.md +105 -0
- package/README.md +117 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +1473 -0
- package/dist/index.d.ts +1473 -0
- package/dist/index.mjs +28 -0
- package/dist/packem_shared/CONTAINERS_FILENAME-0K-pjNb8.mjs +224 -0
- package/dist/packem_shared/CodegenDiagnosticError-54jWDxA9.mjs +22 -0
- package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +61 -0
- package/dist/packem_shared/buildOpenApiDocument-yHVN66Xd.mjs +183 -0
- package/dist/packem_shared/buildOpenRpcDocument-BZGY1-jT.mjs +60 -0
- package/dist/packem_shared/buildSchemaSnapshot-DzLDbWk3.mjs +233 -0
- package/dist/packem_shared/createCodegenProject-DGJm0_Pk.mjs +895 -0
- package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +13 -0
- package/dist/packem_shared/discoverAuthApiCalls-C35R6z0T.mjs +62 -0
- package/dist/packem_shared/discoverCrons-BL6iGuJ3.mjs +254 -0
- package/dist/packem_shared/discoverFunctions-DEgAcRuD.mjs +460 -0
- package/dist/packem_shared/discoverHttpRoutes-C978pBiG.mjs +131 -0
- package/dist/packem_shared/discoverInserts-CRQdXvHO.mjs +39 -0
- package/dist/packem_shared/discoverMaskProcedures-B64zA740.mjs +217 -0
- package/dist/packem_shared/discoverMigrations-Doj_-BAA.mjs +91 -0
- package/dist/packem_shared/discoverNondeterministicCalls-4KiPQxQU.mjs +122 -0
- package/dist/packem_shared/discoverQueries-BkIi0dBD.mjs +62 -0
- package/dist/packem_shared/discoverRlsMetadata-DpRB1HMe.mjs +280 -0
- package/dist/packem_shared/discoverSchema-BBulgGbH.mjs +542 -0
- package/dist/packem_shared/discoverStorageRulesMetadata-DAqJUxUv.mjs +97 -0
- package/dist/packem_shared/discoverWorkflows-DRDQdhfq.mjs +84 -0
- package/dist/packem_shared/emitApi-hRVC-kE7.mjs +2426 -0
- package/dist/packem_shared/emitApp-CzZ6GbrD.mjs +593 -0
- package/dist/packem_shared/lintSchema-DicbOHvH.mjs +68 -0
- package/dist/packem_shared/parse-validator-tuQtHrsr.mjs +132 -0
- package/dist/packem_shared/paths-BRd6JHuF.mjs +11 -0
- package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +57 -0
- package/package.json +45 -17
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
import { SyntaxKind, Node } from 'ts-morph';
|
|
2
|
+
import { diagnosticAt } from './CodegenDiagnosticError-54jWDxA9.mjs';
|
|
3
|
+
import { a as parseObjectShape } from './parse-validator-tuQtHrsr.mjs';
|
|
4
|
+
|
|
5
|
+
const VECTOR_METRICS = /* @__PURE__ */ new Set(["cosine", "dot-product", "euclidean"]);
|
|
6
|
+
const ON_DELETE_ACTIONS = /* @__PURE__ */ new Set(["cascade", "restrict", "set null"]);
|
|
7
|
+
const RESERVED_TABLE_NAMES = /* @__PURE__ */ new Set(["delete", "get", "insert", "normalizeId", "patch", "query", "replace", "system"]);
|
|
8
|
+
const assertTableNameAllowed = (name, node) => {
|
|
9
|
+
if (RESERVED_TABLE_NAMES.has(name)) {
|
|
10
|
+
throw diagnosticAt(
|
|
11
|
+
node,
|
|
12
|
+
`table name "${name}" is reserved — it collides with a \`ctx.db\` member (one of ${[...RESERVED_TABLE_NAMES].map((reserved) => `"${reserved}"`).join(", ")}). Rename the table.`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const getStringProperty = (object, key) => {
|
|
17
|
+
const property = object.getProperty(key);
|
|
18
|
+
if (property && Node.isPropertyAssignment(property)) {
|
|
19
|
+
const initializer = property.getInitializer();
|
|
20
|
+
if (initializer && Node.isStringLiteral(initializer)) {
|
|
21
|
+
return initializer.getLiteralText();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return void 0;
|
|
25
|
+
};
|
|
26
|
+
const getNumberProperty = (object, key) => {
|
|
27
|
+
const property = object.getProperty(key);
|
|
28
|
+
if (property && Node.isPropertyAssignment(property)) {
|
|
29
|
+
const initializer = property.getInitializer();
|
|
30
|
+
if (initializer && Node.isNumericLiteral(initializer)) {
|
|
31
|
+
return Number(initializer.getLiteralText());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return void 0;
|
|
35
|
+
};
|
|
36
|
+
const getStringArrayProperty = (object, key) => {
|
|
37
|
+
const property = object.getProperty(key);
|
|
38
|
+
if (property && Node.isPropertyAssignment(property)) {
|
|
39
|
+
const initializer = property.getInitializer();
|
|
40
|
+
if (initializer && Node.isArrayLiteralExpression(initializer)) {
|
|
41
|
+
return initializer.getElements().filter((element) => Node.isStringLiteral(element)).map((element) => element.getLiteralText());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return void 0;
|
|
45
|
+
};
|
|
46
|
+
const asMetric = (value) => value && VECTOR_METRICS.has(value) ? value : void 0;
|
|
47
|
+
const asOnDelete = (value) => value && ON_DELETE_ACTIONS.has(value) ? value : void 0;
|
|
48
|
+
const relationsObjectBody = (argument) => {
|
|
49
|
+
if (!Node.isArrowFunction(argument)) {
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
let body = argument.getBody();
|
|
53
|
+
if (Node.isParenthesizedExpression(body)) {
|
|
54
|
+
body = body.getExpression();
|
|
55
|
+
} else if (Node.isBlock(body)) {
|
|
56
|
+
const returnStatement = body.getStatements().find((statement) => Node.isReturnStatement(statement));
|
|
57
|
+
body = returnStatement && Node.isReturnStatement(returnStatement) ? returnStatement.getExpression() ?? body : body;
|
|
58
|
+
}
|
|
59
|
+
return Node.isObjectLiteralExpression(body) ? body : void 0;
|
|
60
|
+
};
|
|
61
|
+
const relationFromProperty = (property) => {
|
|
62
|
+
if (!Node.isPropertyAssignment(property)) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
const initializer = property.getInitializer();
|
|
66
|
+
if (!initializer || !Node.isCallExpression(initializer)) {
|
|
67
|
+
return void 0;
|
|
68
|
+
}
|
|
69
|
+
const callee = initializer.getExpression();
|
|
70
|
+
if (!Node.isPropertyAccessExpression(callee)) {
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
const method = callee.getName();
|
|
74
|
+
if (method !== "one" && method !== "many") {
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
const [tableArgument, optionsExpression] = initializer.getArguments();
|
|
78
|
+
const table = tableArgument && Node.isStringLiteral(tableArgument) ? tableArgument.getLiteralText() : "_unknown_";
|
|
79
|
+
let field = "_unknown_";
|
|
80
|
+
let references = "_id";
|
|
81
|
+
let onDelete;
|
|
82
|
+
if (optionsExpression && Node.isObjectLiteralExpression(optionsExpression)) {
|
|
83
|
+
field = getStringProperty(optionsExpression, "field") ?? field;
|
|
84
|
+
references = getStringProperty(optionsExpression, "references") ?? references;
|
|
85
|
+
onDelete = method === "one" ? asOnDelete(getStringProperty(optionsExpression, "onDelete")) : void 0;
|
|
86
|
+
}
|
|
87
|
+
return { field, kind: method, name: property.getName(), onDelete, references, table };
|
|
88
|
+
};
|
|
89
|
+
const parseRelations = (argument) => {
|
|
90
|
+
const body = relationsObjectBody(argument);
|
|
91
|
+
if (!body) {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
const relations = [];
|
|
95
|
+
for (const property of body.getProperties()) {
|
|
96
|
+
const relation = relationFromProperty(property);
|
|
97
|
+
if (relation) {
|
|
98
|
+
relations.push(relation);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return relations;
|
|
102
|
+
};
|
|
103
|
+
const indexNameOf = (nameArgument) => nameArgument && Node.isStringLiteral(nameArgument) ? nameArgument.getLiteralText() : "_unnamed_";
|
|
104
|
+
const parseIndexCall = (args) => {
|
|
105
|
+
const [indexName, fieldsExpression, optionsExpression] = args;
|
|
106
|
+
let unique = false;
|
|
107
|
+
if (optionsExpression && Node.isObjectLiteralExpression(optionsExpression)) {
|
|
108
|
+
const property = optionsExpression.getProperty("unique");
|
|
109
|
+
if (property && Node.isPropertyAssignment(property)) {
|
|
110
|
+
const initializer = property.getInitializer();
|
|
111
|
+
if (initializer && !Node.isTrueLiteral(initializer) && !Node.isFalseLiteral(initializer)) {
|
|
112
|
+
throw diagnosticAt(initializer, `\`unique\` must be a literal \`true\` or \`false\`, got ${JSON.stringify(initializer.getText())}`);
|
|
113
|
+
}
|
|
114
|
+
unique = initializer ? Node.isTrueLiteral(initializer) : false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const fields = fieldsExpression && Node.isArrayLiteralExpression(fieldsExpression) ? fieldsExpression.getElements().filter((element) => Node.isStringLiteral(element)).map((element) => element.getLiteralText()) : [];
|
|
118
|
+
return { fields, name: indexNameOf(indexName), unique };
|
|
119
|
+
};
|
|
120
|
+
const parseSearchIndexCall = (args) => {
|
|
121
|
+
const [indexName, optionsExpression] = args;
|
|
122
|
+
let field = "_unknown_";
|
|
123
|
+
let filterFields;
|
|
124
|
+
if (optionsExpression && Node.isObjectLiteralExpression(optionsExpression)) {
|
|
125
|
+
field = getStringProperty(optionsExpression, "field") ?? field;
|
|
126
|
+
filterFields = getStringArrayProperty(optionsExpression, "filterFields");
|
|
127
|
+
}
|
|
128
|
+
return { field, filterFields, name: indexNameOf(indexName) };
|
|
129
|
+
};
|
|
130
|
+
const rankSortKeyFromElement = (element) => {
|
|
131
|
+
if (!Node.isObjectLiteralExpression(element)) {
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
const field = getStringProperty(element, "field");
|
|
135
|
+
if (field === void 0) {
|
|
136
|
+
return void 0;
|
|
137
|
+
}
|
|
138
|
+
const direction = getStringProperty(element, "direction");
|
|
139
|
+
return { direction: direction === "desc" ? "desc" : "asc", field };
|
|
140
|
+
};
|
|
141
|
+
const parseRankIndexCall = (args) => {
|
|
142
|
+
const [indexName, optionsExpression] = args;
|
|
143
|
+
let sortBy = [];
|
|
144
|
+
let partitionBy;
|
|
145
|
+
if (optionsExpression && Node.isObjectLiteralExpression(optionsExpression)) {
|
|
146
|
+
const sortByProperty = optionsExpression.getProperty("sortBy");
|
|
147
|
+
if (sortByProperty && Node.isPropertyAssignment(sortByProperty)) {
|
|
148
|
+
const initializer = sortByProperty.getInitializer();
|
|
149
|
+
if (initializer && Node.isArrayLiteralExpression(initializer)) {
|
|
150
|
+
sortBy = initializer.getElements().map((element) => rankSortKeyFromElement(element)).filter((key) => key !== void 0);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
partitionBy = getStringArrayProperty(optionsExpression, "partitionBy");
|
|
154
|
+
}
|
|
155
|
+
return { name: indexNameOf(indexName), partitionBy, sortBy };
|
|
156
|
+
};
|
|
157
|
+
const parseVectorizeCall = (args, table) => {
|
|
158
|
+
const [fieldArgument, optionsExpression] = args;
|
|
159
|
+
const field = fieldArgument && Node.isStringLiteral(fieldArgument) ? fieldArgument.getLiteralText() : "_unknown_";
|
|
160
|
+
if (!optionsExpression || !Node.isObjectLiteralExpression(optionsExpression)) {
|
|
161
|
+
return void 0;
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
dimensions: getNumberProperty(optionsExpression, "dimensions"),
|
|
165
|
+
field,
|
|
166
|
+
metadata: getStringArrayProperty(optionsExpression, "metadata"),
|
|
167
|
+
metric: asMetric(getStringProperty(optionsExpression, "metric")),
|
|
168
|
+
name: getStringProperty(optionsExpression, "index") ?? "_unnamed_",
|
|
169
|
+
table
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
const parseGlobalBackend = (args) => {
|
|
173
|
+
const optionsExpression = args[0];
|
|
174
|
+
if (optionsExpression && Node.isObjectLiteralExpression(optionsExpression)) {
|
|
175
|
+
const backend = getStringProperty(optionsExpression, "backend");
|
|
176
|
+
if (backend === "hyperdrive") {
|
|
177
|
+
return "hyperdrive";
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return "d1";
|
|
181
|
+
};
|
|
182
|
+
const applyTableMethod = (accumulator, method, args, name) => {
|
|
183
|
+
switch (method) {
|
|
184
|
+
case "externallyManaged": {
|
|
185
|
+
accumulator.externallyManaged = true;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case "global": {
|
|
189
|
+
accumulator.shardMode = "global";
|
|
190
|
+
accumulator.globalBackend = parseGlobalBackend(args);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case "index": {
|
|
194
|
+
accumulator.indexes.push(parseIndexCall(args));
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
case "rankIndex": {
|
|
198
|
+
accumulator.rankIndexes.push(parseRankIndexCall(args));
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "relations": {
|
|
202
|
+
const builder = args[0];
|
|
203
|
+
if (builder) {
|
|
204
|
+
accumulator.relations.push(...parseRelations(builder));
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case "searchIndex": {
|
|
209
|
+
accumulator.searchIndexes.push(parseSearchIndexCall(args));
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
case "shardBy": {
|
|
213
|
+
const field = args[0];
|
|
214
|
+
accumulator.shardMode = { field: field && Node.isStringLiteral(field) ? field.getLiteralText() : "_unknown_", kind: "shardBy" };
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "vectorize": {
|
|
218
|
+
const vectorIndex = parseVectorizeCall(args, name);
|
|
219
|
+
if (vectorIndex) {
|
|
220
|
+
accumulator.vectorIndexes.push(vectorIndex);
|
|
221
|
+
}
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const parseTableBuilder = (expression, name) => {
|
|
227
|
+
const accumulator = {
|
|
228
|
+
externallyManaged: false,
|
|
229
|
+
indexes: [],
|
|
230
|
+
rankIndexes: [],
|
|
231
|
+
relations: [],
|
|
232
|
+
searchIndexes: [],
|
|
233
|
+
shardMode: "root",
|
|
234
|
+
vectorIndexes: []
|
|
235
|
+
};
|
|
236
|
+
let shape = {};
|
|
237
|
+
let current = expression;
|
|
238
|
+
while (Node.isCallExpression(current)) {
|
|
239
|
+
const callee = current.getExpression();
|
|
240
|
+
const args = current.getArguments();
|
|
241
|
+
if (Node.isPropertyAccessExpression(callee)) {
|
|
242
|
+
applyTableMethod(accumulator, callee.getName(), args, name);
|
|
243
|
+
current = callee.getExpression();
|
|
244
|
+
} else if (Node.isIdentifier(callee) && callee.getText() === "defineTable") {
|
|
245
|
+
const first = args[0];
|
|
246
|
+
if (first && Node.isObjectLiteralExpression(first)) {
|
|
247
|
+
shape = parseObjectShape(first);
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
} else {
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
externallyManaged: accumulator.externallyManaged,
|
|
256
|
+
globalBackend: accumulator.shardMode === "global" ? accumulator.globalBackend ?? "d1" : void 0,
|
|
257
|
+
indexes: accumulator.indexes,
|
|
258
|
+
name,
|
|
259
|
+
rankIndexes: accumulator.rankIndexes,
|
|
260
|
+
relations: accumulator.relations,
|
|
261
|
+
searchIndexes: accumulator.searchIndexes,
|
|
262
|
+
shape,
|
|
263
|
+
shardMode: accumulator.shardMode,
|
|
264
|
+
vectorIndexes: accumulator.vectorIndexes
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
const sourceTableOf = (optionsExpression) => {
|
|
268
|
+
const sourceProperty = optionsExpression.getProperty("source");
|
|
269
|
+
if (sourceProperty && Node.isPropertyAssignment(sourceProperty)) {
|
|
270
|
+
const sourceInitializer = sourceProperty.getInitializer();
|
|
271
|
+
if (sourceInitializer && Node.isObjectLiteralExpression(sourceInitializer)) {
|
|
272
|
+
return getStringProperty(sourceInitializer, "table") ?? "_unknown_";
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return "_unknown_";
|
|
276
|
+
};
|
|
277
|
+
const standaloneVectorIndexFromProperty = (property) => {
|
|
278
|
+
if (!Node.isPropertyAssignment(property)) {
|
|
279
|
+
return void 0;
|
|
280
|
+
}
|
|
281
|
+
const initializer = property.getInitializer();
|
|
282
|
+
if (!initializer || !Node.isCallExpression(initializer)) {
|
|
283
|
+
return void 0;
|
|
284
|
+
}
|
|
285
|
+
const callee = initializer.getExpression();
|
|
286
|
+
if (!Node.isIdentifier(callee) || callee.getText() !== "defineVectorIndex") {
|
|
287
|
+
return void 0;
|
|
288
|
+
}
|
|
289
|
+
const optionsExpression = initializer.getArguments()[0];
|
|
290
|
+
if (!optionsExpression || !Node.isObjectLiteralExpression(optionsExpression)) {
|
|
291
|
+
return void 0;
|
|
292
|
+
}
|
|
293
|
+
const nameNode = property.getNameNode();
|
|
294
|
+
const indexName = Node.isStringLiteral(nameNode) ? nameNode.getLiteralText() : nameNode.getText();
|
|
295
|
+
return {
|
|
296
|
+
dimensions: getNumberProperty(optionsExpression, "dimensions"),
|
|
297
|
+
metric: asMetric(getStringProperty(optionsExpression, "metric")),
|
|
298
|
+
name: indexName,
|
|
299
|
+
table: sourceTableOf(optionsExpression)
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
const parseStandaloneVectorIndexes = (object) => {
|
|
303
|
+
const result = [];
|
|
304
|
+
for (const property of object.getProperties()) {
|
|
305
|
+
const index = standaloneVectorIndexFromProperty(property);
|
|
306
|
+
if (index) {
|
|
307
|
+
result.push(index);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
};
|
|
312
|
+
const prefixTableName = (key, bareName) => `${key}_${bareName}`;
|
|
313
|
+
const rewriteReference = (target, key, bareNames) => bareNames.has(target) ? prefixTableName(key, target) : target;
|
|
314
|
+
const namespaceExtensionTable = (table, key, bareNames) => {
|
|
315
|
+
const ownPrefixed = prefixTableName(key, table.name);
|
|
316
|
+
return {
|
|
317
|
+
...table,
|
|
318
|
+
name: ownPrefixed,
|
|
319
|
+
relations: table.relations.map((relation) => {
|
|
320
|
+
return { ...relation, table: rewriteReference(relation.table, key, bareNames) };
|
|
321
|
+
}),
|
|
322
|
+
// Inline `.vectorize()` stamps `table` with the bare owner name, so it
|
|
323
|
+
// is always one of `bareNames` and resolves to `ownPrefixed`.
|
|
324
|
+
vectorIndexes: table.vectorIndexes.map((index) => {
|
|
325
|
+
return { ...index, table: rewriteReference(index.table, key, bareNames) };
|
|
326
|
+
})
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
const declarationInitializer = (declaration) => {
|
|
330
|
+
if (Node.isVariableDeclaration(declaration) || Node.isPropertyAssignment(declaration)) {
|
|
331
|
+
return declaration.getInitializer();
|
|
332
|
+
}
|
|
333
|
+
if (Node.isShorthandPropertyAssignment(declaration)) {
|
|
334
|
+
return declaration.getNameNode();
|
|
335
|
+
}
|
|
336
|
+
return void 0;
|
|
337
|
+
};
|
|
338
|
+
const objectPropertyInitializer = (objectLiteral, name) => {
|
|
339
|
+
const property = objectLiteral.getProperty(name);
|
|
340
|
+
if (property && Node.isPropertyAssignment(property)) {
|
|
341
|
+
return property.getInitializer();
|
|
342
|
+
}
|
|
343
|
+
return void 0;
|
|
344
|
+
};
|
|
345
|
+
const isInlineExtensionCall = (argument) => {
|
|
346
|
+
if (!Node.isCallExpression(argument)) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
const callee = argument.getExpression();
|
|
350
|
+
return Node.isIdentifier(callee) && callee.getText() === "defineSchemaExtension";
|
|
351
|
+
};
|
|
352
|
+
const extensionTargetIdentifier = (argument) => {
|
|
353
|
+
if (Node.isIdentifier(argument)) {
|
|
354
|
+
return argument;
|
|
355
|
+
}
|
|
356
|
+
if (Node.isPropertyAccessExpression(argument)) {
|
|
357
|
+
return argument.getNameNode();
|
|
358
|
+
}
|
|
359
|
+
return void 0;
|
|
360
|
+
};
|
|
361
|
+
const nextExpressionFromDeclaration = (declaration, argument) => {
|
|
362
|
+
const initializer = declarationInitializer(declaration);
|
|
363
|
+
if (!initializer) {
|
|
364
|
+
return void 0;
|
|
365
|
+
}
|
|
366
|
+
if (Node.isPropertyAccessExpression(argument) && Node.isObjectLiteralExpression(initializer)) {
|
|
367
|
+
return objectPropertyInitializer(initializer, argument.getName());
|
|
368
|
+
}
|
|
369
|
+
return initializer;
|
|
370
|
+
};
|
|
371
|
+
const resolveSchemaExtensionCall = (argument) => {
|
|
372
|
+
let current = argument;
|
|
373
|
+
let hops = 0;
|
|
374
|
+
while (current && hops < 32) {
|
|
375
|
+
hops += 1;
|
|
376
|
+
if (isInlineExtensionCall(current)) {
|
|
377
|
+
return current;
|
|
378
|
+
}
|
|
379
|
+
const target = extensionTargetIdentifier(current);
|
|
380
|
+
const symbol = target && Node.isIdentifier(target) ? target.getSymbol() : void 0;
|
|
381
|
+
const declarations = symbol?.getAliasedSymbol()?.getDeclarations() ?? symbol?.getDeclarations() ?? [];
|
|
382
|
+
const first = declarations[0];
|
|
383
|
+
if (!first) {
|
|
384
|
+
return void 0;
|
|
385
|
+
}
|
|
386
|
+
const declarationFile = first.getSourceFile();
|
|
387
|
+
if (declarationFile.isInNodeModules() || declarationFile.isDeclarationFile()) {
|
|
388
|
+
return void 0;
|
|
389
|
+
}
|
|
390
|
+
current = nextExpressionFromDeclaration(first, current);
|
|
391
|
+
}
|
|
392
|
+
return void 0;
|
|
393
|
+
};
|
|
394
|
+
const extensionPartsOf = (call) => {
|
|
395
|
+
const [keyArgument, optionsArgument] = call.getArguments();
|
|
396
|
+
if (!keyArgument || !Node.isStringLiteral(keyArgument)) {
|
|
397
|
+
return void 0;
|
|
398
|
+
}
|
|
399
|
+
if (!optionsArgument || !Node.isObjectLiteralExpression(optionsArgument)) {
|
|
400
|
+
return void 0;
|
|
401
|
+
}
|
|
402
|
+
return { key: keyArgument.getLiteralText(), options: optionsArgument };
|
|
403
|
+
};
|
|
404
|
+
const parseExtensionTables = (options) => {
|
|
405
|
+
const tablesProperty = options.getProperty("tables");
|
|
406
|
+
if (!tablesProperty || !Node.isPropertyAssignment(tablesProperty)) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
const tablesObject = tablesProperty.getInitializer();
|
|
410
|
+
if (!tablesObject || !Node.isObjectLiteralExpression(tablesObject)) {
|
|
411
|
+
return [];
|
|
412
|
+
}
|
|
413
|
+
const tables = [];
|
|
414
|
+
for (const property of tablesObject.getProperties()) {
|
|
415
|
+
if (!Node.isPropertyAssignment(property)) {
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
const initializer = property.getInitializer();
|
|
419
|
+
if (!initializer) {
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
tables.push(parseTableBuilder(initializer, property.getName()));
|
|
423
|
+
}
|
|
424
|
+
return tables;
|
|
425
|
+
};
|
|
426
|
+
const parseExtensionVectorIndexes = (options) => {
|
|
427
|
+
const property = options.getProperty("vectorIndexes");
|
|
428
|
+
if (!property || !Node.isPropertyAssignment(property)) {
|
|
429
|
+
return [];
|
|
430
|
+
}
|
|
431
|
+
const object = property.getInitializer();
|
|
432
|
+
if (!object || !Node.isObjectLiteralExpression(object)) {
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
return parseStandaloneVectorIndexes(object);
|
|
436
|
+
};
|
|
437
|
+
const mergeExtension = (key, options) => {
|
|
438
|
+
const bareTables = parseExtensionTables(options);
|
|
439
|
+
const bareNames = new Set(bareTables.map((table) => table.name));
|
|
440
|
+
const tables = bareTables.map((table) => namespaceExtensionTable(table, key, bareNames));
|
|
441
|
+
const vectorIndexes = parseExtensionVectorIndexes(options).map((index) => {
|
|
442
|
+
return { ...index, name: prefixTableName(key, index.name), table: rewriteReference(index.table, key, bareNames) };
|
|
443
|
+
});
|
|
444
|
+
return { tables, vectorIndexes };
|
|
445
|
+
};
|
|
446
|
+
const mergeExtendCall = (extendCall) => {
|
|
447
|
+
const extendArgument = extendCall.getArguments()[0];
|
|
448
|
+
if (!extendArgument) {
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
const resolved = resolveSchemaExtensionCall(extendArgument);
|
|
452
|
+
if (!resolved) {
|
|
453
|
+
console.warn(
|
|
454
|
+
`@lunora/codegen: skipping \`.extend(${extendArgument.getText()})\` — its \`defineSchemaExtension(...)\` definition could not be resolved from local sources (cross-package node_modules/.d.ts resolution is a deferred phase). Extension tables will be absent from the generated types.`
|
|
455
|
+
);
|
|
456
|
+
return void 0;
|
|
457
|
+
}
|
|
458
|
+
const parts = extensionPartsOf(resolved);
|
|
459
|
+
if (!parts) {
|
|
460
|
+
console.warn(`@lunora/codegen: skipping \`.extend(...)\` — \`defineSchemaExtension\` requires a string \`key\` and an options object literal.`);
|
|
461
|
+
return void 0;
|
|
462
|
+
}
|
|
463
|
+
return mergeExtension(parts.key, parts.options);
|
|
464
|
+
};
|
|
465
|
+
const extendCallsOf = (defineSchemaCall) => {
|
|
466
|
+
const calls = [];
|
|
467
|
+
let current = defineSchemaCall;
|
|
468
|
+
for (; ; ) {
|
|
469
|
+
const parent = current.getParent();
|
|
470
|
+
if (!parent || !Node.isPropertyAccessExpression(parent)) {
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
const callParent = parent.getParent();
|
|
474
|
+
if (!callParent || !Node.isCallExpression(callParent)) {
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
if (parent.getName() === "extend") {
|
|
478
|
+
calls.push(callParent);
|
|
479
|
+
}
|
|
480
|
+
current = callParent;
|
|
481
|
+
}
|
|
482
|
+
return calls;
|
|
483
|
+
};
|
|
484
|
+
const parseBaseTables = (object) => {
|
|
485
|
+
const tables = [];
|
|
486
|
+
for (const property of object.getProperties()) {
|
|
487
|
+
if (!Node.isPropertyAssignment(property)) {
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
const initializer = property.getInitializer();
|
|
491
|
+
if (initializer) {
|
|
492
|
+
const name = property.getName();
|
|
493
|
+
assertTableNameAllowed(name, property.getNameNode());
|
|
494
|
+
tables.push(parseTableBuilder(initializer, name));
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return tables;
|
|
498
|
+
};
|
|
499
|
+
const applyExtensions = (defineSchemaCall, tables) => {
|
|
500
|
+
const existingTableNames = new Set(tables.map((table) => table.name));
|
|
501
|
+
const vectorIndexes = [];
|
|
502
|
+
for (const extendCall of extendCallsOf(defineSchemaCall)) {
|
|
503
|
+
const merged = mergeExtendCall(extendCall);
|
|
504
|
+
if (!merged) {
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
for (const table of merged.tables) {
|
|
508
|
+
if (existingTableNames.has(table.name)) {
|
|
509
|
+
throw diagnosticAt(
|
|
510
|
+
extendCall,
|
|
511
|
+
`defineSchema(...).extend(...): table "${table.name}" already exists — another extension with the same key already contributed it.`
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
existingTableNames.add(table.name);
|
|
515
|
+
tables.push(table);
|
|
516
|
+
}
|
|
517
|
+
vectorIndexes.push(...merged.vectorIndexes);
|
|
518
|
+
}
|
|
519
|
+
return vectorIndexes;
|
|
520
|
+
};
|
|
521
|
+
const discoverSchema = (project, schemaPath) => {
|
|
522
|
+
const file = project.addSourceFileAtPath(schemaPath);
|
|
523
|
+
const defineSchemaCall = file.getDescendantsOfKind(SyntaxKind.CallExpression).find((call) => {
|
|
524
|
+
const callee = call.getExpression();
|
|
525
|
+
return Node.isIdentifier(callee) && callee.getText() === "defineSchema";
|
|
526
|
+
});
|
|
527
|
+
if (!defineSchemaCall) {
|
|
528
|
+
throw new Error(`defineSchema() not found in ${schemaPath}`);
|
|
529
|
+
}
|
|
530
|
+
const argument = defineSchemaCall.getArguments()[0];
|
|
531
|
+
if (!argument || !Node.isObjectLiteralExpression(argument)) {
|
|
532
|
+
throw diagnosticAt(defineSchemaCall, "defineSchema() expects an object literal");
|
|
533
|
+
}
|
|
534
|
+
const tables = parseBaseTables(argument);
|
|
535
|
+
const standaloneArgument = defineSchemaCall.getArguments()[1];
|
|
536
|
+
const standaloneVectorIndexes = standaloneArgument && Node.isObjectLiteralExpression(standaloneArgument) ? parseStandaloneVectorIndexes(standaloneArgument) : [];
|
|
537
|
+
const extensionStandaloneVectorIndexes = applyExtensions(defineSchemaCall, tables);
|
|
538
|
+
const vectorIndexes = [...tables.flatMap((table) => table.vectorIndexes), ...standaloneVectorIndexes, ...extensionStandaloneVectorIndexes];
|
|
539
|
+
return { tables, vectorIndexes };
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
export { discoverSchema as default };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Node } from 'ts-morph';
|
|
2
|
+
import { listLunoraSourceFiles, lunoraRelativePath, classifyProcedureCall } from './discoverFunctions-DEgAcRuD.mjs';
|
|
3
|
+
|
|
4
|
+
const STORAGE_OPERATIONS = /* @__PURE__ */ new Set(["delete", "list", "read", "write"]);
|
|
5
|
+
const isStorageRulesCall = (node) => {
|
|
6
|
+
if (!Node.isCallExpression(node)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const callee = node.getExpression();
|
|
10
|
+
if (Node.isIdentifier(callee)) {
|
|
11
|
+
return callee.getText() === "storageRules";
|
|
12
|
+
}
|
|
13
|
+
return Node.isPropertyAccessExpression(callee) && callee.getName() === "storageRules";
|
|
14
|
+
};
|
|
15
|
+
const storageRulesCallsInChain = (receiver) => {
|
|
16
|
+
const calls = [];
|
|
17
|
+
let node = receiver;
|
|
18
|
+
while (Node.isCallExpression(node)) {
|
|
19
|
+
const chainCallee = node.getExpression();
|
|
20
|
+
if (!Node.isPropertyAccessExpression(chainCallee)) {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
if (chainCallee.getName() === "use") {
|
|
24
|
+
const argument = node.getArguments()[0];
|
|
25
|
+
if (argument && isStorageRulesCall(argument)) {
|
|
26
|
+
calls.push(argument);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
node = chainCallee.getExpression();
|
|
30
|
+
}
|
|
31
|
+
return calls;
|
|
32
|
+
};
|
|
33
|
+
const stringPropertyOf = (object, name) => {
|
|
34
|
+
if (!Node.isObjectLiteralExpression(object)) {
|
|
35
|
+
return void 0;
|
|
36
|
+
}
|
|
37
|
+
const property = object.getProperty(name);
|
|
38
|
+
if (!property || !Node.isPropertyAssignment(property)) {
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
const initializer = property.getInitializer();
|
|
42
|
+
return initializer && Node.isStringLiteral(initializer) ? initializer.getLiteralText() : void 0;
|
|
43
|
+
};
|
|
44
|
+
const extractRules = (call, file, procedure) => {
|
|
45
|
+
const argument = call.getArguments()[0];
|
|
46
|
+
if (!argument || !Node.isArrayLiteralExpression(argument)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
const rules = [];
|
|
50
|
+
for (const element of argument.getElements()) {
|
|
51
|
+
if (!Node.isObjectLiteralExpression(element)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const on = stringPropertyOf(element, "on");
|
|
55
|
+
if (on === void 0 || !STORAGE_OPERATIONS.has(on)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const prefix = stringPropertyOf(element, "prefix");
|
|
59
|
+
const rule = { bucket: stringPropertyOf(element, "bucket") ?? "", file, on, procedure };
|
|
60
|
+
if (prefix !== void 0) {
|
|
61
|
+
rule.prefix = prefix;
|
|
62
|
+
}
|
|
63
|
+
rules.push(rule);
|
|
64
|
+
}
|
|
65
|
+
return rules;
|
|
66
|
+
};
|
|
67
|
+
const exportedProcedureChains = (sourceFile) => {
|
|
68
|
+
const chains = [];
|
|
69
|
+
for (const statement of sourceFile.getVariableStatements()) {
|
|
70
|
+
if (!statement.isExported()) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
for (const declaration of statement.getDeclarations()) {
|
|
74
|
+
const initializer = declaration.getInitializer();
|
|
75
|
+
const classified = initializer && Node.isCallExpression(initializer) ? classifyProcedureCall(initializer) : void 0;
|
|
76
|
+
if (classified?.receiver) {
|
|
77
|
+
chains.push({ name: declaration.getName(), receiver: classified.receiver });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return chains;
|
|
82
|
+
};
|
|
83
|
+
const discoverStorageRulesMetadata = (project, lunoraDirectory) => {
|
|
84
|
+
const rules = [];
|
|
85
|
+
for (const filePath of listLunoraSourceFiles(lunoraDirectory)) {
|
|
86
|
+
const sourceFile = project.getSourceFile(filePath) ?? project.addSourceFileAtPath(filePath);
|
|
87
|
+
const relativePath = lunoraRelativePath(lunoraDirectory, filePath);
|
|
88
|
+
for (const { name, receiver } of exportedProcedureChains(sourceFile)) {
|
|
89
|
+
for (const call of storageRulesCallsInChain(receiver)) {
|
|
90
|
+
rules.push(...extractRules(call, relativePath, name));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { rules };
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export { discoverStorageRulesMetadata as default };
|