@zenstackhq/sdk 3.0.0-beta.7 → 3.0.0-beta.9

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.js CHANGED
@@ -101,12 +101,365 @@ function getIdFields(dm) {
101
101
  __name(getIdFields, "getIdFields");
102
102
  var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
103
103
 
104
+ // src/expression-utils.ts
105
+ import { match } from "ts-pattern";
106
+ var ExpressionVisitor = class {
107
+ static {
108
+ __name(this, "ExpressionVisitor");
109
+ }
110
+ visit(expr) {
111
+ match(expr).with({
112
+ kind: "literal"
113
+ }, (e) => this.visitLiteral(e)).with({
114
+ kind: "array"
115
+ }, (e) => this.visitArray(e)).with({
116
+ kind: "field"
117
+ }, (e) => this.visitField(e)).with({
118
+ kind: "member"
119
+ }, (e) => this.visitMember(e)).with({
120
+ kind: "binary"
121
+ }, (e) => this.visitBinary(e)).with({
122
+ kind: "unary"
123
+ }, (e) => this.visitUnary(e)).with({
124
+ kind: "call"
125
+ }, (e) => this.visitCall(e)).with({
126
+ kind: "this"
127
+ }, (e) => this.visitThis(e)).with({
128
+ kind: "null"
129
+ }, (e) => this.visitNull(e)).exhaustive();
130
+ }
131
+ visitLiteral(_e) {
132
+ }
133
+ visitArray(e) {
134
+ e.items.forEach((item) => this.visit(item));
135
+ }
136
+ visitField(_e) {
137
+ }
138
+ visitMember(e) {
139
+ this.visit(e.receiver);
140
+ }
141
+ visitBinary(e) {
142
+ this.visit(e.left);
143
+ this.visit(e.right);
144
+ }
145
+ visitUnary(e) {
146
+ this.visit(e.operand);
147
+ }
148
+ visitCall(e) {
149
+ e.args?.forEach((arg) => this.visit(arg));
150
+ }
151
+ visitThis(_e) {
152
+ }
153
+ visitNull(_e) {
154
+ }
155
+ };
156
+
157
+ // src/default-operation-node-visitor.ts
158
+ import { OperationNodeVisitor } from "kysely";
159
+ var DefaultOperationNodeVisitor = class extends OperationNodeVisitor {
160
+ static {
161
+ __name(this, "DefaultOperationNodeVisitor");
162
+ }
163
+ defaultVisit(node) {
164
+ Object.values(node).forEach((value) => {
165
+ if (!value) {
166
+ return;
167
+ }
168
+ if (Array.isArray(value)) {
169
+ value.forEach((el) => this.defaultVisit(el));
170
+ }
171
+ if (typeof value === "object" && "kind" in value && typeof value.kind === "string") {
172
+ this.visitNode(value);
173
+ }
174
+ });
175
+ }
176
+ visitSelectQuery(node) {
177
+ this.defaultVisit(node);
178
+ }
179
+ visitSelection(node) {
180
+ this.defaultVisit(node);
181
+ }
182
+ visitColumn(node) {
183
+ this.defaultVisit(node);
184
+ }
185
+ visitAlias(node) {
186
+ this.defaultVisit(node);
187
+ }
188
+ visitTable(node) {
189
+ this.defaultVisit(node);
190
+ }
191
+ visitFrom(node) {
192
+ this.defaultVisit(node);
193
+ }
194
+ visitReference(node) {
195
+ this.defaultVisit(node);
196
+ }
197
+ visitAnd(node) {
198
+ this.defaultVisit(node);
199
+ }
200
+ visitOr(node) {
201
+ this.defaultVisit(node);
202
+ }
203
+ visitValueList(node) {
204
+ this.defaultVisit(node);
205
+ }
206
+ visitParens(node) {
207
+ this.defaultVisit(node);
208
+ }
209
+ visitJoin(node) {
210
+ this.defaultVisit(node);
211
+ }
212
+ visitRaw(node) {
213
+ this.defaultVisit(node);
214
+ }
215
+ visitWhere(node) {
216
+ this.defaultVisit(node);
217
+ }
218
+ visitInsertQuery(node) {
219
+ this.defaultVisit(node);
220
+ }
221
+ visitDeleteQuery(node) {
222
+ this.defaultVisit(node);
223
+ }
224
+ visitReturning(node) {
225
+ this.defaultVisit(node);
226
+ }
227
+ visitCreateTable(node) {
228
+ this.defaultVisit(node);
229
+ }
230
+ visitAddColumn(node) {
231
+ this.defaultVisit(node);
232
+ }
233
+ visitColumnDefinition(node) {
234
+ this.defaultVisit(node);
235
+ }
236
+ visitDropTable(node) {
237
+ this.defaultVisit(node);
238
+ }
239
+ visitOrderBy(node) {
240
+ this.defaultVisit(node);
241
+ }
242
+ visitOrderByItem(node) {
243
+ this.defaultVisit(node);
244
+ }
245
+ visitGroupBy(node) {
246
+ this.defaultVisit(node);
247
+ }
248
+ visitGroupByItem(node) {
249
+ this.defaultVisit(node);
250
+ }
251
+ visitUpdateQuery(node) {
252
+ this.defaultVisit(node);
253
+ }
254
+ visitColumnUpdate(node) {
255
+ this.defaultVisit(node);
256
+ }
257
+ visitLimit(node) {
258
+ this.defaultVisit(node);
259
+ }
260
+ visitOffset(node) {
261
+ this.defaultVisit(node);
262
+ }
263
+ visitOnConflict(node) {
264
+ this.defaultVisit(node);
265
+ }
266
+ visitOnDuplicateKey(node) {
267
+ this.defaultVisit(node);
268
+ }
269
+ visitCheckConstraint(node) {
270
+ this.defaultVisit(node);
271
+ }
272
+ visitDataType(node) {
273
+ this.defaultVisit(node);
274
+ }
275
+ visitSelectAll(node) {
276
+ this.defaultVisit(node);
277
+ }
278
+ visitIdentifier(node) {
279
+ this.defaultVisit(node);
280
+ }
281
+ visitSchemableIdentifier(node) {
282
+ this.defaultVisit(node);
283
+ }
284
+ visitValue(node) {
285
+ this.defaultVisit(node);
286
+ }
287
+ visitPrimitiveValueList(node) {
288
+ this.defaultVisit(node);
289
+ }
290
+ visitOperator(node) {
291
+ this.defaultVisit(node);
292
+ }
293
+ visitCreateIndex(node) {
294
+ this.defaultVisit(node);
295
+ }
296
+ visitDropIndex(node) {
297
+ this.defaultVisit(node);
298
+ }
299
+ visitList(node) {
300
+ this.defaultVisit(node);
301
+ }
302
+ visitPrimaryKeyConstraint(node) {
303
+ this.defaultVisit(node);
304
+ }
305
+ visitUniqueConstraint(node) {
306
+ this.defaultVisit(node);
307
+ }
308
+ visitReferences(node) {
309
+ this.defaultVisit(node);
310
+ }
311
+ visitWith(node) {
312
+ this.defaultVisit(node);
313
+ }
314
+ visitCommonTableExpression(node) {
315
+ this.defaultVisit(node);
316
+ }
317
+ visitCommonTableExpressionName(node) {
318
+ this.defaultVisit(node);
319
+ }
320
+ visitHaving(node) {
321
+ this.defaultVisit(node);
322
+ }
323
+ visitCreateSchema(node) {
324
+ this.defaultVisit(node);
325
+ }
326
+ visitDropSchema(node) {
327
+ this.defaultVisit(node);
328
+ }
329
+ visitAlterTable(node) {
330
+ this.defaultVisit(node);
331
+ }
332
+ visitDropColumn(node) {
333
+ this.defaultVisit(node);
334
+ }
335
+ visitRenameColumn(node) {
336
+ this.defaultVisit(node);
337
+ }
338
+ visitAlterColumn(node) {
339
+ this.defaultVisit(node);
340
+ }
341
+ visitModifyColumn(node) {
342
+ this.defaultVisit(node);
343
+ }
344
+ visitAddConstraint(node) {
345
+ this.defaultVisit(node);
346
+ }
347
+ visitDropConstraint(node) {
348
+ this.defaultVisit(node);
349
+ }
350
+ visitForeignKeyConstraint(node) {
351
+ this.defaultVisit(node);
352
+ }
353
+ visitCreateView(node) {
354
+ this.defaultVisit(node);
355
+ }
356
+ visitDropView(node) {
357
+ this.defaultVisit(node);
358
+ }
359
+ visitGenerated(node) {
360
+ this.defaultVisit(node);
361
+ }
362
+ visitDefaultValue(node) {
363
+ this.defaultVisit(node);
364
+ }
365
+ visitOn(node) {
366
+ this.defaultVisit(node);
367
+ }
368
+ visitValues(node) {
369
+ this.defaultVisit(node);
370
+ }
371
+ visitSelectModifier(node) {
372
+ this.defaultVisit(node);
373
+ }
374
+ visitCreateType(node) {
375
+ this.defaultVisit(node);
376
+ }
377
+ visitDropType(node) {
378
+ this.defaultVisit(node);
379
+ }
380
+ visitExplain(node) {
381
+ this.defaultVisit(node);
382
+ }
383
+ visitDefaultInsertValue(node) {
384
+ this.defaultVisit(node);
385
+ }
386
+ visitAggregateFunction(node) {
387
+ this.defaultVisit(node);
388
+ }
389
+ visitOver(node) {
390
+ this.defaultVisit(node);
391
+ }
392
+ visitPartitionBy(node) {
393
+ this.defaultVisit(node);
394
+ }
395
+ visitPartitionByItem(node) {
396
+ this.defaultVisit(node);
397
+ }
398
+ visitSetOperation(node) {
399
+ this.defaultVisit(node);
400
+ }
401
+ visitBinaryOperation(node) {
402
+ this.defaultVisit(node);
403
+ }
404
+ visitUnaryOperation(node) {
405
+ this.defaultVisit(node);
406
+ }
407
+ visitUsing(node) {
408
+ this.defaultVisit(node);
409
+ }
410
+ visitFunction(node) {
411
+ this.defaultVisit(node);
412
+ }
413
+ visitCase(node) {
414
+ this.defaultVisit(node);
415
+ }
416
+ visitWhen(node) {
417
+ this.defaultVisit(node);
418
+ }
419
+ visitJSONReference(node) {
420
+ this.defaultVisit(node);
421
+ }
422
+ visitJSONPath(node) {
423
+ this.defaultVisit(node);
424
+ }
425
+ visitJSONPathLeg(node) {
426
+ this.defaultVisit(node);
427
+ }
428
+ visitJSONOperatorChain(node) {
429
+ this.defaultVisit(node);
430
+ }
431
+ visitTuple(node) {
432
+ this.defaultVisit(node);
433
+ }
434
+ visitMergeQuery(node) {
435
+ this.defaultVisit(node);
436
+ }
437
+ visitMatched(node) {
438
+ this.defaultVisit(node);
439
+ }
440
+ visitAddIndex(node) {
441
+ this.defaultVisit(node);
442
+ }
443
+ visitCast(node) {
444
+ this.defaultVisit(node);
445
+ }
446
+ visitFetch(node) {
447
+ this.defaultVisit(node);
448
+ }
449
+ visitTop(node) {
450
+ this.defaultVisit(node);
451
+ }
452
+ visitOutput(node) {
453
+ this.defaultVisit(node);
454
+ }
455
+ };
456
+
104
457
  // src/prisma/prisma-schema-generator.ts
105
458
  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 as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
107
- import { getAllAttributes, getAllFields as getAllFields2, isDelegateModel as isDelegateModel2 } from "@zenstackhq/language/utils";
459
+ import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
460
+ import { getAllAttributes, getAllFields as getAllFields2, isAuthInvocation, isDelegateModel as isDelegateModel2 } from "@zenstackhq/language/utils";
108
461
  import { AstUtils } from "langium";
109
- import { match } from "ts-pattern";
462
+ import { match as match2 } from "ts-pattern";
110
463
 
111
464
  // src/prisma/indent-string.ts
112
465
  function indentString(string, count = 4) {
@@ -586,13 +939,20 @@ var PrismaSchemaGenerator = class {
586
939
  }
587
940
  }
588
941
  const allAttributes = getAllAttributes(decl);
589
- for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
942
+ for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2) && !this.isInheritedMapAttribute(attr2, decl))) {
590
943
  this.generateContainerAttribute(model, attr);
591
944
  }
