@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.d.cts
CHANGED
|
@@ -90,10 +90,8 @@ declare class PrismaSchemaGenerator {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
declare class TsSchemaGenerator {
|
|
93
|
-
generate(schemaFile: string, pluginModelFiles: string[],
|
|
94
|
-
|
|
95
|
-
warnings: string[];
|
|
96
|
-
}>;
|
|
93
|
+
generate(schemaFile: string, pluginModelFiles: string[], outputDir: string): Promise<void>;
|
|
94
|
+
private generateSchema;
|
|
97
95
|
private generateSchemaStatements;
|
|
98
96
|
private createSchemaObject;
|
|
99
97
|
private createProviderObject;
|
|
@@ -134,6 +132,10 @@ declare class TsSchemaGenerator {
|
|
|
134
132
|
private createRefExpression;
|
|
135
133
|
private createCallExpression;
|
|
136
134
|
private createLiteralExpression;
|
|
135
|
+
private generateModels;
|
|
136
|
+
private generateSchemaTypeImport;
|
|
137
|
+
private generateDocs;
|
|
138
|
+
private generateInputTypes;
|
|
137
139
|
}
|
|
138
140
|
|
|
139
141
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -90,10 +90,8 @@ declare class PrismaSchemaGenerator {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
declare class TsSchemaGenerator {
|
|
93
|
-
generate(schemaFile: string, pluginModelFiles: string[],
|
|
94
|
-
|
|
95
|
-
warnings: string[];
|
|
96
|
-
}>;
|
|
93
|
+
generate(schemaFile: string, pluginModelFiles: string[], outputDir: string): Promise<void>;
|
|
94
|
+
private generateSchema;
|
|
97
95
|
private generateSchemaStatements;
|
|
98
96
|
private createSchemaObject;
|
|
99
97
|
private createProviderObject;
|
|
@@ -134,6 +132,10 @@ declare class TsSchemaGenerator {
|
|
|
134
132
|
private createRefExpression;
|
|
135
133
|
private createCallExpression;
|
|
136
134
|
private createLiteralExpression;
|
|
135
|
+
private generateModels;
|
|
136
|
+
private generateSchemaTypeImport;
|
|
137
|
+
private generateDocs;
|
|
138
|
+
private generateInputTypes;
|
|
137
139
|
}
|
|
138
140
|
|
|
139
141
|
/**
|
package/dist/index.js
CHANGED
|
@@ -787,26 +787,28 @@ var TsSchemaGenerator = class {
|
|
|
787
787
|
static {
|
|
788
788
|
__name(this, "TsSchemaGenerator");
|
|
789
789
|
}
|
|
790
|
-
async generate(schemaFile, pluginModelFiles,
|
|
790
|
+
async generate(schemaFile, pluginModelFiles, outputDir) {
|
|
791
791
|
const loaded = await loadDocument(schemaFile, pluginModelFiles);
|
|
792
792
|
if (!loaded.success) {
|
|
793
793
|
throw new Error(`Error loading schema:${loaded.errors.join("\n")}`);
|
|
794
794
|
}
|
|
795
|
-
const { model
|
|
795
|
+
const { model } = loaded;
|
|
796
|
+
fs.mkdirSync(outputDir, {
|
|
797
|
+
recursive: true
|
|
798
|
+
});
|
|
799
|
+
this.generateSchema(model, outputDir);
|
|
800
|
+
this.generateModels(model, outputDir);
|
|
801
|
+
this.generateInputTypes(model, outputDir);
|
|
802
|
+
}
|
|
803
|
+
generateSchema(model, outputDir) {
|
|
796
804
|
const statements = [];
|
|
797
805
|
this.generateSchemaStatements(model, statements);
|
|
798
806
|
this.generateBannerComments(statements);
|
|
799
|
-
const
|
|
807
|
+
const schemaOutputFile = path.join(outputDir, "schema.ts");
|
|
808
|
+
const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
800
809
|
const printer = ts.createPrinter();
|
|
801
810
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
802
|
-
fs.
|
|
803
|
-
recursive: true
|
|
804
|
-
});
|
|
805
|
-
fs.writeFileSync(outputFile, result);
|
|
806
|
-
return {
|
|
807
|
-
model,
|
|
808
|
-
warnings
|
|
809
|
-
};
|
|
811
|
+
fs.writeFileSync(schemaOutputFile, result);
|
|
810
812
|
}
|
|
811
813
|
generateSchemaStatements(model, statements) {
|
|
812
814
|
const hasComputedFields = model.declarations.some((d) => isDataModel2(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
@@ -1318,6 +1320,132 @@ var TsSchemaGenerator = class {
|
|
|
1318
1320
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
1319
1321
|
});
|
|
1320
1322
|
}
|
|
1323
|
+
generateModels(model, outputDir) {
|
|
1324
|
+
const statements = [];
|
|
1325
|
+
statements.push(this.generateSchemaTypeImport(true, true));
|
|
1326
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1327
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`))
|
|
1328
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1329
|
+
const dataModels = model.declarations.filter(isDataModel2);
|
|
1330
|
+
for (const dm of dataModels) {
|
|
1331
|
+
let modelType = ts.factory.createTypeAliasDeclaration([
|
|
1332
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1333
|
+
], dm.name, void 0, ts.factory.createTypeReferenceNode("$ModelResult", [
|
|
1334
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1335
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1336
|
+
]));
|
|
1337
|
+
if (dm.comments.length > 0) {
|
|
1338
|
+
modelType = this.generateDocs(modelType, dm);
|
|
1339
|
+
}
|
|
1340
|
+
statements.push(modelType);
|
|
1341
|
+
}
|
|
1342
|
+
const enums = model.declarations.filter(isEnum);
|
|
1343
|
+
for (const e of enums) {
|
|
1344
|
+
let enumDecl = ts.factory.createVariableStatement([
|
|
1345
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1346
|
+
], ts.factory.createVariableDeclarationList([
|
|
1347
|
+
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)))
|
|
1348
|
+
], ts.NodeFlags.Const));
|
|
1349
|
+
if (e.comments.length > 0) {
|
|
1350
|
+
enumDecl = this.generateDocs(enumDecl, e);
|
|
1351
|
+
}
|
|
1352
|
+
statements.push(enumDecl);
|
|
1353
|
+
let typeAlias = ts.factory.createTypeAliasDeclaration([
|
|
1354
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1355
|
+
], 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)))));
|
|
1356
|
+
if (e.comments.length > 0) {
|
|
1357
|
+
typeAlias = this.generateDocs(typeAlias, e);
|
|
1358
|
+
}
|
|
1359
|
+
statements.push(typeAlias);
|
|
1360
|
+
}
|
|
1361
|
+
this.generateBannerComments(statements);
|
|
1362
|
+
const outputFile = path.join(outputDir, "models.ts");
|
|
1363
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1364
|
+
const printer = ts.createPrinter();
|
|
1365
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1366
|
+
fs.writeFileSync(outputFile, result);
|
|
1367
|
+
}
|
|
1368
|
+
generateSchemaTypeImport(schemaObject, schemaType) {
|
|
1369
|
+
const importSpecifiers = [];
|
|
1370
|
+
if (schemaObject) {
|
|
1371
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("schema"), ts.factory.createIdentifier("$schema")));
|
|
1372
|
+
}
|
|
1373
|
+
if (schemaType) {
|
|
1374
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
|
|
1375
|
+
}
|
|
1376
|
+
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
|
|
1377
|
+
}
|
|
1378
|
+
generateDocs(tsDecl, decl) {
|
|
1379
|
+
return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
|
|
1380
|
+
* ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
|
|
1381
|
+
`, true);
|
|
1382
|
+
}
|
|
1383
|
+
generateInputTypes(model, outputDir) {
|
|
1384
|
+
const dataModels = model.declarations.filter(isDataModel2);
|
|
1385
|
+
const statements = [];
|
|
1386
|
+
statements.push(this.generateSchemaTypeImport(false, true));
|
|
1387
|
+
const inputTypes = [
|
|
1388
|
+
"FindManyArgs",
|
|
1389
|
+
"FindUniqueArgs",
|
|
1390
|
+
"FindFirstArgs",
|
|
1391
|
+
"CreateArgs",
|
|
1392
|
+
"CreateManyArgs",
|
|
1393
|
+
"CreateManyAndReturnArgs",
|
|
1394
|
+
"UpdateArgs",
|
|
1395
|
+
"UpdateManyArgs",
|
|
1396
|
+
"UpdateManyAndReturnArgs",
|
|
1397
|
+
"UpsertArgs",
|
|
1398
|
+
"DeleteArgs",
|
|
1399
|
+
"DeleteManyArgs",
|
|
1400
|
+
"CountArgs",
|
|
1401
|
+
"AggregateArgs",
|
|
1402
|
+
"GroupByArgs",
|
|
1403
|
+
"WhereInput",
|
|
1404
|
+
"SelectInput",
|
|
1405
|
+
"IncludeInput",
|
|
1406
|
+
"OmitInput"
|
|
1407
|
+
];
|
|
1408
|
+
const inputTypeNameFixes = {
|
|
1409
|
+
SelectInput: "Select",
|
|
1410
|
+
IncludeInput: "Include",
|
|
1411
|
+
OmitInput: "Omit"
|
|
1412
|
+
};
|
|
1413
|
+
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")));
|
|
1414
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
|
|
1415
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedModelResult as $SimplifiedModelResult")),
|
|
1416
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SelectIncludeOmit as $SelectIncludeOmit"))
|
|
1417
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1418
|
+
for (const dm of dataModels) {
|
|
1419
|
+
for (const inputType of inputTypes) {
|
|
1420
|
+
const exportName = inputTypeNameFixes[inputType] ? `${dm.name}${inputTypeNameFixes[inputType]}` : `${dm.name}${inputType}`;
|
|
1421
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1422
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1423
|
+
], exportName, void 0, ts.factory.createTypeReferenceNode(`$${inputType}`, [
|
|
1424
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1425
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1426
|
+
])));
|
|
1427
|
+
}
|
|
1428
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1429
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1430
|
+
], `${dm.name}GetPayload`, [
|
|
1431
|
+
ts.factory.createTypeParameterDeclaration(void 0, "Args", ts.factory.createTypeReferenceNode("$SelectIncludeOmit", [
|
|
1432
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1433
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1434
|
+
ts.factory.createLiteralTypeNode(ts.factory.createTrue())
|
|
1435
|
+
]))
|
|
1436
|
+
], ts.factory.createTypeReferenceNode("$SimplifiedModelResult", [
|
|
1437
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1438
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1439
|
+
ts.factory.createTypeReferenceNode("Args")
|
|
1440
|
+
])));
|
|
1441
|
+
}
|
|
1442
|
+
this.generateBannerComments(statements);
|
|
1443
|
+
const outputFile = path.join(outputDir, "input.ts");
|
|
1444
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1445
|
+
const printer = ts.createPrinter();
|
|
1446
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1447
|
+
fs.writeFileSync(outputFile, result);
|
|
1448
|
+
}
|
|
1321
1449
|
};
|
|
1322
1450
|
|
|
1323
1451
|
// src/zmodel-code-generator.ts
|