@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/dist/index.d.ts
CHANGED
|
@@ -26,3 +26,5 @@ export type { Where, WhereOperator, WhereValue, QueryComparisonOperator, QueryCo
|
|
|
26
26
|
export * from "./errors/index.js";
|
|
27
27
|
export { TransactionConflictError, CommitOutcomeUnknownError } from "@duraxis/core";
|
|
28
28
|
export { exportSchema } from "./schema/schema-exporter.js";
|
|
29
|
+
export { discoverEntities, isDuraxisEntity } from "./discovery.js";
|
|
30
|
+
export { createORM } from "./database/create-orm.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { CommitOutcomeUnknownError, CommitOutcomeUnknownError as CommitOutcomeUnknownError$1, TransactionConflictError, TransactionConflictError as TransactionConflictError$1, UniqueIndexViolationError, normalizeCollection } from "@duraxis/core";
|
|
1
|
+
import { CommitOutcomeUnknownError, CommitOutcomeUnknownError as CommitOutcomeUnknownError$1, TransactionConflictError, TransactionConflictError as TransactionConflictError$1, UniqueIndexViolationError, createDatabase, normalizeCollection, normalizeDuraxisConnection, selectDuraxisConnection } from "@duraxis/core";
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
+
import { lstat, readdir, realpath } from "node:fs/promises";
|
|
4
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
3
6
|
// src/metadata/decorator-metadata.ts
|
|
4
7
|
var drafts = /* @__PURE__ */ new WeakMap();
|
|
5
8
|
function ownDraft(entity) {
|
|
@@ -1439,6 +1442,120 @@ var DuraxisORM = class {
|
|
|
1439
1442
|
if (this.#lifecycle != "initialized") throw new OrmNotInitializedError();
|
|
1440
1443
|
}
|
|
1441
1444
|
};
|
|
1442
|
-
|
|
1445
|
+
// src/discovery.ts
|
|
1446
|
+
function isDuraxisEntity(value) {
|
|
1447
|
+
return typeof value === "function" && (ownDraft(value)?.entityApplications ?? 0) > 0;
|
|
1448
|
+
}
|
|
1449
|
+
var excluded = /* @__PURE__ */ new Set([
|
|
1450
|
+
"node_modules",
|
|
1451
|
+
".git",
|
|
1452
|
+
"test",
|
|
1453
|
+
"tests",
|
|
1454
|
+
"__test__",
|
|
1455
|
+
"__tests__",
|
|
1456
|
+
"fixtures",
|
|
1457
|
+
"__fixtures__",
|
|
1458
|
+
"migrations",
|
|
1459
|
+
"snapshots",
|
|
1460
|
+
"dist",
|
|
1461
|
+
"build",
|
|
1462
|
+
"coverage"
|
|
1463
|
+
]);
|
|
1464
|
+
var entityFile = /\.entity\.(?:[cm]?[jt]s)$/;
|
|
1465
|
+
async function noSymlinks(path) {
|
|
1466
|
+
let cursor = resolve(path);
|
|
1467
|
+
while (true) {
|
|
1468
|
+
if ((await lstat(cursor)).isSymbolicLink()) throw new Error(`Entity discovery does not follow symlinks: ${cursor}`);
|
|
1469
|
+
if (dirname(cursor) === cursor) return;
|
|
1470
|
+
cursor = dirname(cursor);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
async function discoverEntities(config, importer = (url) => import(url.href)) {
|
|
1474
|
+
const options = normalizeDuraxisConnection(config);
|
|
1475
|
+
if (options.entities !== void 0) {
|
|
1476
|
+
const entities = typeof options.entities === "function" ? await options.entities() : options.entities;
|
|
1477
|
+
if (!Array.isArray(entities) || entities.some((entity) => !isDuraxisEntity(entity))) throw new Error("Explicit entities must be Duraxis-decorated classes");
|
|
1478
|
+
const unique = [...new Set(entities)];
|
|
1479
|
+
new MetadataRegistry(unique).finalize();
|
|
1480
|
+
return unique;
|
|
1481
|
+
}
|
|
1482
|
+
if (!options.autoLoadEntities) return [];
|
|
1483
|
+
const discovery = typeof options.autoLoadEntities === "object" ? options.autoLoadEntities : {};
|
|
1484
|
+
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`);
|
|
1485
|
+
const files = /* @__PURE__ */ new Set();
|
|
1486
|
+
let inspected = 0;
|
|
1487
|
+
const boundary = await realpath(options.context.root);
|
|
1488
|
+
for (const directory of discovery.directories ?? ["."]) {
|
|
1489
|
+
const path = resolve(options.context.directory, directory);
|
|
1490
|
+
const within = relative(options.context.root, path);
|
|
1491
|
+
if (within === ".." || within.startsWith(`..${sep}`) || resolve(path) === resolve(sep)) throw new Error("Entity directory escapes project boundary");
|
|
1492
|
+
await noSymlinks(path);
|
|
1493
|
+
const root = await realpath(path);
|
|
1494
|
+
const actual = relative(boundary, root);
|
|
1495
|
+
if (actual === ".." || actual.startsWith(`..${sep}`)) throw new Error("Entity directory escapes project boundary");
|
|
1496
|
+
const walk = async (current, depth) => {
|
|
1497
|
+
if ((await lstat(current)).isSymbolicLink() || await realpath(current) !== current) throw new Error("Entity discovery directory changed or became a symlink");
|
|
1498
|
+
if (depth > 64) throw new Error("Entity discovery depth limit exceeded");
|
|
1499
|
+
for (const entry of (await readdir(current, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name, "en"))) {
|
|
1500
|
+
if (++inspected > 1e4) throw new Error("Entity discovery file limit exceeded");
|
|
1501
|
+
if (entry.isSymbolicLink()) continue;
|
|
1502
|
+
const child = resolve(current, entry.name);
|
|
1503
|
+
if (entry.isDirectory() && !excluded.has(entry.name) && !entry.name.startsWith(".")) await walk(child, depth + 1);
|
|
1504
|
+
if (entry.isFile() && entityFile.test(entry.name) && !/\.(?:test|spec|d)\./.test(entry.name)) files.add(child);
|
|
1505
|
+
}
|
|
1506
|
+
};
|
|
1507
|
+
await walk(root, 0);
|
|
1508
|
+
}
|
|
1509
|
+
const entities = /* @__PURE__ */ new Set();
|
|
1510
|
+
const identities = /* @__PURE__ */ new Set();
|
|
1511
|
+
for (const file of [...files].sort()) {
|
|
1512
|
+
const identity = file.replace(/\.[cm]?[jt]s$/, "");
|
|
1513
|
+
if (identities.has(identity)) throw new Error(`Duplicate source/compiled entity representation: ${identity}`);
|
|
1514
|
+
identities.add(identity);
|
|
1515
|
+
await noSymlinks(file);
|
|
1516
|
+
if (await realpath(file) !== file) throw new Error("Entity file changed or became a symlink");
|
|
1517
|
+
let module;
|
|
1518
|
+
try {
|
|
1519
|
+
module = await importer(pathToFileURL(file));
|
|
1520
|
+
} catch (cause) {
|
|
1521
|
+
throw new Error(`Cannot load entity file ${file}`, { cause });
|
|
1522
|
+
}
|
|
1523
|
+
if (module && typeof module === "object") {
|
|
1524
|
+
for (const value of Object.values(module)) if (isDuraxisEntity(value)) entities.add(value);
|
|
1525
|
+
else if (value && typeof value === "object") {
|
|
1526
|
+
for (const item of Object.values(value)) if (isDuraxisEntity(item)) entities.add(item);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
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");
|
|
1531
|
+
const result = [...entities];
|
|
1532
|
+
new MetadataRegistry(result).finalize();
|
|
1533
|
+
return result;
|
|
1534
|
+
}
|
|
1535
|
+
// src/database/create-orm.ts
|
|
1536
|
+
async function createORM(config, name) {
|
|
1537
|
+
const options = normalizeDuraxisConnection(selectDuraxisConnection(config, name));
|
|
1538
|
+
const entities = await discoverEntities(options);
|
|
1539
|
+
const database = await createDatabase(options);
|
|
1540
|
+
try {
|
|
1541
|
+
const orm = new DuraxisORM({
|
|
1542
|
+
database,
|
|
1543
|
+
entities,
|
|
1544
|
+
schema: options.schema,
|
|
1545
|
+
closeDatabase: true,
|
|
1546
|
+
closeStorage: options.closeStorage ?? typeof options.driver === "function"
|
|
1547
|
+
});
|
|
1548
|
+
await orm.initialize();
|
|
1549
|
+
return orm;
|
|
1550
|
+
} catch (cause) {
|
|
1551
|
+
try {
|
|
1552
|
+
await database.close();
|
|
1553
|
+
} catch (cleanup) {
|
|
1554
|
+
throw new AggregateError([cause, cleanup], "ORM initialization and cleanup failed");
|
|
1555
|
+
}
|
|
1556
|
+
throw cause;
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
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, createORM, discoverEntities, exportSchema, isDuraxisEntity };
|
|
1443
1560
|
|
|
1444
1561
|
//# sourceMappingURL=index.js.map
|