592
945
  decl.comments.forEach((c) => model.addComment(c));
593
946
  this.generateDelegateRelationForBase(model, decl);
594
947
  this.generateDelegateRelationForConcrete(model, decl);
595
948
  }
949
+ isInheritedMapAttribute(attr, contextModel) {
950
+ if (attr.$container === contextModel) {
951
+ return false;
952
+ }
953
+ const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
954
+ return attrName === "@@map";
955
+ }
596
956
  isPrismaAttribute(attr) {
597
957
  if (!attr.decl.ref) {
598
958
  return false;
@@ -638,7 +998,7 @@ var PrismaSchemaGenerator = class {
638
998
  isTypeDef2(field.type.reference?.ref) ? false : field.type.array
639
999
  );
640
1000
  const type = new ModelFieldType(fieldType, isArray, field.type.optional);
641
- const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
1001
+ const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithAuthInvocation(attr)).filter((attr) => (
642
1002
  // when building physical schema, exclude `@default` for id fields inherited from delegate base
643
1003
  !(model_utils_exports.isIdField(field, contextModel) && this.isInheritedFromDelegate(field, contextModel) && attr.decl.$refText === "@default")
644
1004
  )).map((attr) => this.makeFieldAttribute(attr));
@@ -648,7 +1008,7 @@ var PrismaSchemaGenerator = class {
648
1008
  const result = model.addField(field.name, type, attributes, docs, addToFront);
649
1009
  return result;
650
1010
  }
651
- isDefaultWithPluginInvocation(attr) {
1011
+ isDefaultWithAuthInvocation(attr) {
652
1012
  if (attr.decl.ref?.name !== "@default") {
653
1013
  return false;
654
1014
  }
@@ -656,11 +1016,7 @@ var PrismaSchemaGenerator = class {
656
1016
  if (!expr) {
657
1017
  return false;
658
1018
  }
659
- return AstUtils.streamAst(expr).some((node) => isInvocationExpr(node) && this.isFromPlugin(node.function.ref));
660
- }
661
- isFromPlugin(node) {
662
- const model = AstUtils.getContainerOfType(node, isModel2);
663
- return !!model && !!model.$document && model.$document.uri.path.endsWith("plugin.zmodel");
1019
+ return AstUtils.streamAst(expr).some(isAuthInvocation);
664
1020
  }
665
1021
  isInheritedFromDelegate(field, contextModel) {
666
1022
  return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
@@ -674,7 +1030,7 @@ var PrismaSchemaGenerator = class {
674
1030
  }
675
1031
  makeAttributeArgValue(node) {
676
1032
  if (isLiteralExpr2(node)) {
677
- const argType = match(node.$type).with(StringLiteral, () => "String").with(NumberLiteral, () => "Number").with(BooleanLiteral, () => "Boolean").exhaustive();
1033
+ const argType = match2(node.$type).with(StringLiteral, () => "String").with(NumberLiteral, () => "Number").with(BooleanLiteral, () => "Boolean").exhaustive();
678
1034
  return new AttributeArgValue(argType, node.value);
679
1035
  } else if (isArrayExpr(node)) {
680
1036
  return new AttributeArgValue("Array", new Array(...node.items.map((item) => this.makeAttributeArgValue(item))));
@@ -693,7 +1049,7 @@ var PrismaSchemaGenerator = class {
693
1049
  }
694
1050
  makeFunctionCall(node) {
695
1051
  return new FunctionCall(node.function.ref.name, node.args.map((arg) => {
696
- const val = match(arg.value).when(isStringLiteral, (v) => `"${v.value}"`).when(isLiteralExpr2, (v) => v.value.toString()).when(isNullExpr, () => "null").otherwise(() => {
1052
+ const val = match2(arg.value).when(isStringLiteral, (v) => `"${v.value}"`).when(isLiteralExpr2, (v) => v.value.toString()).when(isNullExpr, () => "null").otherwise(() => {
697
1053
  throw new Error("Function call argument must be literal or null");
698
1054
  });
699
1055
  return new FunctionCallArg(val);
@@ -782,7 +1138,7 @@ import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as
782
1138
  import { getAllAttributes as getAllAttributes2, getAllFields as getAllFields3, isDataFieldReference } from "@zenstackhq/language/utils";
783
1139
  import fs from "fs";
784
1140
  import path from "path";
785
- import { match as match2 } from "ts-pattern";
1141
+ import { match as match3 } from "ts-pattern";
786
1142
  import * as ts from "typescript";
787
1143
  var TsSchemaGenerator = class {
788
1144
  static {
@@ -951,7 +1307,7 @@ var TsSchemaGenerator = class {
951
1307
  ], true))), true);
952
1308
  }
953
1309
  mapFieldTypeToTSType(type) {
954
- let result = match2(type.type).with("String", () => "string").with("Boolean", () => "boolean").with("Int", () => "number").with("Float", () => "number").with("BigInt", () => "bigint").with("Decimal", () => "number").otherwise(() => "unknown");
1310
+ let result = match3(type.type).with("String", () => "string").with("Boolean", () => "boolean").with("Int", () => "number").with("Float", () => "number").with("BigInt", () => "bigint").with("Decimal", () => "number").otherwise(() => "unknown");
955
1311
  if (type.array) {
956
1312
  result = `${result}[]`;
957
1313
  }
@@ -999,7 +1355,9 @@ var TsSchemaGenerator = class {
999
1355
  objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1000
1356
  ts.factory.createStringLiteral(defaultValue.call),
1001
1357
  ...defaultValue.args.length > 0 ? [
1002
- ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
1358
+ ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createExpressionUtilsCall("literal", [
1359
+ this.createLiteralNode(arg)
1360
+ ])))
1003
1361
  ] : []
1004
1362
  ])));
1005
1363
  } else if ("authMember" in defaultValue) {
@@ -1313,7 +1671,7 @@ var TsSchemaGenerator = class {
1313
1671
  ]);
1314
1672
  }
1315
1673
  createExpression(value) {
1316
- return match2(value).when(isLiteralExpr3, (expr) => this.createLiteralExpression(expr.$type, expr.value)).when(isInvocationExpr2, (expr) => this.createCallExpression(expr)).when(isReferenceExpr2, (expr) => this.createRefExpression(expr)).when(isArrayExpr2, (expr) => this.createArrayExpression(expr)).when(isUnaryExpr, (expr) => this.createUnaryExpression(expr)).when(isBinaryExpr, (expr) => this.createBinaryExpression(expr)).when(isMemberAccessExpr, (expr) => this.createMemberExpression(expr)).when(isNullExpr2, () => this.createNullExpression()).when(isThisExpr, () => this.createThisExpression()).otherwise(() => {
1674
+ return match3(value).when(isLiteralExpr3, (expr) => this.createLiteralExpression(expr.$type, expr.value)).when(isInvocationExpr2, (expr) => this.createCallExpression(expr)).when(isReferenceExpr2, (expr) => this.createRefExpression(expr)).when(isArrayExpr2, (expr) => this.createArrayExpression(expr)).when(isUnaryExpr, (expr) => this.createUnaryExpression(expr)).when(isBinaryExpr, (expr) => this.createBinaryExpression(expr)).when(isMemberAccessExpr, (expr) => this.createMemberExpression(expr)).when(isNullExpr2, () => this.createNullExpression()).when(isThisExpr, () => this.createThisExpression()).otherwise(() => {
1317
1675
  throw new Error(`Unsupported attribute arg value: ${value.$type}`);
1318
1676
  });
1319
1677
  }
@@ -1375,7 +1733,7 @@ var TsSchemaGenerator = class {
1375
1733
  ]);
1376
1734
  }
1377
1735
  createLiteralExpression(type, value) {
1378
- return match2(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1736
+ return match3(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1379
1737
  this.createLiteralNode(value)
1380
1738
  ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1381
1739
  ts.factory.createIdentifier(value)
@@ -2031,6 +2389,8 @@ _ts_decorate([
2031
2389
  _ts_metadata("design:returntype", void 0)
2032
2390
  ], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
2033
2391
  export {
2392
+ DefaultOperationNodeVisitor,
2393
+ ExpressionVisitor,
2034
2394
  model_utils_exports as ModelUtils,
2035
2395
  PrismaSchemaGenerator,
2036
2396
  TsSchemaGenerator,