@zenstackhq/runtime 3.0.0-alpha.21 → 3.0.0-alpha.23

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.
@@ -615,11 +615,11 @@ type MutationInterceptionFilterResult = {
615
615
  /**
616
616
  * Whether entities should be loaded before the mutation.
617
617
  */
618
- loadBeforeMutationEntity?: boolean;
618
+ loadBeforeMutationEntities?: boolean;
619
619
  /**
620
620
  * Whether entities should be loaded after the mutation.
621
621
  */
622
- loadAfterMutationEntity?: boolean;
622
+ loadAfterMutationEntities?: boolean;
623
623
  };
624
624
  type MutationHooksArgs<Schema extends SchemaDef> = {
625
625
  /**
@@ -685,15 +685,15 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
685
685
  mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
686
686
  /**
687
687
  * Called before an entity is mutated.
688
- * @param args.entity Only available if `loadBeforeMutationEntity` is set to true in the
688
+ * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
689
689
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
690
690
  */
691
691
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
692
692
  /**
693
693
  * Called after an entity is mutated.
694
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntity` is set to true in the
694
+ * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
695
695
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
696
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntity` is set to true in the
696
+ * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
697
697
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
698
698
  */
699
699
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
@@ -615,11 +615,11 @@ type MutationInterceptionFilterResult = {
615
615
  /**
616
616
  * Whether entities should be loaded before the mutation.
617
617
  */
618
- loadBeforeMutationEntity?: boolean;
618
+ loadBeforeMutationEntities?: boolean;
619
619
  /**
620
620
  * Whether entities should be loaded after the mutation.
621
621
  */
622
- loadAfterMutationEntity?: boolean;
622
+ loadAfterMutationEntities?: boolean;
623
623
  };
624
624
  type MutationHooksArgs<Schema extends SchemaDef> = {
625
625
  /**
@@ -685,15 +685,15 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
685
685
  mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
686
686
  /**
687
687
  * Called before an entity is mutated.
688
- * @param args.entity Only available if `loadBeforeMutationEntity` is set to true in the
688
+ * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
689
689
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
690
690
  */
691
691
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
692
692
  /**
693
693
  * Called after an entity is mutated.
694
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntity` is set to true in the
694
+ * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
695
695
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
696
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntity` is set to true in the
696
+ * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
697
697
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
698
698
  */
699
699
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
package/dist/index.cjs CHANGED
@@ -5908,7 +5908,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely13
5908
5908
  await this.callBeforeMutationHooks(queryNode, mutationInterceptionInfo);
5909
5909
  }
5910
5910
  const oldQueryNode = queryNode;
5911
- if ((import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) && mutationInterceptionInfo?.loadAfterMutationEntity) {
5911
+ if ((import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) && mutationInterceptionInfo?.loadAfterMutationEntities) {
5912
5912
  queryNode = {
5913
5913
  ...queryNode,
5914
5914
  returning: import_kysely13.ReturningNode.create([
@@ -6054,12 +6054,12 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6054
6054
  queryNode
6055
6055
  });
6056
6056
  result.intercept ||= filterResult.intercept;
6057
- result.loadBeforeMutationEntity ||= filterResult.loadBeforeMutationEntity;
6058
- result.loadAfterMutationEntity ||= filterResult.loadAfterMutationEntity;
6057
+ result.loadBeforeMutationEntities ||= filterResult.loadBeforeMutationEntities;
6058
+ result.loadAfterMutationEntities ||= filterResult.loadAfterMutationEntities;
6059
6059
  }
6060
6060
  }
6061
6061
  let beforeMutationEntities;
6062
- if (result.loadBeforeMutationEntity && (import_kysely13.UpdateQueryNode.is(queryNode) || import_kysely13.DeleteQueryNode.is(queryNode))) {
6062
+ if (result.loadBeforeMutationEntities && (import_kysely13.UpdateQueryNode.is(queryNode) || import_kysely13.DeleteQueryNode.is(queryNode))) {
6063
6063
  beforeMutationEntities = await this.loadEntities(mutationModel, where);
6064
6064
  }
6065
6065
  return {
@@ -6108,7 +6108,7 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6108
6108
  const inTransaction = this.driver.isTransactionConnection(connection);
6109
6109
  for (const hook of hooks) {
6110
6110
  let afterMutationEntities = void 0;
6111
- if (mutationInterceptionInfo.loadAfterMutationEntity) {
6111
+ if (mutationInterceptionInfo.loadAfterMutationEntities) {
6112
6112
  if (import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) {
6113
6113
  afterMutationEntities = queryResult.rows;
6114
6114
  }
@@ -6290,6 +6290,7 @@ __name(processCasing, "processCasing");
6290
6290
  // src/client/helpers/schema-db-pusher.ts
6291
6291
  var import_common_helpers11 = require("@zenstackhq/common-helpers");
6292
6292
  var import_kysely15 = require("kysely");
6293
+ var import_toposort = __toESM(require("toposort"), 1);
6293
6294
  var import_ts_pattern18 = require("ts-pattern");
6294
6295
  var SchemaDbPusher = class {
6295
6296
  static {
@@ -6309,41 +6310,79 @@ var SchemaDbPusher = class {
6309
6310
  await createEnum.execute();
6310
6311
  }
6311
6312
  }
6312
- for (const model of Object.keys(this.schema.models)) {
6313
- const createTable = this.createModelTable(tx, model);
6313
+ const sortedModels = this.sortModels(this.schema.models);
6314
+ for (const modelDef of sortedModels) {
6315
+ const createTable = this.createModelTable(tx, modelDef);
6314
6316
  await createTable.execute();
6315
6317
  }
6316
6318
  });
6317
6319
  }
6318
- createModelTable(kysely, model) {
6319
- let table = kysely.schema.createTable(model).ifNotExists();
6320
- const modelDef = requireModel(this.schema, model);
6320
+ sortModels(models) {
6321
+ const graph = [];
6322
+ for (const model of Object.values(models)) {
6323
+ let added = false;
6324
+ if (model.baseModel) {
6325
+ const baseDef = requireModel(this.schema, model.baseModel);
6326
+ graph.push([
6327
+ model,
6328
+ baseDef
6329
+ ]);
6330
+ added = true;
6331
+ }
6332
+ for (const field of Object.values(model.fields)) {
6333
+ if (field.relation && field.relation.fields && field.relation.references) {
6334
+ const targetModel = requireModel(this.schema, field.type);
6335
+ graph.push([
6336
+ model,
6337
+ targetModel
6338
+ ]);
6339
+ added = true;
6340
+ }
6341
+ }
6342
+ if (!added) {
6343
+ graph.push([
6344
+ model,
6345
+ void 0
6346
+ ]);
6347
+ }
6348
+ }
6349
+ return (0, import_toposort.default)(graph).reverse().filter((m) => !!m);
6350
+ }
6351
+ createModelTable(kysely, modelDef) {
6352
+ let table = kysely.schema.createTable(modelDef.name).ifNotExists();
6321
6353
  for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
6354
+ if (fieldDef.originModel && !fieldDef.id) {
6355
+ continue;
6356
+ }
6322
6357
  if (fieldDef.relation) {
6323
- table = this.addForeignKeyConstraint(table, model, fieldName, fieldDef);
6358
+ table = this.addForeignKeyConstraint(table, modelDef.name, fieldName, fieldDef);
6324
6359
  } else if (!this.isComputedField(fieldDef)) {
6325
- table = this.createModelField(table, fieldName, fieldDef, modelDef);
6360
+ table = this.createModelField(table, fieldDef, modelDef);
6326
6361
  }
6327
6362
  }
6328
- table = this.addPrimaryKeyConstraint(table, model, modelDef);
6329
- table = this.addUniqueConstraint(table, model, modelDef);
6363
+ if (modelDef.baseModel) {
6364
+ const baseModelDef = requireModel(this.schema, modelDef.baseModel);
6365
+ table = table.addForeignKeyConstraint(`fk_${modelDef.baseModel}_delegate`, baseModelDef.idFields, modelDef.baseModel, baseModelDef.idFields, (cb) => cb.onDelete("cascade").onUpdate("cascade"));
6366
+ }
6367
+ table = this.addPrimaryKeyConstraint(table, modelDef);
6368
+ table = this.addUniqueConstraint(table, modelDef);
6330
6369
  return table;
6331
6370
  }
6332
6371
  isComputedField(fieldDef) {
6333
6372
  return fieldDef.attributes?.some((a) => a.name === "@computed");
6334
6373
  }
6335
- addPrimaryKeyConstraint(table, model, modelDef) {
6374
+ addPrimaryKeyConstraint(table, modelDef) {
6336
6375
  if (modelDef.idFields.length === 1) {
6337
6376
  if (Object.values(modelDef.fields).some((f) => f.id)) {
6338
6377
  return table;
6339
6378
  }
6340
6379
  }
6341
6380
  if (modelDef.idFields.length > 0) {
6342
- table = table.addPrimaryKeyConstraint(`pk_${model}`, modelDef.idFields);
6381
+ table = table.addPrimaryKeyConstraint(`pk_${modelDef.name}`, modelDef.idFields);
6343
6382
  }
6344
6383
  return table;
6345
6384
  }
6346
- addUniqueConstraint(table, model, modelDef) {
6385
+ addUniqueConstraint(table, modelDef) {
6347
6386
  for (const [key, value] of Object.entries(modelDef.uniqueFields)) {
6348
6387
  (0, import_common_helpers11.invariant)(typeof value === "object", "expecting an object");
6349
6388
  if ("type" in value) {
@@ -6351,17 +6390,17 @@ var SchemaDbPusher = class {
6351
6390
  if (fieldDef.unique) {
6352
6391
  continue;
6353
6392
  }
6354
- table = table.addUniqueConstraint(`unique_${model}_${key}`, [
6393
+ table = table.addUniqueConstraint(`unique_${modelDef.name}_${key}`, [
6355
6394
  key
6356
6395
  ]);
6357
6396
  } else {
6358
- table = table.addUniqueConstraint(`unique_${model}_${key}`, Object.keys(value));
6397
+ table = table.addUniqueConstraint(`unique_${modelDef.name}_${key}`, Object.keys(value));
6359
6398
  }
6360
6399
  }
6361
6400
  return table;
6362
6401
  }
6363
- createModelField(table, fieldName, fieldDef, modelDef) {
6364
- return table.addColumn(fieldName, this.mapFieldType(fieldDef), (col) => {
6402
+ createModelField(table, fieldDef, modelDef) {
6403
+ return table.addColumn(fieldDef.name, this.mapFieldType(fieldDef), (col) => {
6365
6404
  if (fieldDef.id && modelDef.idFields.length === 1) {
6366
6405
  col = col.primaryKey();
6367
6406
  }