@zenstackhq/sdk 3.0.0-beta.15 → 3.0.0-beta.17
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 +54 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +54 -39
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.cts
CHANGED
|
@@ -121,9 +121,14 @@ declare class PrismaSchemaGenerator {
|
|
|
121
121
|
private truncate;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
type TsSchemaGeneratorOptions = {
|
|
125
|
+
outDir: string;
|
|
126
|
+
lite?: boolean;
|
|
127
|
+
liteOnly?: boolean;
|
|
128
|
+
};
|
|
124
129
|
declare class TsSchemaGenerator {
|
|
125
130
|
private usedExpressionUtils;
|
|
126
|
-
generate(model: Model,
|
|
131
|
+
generate(model: Model, options: TsSchemaGeneratorOptions): Promise<void>;
|
|
127
132
|
private generateSchema;
|
|
128
133
|
private generateSchemaStatements;
|
|
129
134
|
private createExpressionUtilsCall;
|
|
@@ -241,4 +246,4 @@ declare class ZModelCodeGenerator {
|
|
|
241
246
|
private isCollectionPredicateOperator;
|
|
242
247
|
}
|
|
243
248
|
|
|
244
|
-
export { type CliGeneratorContext, type CliPlugin, modelUtils as ModelUtils, PrismaSchemaGenerator, TsSchemaGenerator, ZModelCodeGenerator, type ZModelCodeOptions };
|
|
249
|
+
export { type CliGeneratorContext, type CliPlugin, modelUtils as ModelUtils, PrismaSchemaGenerator, TsSchemaGenerator, type TsSchemaGeneratorOptions, ZModelCodeGenerator, type ZModelCodeOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -121,9 +121,14 @@ declare class PrismaSchemaGenerator {
|
|
|
121
121
|
private truncate;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
type TsSchemaGeneratorOptions = {
|
|
125
|
+
outDir: string;
|
|
126
|
+
lite?: boolean;
|
|
127
|
+
liteOnly?: boolean;
|
|
128
|
+
};
|
|
124
129
|
declare class TsSchemaGenerator {
|
|
125
130
|
private usedExpressionUtils;
|
|
126
|
-
generate(model: Model,
|
|
131
|
+
generate(model: Model, options: TsSchemaGeneratorOptions): Promise<void>;
|
|
127
132
|
private generateSchema;
|
|
128
133
|
private generateSchemaStatements;
|
|
129
134
|
private createExpressionUtilsCall;
|
|
@@ -241,4 +246,4 @@ declare class ZModelCodeGenerator {
|
|
|
241
246
|
private isCollectionPredicateOperator;
|
|
242
247
|
}
|
|
243
248
|
|
|
244
|
-
export { type CliGeneratorContext, type CliPlugin, modelUtils as ModelUtils, PrismaSchemaGenerator, TsSchemaGenerator, ZModelCodeGenerator, type ZModelCodeOptions };
|
|
249
|
+
export { type CliGeneratorContext, type CliPlugin, modelUtils as ModelUtils, PrismaSchemaGenerator, TsSchemaGenerator, type TsSchemaGeneratorOptions, ZModelCodeGenerator, type ZModelCodeOptions };
|
package/dist/index.js
CHANGED
|
@@ -792,28 +792,43 @@ var TsSchemaGenerator = class {
|
|
|
792
792
|
__name(this, "TsSchemaGenerator");
|
|
793
793
|
}
|
|
794
794
|
usedExpressionUtils = false;
|
|
795
|
-
async generate(model,
|
|
796
|
-
fs.mkdirSync(
|
|
795
|
+
async generate(model, options) {
|
|
796
|
+
fs.mkdirSync(options.outDir, {
|
|
797
797
|
recursive: true
|
|
798
798
|
});
|
|
799
799
|
this.usedExpressionUtils = false;
|
|
800
|
-
this.generateSchema(model,
|
|
801
|
-
this.generateModelsAndTypeDefs(model,
|
|
802
|
-
this.generateInputTypes(model,
|
|
803
|
-
}
|
|
804
|
-
generateSchema(model,
|
|
805
|
-
const
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
800
|
+
this.generateSchema(model, options);
|
|
801
|
+
this.generateModelsAndTypeDefs(model, options);
|
|
802
|
+
this.generateInputTypes(model, options);
|
|
803
|
+
}
|
|
804
|
+
generateSchema(model, options) {
|
|
805
|
+
const targets = [];
|
|
806
|
+
if (!options.liteOnly) {
|
|
807
|
+
targets.push({
|
|
808
|
+
lite: false,
|
|
809
|
+
file: "schema.ts"
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
if (options.lite || options.liteOnly) {
|
|
813
|
+
targets.push({
|
|
814
|
+
lite: true,
|
|
815
|
+
file: "schema-lite.ts"
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
for (const { lite, file } of targets) {
|
|
819
|
+
const statements = [];
|
|
820
|
+
this.generateSchemaStatements(model, statements, lite);
|
|
821
|
+
this.generateBannerComments(statements);
|
|
822
|
+
const schemaOutputFile = path.join(options.outDir, file);
|
|
823
|
+
const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
824
|
+
const printer = ts.createPrinter();
|
|
825
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
826
|
+
fs.writeFileSync(schemaOutputFile, result);
|
|
827
|
+
}
|
|
813
828
|
}
|
|
814
|
-
generateSchemaStatements(model, statements) {
|
|
829
|
+
generateSchemaStatements(model, statements, lite) {
|
|
815
830
|
const hasComputedFields = model.declarations.some((d) => isDataModel3(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
816
|
-
const schemaObject = this.createSchemaObject(model);
|
|
831
|
+
const schemaObject = this.createSchemaObject(model, lite);
|
|
817
832
|
const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
818
833
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
|
|
819
834
|
...hasComputedFields ? [
|
|
@@ -839,15 +854,15 @@ var TsSchemaGenerator = class {
|
|
|
839
854
|
this.usedExpressionUtils = true;
|
|
840
855
|
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
|
|
841
856
|
}
|
|
842
|
-
createSchemaObject(model) {
|
|
857
|
+
createSchemaObject(model, lite) {
|
|
843
858
|
const properties = [
|
|
844
859
|
// provider
|
|
845
860
|
ts.factory.createPropertyAssignment("provider", this.createProviderObject(model)),
|
|
846
861
|
// models
|
|
847
|
-
ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
|
|
862
|
+
ts.factory.createPropertyAssignment("models", this.createModelsObject(model, lite)),
|
|
848
863
|
// typeDefs
|
|
849
864
|
...model.declarations.some(isTypeDef3) ? [
|
|
850
|
-
ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
|
|
865
|
+
ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model, lite))
|
|
851
866
|
] : []
|
|
852
867
|
];
|
|
853
868
|
const enums = model.declarations.filter(isEnum);
|
|
@@ -871,15 +886,15 @@ var TsSchemaGenerator = class {
|
|
|
871
886
|
ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider.type))
|
|
872
887
|
], true);
|
|
873
888
|
}
|
|
874
|
-
createModelsObject(model) {
|
|
875
|
-
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isDataModel3(d) && !hasAttribute(d, "@@ignore")).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm))), true);
|
|
889
|
+
createModelsObject(model, lite) {
|
|
890
|
+
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isDataModel3(d) && !hasAttribute(d, "@@ignore")).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite))), true);
|
|
876
891
|
}
|
|
877
|
-
createTypeDefsObject(model) {
|
|
878
|
-
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isTypeDef3(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
|
|
892
|
+
createTypeDefsObject(model, lite) {
|
|
893
|
+
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isTypeDef3(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite))), true);
|
|
879
894
|
}
|
|
880
|
-
createDataModelObject(dm) {
|
|
895
|
+
createDataModelObject(dm, lite) {
|
|
881
896
|
const allFields = getAllFields3(dm);
|
|
882
|
-
const allAttributes = getAllAttributes2(dm).filter((attr) => {
|
|
897
|
+
const allAttributes = lite ? [] : getAllAttributes2(dm).filter((attr) => {
|
|
883
898
|
if (attr.decl.$refText === "@@delegate" && attr.$container !== dm) {
|
|
884
899
|
return false;
|
|
885
900
|
}
|
|
@@ -894,7 +909,7 @@ var TsSchemaGenerator = class {
|
|
|
894
909
|
ts.factory.createPropertyAssignment("baseModel", ts.factory.createStringLiteral(dm.baseModel.$refText))
|
|
895
910
|
] : [],
|
|
896
911
|
// fields
|
|
897
|
-
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm))), true)),
|
|
912
|
+
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm, lite))), true)),
|
|
898
913
|
// attributes
|
|
899
914
|
...allAttributes.length > 0 ? [
|
|
900
915
|
ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
|
|
@@ -924,14 +939,14 @@ var TsSchemaGenerator = class {
|
|
|
924
939
|
getSubModels(dm) {
|
|
925
940
|
return dm.$container.declarations.filter(isDataModel3).filter((d) => d.baseModel?.ref === dm).map((d) => d.name);
|
|
926
941
|
}
|
|
927
|
-
createTypeDefObject(td) {
|
|
942
|
+
createTypeDefObject(td, lite) {
|
|
928
943
|
const allFields = getAllFields3(td);
|
|
929
944
|
const allAttributes = getAllAttributes2(td);
|
|
930
945
|
const fields = [
|
|
931
946
|
// name
|
|
932
947
|
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(td.name)),
|
|
933
948
|
// fields
|
|
934
|
-
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0))), true)),
|
|
949
|
+
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
|
|
935
950
|
// attributes
|
|
936
951
|
...allAttributes.length > 0 ? [
|
|
937
952
|
ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
|
|
@@ -963,7 +978,7 @@ var TsSchemaGenerator = class {
|
|
|
963
978
|
}
|
|
964
979
|
return result;
|
|
965
980
|
}
|
|
966
|
-
createDataFieldObject(field, contextModel) {
|
|
981
|
+
createDataFieldObject(field, contextModel, lite) {
|
|
967
982
|
const objectFields = [
|
|
968
983
|
// name
|
|
969
984
|
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
|
|
@@ -992,7 +1007,7 @@ var TsSchemaGenerator = class {
|
|
|
992
1007
|
if (this.isDiscriminatorField(field)) {
|
|
993
1008
|
objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
|
|
994
1009
|
}
|
|
995
|
-
if (field.attributes.length > 0) {
|
|
1010
|
+
if (!lite && field.attributes.length > 0) {
|
|
996
1011
|
objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
|
|
997
1012
|
}
|
|
998
1013
|
const defaultValue = this.getFieldMappedDefault(field);
|
|
@@ -1390,9 +1405,9 @@ var TsSchemaGenerator = class {
|
|
|
1390
1405
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
1391
1406
|
});
|
|
1392
1407
|
}
|
|
1393
|
-
generateModelsAndTypeDefs(model,
|
|
1408
|
+
generateModelsAndTypeDefs(model, options) {
|
|
1394
1409
|
const statements = [];
|
|
1395
|
-
statements.push(this.generateSchemaImport(model, true, true));
|
|
1410
|
+
statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
|
|
1396
1411
|
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1397
1412
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
|
|
1398
1413
|
...model.declarations.some(isTypeDef3) ? [
|
|
@@ -1445,13 +1460,13 @@ var TsSchemaGenerator = class {
|
|
|
1445
1460
|
statements.push(typeAlias);
|
|
1446
1461
|
}
|
|
1447
1462
|
this.generateBannerComments(statements);
|
|
1448
|
-
const outputFile = path.join(
|
|
1463
|
+
const outputFile = path.join(options.outDir, "models.ts");
|
|
1449
1464
|
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1450
1465
|
const printer = ts.createPrinter();
|
|
1451
1466
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1452
1467
|
fs.writeFileSync(outputFile, result);
|
|
1453
1468
|
}
|
|
1454
|
-
generateSchemaImport(model, schemaObject, schemaType) {
|
|
1469
|
+
generateSchemaImport(model, schemaObject, schemaType, useLite) {
|
|
1455
1470
|
const importSpecifiers = [];
|
|
1456
1471
|
if (schemaObject) {
|
|
1457
1472
|
if (model.declarations.some(isEnum)) {
|
|
@@ -1461,17 +1476,17 @@ var TsSchemaGenerator = class {
|
|
|
1461
1476
|
if (schemaType) {
|
|
1462
1477
|
importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
|
|
1463
1478
|
}
|
|
1464
|
-
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
|
|
1479
|
+
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(useLite ? "./schema-lite" : "./schema"));
|
|
1465
1480
|
}
|
|
1466
1481
|
generateDocs(tsDecl, decl) {
|
|
1467
1482
|
return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
|
|
1468
1483
|
* ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
|
|
1469
1484
|
`, true);
|
|
1470
1485
|
}
|
|
1471
|
-
generateInputTypes(model,
|
|
1486
|
+
generateInputTypes(model, options) {
|
|
1472
1487
|
const dataModels = model.declarations.filter(isDataModel3);
|
|
1473
1488
|
const statements = [];
|
|
1474
|
-
statements.push(this.generateSchemaImport(model, false, true));
|
|
1489
|
+
statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
|
|
1475
1490
|
const inputTypes = [
|
|
1476
1491
|
"FindManyArgs",
|
|
1477
1492
|
"FindUniqueArgs",
|
|
@@ -1528,7 +1543,7 @@ var TsSchemaGenerator = class {
|
|
|
1528
1543
|
])));
|
|
1529
1544
|
}
|
|
1530
1545
|
this.generateBannerComments(statements);
|
|
1531
|
-
const outputFile = path.join(
|
|
1546
|
+
const outputFile = path.join(options.outDir, "input.ts");
|
|
1532
1547
|
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1533
1548
|
const printer = ts.createPrinter();
|
|
1534
1549
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|