@zenstackhq/runtime 3.0.0-alpha.30 → 3.0.0-alpha.31

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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as ClientConstructor } from './contract-DUtKB39c.cjs';
2
- export { K as AggregateArgs, L as AggregateResult, B as BatchResult, i as BooleanFilter, h as BytesFilter, c as ClientContract, d as ClientOptions, G as CountArgs, H as CountResult, u as CreateArgs, w as CreateManyAndReturnArgs, v as CreateManyArgs, D as DateTimeFilter, A as DeleteArgs, E as DeleteManyArgs, F as FindArgs, s as FindFirstArgs, r as FindManyArgs, t as FindUniqueArgs, P as GroupByArgs, Q as GroupByResult, I as IncludeInput, J as JsonArray, a as JsonObject, b as JsonValue, M as ModelResult, k as NullsOrder, N as NumberFilter, m as OmitInput, O as OrderBy, n as SelectIncludeOmit, o as SelectInput, q as SelectSubset, S as SimplifiedModelResult, j as SortOrder, g as StringFilter, p as Subset, T as ToKysely, f as TypeDefResult, U as UpdateArgs, y as UpdateManyAndReturnArgs, x as UpdateManyArgs, z as UpsertArgs, W as WhereInput, l as WhereUniqueInput, Z as ZenStackPromise, e as definePlugin } from './contract-DUtKB39c.cjs';
1
+ import { C as ClientConstructor } from './contract-5Wcmlo5v.cjs';
2
+ export { K as AggregateArgs, L as AggregateResult, B as BatchResult, i as BooleanFilter, h as BytesFilter, c as ClientContract, d as ClientOptions, G as CountArgs, H as CountResult, u as CreateArgs, w as CreateManyAndReturnArgs, v as CreateManyArgs, D as DateTimeFilter, A as DeleteArgs, E as DeleteManyArgs, F as FindArgs, s as FindFirstArgs, r as FindManyArgs, t as FindUniqueArgs, P as GroupByArgs, Q as GroupByResult, I as IncludeInput, J as JsonArray, a as JsonObject, b as JsonValue, M as ModelResult, k as NullsOrder, N as NumberFilter, m as OmitInput, O as OrderBy, n as SelectIncludeOmit, o as SelectInput, q as SelectSubset, S as SimplifiedModelResult, j as SortOrder, g as StringFilter, p as Subset, T as ToKysely, f as TypeDefResult, U as UpdateArgs, y as UpdateManyAndReturnArgs, x as UpdateManyArgs, z as UpsertArgs, W as WhereInput, l as WhereUniqueInput, Z as ZenStackPromise, e as definePlugin } from './contract-5Wcmlo5v.cjs';
3
3
  export { sql } from 'kysely';
4
4
  import 'decimal.js';
5
5
  import '@zenstackhq/sdk/schema';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as ClientConstructor } from './contract-DUtKB39c.js';
2
- export { K as AggregateArgs, L as AggregateResult, B as BatchResult, i as BooleanFilter, h as BytesFilter, c as ClientContract, d as ClientOptions, G as CountArgs, H as CountResult, u as CreateArgs, w as CreateManyAndReturnArgs, v as CreateManyArgs, D as DateTimeFilter, A as DeleteArgs, E as DeleteManyArgs, F as FindArgs, s as FindFirstArgs, r as FindManyArgs, t as FindUniqueArgs, P as GroupByArgs, Q as GroupByResult, I as IncludeInput, J as JsonArray, a as JsonObject, b as JsonValue, M as ModelResult, k as NullsOrder, N as NumberFilter, m as OmitInput, O as OrderBy, n as SelectIncludeOmit, o as SelectInput, q as SelectSubset, S as SimplifiedModelResult, j as SortOrder, g as StringFilter, p as Subset, T as ToKysely, f as TypeDefResult, U as UpdateArgs, y as UpdateManyAndReturnArgs, x as UpdateManyArgs, z as UpsertArgs, W as WhereInput, l as WhereUniqueInput, Z as ZenStackPromise, e as definePlugin } from './contract-DUtKB39c.js';
1
+ import { C as ClientConstructor } from './contract-5Wcmlo5v.js';
2
+ export { K as AggregateArgs, L as AggregateResult, B as BatchResult, i as BooleanFilter, h as BytesFilter, c as ClientContract, d as ClientOptions, G as CountArgs, H as CountResult, u as CreateArgs, w as CreateManyAndReturnArgs, v as CreateManyArgs, D as DateTimeFilter, A as DeleteArgs, E as DeleteManyArgs, F as FindArgs, s as FindFirstArgs, r as FindManyArgs, t as FindUniqueArgs, P as GroupByArgs, Q as GroupByResult, I as IncludeInput, J as JsonArray, a as JsonObject, b as JsonValue, M as ModelResult, k as NullsOrder, N as NumberFilter, m as OmitInput, O as OrderBy, n as SelectIncludeOmit, o as SelectInput, q as SelectSubset, S as SimplifiedModelResult, j as SortOrder, g as StringFilter, p as Subset, T as ToKysely, f as TypeDefResult, U as UpdateArgs, y as UpdateManyAndReturnArgs, x as UpdateManyArgs, z as UpsertArgs, W as WhereInput, l as WhereUniqueInput, Z as ZenStackPromise, e as definePlugin } from './contract-5Wcmlo5v.js';
3
3
  export { sql } from 'kysely';
