joist-core 2.5.0-next.23 → 2.5.0-next.24

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.
Files changed (83) hide show
  1. package/build/EntityManager.cjs +74 -27
  2. package/build/EntityManager.cjs.map +1 -1
  3. package/build/EntityManager.d.cts +18 -0
  4. package/build/EntityManager.d.cts.map +1 -1
  5. package/build/EntityManager.d.mts +18 -0
  6. package/build/EntityManager.d.mts.map +1 -1
  7. package/build/EntityManager.js +74 -27
  8. package/build/EntityManager.js.map +1 -1
  9. package/build/InstanceData.cjs +17 -1
  10. package/build/InstanceData.cjs.map +1 -1
  11. package/build/InstanceData.d.cts +6 -0
  12. package/build/InstanceData.d.cts.map +1 -1
  13. package/build/InstanceData.d.mts +6 -0
  14. package/build/InstanceData.d.mts.map +1 -1
  15. package/build/InstanceData.js +17 -1
  16. package/build/InstanceData.js.map +1 -1
  17. package/build/fields.cjs +2 -0
  18. package/build/fields.cjs.map +1 -1
  19. package/build/fields.d.cts.map +1 -1
  20. package/build/fields.d.mts.map +1 -1
  21. package/build/fields.js +2 -0
  22. package/build/fields.js.map +1 -1
  23. package/build/index.cjs +6 -0
  24. package/build/index.cjs.map +1 -1
  25. package/build/index.d.cts +6 -2
  26. package/build/index.d.cts.map +1 -1
  27. package/build/index.d.mts +6 -2
  28. package/build/index.d.mts.map +1 -1
  29. package/build/index.js +5 -2
  30. package/build/index.js.map +1 -1
  31. package/build/reactivity/ReactionsManager.cjs +14 -5
  32. package/build/reactivity/ReactionsManager.cjs.map +1 -1
  33. package/build/reactivity/ReactionsManager.d.cts +1 -1
  34. package/build/reactivity/ReactionsManager.d.cts.map +1 -1
  35. package/build/reactivity/ReactionsManager.d.mts +1 -1
  36. package/build/reactivity/ReactionsManager.d.mts.map +1 -1
  37. package/build/reactivity/ReactionsManager.js +15 -6
  38. package/build/reactivity/ReactionsManager.js.map +1 -1
  39. package/build/rules.cjs +3 -0
  40. package/build/rules.cjs.map +1 -1
  41. package/build/rules.d.cts +2 -0
  42. package/build/rules.d.cts.map +1 -1
  43. package/build/rules.d.mts +2 -0
  44. package/build/rules.d.mts.map +1 -1
  45. package/build/rules.js +3 -1
  46. package/build/rules.js.map +1 -1
  47. package/build/session/Session.cjs +206 -0
  48. package/build/session/Session.cjs.map +1 -0
  49. package/build/session/Session.d.cts +80 -0
  50. package/build/session/Session.d.cts.map +1 -0
  51. package/build/session/Session.d.mts +80 -0
  52. package/build/session/Session.d.mts.map +1 -0
  53. package/build/session/Session.js +205 -0
  54. package/build/session/Session.js.map +1 -0
  55. package/build/session/changes.cjs +58 -0
  56. package/build/session/changes.cjs.map +1 -0
  57. package/build/session/changes.d.cts +25 -0
  58. package/build/session/changes.d.cts.map +1 -0
  59. package/build/session/changes.d.mts +25 -0
  60. package/build/session/changes.d.mts.map +1 -0
  61. package/build/session/changes.js +57 -0
  62. package/build/session/changes.js.map +1 -0
  63. package/build/session/index.cjs +5 -0
  64. package/build/session/index.d.cts +4 -0
  65. package/build/session/index.d.mts +4 -0
  66. package/build/session/index.js +3 -0
  67. package/build/session/json.cjs +163 -0
  68. package/build/session/json.cjs.map +1 -0
  69. package/build/session/json.d.cts +41 -0
  70. package/build/session/json.d.cts.map +1 -0
  71. package/build/session/json.d.mts +41 -0
  72. package/build/session/json.d.mts.map +1 -0
  73. package/build/session/json.js +154 -0
  74. package/build/session/json.js.map +1 -0
  75. package/build/trusted.cjs +5 -0
  76. package/build/trusted.cjs.map +1 -1
  77. package/build/trusted.d.cts +2 -0
  78. package/build/trusted.d.cts.map +1 -1
  79. package/build/trusted.d.mts +2 -0
  80. package/build/trusted.d.mts.map +1 -1
  81. package/build/trusted.js +5 -1
  82. package/build/trusted.js.map +1 -1
  83. package/package.json +2 -2
