@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @duraxis/orm changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a4bed1c: Harden bounded production pilots with hierarchical resource admission, query and transaction deadlines, tracked pending I/O, bounded materialization, weighted caches, incremental listings and journal replay, and verified single-owner garbage collection.
|
|
8
|
+
|
|
9
|
+
Breaking pre-1.0 changes: Core requires bounded-read adapter capabilities; resource limits now reject oversized work; destructive S3 GC fails closed; encrypted expiring v3 cursors replace signed database cursors. Restart pagination and provision stable encryption keys. Tenant context checks strengthen application isolation but do not authorize raw storage access.
|
|
10
|
+
|
|
11
|
+
Add conservative commit reconciliation and bounded atomic idempotency receipts without callback retries. Preserve manifest CAS, physical persistence formats and migration checksum identities. See docs/PRODUCTION-HARDENING.md for defaults, deployment constraints, receipt retention and remaining index/FK accounting limitations.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [a4bed1c]
|
|
16
|
+
- @duraxis/core@0.4.0
|
|
17
|
+
|
|
18
|
+
## 0.3.0
|
|
19
|
+
|
|
20
|
+
### Minor Changes
|
|
21
|
+
|
|
22
|
+
- eb56e40: Add transactional collection alteration/removal, final-schema validation, schema-aware index rebuilding, and protected subsystem metadata. Add standalone ORM schema export and opt-in strict managed validation. Introduce the migration runtime and CLI with snapshots, schema comparison, atomic history, conservative rollback, baseline adoption, and recovery-aware publication. Document deployment sequencing and transaction memory limits.
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- Updated dependencies [eb56e40]
|
|
27
|
+
- @duraxis/core@0.3.0
|
|
28
|
+
|
|
3
29
|
## 0.2.2
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -55,4 +55,22 @@ await orm.close();
|
|
|
55
55
|
O ORM exige inicialização antes de obter repositórios. O modo `create` cria estruturas
|
|
56
56
|
ausentes, mas não é uma ferramenta geral de migrações destrutivas; `validate` verifica o schema
|
|
57
57
|
existente. Decorators registram metadados quando a classe é avaliada, por isso este pacote não é
|
|
58
|
-
marcado como livre de efeitos colaterais. Requer Node.js
|
|
58
|
+
marcado como livre de efeitos colaterais. Requer Node.js 24.15.0+.
|
|
59
|
+
|
|
60
|
+
## Schema export and migrations
|
|
61
|
+
|
|
62
|
+
`exportSchema(entities)` returns deterministic normalized Core collection
|
|
63
|
+
definitions without opening a database. It reuses finalized decorator metadata,
|
|
64
|
+
including inherited fields, junction collections, foreign keys, and supporting
|
|
65
|
+
indexes. Use this API with `@duraxis/migrations` schema comparison, or use
|
|
66
|
+
[`@duraxis/cli`](../cli/README.md) for snapshot-based generation.
|
|
67
|
+
|
|
68
|
+
The opt-in `schema: { mode: "validate-managed" }` also rejects unexpected indexes
|
|
69
|
+
on registered entity/junction collections. Unrelated collections remain allowed.
|
|
70
|
+
Existing `create` and `validate` modes preserve their behavior and declaration
|
|
71
|
+
order. Production can run `duraxis migrate:up` before ORM initialization with
|
|
72
|
+
schema validation; startup does not automatically execute migrations.
|
|
73
|
+
|
|
74
|
+
## Production resource and lifecycle limits
|
|
75
|
+
|
|
76
|
+
See the [production hardening guide](../../docs/PRODUCTION-HARDENING.md) for bounded defaults, cancellation, verified maintenance, tenant trust boundaries, encrypted cursors, upgrade instructions and remaining pilot limitations. Core rejects storage adapters without bounded-read capability; destructive shared-storage garbage collection is unsupported.
|
|
@@ -10,6 +10,7 @@ export declare class DuraxisORM {
|
|
|
10
10
|
initialize(): Promise<void>;
|
|
11
11
|
getRepository<TEntity extends object>(entity: EntityClass<TEntity>): Repository<TEntity>;
|
|
12
12
|
transaction<TResult>(callback: (manager: TransactionContext) => Promise<TResult> | TResult): Promise<TResult>;
|
|
13
|
+
resolveCommitOutcome(transactionId: string): Promise<"COMMITTED" | "NOT_COMMITTED" | "UNKNOWN">;
|
|
13
14
|
close(): Promise<void>;
|
|
14
15
|
get metadata(): MetadataRegistry;
|
|
15
16
|
private assertReady;
|
package/dist/index.cjs
CHANGED
|
@@ -761,17 +761,25 @@ var MAX_RELATED_ENTITIES = 1e5;
|
|
|
761
761
|
var RelationLoader = class {
|
|
762
762
|
metadata;
|
|
763
763
|
runtime;
|
|
764
|
-
#loaded = 0;
|
|
765
764
|
constructor(metadata, runtime) {
|
|
766
765
|
this.metadata = metadata;
|
|
767
766
|
this.runtime = runtime;
|
|
768
767
|
}
|
|
769
768
|
async load(entities, requested, signal) {
|
|
770
769
|
if (!requested || entities.length == 0) return entities;
|
|
771
|
-
|
|
772
|
-
|
|
770
|
+
const state = {
|
|
771
|
+
loaded: 0,
|
|
772
|
+
budget: this.runtime.database.createMaterializationBudget()
|
|
773
|
+
};
|
|
774
|
+
try {
|
|
775
|
+
for (const entity of entities) state.budget.add(entity);
|
|
776
|
+
await this.loadLevel(this.metadata, entities, requested, signal, 0, /* @__PURE__ */ new Set(), state);
|
|
777
|
+
return entities;
|
|
778
|
+
} finally {
|
|
779
|
+
state.budget.close();
|
|
780
|
+
}
|
|
773
781
|
}
|
|
774
|
-
async loadLevel(metadata, entities, requested, signal, depth, path) {
|
|
782
|
+
async loadLevel(metadata, entities, requested, signal, depth, path, state) {
|
|
775
783
|
if (depth >= MAX_DEPTH) throw new Error(`Relation depth exceeds ${MAX_DEPTH}`);
|
|
776
784
|
for (const [property, nested] of Object.entries(requested)) {
|
|
777
785
|
if (!nested) continue;
|
|
@@ -781,11 +789,12 @@ var RelationLoader = class {
|
|
|
781
789
|
const edge = `${metadata.collection}.${property}`;
|
|
782
790
|
if (path.has(edge)) continue;
|
|
783
791
|
const related = await this.loadRelation(metadata, entities, relation, signal);
|
|
784
|
-
|
|
785
|
-
|
|
792
|
+
state.loaded += related.length;
|
|
793
|
+
for (const entity of related) state.budget.add(entity);
|
|
794
|
+
if (state.loaded > MAX_RELATED_ENTITIES) throw new Error("Related entity expansion limit exceeded");
|
|
786
795
|
if (nested !== true && related.length) {
|
|
787
796
|
const target = this.runtime.registry.get(relation.target);
|
|
788
|
-
await this.loadLevel(target, related, nested, signal, depth + 1, /* @__PURE__ */ new Set([...path, edge]));
|
|
797
|
+
await this.loadLevel(target, related, nested, signal, depth + 1, /* @__PURE__ */ new Set([...path, edge]), state);
|
|
789
798
|
}
|
|
790
799
|
}
|
|
791
800
|
}
|
|
@@ -851,22 +860,30 @@ var RelationLoader = class {
|
|
|
851
860
|
async queryBatches(collection, field, values, signal) {
|
|
852
861
|
const all = [...values];
|
|
853
862
|
const rows = [];
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
863
|
+
const memory = this.runtime.database.createMaterializationBudget();
|
|
864
|
+
try {
|
|
865
|
+
for (let offset = 0; offset < all.length; offset += BATCH_SIZE) {
|
|
866
|
+
signal?.throwIfAborted();
|
|
867
|
+
const batch = all.slice(offset, offset + BATCH_SIZE);
|
|
868
|
+
if (!batch.length) continue;
|
|
869
|
+
const query = {
|
|
870
|
+
collection,
|
|
871
|
+
where: {
|
|
872
|
+
type: "in",
|
|
873
|
+
field,
|
|
874
|
+
values: batch
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
const source = this.runtime.transaction ? this.runtime.transaction.query(query, { signal }) : this.runtime.database.query(query, { signal });
|
|
878
|
+
for await (const row of source) {
|
|
879
|
+
memory.add(row);
|
|
880
|
+
rows.push(row);
|
|
864
881
|
}
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
|
|
882
|
+
}
|
|
883
|
+
return rows;
|
|
884
|
+
} finally {
|
|
885
|
+
memory.close();
|
|
868
886
|
}
|
|
869
|
-
return rows;
|
|
870
887
|
}
|
|
871
888
|
values(entities, property) {
|
|
872
889
|
const result = /* @__PURE__ */ new Set();
|
|
@@ -1100,7 +1117,15 @@ var Repository = class {
|
|
|
1100
1117
|
async queryRows(query, signal) {
|
|
1101
1118
|
this.runtime.assertUsable();
|
|
1102
1119
|
const rows = [];
|
|
1103
|
-
|
|
1120
|
+
const memory = this.runtime.database.createMaterializationBudget();
|
|
1121
|
+
try {
|
|
1122
|
+
for await (const row of this.sourceQuery(query, signal)) {
|
|
1123
|
+
memory.add(row);
|
|
1124
|
+
rows.push(row);
|
|
1125
|
+
}
|
|
1126
|
+
} finally {
|
|
1127
|
+
memory.close();
|
|
1128
|
+
}
|
|
1104
1129
|
return rows;
|
|
1105
1130
|
}
|
|
1106
1131
|
queryPage(query, signal) {
|
|
@@ -1224,6 +1249,17 @@ function mapJunctionSchemas(metadata) {
|
|
|
1224
1249
|
}
|
|
1225
1250
|
return [...schemas.values()];
|
|
1226
1251
|
}
|
|
1252
|
+
// src/schema/schema-exporter.ts
|
|
1253
|
+
function exportRegistrySchema(registry) {
|
|
1254
|
+
return [...registry.all().map(mapEntitySchema), ...mapJunctionSchemas(registry.all())].map(_duraxis_core.normalizeCollection);
|
|
1255
|
+
}
|
|
1256
|
+
function exportSchema(entities) {
|
|
1257
|
+
return exportRegistrySchema(new MetadataRegistry(entities).finalize()).map((schema) => ({
|
|
1258
|
+
...schema,
|
|
1259
|
+
indexes: [...schema.indexes].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0),
|
|
1260
|
+
...schema.foreignKeys ? { foreignKeys: [...schema.foreignKeys].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0) } : {}
|
|
1261
|
+
})).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
1262
|
+
}
|
|
1227
1263
|
// src/schema/schema-manager.ts
|
|
1228
1264
|
function same(left, right) {
|
|
1229
1265
|
return JSON.stringify(left) == JSON.stringify(right);
|
|
@@ -1242,9 +1278,9 @@ var SchemaManager = class {
|
|
|
1242
1278
|
}
|
|
1243
1279
|
async initialize() {
|
|
1244
1280
|
const persisted = new Map((await this.database.getCollectionDefinitions()).map((definition) => [definition.name, (0, _duraxis_core.normalizeCollection)(definition)]));
|
|
1245
|
-
const expectedSchemas =
|
|
1281
|
+
const expectedSchemas = exportRegistrySchema(this.registry);
|
|
1246
1282
|
const missing = expectedSchemas.filter((schema) => !persisted.has(schema.name));
|
|
1247
|
-
if (missing.length && this.mode
|
|
1283
|
+
if (missing.length && this.mode != "create") throw new SchemaViolationError(`Missing collection: ${missing[0].name}`);
|
|
1248
1284
|
if (missing.length) {
|
|
1249
1285
|
await this.mutate(() => this.database.defineCollections(missing), `Could not create collections: ${missing.map((schema) => schema.name).join(", ")}`);
|
|
1250
1286
|
for (const schema of missing) persisted.set(schema.name, schema);
|
|
@@ -1254,11 +1290,12 @@ var SchemaManager = class {
|
|
|
1254
1290
|
if (!current) throw new SchemaViolationError(`Missing collection: ${expected.name}`);
|
|
1255
1291
|
this.validateFields(expected, current);
|
|
1256
1292
|
if (!same(expected.foreignKeys ?? [], current.foreignKeys ?? [])) throw new SchemaViolationError(`Incompatible foreign keys: ${expected.name}`);
|
|
1293
|
+
if (this.mode == "validate-managed" && current.indexes.length != expected.indexes.length) throw new SchemaViolationError(`Unexpected managed index: ${expected.name}`);
|
|
1257
1294
|
const actualIndexes = indexMap(current);
|
|
1258
1295
|
for (const expectedIndex of expected.indexes) {
|
|
1259
1296
|
const actual = actualIndexes.get(expectedIndex.name);
|
|
1260
1297
|
if (!actual) {
|
|
1261
|
-
if (this.mode
|
|
1298
|
+
if (this.mode != "create") throw new SchemaViolationError(`Missing index: ${expected.name}/${expectedIndex.name}`);
|
|
1262
1299
|
await this.mutate(() => this.database.addIndex(expected.name, expectedIndex), `Could not create index: ${expected.name}/${expectedIndex.name}`);
|
|
1263
1300
|
continue;
|
|
1264
1301
|
}
|
|
@@ -1285,10 +1322,16 @@ var OrmTransactionManager = class {
|
|
|
1285
1322
|
registry;
|
|
1286
1323
|
#repositories = /* @__PURE__ */ new Map();
|
|
1287
1324
|
#active = true;
|
|
1325
|
+
#abort = () => this.close();
|
|
1326
|
+
get signal() {
|
|
1327
|
+
return this.transaction.signal;
|
|
1328
|
+
}
|
|
1288
1329
|
constructor(database, transaction, registry) {
|
|
1289
1330
|
this.database = database;
|
|
1290
1331
|
this.transaction = transaction;
|
|
1291
1332
|
this.registry = registry;
|
|
1333
|
+
this.transaction.signal.addEventListener("abort", this.#abort, { once: true });
|
|
1334
|
+
if (this.transaction.signal.aborted) this.close();
|
|
1292
1335
|
}
|
|
1293
1336
|
getRepository(entity) {
|
|
1294
1337
|
this.assertActive();
|
|
@@ -1305,10 +1348,11 @@ var OrmTransactionManager = class {
|
|
|
1305
1348
|
}
|
|
1306
1349
|
close() {
|
|
1307
1350
|
this.#active = false;
|
|
1351
|
+
this.transaction.signal.removeEventListener("abort", this.#abort);
|
|
1308
1352
|
this.#repositories.clear();
|
|
1309
1353
|
}
|
|
1310
1354
|
assertActive() {
|
|
1311
|
-
if (!this.#active) throw new TransactionManagerClosedError();
|
|
1355
|
+
if (!this.#active || this.transaction.state != "ACTIVE") throw new TransactionManagerClosedError();
|
|
1312
1356
|
}
|
|
1313
1357
|
};
|
|
1314
1358
|
// src/database/duraxis-orm.ts
|
|
@@ -1318,6 +1362,8 @@ var DuraxisORM = class {
|
|
|
1318
1362
|
#repositories = /* @__PURE__ */ new Map();
|
|
1319
1363
|
#lifecycle = "new";
|
|
1320
1364
|
#initialization;
|
|
1365
|
+
#closing;
|
|
1366
|
+
#resourcesClosed = false;
|
|
1321
1367
|
constructor(options) {
|
|
1322
1368
|
this.options = options;
|
|
1323
1369
|
this.#registry = new MetadataRegistry(options.entities).finalize();
|
|
@@ -1368,13 +1414,23 @@ var DuraxisORM = class {
|
|
|
1368
1414
|
throw cause;
|
|
1369
1415
|
}
|
|
1370
1416
|
}
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1417
|
+
resolveCommitOutcome(transactionId) {
|
|
1418
|
+
return this.options.database.resolveCommitOutcome(transactionId);
|
|
1419
|
+
}
|
|
1420
|
+
close() {
|
|
1421
|
+
if (this.#resourcesClosed) return Promise.resolve();
|
|
1422
|
+
if (this.#closing) return this.#closing;
|
|
1375
1423
|
this.#lifecycle = "closed";
|
|
1376
1424
|
this.#repositories.clear();
|
|
1377
|
-
|
|
1425
|
+
this.#closing = (async () => {
|
|
1426
|
+
if (this.#initialization) await this.#initialization.catch(() => void 0);
|
|
1427
|
+
if (this.options.closeDatabase || this.options.closeStorage) await this.options.database.close({ closeStorage: this.options.closeStorage ?? false });
|
|
1428
|
+
this.#resourcesClosed = true;
|
|
1429
|
+
})();
|
|
1430
|
+
this.#closing.catch(() => {
|
|
1431
|
+
this.#closing = void 0;
|
|
1432
|
+
});
|
|
1433
|
+
return this.#closing;
|
|
1378
1434
|
}
|
|
1379
1435
|
get metadata() {
|
|
1380
1436
|
return this.#registry;
|
|
@@ -1430,5 +1486,6 @@ Object.defineProperty(exports, "TransactionConflictError", {
|
|
|
1430
1486
|
exports.TransactionManagerClosedError = TransactionManagerClosedError;
|
|
1431
1487
|
exports.UniqueIndexViolationOrmError = UniqueIndexViolationOrmError;
|
|
1432
1488
|
exports.ValueTransformer = ValueTransformer;
|
|
1489
|
+
exports.exportSchema = exportSchema;
|
|
1433
1490
|
|
|
1434
1491
|
//# sourceMappingURL=index.cjs.map
|