@duraxis/orm 0.4.0 → 0.5.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 +11 -0
- package/README.md +6 -0
- package/dist/database/create-orm.d.ts +3 -0
- package/dist/discovery.d.ts +4 -0
- package/dist/index.cjs +120 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +119 -2
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @duraxis/orm changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 6377fde: Add framework-independent configuration and owned database/ORM factories, bounded decorated-entity discovery, and shared NestJS/CLI schema resolution. Initialize projects with one source-root configuration and direct AST-guided AppModule integration. Support static inspection/mutation, centralized named connections, registry-free generators and source/compiled deployments without runtime CLI dependencies. Use shared typed configuration across runtime and CLI, standard Nest feature modules, and authenticated encrypted cursors. Document storage, migration, discovery and lifecycle contracts.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [6377fde]
|
|
12
|
+
- @duraxis/core@0.5.0
|
|
13
|
+
|
|
3
14
|
## 0.4.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -74,3 +74,9 @@ schema validation; startup does not automatically execute migrations.
|
|
|
74
74
|
## Production resource and lifecycle limits
|
|
75
75
|
|
|
76
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.
|
|
77
|
+
|
|
78
|
+
## CLI project setup
|
|
79
|
+
|
|
80
|
+
Run `npx --package @duraxis/cli@latest duraxis init` to generate one source-root configuration. NestJS AppModule integration is automatic; standalone applications call the public runtime factories. See the [shared configuration API and examples](../../docs/CLI-SETUP.md) and [setup guide](../../docs/CLI-SETUP.md).
|
|
81
|
+
|
|
82
|
+
`createORM(config, connectionName?)` resolves/validates entities before acquiring storage and releases owned resources after initialization failure or `orm.close()`. `discoverEntities` and `isDuraxisEntity` expose conventional file discovery and decorator checks. Explicit `entities` arrays/factories override discovery. Empty/missing/failed discovery is an error by default; named connections require isolated directories/lists. See the shared guide for filesystem boundaries, compiled deployments and Nest feature consistency.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type DuraxisConnectionOptions } from "@duraxis/core";
|
|
2
|
+
import type { EntityClass } from "./metadata/entity-metadata.js";
|
|
3
|
+
export declare function isDuraxisEntity(value: unknown): value is EntityClass;
|
|
4
|
+
export declare function discoverEntities(config: DuraxisConnectionOptions, importer?: (url: URL) => Promise<unknown>): Promise<readonly EntityClass[]>;
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _duraxis_core = require("@duraxis/core");
|
|
3
3
|
let node_crypto = require("node:crypto");
|
|
4
|
+
let node_fs_promises = require("node:fs/promises");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
let node_url = require("node:url");
|
|
4
7
|
// src/metadata/decorator-metadata.ts
|
|
5
8
|
var drafts = /* @__PURE__ */ new WeakMap();
|
|
6
9
|
function ownDraft(entity) {
|
|
@@ -1440,6 +1443,120 @@ var DuraxisORM = class {
|
|
|
1440
1443
|
if (this.#lifecycle != "initialized") throw new OrmNotInitializedError();
|
|
1441
1444
|
}
|
|
1442
1445
|
};
|
|
1446
|
+
// src/discovery.ts
|
|
1447
|
+
function isDuraxisEntity(value) {
|
|
1448
|
+
return typeof value === "function" && (ownDraft(value)?.entityApplications ?? 0) > 0;
|
|
1449
|
+
}
|
|
1450
|
+
var excluded = /* @__PURE__ */ new Set([
|
|
1451
|
+
"node_modules",
|
|
1452
|
+
".git",
|
|
1453
|
+
"test",
|
|
1454
|
+
"tests",
|
|
1455
|
+
"__test__",
|
|
1456
|
+
"__tests__",
|
|
1457
|
+
"fixtures",
|
|
1458
|
+
"__fixtures__",
|
|
1459
|
+
"migrations",
|
|
1460
|
+
"snapshots",
|
|
1461
|
+
"dist",
|
|
1462
|
+
"build",
|
|
1463
|
+
"coverage"
|
|
1464
|
+
]);
|
|
1465
|
+
var entityFile = /\.entity\.(?:[cm]?[jt]s)$/;
|
|
1466
|
+
async function noSymlinks(path) {
|
|
1467
|
+
let cursor = (0, node_path.resolve)(path);
|
|
1468
|
+
while (true) {
|
|
1469
|
+
if ((await (0, node_fs_promises.lstat)(cursor)).isSymbolicLink()) throw new Error(`Entity discovery does not follow symlinks: ${cursor}`);
|
|
1470
|
+
if ((0, node_path.dirname)(cursor) === cursor) return;
|
|
1471
|
+
cursor = (0, node_path.dirname)(cursor);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
async function discoverEntities(config, importer = (url) => import(url.href)) {
|
|
1475
|
+
const options = (0, _duraxis_core.normalizeDuraxisConnection)(config);
|
|
1476
|
+
if (options.entities !== void 0) {
|
|
1477
|
+
const entities = typeof options.entities === "function" ? await options.entities() : options.entities;
|
|
1478
|
+
if (!Array.isArray(entities) || entities.some((entity) => !isDuraxisEntity(entity))) throw new Error("Explicit entities must be Duraxis-decorated classes");
|
|
1479
|
+
const unique = [...new Set(entities)];
|
|
1480
|
+
new MetadataRegistry(unique).finalize();
|
|
1481
|
+
return unique;
|
|
1482
|
+
}
|
|
1483
|
+
if (!options.autoLoadEntities) return [];
|
|
1484
|
+
const discovery = typeof options.autoLoadEntities === "object" ? options.autoLoadEntities : {};
|
|
1485
|
+
if (options.name && options.name !== "default" && !discovery.directories?.length) throw new Error(`Named connection ${options.name} requires explicit entity directories or entities to prevent cross-connection discovery`);
|
|
1486
|
+
const files = /* @__PURE__ */ new Set();
|
|
1487
|
+
let inspected = 0;
|
|
1488
|
+
const boundary = await (0, node_fs_promises.realpath)(options.context.root);
|
|
1489
|
+
for (const directory of discovery.directories ?? ["."]) {
|
|
1490
|
+
const path = (0, node_path.resolve)(options.context.directory, directory);
|
|
1491
|
+
const within = (0, node_path.relative)(options.context.root, path);
|
|
1492
|
+
if (within === ".." || within.startsWith(`..${node_path.sep}`) || (0, node_path.resolve)(path) === (0, node_path.resolve)(node_path.sep)) throw new Error("Entity directory escapes project boundary");
|
|
1493
|
+
await noSymlinks(path);
|
|
1494
|
+
const root = await (0, node_fs_promises.realpath)(path);
|
|
1495
|
+
const actual = (0, node_path.relative)(boundary, root);
|
|
1496
|
+
if (actual === ".." || actual.startsWith(`..${node_path.sep}`)) throw new Error("Entity directory escapes project boundary");
|
|
1497
|
+
const walk = async (current, depth) => {
|
|
1498
|
+
if ((await (0, node_fs_promises.lstat)(current)).isSymbolicLink() || await (0, node_fs_promises.realpath)(current) !== current) throw new Error("Entity discovery directory changed or became a symlink");
|
|
1499
|
+
if (depth > 64) throw new Error("Entity discovery depth limit exceeded");
|
|
1500
|
+
for (const entry of (await (0, node_fs_promises.readdir)(current, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name, "en"))) {
|
|
1501
|
+
if (++inspected > 1e4) throw new Error("Entity discovery file limit exceeded");
|
|
1502
|
+
if (entry.isSymbolicLink()) continue;
|
|
1503
|
+
const child = (0, node_path.resolve)(current, entry.name);
|
|
1504
|
+
if (entry.isDirectory() && !excluded.has(entry.name) && !entry.name.startsWith(".")) await walk(child, depth + 1);
|
|
1505
|
+
if (entry.isFile() && entityFile.test(entry.name) && !/\.(?:test|spec|d)\./.test(entry.name)) files.add(child);
|
|
1506
|
+
}
|
|
1507
|
+
};
|
|
1508
|
+
await walk(root, 0);
|
|
1509
|
+
}
|
|
1510
|
+
const entities = /* @__PURE__ */ new Set();
|
|
1511
|
+
const identities = /* @__PURE__ */ new Set();
|
|
1512
|
+
for (const file of [...files].sort()) {
|
|
1513
|
+
const identity = file.replace(/\.[cm]?[jt]s$/, "");
|
|
1514
|
+
if (identities.has(identity)) throw new Error(`Duplicate source/compiled entity representation: ${identity}`);
|
|
1515
|
+
identities.add(identity);
|
|
1516
|
+
await noSymlinks(file);
|
|
1517
|
+
if (await (0, node_fs_promises.realpath)(file) !== file) throw new Error("Entity file changed or became a symlink");
|
|
1518
|
+
let module;
|
|
1519
|
+
try {
|
|
1520
|
+
module = await importer((0, node_url.pathToFileURL)(file));
|
|
1521
|
+
} catch (cause) {
|
|
1522
|
+
throw new Error(`Cannot load entity file ${file}`, { cause });
|
|
1523
|
+
}
|
|
1524
|
+
if (module && typeof module === "object") {
|
|
1525
|
+
for (const value of Object.values(module)) if (isDuraxisEntity(value)) entities.add(value);
|
|
1526
|
+
else if (value && typeof value === "object") {
|
|
1527
|
+
for (const item of Object.values(value)) if (isDuraxisEntity(item)) entities.add(item);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
if (!entities.size && !discovery.allowEmpty) throw new Error("No Duraxis entities discovered. Check directories and decorators; use allowEmpty:true only for an intentionally empty schema");
|
|
1532
|
+
const result = [...entities];
|
|
1533
|
+
new MetadataRegistry(result).finalize();
|
|
1534
|
+
return result;
|
|
1535
|
+
}
|
|
1536
|
+
// src/database/create-orm.ts
|
|
1537
|
+
async function createORM(config, name) {
|
|
1538
|
+
const options = (0, _duraxis_core.normalizeDuraxisConnection)((0, _duraxis_core.selectDuraxisConnection)(config, name));
|
|
1539
|
+
const entities = await discoverEntities(options);
|
|
1540
|
+
const database = await (0, _duraxis_core.createDatabase)(options);
|
|
1541
|
+
try {
|
|
1542
|
+
const orm = new DuraxisORM({
|
|
1543
|
+
database,
|
|
1544
|
+
entities,
|
|
1545
|
+
schema: options.schema,
|
|
1546
|
+
closeDatabase: true,
|
|
1547
|
+
closeStorage: options.closeStorage ?? typeof options.driver === "function"
|
|
1548
|
+
});
|
|
1549
|
+
await orm.initialize();
|
|
1550
|
+
return orm;
|
|
1551
|
+
} catch (cause) {
|
|
1552
|
+
try {
|
|
1553
|
+
await database.close();
|
|
1554
|
+
} catch (cleanup) {
|
|
1555
|
+
throw new AggregateError([cause, cleanup], "ORM initialization and cleanup failed");
|
|
1556
|
+
}
|
|
1557
|
+
throw cause;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1443
1560
|
exports.Column = Column;
|
|
1444
1561
|
Object.defineProperty(exports, "CommitOutcomeUnknownError", {
|
|
1445
1562
|
enumerable: true,
|
|
@@ -1486,6 +1603,9 @@ Object.defineProperty(exports, "TransactionConflictError", {
|
|
|
1486
1603
|
exports.TransactionManagerClosedError = TransactionManagerClosedError;
|
|
1487
1604
|
exports.UniqueIndexViolationOrmError = UniqueIndexViolationOrmError;
|
|
1488
1605
|
exports.ValueTransformer = ValueTransformer;
|
|
1606
|
+
exports.createORM = createORM;
|
|
1607
|
+
exports.discoverEntities = discoverEntities;
|
|
1489
1608
|
exports.exportSchema = exportSchema;
|
|
1609
|
+
exports.isDuraxisEntity = isDuraxisEntity;
|
|
1490
1610
|
|
|
1491
1611
|
//# sourceMappingURL=index.cjs.map
|