@duraxis/orm 0.2.2 → 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/CHANGELOG.md +26 -0
- package/README.md +19 -1
- package/dist/database/duraxis-orm.d.ts +1 -0
- package/dist/index.cjs +88 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +88 -32
- package/dist/index.js.map +1 -1
- package/dist/relations/relation-loader.d.ts +0 -1
- package/dist/schema/schema-exporter.d.ts +5 -0
- package/dist/schema/schema-manager.d.ts +1 -1
- package/dist/transactions/orm-transaction.d.ts +1 -0
- package/dist/transactions/transaction-context.d.ts +1 -0
- package/package.json +6 -6
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
|
-
|
|
771
|
-
|
|
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
|
-
|
|
784
|
-
|
|
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
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
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
|
-
|
|
866
|
-
|
|
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
|
-
|
|
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) {
|
|
@@ -1223,6 +1248,17 @@ function mapJunctionSchemas(metadata) {
|
|
|
1223
1248
|
}
|
|
1224
1249
|
return [...schemas.values()];
|
|
1225
1250
|
}
|
|
1251
|
+
// src/schema/schema-exporter.ts
|
|
1252
|
+
function exportRegistrySchema(registry) {
|
|
1253
|
+
return [...registry.all().map(mapEntitySchema), ...mapJunctionSchemas(registry.all())].map(normalizeCollection);
|
|
1254
|
+
}
|
|
1255
|
+
function exportSchema(entities) {
|
|
1256
|
+
return exportRegistrySchema(new MetadataRegistry(entities).finalize()).map((schema) => ({
|
|
1257
|
+
...schema,
|
|
1258
|
+
indexes: [...schema.indexes].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0),
|
|
1259
|
+
...schema.foreignKeys ? { foreignKeys: [...schema.foreignKeys].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0) } : {}
|
|
1260
|
+
})).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
1261
|
+
}
|
|
1226
1262
|
// src/schema/schema-manager.ts
|
|
1227
1263
|
function same(left, right) {
|
|
1228
1264
|
return JSON.stringify(left) == JSON.stringify(right);
|
|
@@ -1241,9 +1277,9 @@ var SchemaManager = class {
|
|
|
1241
1277
|
}
|
|
1242
1278
|
async initialize() {
|
|
1243
1279
|
const persisted = new Map((await this.database.getCollectionDefinitions()).map((definition) => [definition.name, normalizeCollection(definition)]));
|
|
1244
|
-
const expectedSchemas =
|
|
1280
|
+
const expectedSchemas = exportRegistrySchema(this.registry);
|
|
1245
1281
|
const missing = expectedSchemas.filter((schema) => !persisted.has(schema.name));
|
|
1246
|
-
if (missing.length && this.mode
|
|
1282
|
+
if (missing.length && this.mode != "create") throw new SchemaViolationError(`Missing collection: ${missing[0].name}`);
|
|
1247
1283
|
if (missing.length) {
|
|
1248
1284
|
await this.mutate(() => this.database.defineCollections(missing), `Could not create collections: ${missing.map((schema) => schema.name).join(", ")}`);
|
|
1249
1285
|
for (const schema of missing) persisted.set(schema.name, schema);
|
|
@@ -1253,11 +1289,12 @@ var SchemaManager = class {
|
|
|
1253
1289
|
if (!current) throw new SchemaViolationError(`Missing collection: ${expected.name}`);
|
|
1254
1290
|
this.validateFields(expected, current);
|
|
1255
1291
|
if (!same(expected.foreignKeys ?? [], current.foreignKeys ?? [])) throw new SchemaViolationError(`Incompatible foreign keys: ${expected.name}`);
|
|
1292
|
+
if (this.mode == "validate-managed" && current.indexes.length != expected.indexes.length) throw new SchemaViolationError(`Unexpected managed index: ${expected.name}`);
|
|
1256
1293
|
const actualIndexes = indexMap(current);
|
|
1257
1294
|
for (const expectedIndex of expected.indexes) {
|
|
1258
1295
|
const actual = actualIndexes.get(expectedIndex.name);
|
|
1259
1296
|
if (!actual) {
|
|
1260
|
-
if (this.mode
|
|
1297
|
+
if (this.mode != "create") throw new SchemaViolationError(`Missing index: ${expected.name}/${expectedIndex.name}`);
|
|
1261
1298
|
await this.mutate(() => this.database.addIndex(expected.name, expectedIndex), `Could not create index: ${expected.name}/${expectedIndex.name}`);
|
|
1262
1299
|
continue;
|
|
1263
1300
|
}
|
|
@@ -1284,10 +1321,16 @@ var OrmTransactionManager = class {
|
|
|
1284
1321
|
registry;
|
|
1285
1322
|
#repositories = /* @__PURE__ */ new Map();
|
|
1286
1323
|
#active = true;
|
|
1324
|
+
#abort = () => this.close();
|
|
1325
|
+
get signal() {
|
|
1326
|
+
return this.transaction.signal;
|
|
1327
|
+
}
|
|
1287
1328
|
constructor(database, transaction, registry) {
|
|
1288
1329
|
this.database = database;
|
|
1289
1330
|
this.transaction = transaction;
|
|
1290
1331
|
this.registry = registry;
|
|
1332
|
+
this.transaction.signal.addEventListener("abort", this.#abort, { once: true });
|
|
1333
|
+
if (this.transaction.signal.aborted) this.close();
|
|
1291
1334
|
}
|
|
1292
1335
|
getRepository(entity) {
|
|
1293
1336
|
this.assertActive();
|
|
@@ -1304,10 +1347,11 @@ var OrmTransactionManager = class {
|
|
|
1304
1347
|
}
|
|
1305
1348
|
close() {
|
|
1306
1349
|
this.#active = false;
|
|
1350
|
+
this.transaction.signal.removeEventListener("abort", this.#abort);
|
|
1307
1351
|
this.#repositories.clear();
|
|
1308
1352
|
}
|
|
1309
1353
|
assertActive() {
|
|
1310
|
-
if (!this.#active) throw new TransactionManagerClosedError();
|
|
1354
|
+
if (!this.#active || this.transaction.state != "ACTIVE") throw new TransactionManagerClosedError();
|
|
1311
1355
|
}
|
|
1312
1356
|
};
|
|
1313
1357
|
// src/database/duraxis-orm.ts
|
|
@@ -1317,6 +1361,8 @@ var DuraxisORM = class {
|
|
|
1317
1361
|
#repositories = /* @__PURE__ */ new Map();
|
|
1318
1362
|
#lifecycle = "new";
|
|
1319
1363
|
#initialization;
|
|
1364
|
+
#closing;
|
|
1365
|
+
#resourcesClosed = false;
|
|
1320
1366
|
constructor(options) {
|
|
1321
1367
|
this.options = options;
|
|
1322
1368
|
this.#registry = new MetadataRegistry(options.entities).finalize();
|
|
@@ -1367,13 +1413,23 @@ var DuraxisORM = class {
|
|
|
1367
1413
|
throw cause;
|
|
1368
1414
|
}
|
|
1369
1415
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
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;
|
|
1374
1422
|
this.#lifecycle = "closed";
|
|
1375
1423
|
this.#repositories.clear();
|
|
1376
|
-
|
|
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;
|
|
1377
1433
|
}
|
|
1378
1434
|
get metadata() {
|
|
1379
1435
|
return this.#registry;
|
|
@@ -1383,6 +1439,6 @@ var DuraxisORM = class {
|
|
|
1383
1439
|
if (this.#lifecycle != "initialized") throw new OrmNotInitializedError();
|
|
1384
1440
|
}
|
|
1385
1441
|
};
|
|
1386
|
-
export { Column, CommitOutcomeUnknownError, DuplicateRecordError, DuraxisORM, DuraxisOrmError, Entity, EntityHydrator, EntityMapper, EntityMetadata, EntityNotRegisteredError, Index, InvalidColumnError, InvalidMetadataError, InvalidQueryError, JoinTable, ManyToMany, ManyToOne, MetadataRegistry, MissingPrimaryKeyError, OneToMany, OneToOne, OrmClosedError, OrmNotInitializedError, OrmTransactionManager, PrimaryKey, QueryBuilder, RecordNotFoundError, RelationLoader, RelationOperations, Repository, SchemaManager, SchemaViolationError, TransactionConflictError, TransactionManagerClosedError, UniqueIndexViolationOrmError, ValueTransformer };
|
|
1442
|
+
export { Column, CommitOutcomeUnknownError, DuplicateRecordError, DuraxisORM, DuraxisOrmError, Entity, EntityHydrator, EntityMapper, EntityMetadata, EntityNotRegisteredError, Index, InvalidColumnError, InvalidMetadataError, InvalidQueryError, JoinTable, ManyToMany, ManyToOne, MetadataRegistry, MissingPrimaryKeyError, OneToMany, OneToOne, OrmClosedError, OrmNotInitializedError, OrmTransactionManager, PrimaryKey, QueryBuilder, RecordNotFoundError, RelationLoader, RelationOperations, Repository, SchemaManager, SchemaViolationError, TransactionConflictError, TransactionManagerClosedError, UniqueIndexViolationOrmError, ValueTransformer, exportSchema };
|
|
1387
1443
|
|
|
1388
1444
|
//# sourceMappingURL=index.js.map
|