@zenstackhq/sdk 3.0.0-beta.8 → 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
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";
107
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;
@@ -670,7 +1030,7 @@ var PrismaSchemaGenerator = class {
670
1030
  }
671
1031
  makeAttributeArgValue(node) {
672
1032
  if (isLiteralExpr2(node)) {
673
- 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();
674
1034
  return new AttributeArgValue(argType, node.value);
675
1035
  } else if (isArrayExpr(node)) {
676
1036
  return new AttributeArgValue("Array", new Array(...node.items.map((item) => this.makeAttributeArgValue(item))));
@@ -689,7 +1049,7 @@ var PrismaSchemaGenerator = class {
689
1049
  }
690
1050
  makeFunctionCall(node) {
691
1051
  return new FunctionCall(node.function.ref.name, node.args.map((arg) => {
692
- 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(() => {
693
1053
  throw new Error("Function call argument must be literal or null");
694
1054
  });
695
1055
  return new FunctionCallArg(val);
@@ -778,7 +1138,7 @@ import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as
778
1138
  import { getAllAttributes as getAllAttributes2, getAllFields as getAllFields3, isDataFieldReference } from "@zenstackhq/language/utils";
779
1139
  import fs from "fs";
780
1140
  import path from "path";
781
- import { match as match2 } from "ts-pattern";
1141
+ import { match as match3 } from "ts-pattern";
782
1142
  import * as ts from "typescript";
783
1143
  var TsSchemaGenerator = class {
784
1144
  static {
@@ -947,7 +1307,7 @@ var TsSchemaGenerator = class {
947
1307
  ], true))), true);
948
1308
  }
949
1309
  mapFieldTypeToTSType(type) {
950
- 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");
951
1311
  if (type.array) {
952
1312
  result = `${result}[]`;
953
1313
  }
@@ -995,7 +1355,9 @@ var TsSchemaGenerator = class {
995
1355
  objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
996
1356
  ts.factory.createStringLiteral(defaultValue.call),
997
1357
  ...defaultValue.args.length > 0 ? [
998
- 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
+ ])))
999
1361
  ] : []
1000
1362
  ])));
1001
1363
  } else if ("authMember" in defaultValue) {
@@ -1309,7 +1671,7 @@ var TsSchemaGenerator = class {
1309
1671
  ]);
1310
1672
  }
1311
1673
  createExpression(value) {
1312
- 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(() => {
1313
1675
  throw new Error(`Unsupported attribute arg value: ${value.$type}`);
1314
1676
  });
1315
1677
  }
@@ -1371,7 +1733,7 @@ var TsSchemaGenerator = class {
1371
1733
  ]);
1372
1734
  }
1373
1735
  createLiteralExpression(type, value) {
1374
- return match2(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1736
+ return match3(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1375
1737
  this.createLiteralNode(value)
1376
1738
  ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1377
1739
  ts.factory.createIdentifier(value)
@@ -2027,6 +2389,8 @@ _ts_decorate([
2027
2389
  _ts_metadata("design:returntype", void 0)
2028
2390
  ], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
2029
2391
  export {
2392
+ DefaultOperationNodeVisitor,
2393
+ ExpressionVisitor,
2030
2394
  model_utils_exports as ModelUtils,
2031
2395
  PrismaSchemaGenerator,
2032
2396
  TsSchemaGenerator,