@zenstackhq/sdk 3.0.0-alpha.10 → 3.0.0-alpha.11
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.cjs +139 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +139 -11
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -820,26 +820,28 @@ var TsSchemaGenerator = class {
|
|
|
820
820
|
static {
|
|
821
821
|
__name(this, "TsSchemaGenerator");
|
|
822
822
|
}
|
|
823
|
-
async generate(schemaFile, pluginModelFiles,
|
|
823
|
+
async generate(schemaFile, pluginModelFiles, outputDir) {
|
|
824
824
|
const loaded = await (0, import_language.loadDocument)(schemaFile, pluginModelFiles);
|
|
825
825
|
if (!loaded.success) {
|
|
826
826
|
throw new Error(`Error loading schema:${loaded.errors.join("\n")}`);
|
|
827
827
|
}
|
|
828
|
-
const { model
|
|
828
|
+
const { model } = loaded;
|
|
829
|
+
import_node_fs.default.mkdirSync(outputDir, {
|
|
830
|
+
recursive: true
|
|
831
|
+
});
|
|
832
|
+
this.generateSchema(model, outputDir);
|
|
833
|
+
this.generateModels(model, outputDir);
|
|
834
|
+
this.generateInputTypes(model, outputDir);
|
|
835
|
+
}
|
|
836
|
+
generateSchema(model, outputDir) {
|
|
829
837
|
const statements = [];
|
|
830
838
|
this.generateSchemaStatements(model, statements);
|
|
831
839
|
this.generateBannerComments(statements);
|
|
832
|
-
const
|
|
840
|
+
const schemaOutputFile = import_node_path.default.join(outputDir, "schema.ts");
|
|
841
|
+
const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
833
842
|
const printer = ts.createPrinter();
|
|
834
843
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
835
|
-
import_node_fs.default.
|
|
836
|
-
recursive: true
|
|
837
|
-
});
|
|
838
|
-
import_node_fs.default.writeFileSync(outputFile, result);
|
|
839
|
-
return {
|
|
840
|
-
model,
|
|
841
|
-
warnings
|
|
842
|
-
};
|
|
844
|
+
import_node_fs.default.writeFileSync(schemaOutputFile, result);
|
|
843
845
|
}
|
|
844
846
|
generateSchemaStatements(model, statements) {
|
|
845
847
|
const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
@@ -1351,6 +1353,132 @@ var TsSchemaGenerator = class {
|
|
|
1351
1353
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
1352
1354
|
});
|
|
1353
1355
|
}
|
|
1356
|
+
generateModels(model, outputDir) {
|
|
1357
|
+
const statements = [];
|
|
1358
|
+
statements.push(this.generateSchemaTypeImport(true, true));
|
|
1359
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1360
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`))
|
|
1361
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1362
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1363
|
+
for (const dm of dataModels) {
|
|
1364
|
+
let modelType = ts.factory.createTypeAliasDeclaration([
|
|
1365
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1366
|
+
], dm.name, void 0, ts.factory.createTypeReferenceNode("$ModelResult", [
|
|
1367
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1368
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1369
|
+
]));
|
|
1370
|
+
if (dm.comments.length > 0) {
|
|
1371
|
+
modelType = this.generateDocs(modelType, dm);
|
|
1372
|
+
}
|
|
1373
|
+
statements.push(modelType);
|
|
1374
|
+
}
|
|
1375
|
+
const enums = model.declarations.filter(import_ast3.isEnum);
|
|
1376
|
+
for (const e of enums) {
|
|
1377
|
+
let enumDecl = ts.factory.createVariableStatement([
|
|
1378
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1379
|
+
], ts.factory.createVariableDeclarationList([
|
|
1380
|
+
ts.factory.createVariableDeclaration(e.name, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("$schema"), ts.factory.createIdentifier("enums")), ts.factory.createIdentifier(e.name)))
|
|
1381
|
+
], ts.NodeFlags.Const));
|
|
1382
|
+
if (e.comments.length > 0) {
|
|
1383
|
+
enumDecl = this.generateDocs(enumDecl, e);
|
|
1384
|
+
}
|
|
1385
|
+
statements.push(enumDecl);
|
|
1386
|
+
let typeAlias = ts.factory.createTypeAliasDeclaration([
|
|
1387
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1388
|
+
], e.name, void 0, ts.factory.createIndexedAccessTypeNode(ts.factory.createTypeQueryNode(ts.factory.createIdentifier(e.name)), ts.factory.createTypeOperatorNode(ts.SyntaxKind.KeyOfKeyword, ts.factory.createTypeQueryNode(ts.factory.createIdentifier(e.name)))));
|
|
1389
|
+
if (e.comments.length > 0) {
|
|
1390
|
+
typeAlias = this.generateDocs(typeAlias, e);
|
|
1391
|
+
}
|
|
1392
|
+
statements.push(typeAlias);
|
|
1393
|
+
}
|
|
1394
|
+
this.generateBannerComments(statements);
|
|
1395
|
+
const outputFile = import_node_path.default.join(outputDir, "models.ts");
|
|
1396
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1397
|
+
const printer = ts.createPrinter();
|
|
1398
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1399
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1400
|
+
}
|
|
1401
|
+
generateSchemaTypeImport(schemaObject, schemaType) {
|
|
1402
|
+
const importSpecifiers = [];
|
|
1403
|
+
if (schemaObject) {
|
|
1404
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("schema"), ts.factory.createIdentifier("$schema")));
|
|
1405
|
+
}
|
|
1406
|
+
if (schemaType) {
|
|
1407
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
|
|
1408
|
+
}
|
|
1409
|
+
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
|
|
1410
|
+
}
|
|
1411
|
+
generateDocs(tsDecl, decl) {
|
|
1412
|
+
return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
|
|
1413
|
+
* ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
|
|
1414
|
+
`, true);
|
|
1415
|
+
}
|
|
1416
|
+
generateInputTypes(model, outputDir) {
|
|
1417
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1418
|
+
const statements = [];
|
|
1419
|
+
statements.push(this.generateSchemaTypeImport(false, true));
|
|
1420
|
+
const inputTypes = [
|
|
1421
|
+
"FindManyArgs",
|
|
1422
|
+
"FindUniqueArgs",
|
|
1423
|
+
"FindFirstArgs",
|
|
1424
|
+
"CreateArgs",
|
|
1425
|
+
"CreateManyArgs",
|
|
1426
|
+
"CreateManyAndReturnArgs",
|
|
1427
|
+
"UpdateArgs",
|
|
1428
|
+
"UpdateManyArgs",
|
|
1429
|
+
"UpdateManyAndReturnArgs",
|
|
1430
|
+
"UpsertArgs",
|
|
1431
|
+
"DeleteArgs",
|
|
1432
|
+
"DeleteManyArgs",
|
|
1433
|
+
"CountArgs",
|
|
1434
|
+
"AggregateArgs",
|
|
1435
|
+
"GroupByArgs",
|
|
1436
|
+
"WhereInput",
|
|
1437
|
+
"SelectInput",
|
|
1438
|
+
"IncludeInput",
|
|
1439
|
+
"OmitInput"
|
|
1440
|
+
];
|
|
1441
|
+
const inputTypeNameFixes = {
|
|
1442
|
+
SelectInput: "Select",
|
|
1443
|
+
IncludeInput: "Include",
|
|
1444
|
+
OmitInput: "Omit"
|
|
1445
|
+
};
|
|
1446
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports(inputTypes.map((inputType) => ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier(`${inputType} as $${inputType}`))))), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1447
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
|
|
1448
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedModelResult as $SimplifiedModelResult")),
|
|
1449
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SelectIncludeOmit as $SelectIncludeOmit"))
|
|
1450
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1451
|
+
for (const dm of dataModels) {
|
|
1452
|
+
for (const inputType of inputTypes) {
|
|
1453
|
+
const exportName = inputTypeNameFixes[inputType] ? `${dm.name}${inputTypeNameFixes[inputType]}` : `${dm.name}${inputType}`;
|
|
1454
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1455
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1456
|
+
], exportName, void 0, ts.factory.createTypeReferenceNode(`$${inputType}`, [
|
|
1457
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1458
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1459
|
+
])));
|
|
1460
|
+
}
|
|
1461
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1462
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1463
|
+
], `${dm.name}GetPayload`, [
|
|
1464
|
+
ts.factory.createTypeParameterDeclaration(void 0, "Args", ts.factory.createTypeReferenceNode("$SelectIncludeOmit", [
|
|
1465
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1466
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1467
|
+
ts.factory.createLiteralTypeNode(ts.factory.createTrue())
|
|
1468
|
+
]))
|
|
1469
|
+
], ts.factory.createTypeReferenceNode("$SimplifiedModelResult", [
|
|
1470
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1471
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1472
|
+
ts.factory.createTypeReferenceNode("Args")
|
|
1473
|
+
])));
|
|
1474
|
+
}
|
|
1475
|
+
this.generateBannerComments(statements);
|
|
1476
|
+
const outputFile = import_node_path.default.join(outputDir, "input.ts");
|
|
1477
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1478
|
+
const printer = ts.createPrinter();
|
|
1479
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1480
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1481
|
+
}
|
|
1354
1482
|
};
|
|
1355
1483
|
|
|
1356
1484
|
// src/zmodel-code-generator.ts
|