@@ -35,20 +35,19 @@ require("./dataloaders/lensDataLoader.cjs");
35
35
  const require_defaults = require("./defaults.cjs");
36
36
  require("./drivers/index.cjs");
37
37
  const require_Entity = require("./Entity.cjs");
38
- const require_rules = require("./rules.cjs");
39
38
  const require_fields = require("./fields.cjs");
40
39
  const require_flush_FlushLock = require("./flush/FlushLock.cjs");
41
- const require_EntityMetadata = require("./EntityMetadata.cjs");
42
40
  const require_flush_JoinRows = require("./flush/JoinRows.cjs");
41
+ const require_EntityMetadata = require("./EntityMetadata.cjs");
43
42
  const require_flush_ReadDrift = require("./flush/ReadDrift.cjs");
44
- const require_loading_loadLens = require("./loading/loadLens.cjs");
45
43
  const require_flush_Todo = require("./flush/Todo.cjs");
44
+ const require_loading_loadLens = require("./loading/loadLens.cjs");
46
45
  const require_index = require("./index.cjs");
47
- const require_queries_find_EntityFilter = require("./queries/find/EntityFilter.cjs");
48
46
  const require_IndexManager = require("./IndexManager.cjs");
47
+ const require_queries_find_EntityFilter = require("./queries/find/EntityFilter.cjs");
49
48
  const require_loading_IsLoadedCache = require("./loading/IsLoadedCache.cjs");
50
- const require_queries_find_QueryParser_collectionJoins = require("./queries/find/QueryParser.collectionJoins.cjs");
51
49
  const require_loading_loadHints = require("./loading/loadHints.cjs");
50
+ const require_queries_find_QueryParser_collectionJoins = require("./queries/find/QueryParser.collectionJoins.cjs");
52
51
  require("./logging/FactoryLogger.cjs");
53
52
  const require_logging_FieldLogger = require("./logging/FieldLogger.cjs");
54
53
  const require_logging_ReactionLogger = require("./logging/ReactionLogger.cjs");
@@ -69,6 +68,7 @@ require("./relations/index.cjs");
69
68
  const require_relations_LazyField = require("./relations/LazyField.cjs");
70
69
  require("./relations/RecursiveCollection.cjs");
71
70
  const require_RowData = require("./RowData.cjs");
71
+ const require_rules = require("./rules.cjs");
72
72
  const require_trusted = require("./trusted.cjs");
73
73
  require("./typeMap.cjs");
74
74
  const require_upsert = require("./upsert.cjs");
