@zenstackhq/sdk 3.0.0-beta.3 → 3.0.0-beta.4
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 +26 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +35 -26
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.cts
CHANGED
|
@@ -122,9 +122,11 @@ declare class PrismaSchemaGenerator {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
declare class TsSchemaGenerator {
|
|
125
|
+
private usedExpressionUtils;
|
|
125
126
|
generate(model: Model, outputDir: string): Promise<void>;
|
|
126
127
|
private generateSchema;
|
|
127
128
|
private generateSchemaStatements;
|
|
129
|
+
private createExpressionUtilsCall;
|
|
128
130
|
private createSchemaObject;
|
|
129
131
|
private createProviderObject;
|
|
130
132
|
private createModelsObject;
|
package/dist/index.d.ts
CHANGED
|
@@ -122,9 +122,11 @@ declare class PrismaSchemaGenerator {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
declare class TsSchemaGenerator {
|
|
125
|
+
private usedExpressionUtils;
|
|
125
126
|
generate(model: Model, outputDir: string): Promise<void>;
|
|
126
127
|
private generateSchema;
|
|
127
128
|
private generateSchemaStatements;
|
|
129
|
+
private createExpressionUtilsCall;
|
|
128
130
|
private createSchemaObject;
|
|
129
131
|
private createProviderObject;
|
|
130
132
|
private createModelsObject;
|
package/dist/index.js
CHANGED
|
@@ -20,7 +20,7 @@ __export(model_utils_exports, {
|
|
|
20
20
|
isUniqueField: () => isUniqueField,
|
|
21
21
|
resolved: () => resolved
|
|
22
22
|
});
|
|
23
|
-
import { isDataModel, isLiteralExpr, isModel } from "@zenstackhq/language/ast";
|
|
23
|
+
import { isDataModel, isLiteralExpr, isModel, isTypeDef } from "@zenstackhq/language/ast";
|
|
24
24
|
import { getAllFields, getModelIdFields, getModelUniqueFields } from "@zenstackhq/language/utils";
|
|
25
25
|
function isIdField(field, contextModel) {
|
|
26
26
|
if (hasAttribute(field, "@id")) {
|
|
@@ -88,7 +88,7 @@ function resolved(ref) {
|
|
|
88
88
|
}
|
|
89
89
|
__name(resolved, "resolved");
|
|
90
90
|
function getAuthDecl(model) {
|
|
91
|
-
let found = model.declarations.find((d) => isDataModel(d) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
|
|
91
|
+
let found = model.declarations.find((d) => (isDataModel(d) || isTypeDef(d)) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
|
|
92
92
|
if (!found) {
|
|
93
93
|
found = model.declarations.find((d) => isDataModel(d) && d.name === "User");
|
|
94
94
|
}
|
|
@@ -103,7 +103,7 @@ var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
|
|
|
103
103
|
|
|
104
104
|
// src/prisma/prisma-schema-generator.ts
|
|
105
105
|
import { lowerCaseFirst } from "@zenstackhq/common-helpers";
|
|
106
|
-
import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isModel as isModel2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
|
|
106
|
+
import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isModel as isModel2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
|
|
107
107
|
import { getAllAttributes, getAllFields as getAllFields2, isDelegateModel as isDelegateModel2 } from "@zenstackhq/language/utils";
|
|
108
108
|
import { AstUtils } from "langium";
|
|
109
109
|
import { match } from "ts-pattern";
|
|
@@ -619,7 +619,7 @@ var PrismaSchemaGenerator = class {
|
|
|
619
619
|
if (field.type.type) {
|
|
620
620
|
fieldType = field.type.type;
|
|
621
621
|
} else if (field.type.reference?.ref) {
|
|
622
|
-
if (
|
|
622
|
+
if (isTypeDef2(field.type.reference.ref)) {
|
|
623
623
|
fieldType = "Json";
|
|
624
624
|
} else {
|
|
625
625
|
fieldType = field.type.reference.ref.name;
|
|
@@ -635,7 +635,7 @@ var PrismaSchemaGenerator = class {
|
|
|
635
635
|
}
|
|
636
636
|
const isArray = (
|
|
637
637
|
// typed-JSON fields should be translated to scalar Json type
|
|
638
|
-
|
|
638
|
+
isTypeDef2(field.type.reference?.ref) ? false : field.type.array
|
|
639
639
|
);
|
|
640
640
|
const type = new ModelFieldType(fieldType, isArray, field.type.optional);
|
|
641
641
|
const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
|
|
@@ -778,7 +778,7 @@ var PrismaSchemaGenerator = class {
|
|
|
778
778
|
|
|
779
779
|
// src/ts-schema-generator.ts
|
|
780
780
|
import { invariant } from "@zenstackhq/common-helpers";
|
|
781
|
-
import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as isDataModel3, isDataSource, isEnum, isEnumField, isInvocationExpr as isInvocationExpr2, isLiteralExpr as isLiteralExpr3, isMemberAccessExpr, isNullExpr as isNullExpr2, isProcedure, isReferenceExpr as isReferenceExpr2, isThisExpr, isTypeDef as
|
|
781
|
+
import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as isDataModel3, isDataSource, isEnum, isEnumField, isInvocationExpr as isInvocationExpr2, isLiteralExpr as isLiteralExpr3, isMemberAccessExpr, isNullExpr as isNullExpr2, isProcedure, isReferenceExpr as isReferenceExpr2, isThisExpr, isTypeDef as isTypeDef3, isUnaryExpr } from "@zenstackhq/language/ast";
|
|
782
782
|
import { getAllAttributes as getAllAttributes2, getAllFields as getAllFields3, isDataFieldReference } from "@zenstackhq/language/utils";
|
|
783
783
|
import fs from "fs";
|
|
784
784
|
import path from "path";
|
|
@@ -788,10 +788,12 @@ var TsSchemaGenerator = class {
|
|
|
788
788
|
static {
|
|
789
789
|
__name(this, "TsSchemaGenerator");
|
|
790
790
|
}
|
|
791
|
+
usedExpressionUtils = false;
|
|
791
792
|
async generate(model, outputDir) {
|
|
792
793
|
fs.mkdirSync(outputDir, {
|
|
793
794
|
recursive: true
|
|
794
795
|
});
|
|
796
|
+
this.usedExpressionUtils = false;
|
|
795
797
|
this.generateSchema(model, outputDir);
|
|
796
798
|
this.generateModelsAndTypeDefs(model, outputDir);
|
|
797
799
|
this.generateInputTypes(model, outputDir);
|
|
@@ -808,18 +810,21 @@ var TsSchemaGenerator = class {
|
|
|
808
810
|
}
|
|
809
811
|
generateSchemaStatements(model, statements) {
|
|
810
812
|
const hasComputedFields = model.declarations.some((d) => isDataModel3(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
813
|
+
const schemaObject = this.createSchemaObject(model);
|
|
811
814
|
const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
812
815
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
|
|
813
816
|
...hasComputedFields ? [
|
|
814
817
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("OperandExpression"))
|
|
815
818
|
] : [],
|
|
816
|
-
|
|
819
|
+
...this.usedExpressionUtils ? [
|
|
820
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
|
|
821
|
+
] : []
|
|
817
822
|
])), ts.factory.createStringLiteral("@zenstackhq/runtime/schema"));
|
|
818
823
|
statements.push(runtimeImportDecl);
|
|
819
824
|
const declaration = ts.factory.createVariableStatement([
|
|
820
825
|
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
821
826
|
], ts.factory.createVariableDeclarationList([
|
|
822
|
-
ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(
|
|
827
|
+
ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
|
|
823
828
|
], ts.NodeFlags.Const));
|
|
824
829
|
statements.push(declaration);
|
|
825
830
|
const typeDeclaration = ts.factory.createTypeAliasDeclaration([
|
|
@@ -827,6 +832,10 @@ var TsSchemaGenerator = class {
|
|
|
827
832
|
], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
|
|
828
833
|
statements.push(typeDeclaration);
|
|
829
834
|
}
|
|
835
|
+
createExpressionUtilsCall(method, args) {
|
|
836
|
+
this.usedExpressionUtils = true;
|
|
837
|
+
return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
|
|
838
|
+
}
|
|
830
839
|
createSchemaObject(model) {
|
|
831
840
|
const properties = [
|
|
832
841
|
// provider
|
|
@@ -834,7 +843,7 @@ var TsSchemaGenerator = class {
|
|
|
834
843
|
// models
|
|
835
844
|
ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
|
|
836
845
|
// typeDefs
|
|
837
|
-
...model.declarations.some(
|
|
846
|
+
...model.declarations.some(isTypeDef3) ? [
|
|
838
847
|
ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
|
|
839
848
|
] : []
|
|
840
849
|
];
|
|
@@ -863,7 +872,7 @@ var TsSchemaGenerator = class {
|
|
|
863
872
|
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isDataModel3(d) && !hasAttribute(d, "@@ignore")).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm))), true);
|
|
864
873
|
}
|
|
865
874
|
createTypeDefsObject(model) {
|
|
866
|
-
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) =>
|
|
875
|
+
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isTypeDef3(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
|
|
867
876
|
}
|
|
868
877
|
createDataModelObject(dm) {
|
|
869
878
|
const allFields = getAllFields3(dm);
|
|
@@ -984,15 +993,15 @@ var TsSchemaGenerator = class {
|
|
|
984
993
|
if (defaultValue !== void 0) {
|
|
985
994
|
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
|
986
995
|
if ("call" in defaultValue) {
|
|
987
|
-
objectFields.push(ts.factory.createPropertyAssignment("default",
|
|
996
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
|
|
988
997
|
ts.factory.createStringLiteral(defaultValue.call),
|
|
989
998
|
...defaultValue.args.length > 0 ? [
|
|
990
999
|
ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
|
|
991
1000
|
] : []
|
|
992
1001
|
])));
|
|
993
1002
|
} else if ("authMember" in defaultValue) {
|
|
994
|
-
objectFields.push(ts.factory.createPropertyAssignment("default",
|
|
995
|
-
|
|
1003
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
|
|
1004
|
+
this.createExpressionUtilsCall("call", [
|
|
996
1005
|
ts.factory.createStringLiteral("auth")
|
|
997
1006
|
]),
|
|
998
1007
|
ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
|
|
@@ -1306,7 +1315,7 @@ var TsSchemaGenerator = class {
|
|
|
1306
1315
|
});
|
|
1307
1316
|
}
|
|
1308
1317
|
createThisExpression() {
|
|
1309
|
-
return
|
|
1318
|
+
return this.createExpressionUtilsCall("_this");
|
|
1310
1319
|
}
|
|
1311
1320
|
createMemberExpression(expr) {
|
|
1312
1321
|
const members = [];
|
|
@@ -1320,32 +1329,32 @@ var TsSchemaGenerator = class {
|
|
|
1320
1329
|
this.createExpression(receiver),
|
|
1321
1330
|
ts.factory.createArrayLiteralExpression(members.map((m) => ts.factory.createStringLiteral(m)))
|
|
1322
1331
|
];
|
|
1323
|
-
return
|
|
1332
|
+
return this.createExpressionUtilsCall("member", args);
|
|
1324
1333
|
}
|
|
1325
1334
|
createNullExpression() {
|
|
1326
|
-
return
|
|
1335
|
+
return this.createExpressionUtilsCall("_null");
|
|
1327
1336
|
}
|
|
1328
1337
|
createBinaryExpression(expr) {
|
|
1329
|
-
return
|
|
1338
|
+
return this.createExpressionUtilsCall("binary", [
|
|
1330
1339
|
this.createExpression(expr.left),
|
|
1331
1340
|
this.createLiteralNode(expr.operator),
|
|
1332
1341
|
this.createExpression(expr.right)
|
|
1333
1342
|
]);
|
|
1334
1343
|
}
|
|
1335
1344
|
createUnaryExpression(expr) {
|
|
1336
|
-
return
|
|
1345
|
+
return this.createExpressionUtilsCall("unary", [
|
|
1337
1346
|
this.createLiteralNode(expr.operator),
|
|
1338
1347
|
this.createExpression(expr.operand)
|
|
1339
1348
|
]);
|
|
1340
1349
|
}
|
|
1341
1350
|
createArrayExpression(expr) {
|
|
1342
|
-
return
|
|
1351
|
+
return this.createExpressionUtilsCall("array", [
|
|
1343
1352
|
ts.factory.createArrayLiteralExpression(expr.items.map((item) => this.createExpression(item)))
|
|
1344
1353
|
]);
|
|
1345
1354
|
}
|
|
1346
1355
|
createRefExpression(expr) {
|
|
1347
1356
|
if (isDataField(expr.target.ref)) {
|
|
1348
|
-
return
|
|
1357
|
+
return this.createExpressionUtilsCall("field", [
|
|
1349
1358
|
this.createLiteralNode(expr.target.$refText)
|
|
1350
1359
|
]);
|
|
1351
1360
|
} else if (isEnumField(expr.target.ref)) {
|
|
@@ -1355,7 +1364,7 @@ var TsSchemaGenerator = class {
|
|
|
1355
1364
|
}
|
|
1356
1365
|
}
|
|
1357
1366
|
createCallExpression(expr) {
|
|
1358
|
-
return
|
|
1367
|
+
return this.createExpressionUtilsCall("call", [
|
|
1359
1368
|
ts.factory.createStringLiteral(expr.function.$refText),
|
|
1360
1369
|
...expr.args.length > 0 ? [
|
|
1361
1370
|
ts.factory.createArrayLiteralExpression(expr.args.map((arg) => this.createExpression(arg.value)))
|
|
@@ -1363,11 +1372,11 @@ var TsSchemaGenerator = class {
|
|
|
1363
1372
|
]);
|
|
1364
1373
|
}
|
|
1365
1374
|
createLiteralExpression(type, value) {
|
|
1366
|
-
return match2(type).with("BooleanLiteral", () =>
|
|
1375
|
+
return match2(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
|
|
1367
1376
|
this.createLiteralNode(value)
|
|
1368
|
-
])).with("NumberLiteral", () =>
|
|
1377
|
+
])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
|
|
1369
1378
|
ts.factory.createIdentifier(value)
|
|
1370
|
-
])).with("StringLiteral", () =>
|
|
1379
|
+
])).with("StringLiteral", () => this.createExpressionUtilsCall("literal", [
|
|
1371
1380
|
this.createLiteralNode(value)
|
|
1372
1381
|
])).otherwise(() => {
|
|
1373
1382
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
@@ -1378,7 +1387,7 @@ var TsSchemaGenerator = class {
|
|
|
1378
1387
|
statements.push(this.generateSchemaImport(model, true, true));
|
|
1379
1388
|
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1380
1389
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
|
|
1381
|
-
...model.declarations.some(
|
|
1390
|
+
...model.declarations.some(isTypeDef3) ? [
|
|
1382
1391
|
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`TypeDefResult as $TypeDefResult`))
|
|
1383
1392
|
] : []
|
|
1384
1393
|
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
@@ -1395,7 +1404,7 @@ var TsSchemaGenerator = class {
|
|
|
1395
1404
|
}
|
|
1396
1405
|
statements.push(modelType);
|
|
1397
1406
|
}
|
|
1398
|
-
const typeDefs = model.declarations.filter(
|
|
1407
|
+
const typeDefs = model.declarations.filter(isTypeDef3);
|
|
1399
1408
|
for (const td of typeDefs) {
|
|
1400
1409
|
let typeDef = ts.factory.createTypeAliasDeclaration([
|
|
1401
1410
|
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|