@zenstackhq/runtime 3.0.0-alpha.4 → 3.0.0-alpha.7

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
7
  var __export = (target, all) => {
@@ -18,14 +16,6 @@ var __copyProps = (to, from, except, desc) => {
18
16
  }
19
17
  return to;
20
18
  };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
20
 
31
21
  // src/plugins/policy/index.ts
@@ -49,16 +39,16 @@ var RejectedByPolicyError = class extends Error {
49
39
  };
50
40
 
51
41
  // src/plugins/policy/policy-handler.ts
42
+ var import_common_helpers6 = require("@zenstackhq/common-helpers");
52
43
  var import_kysely7 = require("kysely");
53
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"), 1);
54
44
  var import_ts_pattern7 = require("ts-pattern");
55
45
 
56
46
  // src/client/crud/dialects/index.ts
57
47
  var import_ts_pattern4 = require("ts-pattern");
58
48
 
59
49
  // src/client/crud/dialects/postgresql.ts
50
+ var import_common_helpers2 = require("@zenstackhq/common-helpers");
60
51
  var import_kysely2 = require("kysely");
61
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"), 1);
62
52
  var import_ts_pattern2 = require("ts-pattern");
63
53
 
64
54
  // src/client/errors.ts
@@ -265,8 +255,8 @@ function flattenCompoundUniqueFilters(schema, model, filter) {
265
255
  __name(flattenCompoundUniqueFilters, "flattenCompoundUniqueFilters");
266
256
 
267
257
  // src/client/crud/dialects/base.ts
258
+ var import_common_helpers = require("@zenstackhq/common-helpers");
268
259
  var import_kysely = require("kysely");
269
- var import_tiny_invariant = __toESM(require("tiny-invariant"), 1);
270
260
  var import_ts_pattern = require("ts-pattern");
271
261
 
272
262
  // src/utils/enumerate.ts
@@ -284,7 +274,6 @@ function enumerate(x) {
284
274
  __name(enumerate, "enumerate");
285
275
 
286
276
  // src/client/crud/dialects/base.ts
287
- var import_is_plain_object = require("is-plain-object");
288
277
  var BaseCrudDialect = class {
289
278
  static {
290
279
  __name(this, "BaseCrudDialect");
@@ -482,7 +471,7 @@ var BaseCrudDialect = class {
482
471
  return eb(lhs, "=", rhs !== null && rhs !== void 0 ? this.transformPrimitive(rhs, type) : rhs);
483
472
  }
484
473
  buildStandardFilter(eb, type, payload, lhs, getRhs, recurse, throwIfInvalid = false, onlyForKeys = void 0) {
485
- if (payload === null || !(0, import_is_plain_object.isPlainObject)(payload)) {
474
+ if (payload === null || !(0, import_common_helpers.isPlainObject)(payload)) {
486
475
  return {
487
476
  conditions: [
488
477
  this.buildLiteralFilter(eb, lhs, type, payload)
@@ -498,14 +487,14 @@ var BaseCrudDialect = class {
498
487
  }
499
488
  const rhs = Array.isArray(value) ? value.map(getRhs) : getRhs(value);
500
489
  const condition = (0, import_ts_pattern.match)(op).with("equals", () => rhs === null ? eb(lhs, "is", null) : eb(lhs, "=", rhs)).with("in", () => {
501
- (0, import_tiny_invariant.default)(Array.isArray(rhs), "right hand side must be an array");
490
+ (0, import_common_helpers.invariant)(Array.isArray(rhs), "right hand side must be an array");
502
491
  if (rhs.length === 0) {
503
492
  return this.false(eb);
504
493
  } else {
505
494
  return eb(lhs, "in", rhs);
506
495
  }
507
496
  }).with("notIn", () => {
508
- (0, import_tiny_invariant.default)(Array.isArray(rhs), "right hand side must be an array");
497
+ (0, import_common_helpers.invariant)(Array.isArray(rhs), "right hand side must be an array");
509
498
  if (rhs.length === 0) {
510
499
  return this.true(eb);
511
500
  } else {
@@ -619,9 +608,9 @@ var BaseCrudDialect = class {
619
608
  "_min",
620
609
  "_max"
621
610
  ].includes(field)) {
622
- (0, import_tiny_invariant.default)(value && typeof value === "object", `invalid orderBy value for field "${field}"`);
611
+ (0, import_common_helpers.invariant)(value && typeof value === "object", `invalid orderBy value for field "${field}"`);
623
612
  for (const [k, v] of Object.entries(value)) {
624
- (0, import_tiny_invariant.default)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
613
+ (0, import_common_helpers.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
625
614
  result = result.orderBy((eb) => eb.fn(field.slice(1), [
626
615
  import_kysely.sql.ref(k)
627
616
  ]), import_kysely.sql.raw(this.negateSort(v, negated)));
@@ -630,9 +619,9 @@ var BaseCrudDialect = class {
630
619
  }
631
620
  switch (field) {
632
621
  case "_count": {
633
- (0, import_tiny_invariant.default)(value && typeof value === "object", 'invalid orderBy value for field "_count"');
622
+ (0, import_common_helpers.invariant)(value && typeof value === "object", 'invalid orderBy value for field "_count"');
634
623
  for (const [k, v] of Object.entries(value)) {
635
- (0, import_tiny_invariant.default)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
624
+ (0, import_common_helpers.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
636
625
  result = result.orderBy((eb) => eb.fn.count(import_kysely.sql.ref(k)), import_kysely.sql.raw(this.negateSort(v, negated)));
637
626
  }
638
627
  continue;
@@ -654,7 +643,7 @@ var BaseCrudDialect = class {
654
643
  throw new QueryError(`invalid orderBy value for field "${field}"`);
655
644
  }
656
645
  if ("_count" in value) {
657
- (0, import_tiny_invariant.default)(value._count === "asc" || value._count === "desc", 'invalid orderBy value for field "_count"');
646
+ (0, import_common_helpers.invariant)(value._count === "asc" || value._count === "desc", 'invalid orderBy value for field "_count"');
658
647
  const sort = this.negateSort(value._count, negated);
659
648
  result = result.orderBy((eb) => {
660
649
  let subQuery = eb.selectFrom(relationModel);
@@ -772,8 +761,8 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
772
761
  if (m2m) {
773
762
  const parentIds = getIdFields(this.schema, model);
774
763
  const relationIds = getIdFields(this.schema, relationModel);
775
- (0, import_tiny_invariant2.default)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
776
- (0, import_tiny_invariant2.default)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
764
+ (0, import_common_helpers2.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
765
+ (0, import_common_helpers2.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
777
766
  subQuery = subQuery.where(eb(eb.ref(`${relationModel}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentName}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
778
767
  } else {
779
768
  const joinPairs = buildJoinPairs(this.schema, model, parentName, relationField, relationModel);
@@ -867,8 +856,8 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
867
856
  };
868
857
 
869
858
  // src/client/crud/dialects/sqlite.ts
859
+ var import_common_helpers3 = require("@zenstackhq/common-helpers");
870
860
  var import_kysely3 = require("kysely");
871
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"), 1);
872
861
  var import_ts_pattern3 = require("ts-pattern");
873
862
  var SqliteCrudDialect = class extends BaseCrudDialect {
874
863
  static {
@@ -915,8 +904,8 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
915
904
  if (m2m) {
916
905
  const parentIds = getIdFields(this.schema, model);
917
906
  const relationIds = getIdFields(this.schema, relationModel);
918
- (0, import_tiny_invariant3.default)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
919
- (0, import_tiny_invariant3.default)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
907
+ (0, import_common_helpers3.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
908
+ (0, import_common_helpers3.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
920
909
  subQuery = subQuery.where(eb(eb.ref(`${relationModel}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentName}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
921
910
  } else {
922
911
  const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
@@ -1412,12 +1401,12 @@ var ColumnCollector = class extends DefaultOperationNodeVisitor {
1412
1401
  };
1413
1402
 
1414
1403
  // src/plugins/policy/expression-transformer.ts
1404
+ var import_common_helpers5 = require("@zenstackhq/common-helpers");
1415
1405
  var import_kysely6 = require("kysely");
1416
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"), 1);
1417
1406
  var import_ts_pattern6 = require("ts-pattern");
1418
1407
 
1419
1408
  // src/plugins/policy/expression-evaluator.ts
1420
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"), 1);
1409
+ var import_common_helpers4 = require("@zenstackhq/common-helpers");
1421
1410
  var import_ts_pattern5 = require("ts-pattern");
1422
1411
  var ExpressionEvaluator = class {
1423
1412
  static {
@@ -1461,18 +1450,18 @@ var ExpressionEvaluator = class {
1461
1450
  const right = this.evaluate(expr2.right, context);
1462
1451
  return (0, import_ts_pattern5.match)(expr2.op).with("==", () => left === right).with("!=", () => left !== right).with(">", () => left > right).with(">=", () => left >= right).with("<", () => left < right).with("<=", () => left <= right).with("&&", () => left && right).with("||", () => left || right).with("in", () => {
1463
1452
  const _right = right ?? [];
1464
- (0, import_tiny_invariant4.default)(Array.isArray(_right), 'expected array for "in" operator');
1453
+ (0, import_common_helpers4.invariant)(Array.isArray(_right), 'expected array for "in" operator');
1465
1454
  return _right.includes(left);
1466
1455
  }).exhaustive();
1467
1456
  }
1468
1457
  evaluateCollectionPredicate(expr2, context) {
1469
1458
  const op = expr2.op;
1470
- (0, import_tiny_invariant4.default)(op === "?" || op === "!" || op === "^", 'expected "?" or "!" or "^" operator');
1459
+ (0, import_common_helpers4.invariant)(op === "?" || op === "!" || op === "^", 'expected "?" or "!" or "^" operator');
1471
1460
  const left = this.evaluate(expr2.left, context);
1472
1461
  if (!left) {
1473
1462
  return false;
1474
1463
  }
1475
- (0, import_tiny_invariant4.default)(Array.isArray(left), "expected array");
1464
+ (0, import_common_helpers4.invariant)(Array.isArray(left), "expected array");
1476
1465
  return (0, import_ts_pattern5.match)(op).with("?", () => left.some((item) => this.evaluate(expr2.right, {
1477
1466
  ...context,
1478
1467
  thisValue: item
@@ -1694,20 +1683,20 @@ var ExpressionTransformer = class {
1694
1683
  return import_kysely6.BinaryOperationNode.create(left, this.transformOperator(op), right);
1695
1684
  }
1696
1685
  transformCollectionPredicate(expr2, context) {
1697
- (0, import_tiny_invariant5.default)(expr2.op === "?" || expr2.op === "!" || expr2.op === "^", 'expected "?" or "!" or "^" operator');
1686
+ (0, import_common_helpers5.invariant)(expr2.op === "?" || expr2.op === "!" || expr2.op === "^", 'expected "?" or "!" or "^" operator');
1698
1687
  if (this.isAuthCall(expr2.left) || this.isAuthMember(expr2.left)) {
1699
1688
  const value = new ExpressionEvaluator().evaluate(expr2, {
1700
1689
  auth: this.auth
1701
1690
  });
1702
1691
  return this.transformValue(value, "Boolean");
1703
1692
  }
1704
- (0, import_tiny_invariant5.default)(ExpressionUtils.isField(expr2.left) || ExpressionUtils.isMember(expr2.left), "left operand must be field or member access");
1693
+ (0, import_common_helpers5.invariant)(ExpressionUtils.isField(expr2.left) || ExpressionUtils.isMember(expr2.left), "left operand must be field or member access");
1705
1694
  let newContextModel;
1706
1695
  if (ExpressionUtils.isField(expr2.left)) {
1707
1696
  const fieldDef = requireField(this.schema, context.model, expr2.left.field);
1708
1697
  newContextModel = fieldDef.type;
1709
1698
  } else {
1710
- (0, import_tiny_invariant5.default)(ExpressionUtils.isField(expr2.left.receiver));
1699
+ (0, import_common_helpers5.invariant)(ExpressionUtils.isField(expr2.left.receiver));
1711
1700
  const fieldDef = requireField(this.schema, context.model, expr2.left.receiver.field);
1712
1701
  newContextModel = fieldDef.type;
1713
1702
  for (const member of expr2.left.members) {
@@ -1754,7 +1743,7 @@ var ExpressionTransformer = class {
1754
1743
  return import_kysely6.ValueNode.create(this.dialect.transformPrimitive(value, type) ?? null);
1755
1744
  }
1756
1745
  _unary(expr2, context) {
1757
- (0, import_tiny_invariant5.default)(expr2.op === "!", 'only "!" operator is supported');
1746
+ (0, import_common_helpers5.invariant)(expr2.op === "!", 'only "!" operator is supported');
1758
1747
  return import_kysely6.BinaryOperationNode.create(this.transform(expr2.operand, context), this.transformOperator("!="), trueNode(this.dialect));
1759
1748
  }
1760
1749
  transformOperator(op) {
@@ -1797,10 +1786,10 @@ var ExpressionTransformer = class {
1797
1786
  if (this.isAuthCall(expr2.receiver)) {
1798
1787
  return this.valueMemberAccess(this.auth, expr2, this.authType);
1799
1788
  }
1800
- (0, import_tiny_invariant5.default)(ExpressionUtils.isField(expr2.receiver), "expect receiver to be field expression");
1789
+ (0, import_common_helpers5.invariant)(ExpressionUtils.isField(expr2.receiver), "expect receiver to be field expression");
1801
1790
  const { memberFilter, memberSelect, ...restContext } = context;
1802
1791
  const receiver = this.transform(expr2.receiver, restContext);
1803
- (0, import_tiny_invariant5.default)(import_kysely6.SelectQueryNode.is(receiver), "expected receiver to be select query");
1792
+ (0, import_common_helpers5.invariant)(import_kysely6.SelectQueryNode.is(receiver), "expected receiver to be select query");
1804
1793
  const receiverField = requireField(this.schema, context.model, expr2.receiver.field);
1805
1794
  const memberFields = [];
1806
1795
  let currType = receiverField.type;
@@ -1824,7 +1813,7 @@ var ExpressionTransformer = class {
1824
1813
  thisEntity: void 0
1825
1814
  });
1826
1815
  if (currNode) {
1827
- (0, import_tiny_invariant5.default)(import_kysely6.SelectQueryNode.is(currNode), "expected select query node");
1816
+ (0, import_common_helpers5.invariant)(import_kysely6.SelectQueryNode.is(currNode), "expected select query node");
1828
1817
  currNode = {
1829
1818
  ...relation,
1830
1819
  selections: [
@@ -1841,8 +1830,8 @@ var ExpressionTransformer = class {
1841
1830
  };
1842
1831
  }
1843
1832
  } else {
1844
- (0, import_tiny_invariant5.default)(i === expr2.members.length - 1, "plain field access must be the last segment");
1845
- (0, import_tiny_invariant5.default)(!currNode, "plain field access must be the last segment");
1833
+ (0, import_common_helpers5.invariant)(i === expr2.members.length - 1, "plain field access must be the last segment");
1834
+ (0, import_common_helpers5.invariant)(!currNode, "plain field access must be the last segment");
1846
1835
  currNode = import_kysely6.ColumnNode.create(member);
1847
1836
  }
1848
1837
  }
@@ -2087,13 +2076,13 @@ var PolicyHandler = class extends import_kysely7.OperationNodeTransformer {
2087
2076
  }
2088
2077
  }
2089
2078
  unwrapCreateValueRow(data, model, fields) {
2090
- (0, import_tiny_invariant6.default)(data.length === fields.length, "data length must match fields length");
2079
+ (0, import_common_helpers6.invariant)(data.length === fields.length, "data length must match fields length");
2091
2080
  const result = [];
2092
2081
  for (let i = 0; i < data.length; i++) {
2093
2082
  const item = data[i];
2094
2083
  const fieldDef = requireField(this.client.$schema, model, fields[i]);
2095
2084
  if (typeof item === "object" && item && "kind" in item) {
2096
- (0, import_tiny_invariant6.default)(item.kind === "ValueNode", "expecting a ValueNode");
2085
+ (0, import_common_helpers6.invariant)(item.kind === "ValueNode", "expecting a ValueNode");
2097
2086
  result.push({
2098
2087
  node: import_kysely7.ValueNode.create(this.dialect.transformPrimitive(item.value, fieldDef.type)),
2099
2088
  raw: item.value
@@ -2301,8 +2290,8 @@ var PolicyHandler = class extends import_kysely7.OperationNodeTransformer {
2301
2290
  const modelDef = requireModel(this.client.$schema, modelName);
2302
2291
  const result = [];
2303
2292
  const extractOperations = /* @__PURE__ */ __name((expr2) => {
2304
- (0, import_tiny_invariant6.default)(ExpressionUtils.isLiteral(expr2), "expecting a literal");
2305
- (0, import_tiny_invariant6.default)(typeof expr2.value === "string", "expecting a string literal");
2293
+ (0, import_common_helpers6.invariant)(ExpressionUtils.isLiteral(expr2), "expecting a literal");
2294
+ (0, import_common_helpers6.invariant)(typeof expr2.value === "string", "expecting a string literal");
2306
2295
  return expr2.value.split(",").filter((v) => !!v).map((v) => v.trim());
2307
2296
  }, "extractOperations");
2308
2297
  if (modelDef.attributes) {