@@ -114,6 +114,7 @@ var EntityManager = class EntityManager {
114
114
  #isValidating = false;
115
115
  /** The error that failed a previous `em.flush`, after which this EntityManager can't flush again. */
116
116
  #flushError = void 0;
117
+ #hasRunPreWriteValidation = false;
117
118
  #findRestricted = false;
118
119
  #pendingPercolate = /* @__PURE__ */ new Map();
119
120
  #preloadedRelations = /* @__PURE__ */ new Map();
@@ -189,6 +190,9 @@ var EntityManager = class EntityManager {
189
190
  get entities() {
190
191
  return em.#entitiesArray;
191
192
  },
193
+ get hasFailedFlush() {
194
+ return em.#flushError !== void 0;
195
+ },
192
196
  getEntities(type) {
193
197
  const meta = require_EntityMetadata.getMetadata(type);
194
198
  const entities = em.#entitiesByTag.get(meta.tagName) ?? [];
@@ -973,19 +977,23 @@ var EntityManager = class EntityManager {
973
977
  * It returns entities that have changed (an entity is considered changed if it has been deleted, inserted, or updated)
974
978
  */
975
979
  async flush(flushOptions = {}) {
980
+ if (this.txn && flushOptions.dryRun) throw new Error("Dry runs require their own transaction");
976
981
  const entities = await require_trusted.runInTrustedContext(() => this.#flush(flushOptions));
977
982
  return this.#filterVisible(entities);
978
983
  }
979
984
  async #flush(flushOptions = {}) {
980
985
  if (this.mode === "read-only") throw new ReadOnlyError();
981
986
  if (this.#flushError !== void 0) throw new FailedFlushError(this.#flushError);
982
- const { skipValidation = false } = flushOptions;
987
+ const { skipValidation = false, dryRun = false } = flushOptions;
983
988
  this.#fl.startLock();
989
+ const validateOnly = dryRun === "validate";
990
+ if (validateOnly) this.#hasRunPreWriteValidation = true;
984
991
  const allFlushedEntities = /* @__PURE__ */ new Set();
992
+ let failedValidationRule = false;
985
993
  try {
986
994
  await this.#fl.allowWrites(async () => {
987
995
  await this.flushDeletes();
988
- await this.#rm.recalcPendingReactables("reactables");
996
+ await this.#rm.recalcPendingReactables("reactables", validateOnly);
989
997
  });
990
998
  const createdThenDeleted = /* @__PURE__ */ new Set();
991
999
  const hooksInvoked = /* @__PURE__ */ new Set();
@@ -1007,20 +1015,21 @@ var EntityManager = class EntityManager {
1007
1015
  while (pendingHooks.size > 0) await this.#fl.allowWrites(async () => {
1008
1016
  let todos = require_flush_Todo.createTodos(pendingHooks);
1009
1017
  await require_defaults.setAsyncDefaults(suppressedDefaultTypeErrors, this.ctx, require_flush_Todo.Todo.groupInsertsByTypeAndSubType(todos));
1010
- maybeBumpUpdatedAt(this.#rm, todos, now);
1011
- for (const group of maybeSetupHookOrdering(todos)) {
1012
- await beforeCreate(this.ctx, group);
1013
- await beforeUpdate(this.ctx, group);
1014
- await beforeFlush(this.ctx, group);
1018
+ if (dryRun !== "validate") maybeBumpUpdatedAt(this.#rm, todos, now);
1019
+ for (const group of maybeSetupHookOrdering(todos)) if (this.#hasRunPreWriteValidation) await runPreWriteHooks(this.ctx, group, validateOnly);
1020
+ else {
1021
+ await runHookOnTodos(this.ctx, "beforeCreate", group, ["inserts"]);
1022
+ await runHookOnTodos(this.ctx, "beforeUpdate", group, ["updates"]);
1023
+ await runHookOnTodos(this.ctx, "beforeFlush", group, ["inserts", "updates"]);
1015
1024
  }
1016
1025
  recalcSynchronousDerivedFields(todos);
1017
1026
  do {
1018
1027
  await this.flushDeletes();
1019
- await this.#rm.recalcPendingReactables("reactables");
1028
+ await this.#rm.recalcPendingReactables("reactables", validateOnly);
1020
1029
  } while (this.#pendingDeletes.length > 0);
1021
1030
  if (this.#rm.hasPendingTypeErrors) {
1022
1031
  await this.#rm.recalcPendingTypeErrors();
1023
- if (this.#rm.needsRecalc("reactables")) await this.#rm.recalcPendingReactables("reactables");
1032
+ if (this.#rm.needsRecalc("reactables")) await this.#rm.recalcPendingReactables("reactables", validateOnly);
1024
1033
  }
1025
1034
  for (const e of pendingHooks) hooksInvoked.add(e);
1026
1035
  pendingHooks.clear();
@@ -1045,6 +1054,9 @@ var EntityManager = class EntityManager {
1045
1054
  await validateSimpleRules(entityTodos);
1046
1055
  if (suppressedDefaultTypeErrors.length > 0) throw suppressedDefaultTypeErrors[0];
1047
1056
  await validateReactiveRules(this, this.#rm.logger, entityTodos, joinRowTodos);
1057
+ } catch (error) {
1058
+ failedValidationRule = error instanceof require_rules.ValidationErrors;
1059
+ throw error;
1048
1060
  } finally {
1049
1061
  this.#findRestricted = false;
1050
1062
  }
@@ -1103,7 +1115,8 @@ var EntityManager = class EntityManager {
1103
1115
  await runValidation(entityTodos, joinRowTodos, !skipValidation);
1104
1116
  this.#rm.throwIfAnySuppressedTypeErrors();
1105
1117
  if (suppressedDefaultTypeErrors.length > 0) throw suppressedDefaultTypeErrors[0];
1106
- if (Object.keys(entityTodos).length > 0 || Object.keys(joinRowTodos).length > 0 || this.#rm.hasPendingCommitTransitions() || this.#readSet.size > 0 || this.#forceTransaction) {
1118
+ if (dryRun === "validate") return [...allFlushedEntities];
1119
+ if (Object.keys(entityTodos).length > 0 || Object.keys(joinRowTodos).length > 0 || this.#rm.hasPendingCommitTransitions() || this.#readSet.size > 0 || this.#forceTransaction || dryRun) {
1107
1120
  const checkedReads = /* @__PURE__ */ new Set();
1108
1121
  await this.driver.transaction(this, async () => {
1109
1122
  do {
@@ -1135,10 +1148,10 @@ var EntityManager = class EntityManager {
1135
1148
  for (const e of allFlushedEntities) if (e.isNewEntity && !e.isDeletedEntity) this.#entitiesById.set(e.idTagged, e);
1136
1149
  await runCommitValidation(commitRuleTodos, commitRuleJoinRowTodos);
1137
1150
  }
1138
- await beforeCommit(this.ctx, allFlushedEntities);
1151
+ if (!dryRun) await beforeCommit(this.ctx, allFlushedEntities);
1139
1152
  await this.#rm.runCommitTransitions();
1140
1153
  if (this.#readSet.size > 0) await require_flush_ReadDrift.checkReadDrift(this, this.#readSet, allFlushedEntities, checkedReads);
1141
- if (this.mode === "in-memory-writes") throw new InMemoryRollbackError();
1154
+ if (this.mode === "in-memory-writes" || dryRun) throw new InMemoryRollbackError();
1142
1155
  });
1143
1156
  await afterCommit(this.ctx, allFlushedEntities);
1144
1157
  for (const e of allFlushedEntities) {
@@ -1167,7 +1180,8 @@ var EntityManager = class EntityManager {
1167
1180
  this.#rm.clear();
1168
1181
  return [...allFlushedEntities].sort((a, b) => require_BaseEntity.getInstanceData(a).entityIndex - require_BaseEntity.getInstanceData(b).entityIndex);
1169
1182
  } catch (e) {
1170
- if (!(e instanceof InMemoryRollbackError)) this.#flushError = e;
1183
+ const canRetry = dryRun === "validate" && (failedValidationRule || e instanceof require_rules.TransitionGuardErrors);
1184
+ if (!(e instanceof InMemoryRollbackError) && !canRetry) this.#flushError = e;
1171
1185
  if (e instanceof require_relations_RecursiveCycleError.RecursiveCycleError) {
1172
1186
  const entity = e.entities[0];
1173
1187
  for (const meta of require_EntityMetadata.getBaseAndSelfMetas(require_EntityMetadata.getMetadata(entity))) {
@@ -1187,7 +1201,10 @@ var EntityManager = class EntityManager {
1187
1201
  throw errors;
1188
1202
  }
1189
1203
  }
1190
- if (e instanceof InMemoryRollbackError) return [...allFlushedEntities].sort((a, b) => require_BaseEntity.getInstanceData(a).entityIndex - require_BaseEntity.getInstanceData(b).entityIndex);
1204
+ if (e instanceof InMemoryRollbackError) {
1205
+ if (dryRun === true) this.#flushError = e;
1206
+ return [...allFlushedEntities].sort((a, b) => require_BaseEntity.getInstanceData(a).entityIndex - require_BaseEntity.getInstanceData(b).entityIndex);
1207
+ }
1191
1208
  if (detail && e instanceof Error) e.message = `${e.message} — ${detail}`;
1192
1209
  throw e;
1193
1210
  } finally {
@@ -1893,14 +1910,44 @@ async function invokeHook(fn, entity, ctx) {
1893
1910
  function beforeDelete(ctx, todos) {
1894
1911
  return runHookOnTodos(ctx, "beforeDelete", todos, ["deletes"]);
1895
1912
  }
1896
- function beforeFlush(ctx, todos) {
1897
- return runHookOnTodos(ctx, "beforeFlush", todos, ["inserts", "updates"]);
1898
- }
1899
- function beforeCreate(ctx, todos) {
1900
- return runHookOnTodos(ctx, "beforeCreate", todos, ["inserts"]);
1901
- }
1902
- function beforeUpdate(ctx, todos) {
1903
- return runHookOnTodos(ctx, "beforeUpdate", todos, ["updates"]);
1913
+ /**
1914
+ * Run the pending write hooks in create, update, flush order.
1915
+ *
1916
+ * Capture the entities due for this pass before invoking any callbacks. On validate-only flushes
1917
+ * record completion after each hook batch succeeds, before validation rules can reject the changes.
1918
+ * The ordinary flush loop still owns discovery of new entities and all SQL work.
1919
+ */
1920
+ async function runPreWriteHooks(ctx, todos, trackHookCompletion) {
1921
+ const creates = [], updates = [], flushes = [];
1922
+ for (const todo of Object.values(todos)) {
1923
+ const hasCreate = metaHasHook(todo.metadata, "beforeCreate");
1924
+ const hasUpdate = metaHasHook(todo.metadata, "beforeUpdate");
1925
+ const hasFlush = metaHasHook(todo.metadata, "beforeFlush");
1926
+ if (hasCreate || hasFlush) for (const entity of todo.inserts) {
1927
+ const data = require_BaseEntity.getInstanceData(entity);
1928
+ if (hasCreate && !data.beforeCreateRan) creates.push(entity);
1929
+ if (hasFlush && !data.flushHooksCurrent) flushes.push(entity);
1930
+ }
1931
+ if (hasUpdate || hasFlush) for (const entity of todo.updates) {
1932
+ if (require_BaseEntity.getInstanceData(entity).flushHooksCurrent) continue;
1933
+ if (hasUpdate) updates.push(entity);
1934
+ if (hasFlush) flushes.push(entity);
1935
+ }
1936
+ }
1937
+ await dispatch("beforeCreate", creates);
1938
+ await dispatch("beforeUpdate", updates);
1939
+ await dispatch("beforeFlush", flushes);
1940
+ async function dispatch(hook, entities) {
1941
+ if (!entities.length) return;
1942
+ const live = entities.filter((e) => !e.isDeletedEntity);
1943
+ await runHook(ctx, hook, live);
1944
+ if (!trackHookCompletion) return;
1945
+ for (const entity of live) {
1946
+ const data = require_BaseEntity.getInstanceData(entity);
1947
+ if (hook === "beforeCreate") data.markBeforeCreateRan();
1948
+ else data.flushHooksCurrent = true;
1949
+ }
1950
+ }
1904
1951
  }
1905
1952
  function afterValidation(ctx, todos) {
1906
1953
  return runHookOnTodos(ctx, "afterValidation", todos, ["inserts", "updates"]);