@duraxis/orm 0.3.0 → 0.4.0

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
@@ -760,17 +760,25 @@ var MAX_RELATED_ENTITIES = 1e5;
760
760
  var RelationLoader = class {
761
761
  metadata;
762
762
  runtime;
763
- #loaded = 0;
764
763
  constructor(metadata, runtime) {
765
764
  this.metadata = metadata;
766
765
  this.runtime = runtime;
767
766
  }
768
767
  async load(entities, requested, signal) {
769
768
  if (!requested || entities.length == 0) return entities;
770
- await this.loadLevel(this.metadata, entities, requested, signal, 0, /* @__PURE__ */ new Set());
771
- return entities;
769
+ const state = {
770
+ loaded: 0,
771
+ budget: this.runtime.database.createMaterializationBudget()
772
+ };
773
+ try {
774
+ for (const entity of entities) state.budget.add(entity);
775
+ await this.loadLevel(this.metadata, entities, requested, signal, 0, /* @__PURE__ */ new Set(), state);
776
+ return entities;
777
+ } finally {
778
+ state.budget.close();
779
+ }
772
780
  }
773
- async loadLevel(metadata, entities, requested, signal, depth, path) {
781
+ async loadLevel(metadata, entities, requested, signal, depth, path, state) {
774
782
  if (depth >= MAX_DEPTH) throw new Error(`Relation depth exceeds ${MAX_DEPTH}`);
775
783
  for (const [property, nested] of Object.entries(requested)) {
776
784
  if (!nested) continue;
@@ -780,11 +788,12 @@ var RelationLoader = class {
780
788
  const edge = `${metadata.collection}.${property}`;
781
789
  if (path.has(edge)) continue;
782
790
  const related = await this.loadRelation(metadata, entities, relation, signal);
783
- this.#loaded += related.length;
784
- if (this.#loaded > MAX_RELATED_ENTITIES) throw new Error("Related entity expansion limit exceeded");
791
+ state.loaded += related.length;
792
+ for (const entity of related) state.budget.add(entity);
793
+ if (state.loaded > MAX_RELATED_ENTITIES) throw new Error("Related entity expansion limit exceeded");
785
794
  if (nested !== true && related.length) {
786
795
  const target = this.runtime.registry.get(relation.target);
787
- await this.loadLevel(target, related, nested, signal, depth + 1, /* @__PURE__ */ new Set([...path, edge]));
796
+ await this.loadLevel(target, related, nested, signal, depth + 1, /* @__PURE__ */ new Set([...path, edge]), state);
788
797
  }
789
798
  }
790
799
  }
@@ -850,22 +859,30 @@ var RelationLoader = class {
850
859
  async queryBatches(collection, field, values, signal) {
851
860
  const all = [...values];
852
861
  const rows = [];
853
- for (let offset = 0; offset < all.length; offset += BATCH_SIZE) {
854
- signal?.throwIfAborted();
855
- const batch = all.slice(offset, offset + BATCH_SIZE);
856
- if (!batch.length) continue;
857
- const query = {
858
- collection,
859
- where: {
860
- type: "in",
861
- field,
862
- values: batch
862
+ const memory = this.runtime.database.createMaterializationBudget();
863
+ try {
864
+ for (let offset = 0; offset < all.length; offset += BATCH_SIZE) {
865
+ signal?.throwIfAborted();
866
+ const batch = all.slice(offset, offset + BATCH_SIZE);
867
+ if (!batch.length) continue;
868
+ const query = {
869
+ collection,
870
+ where: {
871
+ type: "in",
872
+ field,
873
+ values: batch
874
+ }
875
+ };
876
+ const source = this.runtime.transaction ? this.runtime.transaction.query(query, { signal }) : this.runtime.database.query(query, { signal });
877
+ for await (const row of source) {
878
+ memory.add(row);
879
+ rows.push(row);
863
880
  }
864
- };
865
- const source = this.runtime.transaction ? this.runtime.transaction.query(query, { signal }) : this.runtime.database.query(query, { signal });
866
- for await (const row of source) rows.push(row);
881
+ }
882
+ return rows;
883
+ } finally {
884
+ memory.close();
867
885
  }
868
- return rows;
869
886
  }
870
887
  values(entities, property) {
871
888
  const result = /* @__PURE__ */ new Set();
@@ -1099,7 +1116,15 @@ var Repository = class {
1099
1116
  async queryRows(query, signal) {
1100
1117
  this.runtime.assertUsable();
1101
1118
  const rows = [];
1102
- for await (const row of this.sourceQuery(query, signal)) rows.push(row);
1119
+ const memory = this.runtime.database.createMaterializationBudget();
1120
+ try {
1121
+ for await (const row of this.sourceQuery(query, signal)) {
1122
+ memory.add(row);
1123
+ rows.push(row);
1124
+ }
1125
+ } finally {
1126
+ memory.close();
1127
+ }
1103
1128
  return rows;
1104
1129
  }
1105
1130
  queryPage(query, signal) {
@@ -1296,10 +1321,16 @@ var OrmTransactionManager = class {
1296
1321
  registry;
1297
1322
  #repositories = /* @__PURE__ */ new Map();
1298
1323
  #active = true;
1324
+ #abort = () => this.close();
1325
+ get signal() {
1326
+ return this.transaction.signal;
1327
+ }
1299
1328
  constructor(database, transaction, registry) {
1300
1329
  this.database = database;
1301
1330
  this.transaction = transaction;
1302
1331
  this.registry = registry;
1332
+ this.transaction.signal.addEventListener("abort", this.#abort, { once: true });
1333
+ if (this.transaction.signal.aborted) this.close();
1303
1334
  }
1304
1335
  getRepository(entity) {
1305
1336
  this.assertActive();
@@ -1316,10 +1347,11 @@ var OrmTransactionManager = class {
1316
1347
  }
1317
1348
  close() {
1318
1349
  this.#active = false;
1350
+ this.transaction.signal.removeEventListener("abort", this.#abort);
1319
1351
  this.#repositories.clear();
1320
1352
  }
1321
1353
  assertActive() {
1322
- if (!this.#active) throw new TransactionManagerClosedError();
1354
+ if (!this.#active || this.transaction.state != "ACTIVE") throw new TransactionManagerClosedError();
1323
1355
  }
1324
1356
  };
1325
1357
  // src/database/duraxis-orm.ts
@@ -1329,6 +1361,8 @@ var DuraxisORM = class {
1329
1361
  #repositories = /* @__PURE__ */ new Map();
1330
1362
  #lifecycle = "new";
1331
1363
  #initialization;
1364
+ #closing;
1365
+ #resourcesClosed = false;
1332
1366
  constructor(options) {
1333
1367
  this.options = options;
1334
1368
  this.#registry = new MetadataRegistry(options.entities).finalize();
@@ -1379,13 +1413,23 @@ var DuraxisORM = class {
1379
1413
  throw cause;
1380
1414
  }
1381
1415
  }
1382
- async close() {
1383
- if (this.#lifecycle == "closed") return;
1384
- const initialization = this.#initialization;
1385
- if (initialization) await initialization.catch(() => void 0);
1416
+ resolveCommitOutcome(transactionId) {
1417
+ return this.options.database.resolveCommitOutcome(transactionId);
1418
+ }
1419
+ close() {
1420
+ if (this.#resourcesClosed) return Promise.resolve();
1421
+ if (this.#closing) return this.#closing;
1386
1422
  this.#lifecycle = "closed";
1387
1423
  this.#repositories.clear();
1388
- if (this.options.closeDatabase || this.options.closeStorage) await this.options.database.close({ closeStorage: this.options.closeStorage ?? false });
1424
+ this.#closing = (async () => {
1425
+ if (this.#initialization) await this.#initialization.catch(() => void 0);
1426
+ if (this.options.closeDatabase || this.options.closeStorage) await this.options.database.close({ closeStorage: this.options.closeStorage ?? false });
1427
+ this.#resourcesClosed = true;
1428
+ })();
1429
+ this.#closing.catch(() => {
1430
+ this.#closing = void 0;
1431
+ });
1432
+ return this.#closing;
1389
1433
  }
1390
1434
  get metadata() {
1391
1435
  return this.#registry;