4
4
  import 'decimal.js';
5
5
  import '@zenstackhq/sdk/schema';
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
  };
7
7
 
8
8
  // src/client/client-impl.ts
9
- import { invariant as invariant12 } from "@zenstackhq/common-helpers";
9
+ import { invariant as invariant13 } from "@zenstackhq/common-helpers";
10
10
  import { CompiledQuery, DefaultConnectionProvider, DefaultQueryExecutor as DefaultQueryExecutor2, Kysely, Log, sql as sql9 } from "kysely";
11
11
 
12
12
  // src/client/crud/operations/aggregate.ts
@@ -5350,10 +5350,11 @@ var InputValidator = class {
5350
5350
  makeGroupBySchema(model) {
5351
5351
  const modelDef = requireModel(this.schema, model);
5352
5352
  const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
5353
+ const bySchema = nonRelationFields.length > 0 ? this.orArray(z.enum(nonRelationFields), true) : z.never();
5353
5354
  let schema = z.strictObject({
5354
5355
  where: this.makeWhereSchema(model, false).optional(),
5355
5356
  orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
5356
- by: this.orArray(z.enum(nonRelationFields), true),
5357
+ by: bySchema,
5357
5358
  having: this.makeHavingSchema(model).optional(),
5358
5359
  skip: this.makeSkipSchema().optional(),
5359
5360
  take: this.makeTakeSchema().optional(),
@@ -5625,12 +5626,13 @@ function performanceNow() {
5625
5626
  __name(performanceNow, "performanceNow");
5626
5627
 
5627
5628
  // src/client/executor/zenstack-query-executor.ts
5628
- import { AndNode as AndNode2, DefaultQueryExecutor, DeleteQueryNode as DeleteQueryNode2, InsertQueryNode as InsertQueryNode2, ReturningNode as ReturningNode3, SelectionNode as SelectionNode4, UpdateQueryNode as UpdateQueryNode2, WhereNode as WhereNode3 } from "kysely";
5629
+ import { AndNode as AndNode2, DefaultQueryExecutor, DeleteQueryNode as DeleteQueryNode2, InsertQueryNode as InsertQueryNode2, ReturningNode as ReturningNode2, SelectionNode as SelectionNode4, UpdateQueryNode as UpdateQueryNode2, WhereNode as WhereNode3 } from "kysely";
5629
5630
  import { nanoid as nanoid2 } from "nanoid";
5630
5631
  import { match as match16 } from "ts-pattern";
5631
5632
 
5632
5633
  // src/client/executor/name-mapper.ts
5633
- import { AliasNode as AliasNode4, ColumnNode as ColumnNode3, IdentifierNode as IdentifierNode3, OperationNodeTransformer as OperationNodeTransformer2, ReferenceNode as ReferenceNode3, ReturningNode as ReturningNode2, SelectAllNode, SelectionNode as SelectionNode3, TableNode as TableNode4 } from "kysely";
5634
+ import { invariant as invariant9 } from "@zenstackhq/common-helpers";
5635
+ import { AliasNode as AliasNode4, ColumnNode as ColumnNode3, FromNode as FromNode3, IdentifierNode as IdentifierNode3, OperationNodeTransformer as OperationNodeTransformer2, ReferenceNode as ReferenceNode3, SelectAllNode, SelectionNode as SelectionNode3, TableNode as TableNode4 } from "kysely";
5634
5636
  var QueryNameMapper = class extends OperationNodeTransformer2 {
5635
5637
  static {
5636
5638
  __name(this, "QueryNameMapper");
@@ -5638,7 +5640,7 @@ var QueryNameMapper = class extends OperationNodeTransformer2 {
5638
5640
  schema;
5639
5641
  modelToTableMap = /* @__PURE__ */ new Map();
5640
5642
  fieldToColumnMap = /* @__PURE__ */ new Map();
5641
- modelStack = [];
5643
+ modelScopes = [];
5642
5644
  constructor(schema) {
5643
5645
  super(), this.schema = schema;
5644
5646
  for (const [modelName, modelDef] of Object.entries(schema.models)) {
@@ -5654,150 +5656,182 @@ var QueryNameMapper = class extends OperationNodeTransformer2 {
5654
5656
  }
5655
5657
  }
5656
5658
  }
5657
- get currentModel() {
5658
- return this.modelStack[this.modelStack.length - 1];
5659
- }
5660
- transformCreateTable(node) {
5661
- try {
5662
- this.modelStack.push(node.table.table.identifier.name);
5663
- return super.transformCreateTable(node);
5664
- } finally {
5665
- this.modelStack.pop();
5659
+ // #region overrides
5660
+ transformSelectQuery(node) {
5661
+ if (!node.from?.froms) {
5662
+ return super.transformSelectQuery(node);
5666
5663
  }
5664
+ const scopes = this.createScopesFromFroms(node.from, true);
5665
+ return this.withScopes(scopes, () => {
5666
+ return {
5667
+ ...super.transformSelectQuery(node),
5668
+ // convert "from" to nested query as needed
5669
+ from: this.processFrom(node.from)
5670
+ };
5671
+ });
5667
5672
  }
5668
5673
  transformInsertQuery(node) {
5669
- try {
5670
- if (node.into?.table.identifier.name) {
5671
- this.modelStack.push(node.into.table.identifier.name);
5672
- }
5674
+ if (!node.into) {
5673
5675
  return super.transformInsertQuery(node);
5674
- } finally {
5675
- if (node.into?.table.identifier.name) {
5676
- this.modelStack.pop();
5677
- }
5678
5676
  }
5677
+ return this.withScope({
5678
+ model: node.into.table.identifier.name
5679
+ }, () => ({
5680
+ ...super.transformInsertQuery(node),
5681
+ // map table name
5682
+ into: this.processTableRef(node.into)
5683
+ }));
5679
5684
  }
5680
5685
  transformReturning(node) {
5681
- return ReturningNode2.create(this.transformSelections(node.selections, node));
5686
+ return {
5687
+ kind: node.kind,
5688
+ // map column names in returning selections (include returningAll)
5689
+ selections: this.processSelections(node.selections)
5690
+ };
5682
5691
  }
5683
- transformUpdateQuery(node) {
5684
- let pushed = false;
5685
- if (node.table && TableNode4.is(node.table)) {
5686
- this.modelStack.push(node.table.table.identifier.name);
5687
- pushed = true;
5688
- }
5689
- try {
5690
- return super.transformUpdateQuery(node);
5691
- } finally {
5692
- if (pushed) {
5693
- this.modelStack.pop();
5692
+ transformJoin(node) {
5693
+ const { alias, node: innerNode } = this.stripAlias(node.table);
5694
+ if (TableNode4.is(innerNode)) {
5695
+ const modelName = innerNode.table.identifier.name;
5696
+ if (this.hasMappedColumns(modelName)) {
5697
+ const select = this.createSelectAll(modelName);
5698
+ return {
5699
+ ...super.transformJoin(node),
5700
+ table: this.wrapAlias(select, alias ?? modelName)
5701
+ };
5694
5702
  }
5695
5703
  }
5704
+ return super.transformJoin(node);
5696
5705
  }
5697
- transformDeleteQuery(node) {
5698
- let pushed = false;
5699
- if (node.from?.froms && node.from.froms.length === 1 && node.from.froms[0]) {
5700
- const from = node.from.froms[0];
5701
- if (TableNode4.is(from)) {
5702
- this.modelStack.push(from.table.identifier.name);
5703
- pushed = true;
5704
- } else if (AliasNode4.is(from) && TableNode4.is(from.node)) {
5705
- this.modelStack.push(from.node.table.identifier.name);
5706
- pushed = true;
5707
- }
5706
+ transformReference(node) {
5707
+ if (!ColumnNode3.is(node.column)) {
5708
+ return super.transformReference(node);
5708
5709
  }
5709
- try {
5710
- return super.transformDeleteQuery(node);
5711
- } finally {
5712
- if (pushed) {
5713
- this.modelStack.pop();
5710
+ const { fieldDef, modelDef, scope } = this.resolveFieldFromScopes(node.column.column.name, node.table?.table.identifier.name);
5711
+ if (fieldDef && !scope.namesMapped) {
5712
+ const mappedFieldName = this.mapFieldName(modelDef.name, fieldDef.name);
5713
+ let mappedTableName = node.table?.table.identifier.name;
5714
+ if (mappedTableName) {
5715
+ if (scope.alias === mappedTableName) {
5716
+ } else if (scope.model === mappedTableName) {
5717
+ mappedTableName = this.mapTableName(scope.model);
5718
+ }
5714
5719
  }
5720
+ return ReferenceNode3.create(ColumnNode3.create(mappedFieldName), mappedTableName ? TableNode4.create(mappedTableName) : void 0);
5721
+ } else {
5722
+ return super.transformReference(node);
5715
5723
  }
5716
5724
  }
5717
- transformSelectQuery(node) {
5718
- if (!node.from?.froms || node.from.froms.length === 0) {
5719
- return super.transformSelectQuery(node);
5720
- }
5721
- if (node.from.froms.length > 1) {
5722
- throw new InternalError(`SelectQueryNode must have a single table in from clause`);
5725
+ transformColumn(node) {
5726
+ const { modelDef, fieldDef, scope } = this.resolveFieldFromScopes(node.column.name);
5727
+ if (!fieldDef || scope.namesMapped) {
5728
+ return super.transformColumn(node);
5723
5729
  }
5724
- let pushed = false;
5725
- const from = node.from.froms[0];
5726
- if (TableNode4.is(from)) {
5727
- this.modelStack.push(from.table.identifier.name);
5728
- pushed = true;
5729
- } else if (AliasNode4.is(from) && TableNode4.is(from.node)) {
5730
- this.modelStack.push(from.node.table.identifier.name);
5731
- pushed = true;
5730
+ const mappedName = this.mapFieldName(modelDef.name, fieldDef.name);
5731
+ return ColumnNode3.create(mappedName);
5732
+ }
5733
+ transformUpdateQuery(node) {
5734
+ const { alias, node: innerTable } = this.stripAlias(node.table);
5735
+ if (!innerTable || !TableNode4.is(innerTable)) {
5736
+ return super.transformUpdateQuery(node);
5732
5737
  }
5733
- const selections = node.selections ? this.transformSelections(node.selections, node) : node.selections;
5734
- try {
5738
+ return this.withScope({
5739
+ model: innerTable.table.identifier.name,
5740
+ alias
5741
+ }, () => {
5735
5742
  return {
5736
- ...super.transformSelectQuery(node),
5737
- selections
5743
+ ...super.transformUpdateQuery(node),
5744
+ // map table name
5745
+ table: this.wrapAlias(this.processTableRef(innerTable), alias)
5738
5746
  };
5739
- } finally {
5740
- if (pushed) {
5741
- this.modelStack.pop();
5747
+ });
5748
+ }
5749
+ transformDeleteQuery(node) {
5750
+ const scopes = this.createScopesFromFroms(node.from, false);
5751
+ const froms = node.from.froms.map((from) => {
5752
+ const { alias, node: innerNode } = this.stripAlias(from);
5753
+ if (TableNode4.is(innerNode)) {
5754
+ return this.wrapAlias(this.processTableRef(innerNode), alias);
5755
+ } else {
5756
+ return super.transformNode(from);
5742
5757
  }
5743
- }
5758
+ });
5759
+ return this.withScopes(scopes, () => {
5760
+ return {
5761
+ ...super.transformDeleteQuery(node),
5762
+ from: FromNode3.create(froms)
5763
+ };
5764
+ });
5744
5765
  }
5745
- transformSelections(selections, contextNode) {
5746
- const result = [];
5747
- for (const selection of selections) {
5748
- let selectAllFromModel = void 0;
5749
- let isSelectAll = false;
5750
- if (SelectAllNode.is(selection.selection)) {
5751
- selectAllFromModel = this.currentModel;
5752
- isSelectAll = true;
5753
- } else if (ReferenceNode3.is(selection.selection) && SelectAllNode.is(selection.selection.column)) {
5754
- selectAllFromModel = selection.selection.table?.table.identifier.name ?? this.currentModel;
5755
- isSelectAll = true;
5756
- }
5757
- if (isSelectAll) {
5758
- if (!selectAllFromModel) {
5759
- continue;
5766
+ // #endregion
5767
+ // #region utils
5768
+ resolveFieldFromScopes(name, qualifier) {
5769
+ for (const scope of this.modelScopes.toReversed()) {
5770
+ if (qualifier) {
5771
+ if (scope.alias) {
5772
+ if (qualifier !== scope.alias) {
5773
+ continue;
5774
+ }
5760
5775
  } else {
5761
- const scalarFields = this.getModelScalarFields(contextNode, selectAllFromModel);
5762
- const fromModelDef = requireModel(this.schema, selectAllFromModel);
5763
- const mappedTableName = this.getMappedName(fromModelDef) ?? selectAllFromModel;
5764
- result.push(...scalarFields.map((fieldName) => {
5765
- const fieldRef = ReferenceNode3.create(ColumnNode3.create(this.mapFieldName(fieldName)), TableNode4.create(mappedTableName));
5766
- return SelectionNode3.create(this.fieldHasMappedName(fieldName) ? AliasNode4.create(fieldRef, IdentifierNode3.create(fieldName)) : fieldRef);
5767
- }));
5776
+ if (qualifier !== scope.model) {
5777
+ continue;
5778
+ }
5768
5779
  }
5769
- } else {
5770
- result.push(this.transformSelectionWithAlias(selection));
5780
+ }
5781
+ const modelDef = getModel(this.schema, scope.model);
5782
+ if (!modelDef) {
5783
+ continue;
5784
+ }
5785
+ if (modelDef.fields[name]) {
5786
+ return {
5787
+ modelDef,
5788
+ fieldDef: modelDef.fields[name],
5789
+ scope
5790
+ };
5771
5791
  }
5772
5792
  }
5773
- return result;
5793
+ return {
5794
+ modelDef: void 0,
5795
+ fieldDef: void 0,
5796
+ scope: void 0
5797
+ };
5774
5798
  }
5775
- transformSelectionWithAlias(node) {
5776
- if (ColumnNode3.is(node.selection) && this.fieldHasMappedName(node.selection.column.name)) {
5777
- return SelectionNode3.create(AliasNode4.create(this.transformColumn(node.selection), IdentifierNode3.create(node.selection.column.name)));
5778
- } else if (ReferenceNode3.is(node.selection) && this.fieldHasMappedName(node.selection.column.column.name)) {
5779
- return SelectionNode3.create(AliasNode4.create(this.transformReference(node.selection), IdentifierNode3.create(node.selection.column.column.name)));
5780
- } else {
5781
- return this.transformSelection(node);
5799
+ pushScope(scope) {
5800
+ this.modelScopes.push(scope);
5801
+ }
5802
+ withScope(scope, fn) {
5803
+ this.pushScope(scope);
5804
+ try {
5805
+ return fn();
5806
+ } finally {
5807
+ this.modelScopes.pop();
5782
5808
  }
5783
5809
  }
5784
- fieldHasMappedName(name) {
5785
- if (!this.currentModel) {
5786
- return false;
5810
+ withScopes(scopes, fn) {
5811
+ scopes.forEach((s) => this.pushScope(s));
5812
+ try {
5813
+ return fn();
5814
+ } finally {
5815
+ scopes.forEach(() => this.modelScopes.pop());
5787
5816
  }
5788
- return this.fieldToColumnMap.has(`${this.currentModel}.${name}`);
5789
5817
  }
5790
- transformTable(node) {
5791
- const tableName = node.table.identifier.name;
5792
- const mappedName = this.modelToTableMap.get(tableName);
5793
- if (mappedName) {
5794
- return TableNode4.create(mappedName);
5795
- } else {
5818
+ wrapAlias(node, alias) {
5819
+ return alias ? AliasNode4.create(node, IdentifierNode3.create(alias)) : node;
5820
+ }
5821
+ ensureAlias(node, alias, fallbackName) {
5822
+ if (!node) {
5796
5823
  return node;
5797
5824
  }
5825
+ return alias ? AliasNode4.create(node, IdentifierNode3.create(alias)) : AliasNode4.create(node, IdentifierNode3.create(fallbackName));
5798
5826
  }
5799
- transformColumn(node) {
5800
- return ColumnNode3.create(this.mapFieldName(node.column.name));
5827
+ processTableRef(node) {
5828
+ if (!node) {
5829
+ return node;
5830
+ }
5831
+ if (!TableNode4.is(node)) {
5832
+ return super.transformNode(node);
5833
+ }
5834
+ return TableNode4.create(this.mapTableName(node.table.identifier.name));
5801
5835
  }
5802
5836
  getMappedName(def) {
5803
5837
  const mapAttr = def.attributes?.find((attr) => attr.name === "@@map" || attr.name === "@map");
@@ -5809,28 +5843,151 @@ var QueryNameMapper = class extends OperationNodeTransformer2 {
5809
5843
  }
5810
5844
  return void 0;
5811
5845
  }
5812
- mapFieldName(fieldName) {
5813
- if (!this.currentModel) {
5814
- return fieldName;
5846
+ mapFieldName(model, field) {
5847
+ const mappedName = this.fieldToColumnMap.get(`${model}.${field}`);
5848
+ if (mappedName) {
5849
+ return mappedName;
5850
+ } else {
5851
+ return field;
5815
5852
  }
5816
- const mappedName = this.fieldToColumnMap.get(`${this.currentModel}.${fieldName}`);
5853
+ }
5854
+ mapTableName(tableName) {
5855
+ const mappedName = this.modelToTableMap.get(tableName);
5817
5856
  if (mappedName) {
5818
5857
  return mappedName;
5819
5858
  } else {
5820
- return fieldName;
5859
+ return tableName;
5821
5860
  }
5822
5861
  }
5823
- requireCurrentModel(node) {
5824
- if (!this.currentModel) {
5825
- throw new InternalError(`Missing model context for "${node}"`);
5862
+ stripAlias(node) {
5863
+ if (!node) {
5864
+ return {
5865
+ alias: void 0,
5866
+ node
5867
+ };
5826
5868
  }
5869
+ if (AliasNode4.is(node)) {
5870
+ invariant9(IdentifierNode3.is(node.alias), "Expected identifier as alias");
5871
+ return {
5872
+ alias: node.alias.name,
5873
+ node: node.node
5874
+ };
5875
+ }
5876
+ return {
5877
+ alias: void 0,
5878
+ node
5879
+ };
5827
5880
  }
5828
- getModelScalarFields(contextNode, model) {
5829
- this.requireCurrentModel(contextNode);
5830
- model = model ?? this.currentModel;
5881
+ hasMappedColumns(modelName) {
5882
+ return [
5883
+ ...this.fieldToColumnMap.keys()
5884
+ ].some((key) => key.startsWith(modelName + "."));
5885
+ }
5886
+ createScopesFromFroms(node, namesMapped) {
5887
+ if (!node) {
5888
+ return [];
5889
+ }
5890
+ return node.froms.map((from) => {
5891
+ const { alias, node: innerNode } = this.stripAlias(from);
5892
+ if (innerNode && TableNode4.is(innerNode)) {
5893
+ return {
5894
+ model: innerNode.table.identifier.name,
5895
+ alias,
5896
+ namesMapped
5897
+ };
5898
+ } else {
5899
+ return void 0;
5900
+ }
5901
+ }).filter((s) => !!s);
5902
+ }
5903
+ // convert a "from" node to a nested query if there are columns with name mapping
5904
+ processFrom(node) {
5905
+ return {
5906
+ ...super.transformFrom(node),
5907
+ froms: node.froms.map((from) => {
5908
+ const { alias, node: innerNode } = this.stripAlias(from);
5909
+ if (!innerNode) {
5910
+ return super.transformNode(from);
5911
+ }
5912
+ if (TableNode4.is(innerNode)) {
5913
+ if (this.hasMappedColumns(innerNode.table.identifier.name)) {
5914
+ const selectAll = this.createSelectAll(innerNode.table.identifier.name);
5915
+ return this.ensureAlias(selectAll, alias, innerNode.table.identifier.name);
5916
+ }
5917
+ }
5918
+ return this.transformNode(from);
5919
+ })
5920
+ };
5921
+ }
5922
+ // create a `SelectQueryNode` for the given model with all columns mapped
5923
+ createSelectAll(model) {
5831
5924
  const modelDef = requireModel(this.schema, model);
5832
- const scalarFields = Object.entries(modelDef.fields).filter(([, fieldDef]) => !fieldDef.relation && !fieldDef.computed && !fieldDef.originModel).map(([fieldName]) => fieldName);
5833
- return scalarFields;
5925
+ const tableName = this.mapTableName(model);
5926
+ return {
5927
+ kind: "SelectQueryNode",
5928
+ from: FromNode3.create([
5929
+ TableNode4.create(tableName)
5930
+ ]),
5931
+ selections: this.getModelFields(modelDef).map((fieldDef) => {
5932
+ const columnName = this.mapFieldName(model, fieldDef.name);
5933
+ const columnRef = ReferenceNode3.create(ColumnNode3.create(columnName), TableNode4.create(tableName));
5934
+ if (columnName !== fieldDef.name) {
5935
+ const aliased = AliasNode4.create(columnRef, IdentifierNode3.create(fieldDef.name));
5936
+ return SelectionNode3.create(aliased);
5937
+ } else {
5938
+ return SelectionNode3.create(columnRef);
5939
+ }
5940
+ })
5941
+ };
5942
+ }
5943
+ getModelFields(modelDef) {
5944
+ return Object.values(modelDef.fields).filter((f) => !f.relation && !f.computed && !f.originModel);
5945
+ }
5946
+ processSelections(selections) {
5947
+ const result = [];
5948
+ selections.forEach((selection) => {
5949
+ if (SelectAllNode.is(selection.selection)) {
5950
+ const processed = this.processSelectAll(selection.selection);
5951
+ if (Array.isArray(processed)) {
5952
+ result.push(...processed.map((s) => SelectionNode3.create(s)));
5953
+ } else {
5954
+ result.push(SelectionNode3.create(processed));
5955
+ }
5956
+ } else {
5957
+ result.push(SelectionNode3.create(this.processSelection(selection.selection)));
5958
+ }
5959
+ });
5960
+ return result;
5961
+ }
5962
+ processSelection(node) {
5963
+ let alias;
5964
+ if (!AliasNode4.is(node)) {
5965
+ alias = this.extractFieldName(node);
5966
+ }
5967
+ const result = super.transformNode(node);
5968
+ return this.wrapAlias(result, alias);
5969
+ }
5970
+ processSelectAll(node) {
5971
+ const scope = this.modelScopes[this.modelScopes.length - 1];
5972
+ invariant9(scope);
5973
+ if (!this.hasMappedColumns(scope.model)) {
5974
+ return super.transformSelectAll(node);
5975
+ }
5976
+ const modelDef = requireModel(this.schema, scope.model);
5977
+ return this.getModelFields(modelDef).map((fieldDef) => {
5978
+ const columnName = this.mapFieldName(scope.model, fieldDef.name);
5979
+ const columnRef = ReferenceNode3.create(ColumnNode3.create(columnName));
5980
+ return columnName !== fieldDef.name ? this.wrapAlias(columnRef, fieldDef.name) : columnRef;
5981
+ });
5982
+ }
5983
+ extractFieldName(node) {
5984
+ if (ReferenceNode3.is(node) && ColumnNode3.is(node.column)) {
5985
+ return node.column.column.name;
5986
+ } else if (ColumnNode3.is(node)) {
5987
+ return node.column.name;
5988
+ } else {
5989
+ return void 0;
5990
+ }
5834
5991
  }
5835
5992
  };
5836
5993
 
@@ -5868,7 +6025,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
5868
6025
  if ((InsertQueryNode2.is(queryNode) || UpdateQueryNode2.is(queryNode)) && mutationInterceptionInfo?.loadAfterMutationEntities) {
5869
6026
  queryNode = {
5870
6027
  ...queryNode,
5871
- returning: ReturningNode3.create([
6028
+ returning: ReturningNode2.create([
5872
6029
  SelectionNode4.createSelectAll()
5873
6030
  ])
5874
6031
  };
@@ -6131,7 +6288,7 @@ __export(functions_exports, {
6131
6288
  search: () => search,
6132
6289
  startsWith: () => startsWith
6133
6290
  });
6134
- import { invariant as invariant9, lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
6291
+ import { invariant as invariant10, lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
6135
6292
  import { sql as sql7, ValueNode as ValueNode4 } from "kysely";
6136
6293
  import { match as match17 } from "ts-pattern";
6137
6294
  var contains = /* @__PURE__ */ __name((eb, args) => {
@@ -6238,7 +6395,7 @@ var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
6238
6395
  }, "currentOperation");
6239
6396
  function processCasing(casing, result, model) {
6240
6397
  const opNode = casing.toOperationNode();
6241
- invariant9(ValueNode4.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
6398
+ invariant10(ValueNode4.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
6242
6399
  result = match17(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => upperCaseFirst(result)).with("uncapitalize", () => lowerCaseFirst(result)).otherwise(() => {
6243
6400
  throw new Error(`Invalid casing value: ${opNode.value}. Must be "original", "upper", "lower", "capitalize", or "uncapitalize".`);
6244
6401
  });
@@ -6247,7 +6404,7 @@ function processCasing(casing, result, model) {
6247
6404
  __name(processCasing, "processCasing");
6248
6405
 
6249
6406
  // src/client/helpers/schema-db-pusher.ts
6250
- import { invariant as invariant10 } from "@zenstackhq/common-helpers";
6407
+ import { invariant as invariant11 } from "@zenstackhq/common-helpers";
6251
6408
  import { sql as sql8 } from "kysely";
6252
6409
  import toposort from "toposort";
6253
6410
  import { match as match18 } from "ts-pattern";
@@ -6343,7 +6500,7 @@ var SchemaDbPusher = class {
6343
6500
  }
6344
6501
  addUniqueConstraint(table, modelDef) {
6345
6502
  for (const [key, value] of Object.entries(modelDef.uniqueFields)) {
6346
- invariant10(typeof value === "object", "expecting an object");
6503
+ invariant11(typeof value === "object", "expecting an object");
6347
6504
  if ("type" in value) {
6348
6505
  const fieldDef = modelDef.fields[key];
6349
6506
  if (fieldDef.unique) {
@@ -6411,7 +6568,7 @@ var SchemaDbPusher = class {
6411
6568
  return fieldDef.default && ExpressionUtils.isCall(fieldDef.default) && fieldDef.default.function === "autoincrement";
6412
6569
  }
6413
6570
  addForeignKeyConstraint(table, model, fieldName, fieldDef) {
6414
- invariant10(fieldDef.relation, "field must be a relation");
6571
+ invariant11(fieldDef.relation, "field must be a relation");
6415
6572
  if (!fieldDef.relation.fields || !fieldDef.relation.references) {
6416
6573
  return table;
6417
6574
  }
@@ -6466,7 +6623,7 @@ function valueToPromise(thing) {
6466
6623
  __name(valueToPromise, "valueToPromise");
6467
6624
 
6468
6625
  // src/client/result-processor.ts
6469
- import { invariant as invariant11 } from "@zenstackhq/common-helpers";
6626
+ import { invariant as invariant12 } from "@zenstackhq/common-helpers";
6470
6627
  import Decimal2 from "decimal.js";
6471
6628
  import { match as match19 } from "ts-pattern";
6472
6629
  var ResultProcessor = class {
@@ -6566,14 +6723,14 @@ var ResultProcessor = class {
6566
6723
  if (value instanceof Decimal2) {
6567
6724
  return value;
6568
6725
  }
6569
- invariant11(typeof value === "string" || typeof value === "number" || value instanceof Decimal2, `Expected string, number or Decimal, got ${typeof value}`);
6726
+ invariant12(typeof value === "string" || typeof value === "number" || value instanceof Decimal2, `Expected string, number or Decimal, got ${typeof value}`);
6570
6727
  return new Decimal2(value);
6571
6728
  }
6572
6729
  transformBigInt(value) {
6573
6730
  if (typeof value === "bigint") {
6574
6731
  return value;
6575
6732
  }
6576
- invariant11(typeof value === "string" || typeof value === "number", `Expected string or number, got ${typeof value}`);
6733
+ invariant12(typeof value === "string" || typeof value === "number", `Expected string or number, got ${typeof value}`);
6577
6734
  return BigInt(value);
6578
6735
  }
6579
6736
  transformBoolean(value) {
@@ -6617,7 +6774,7 @@ var ResultProcessor = class {
6617
6774
  }
6618
6775
  transformJson(value) {
6619
6776
  return match19(this.schema.provider.type).with("sqlite", () => {
6620
- invariant11(typeof value === "string", "Expected string, got " + typeof value);
6777
+ invariant12(typeof value === "string", "Expected string, got " + typeof value);
6621
6778
  return JSON.parse(value);
6622
6779
  }).otherwise(() => value);
6623
6780
  }
@@ -6694,7 +6851,7 @@ var ClientImpl = class _ClientImpl {
6694
6851
  }
6695
6852
  // implementation
6696
6853
  async $transaction(input, options) {
6697
- invariant12(typeof input === "function" || Array.isArray(input) && input.every((p) => p.then && p.cb), "Invalid transaction input, expected a function or an array of ZenStackPromise");
6854
+ invariant13(typeof input === "function" || Array.isArray(input) && input.every((p) => p.then && p.cb), "Invalid transaction input, expected a function or an array of ZenStackPromise");
6698
6855
  if (typeof input === "function") {
6699
6856
  return this.interactiveTransaction(input, options);
6700
6857
  } else {