@duraxis/core 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/config.d.ts +72 -0
- package/dist/index.cjs +178 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +172 -51
- package/dist/index.js.map +1 -1
- package/dist/query/model/cursor.d.ts +0 -2
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @duraxis/core 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/storage@0.2.1
|
|
13
|
+
|
|
3
14
|
## 0.4.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -77,3 +77,9 @@ For versioned evolution use [`@duraxis/migrations`](../migrations/README.md).
|
|
|
77
77
|
## Production resource and lifecycle limits
|
|
78
78
|
|
|
79
79
|
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.
|
|
80
|
+
|
|
81
|
+
## CLI project setup
|
|
82
|
+
|
|
83
|
+
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).
|
|
84
|
+
|
|
85
|
+
`defineDuraxisConfig`, `selectDuraxisConnection`, `normalizeDuraxisConnection`, `duraxisDriverContext` and `createDatabase` are framework-independent exports. Configuration accepts a driver/driver factory or a centralized named `connections` map. Core does not import ORM, NestJS or CLI. `createDatabase` owns factory-created drivers and closes them on initialization failure and default shutdown; supplied instances remain externally owned unless requested.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { StorageDriver } from "@duraxis/storage";
|
|
2
|
+
import { DuraxisDatabase, type DuraxisDatabaseOptions } from "./database/duraxis-database.js";
|
|
3
|
+
export type ConfigEntity = new (...args: never[]) => object;
|
|
4
|
+
export interface EntityDiscoveryOptions {
|
|
5
|
+
readonly directories?: readonly string[];
|
|
6
|
+
readonly allowEmpty?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface DuraxisDriverContext {
|
|
9
|
+
readonly root: string;
|
|
10
|
+
readonly directory: string;
|
|
11
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
12
|
+
readonly resolvePath: (path: string) => string;
|
|
13
|
+
}
|
|
14
|
+
export interface DuraxisConnectionOptions {
|
|
15
|
+
readonly name?: string;
|
|
16
|
+
readonly driver: StorageDriver | ((context: DuraxisDriverContext) => StorageDriver | Promise<StorageDriver>);
|
|
17
|
+
readonly location?: string | URL;
|
|
18
|
+
readonly baseDirectory?: string;
|
|
19
|
+
readonly database?: Omit<DuraxisDatabaseOptions, "storage">;
|
|
20
|
+
readonly cursorKeyEnv?: string;
|
|
21
|
+
readonly closeStorage?: boolean;
|
|
22
|
+
readonly entities?: readonly ConfigEntity[] | (() => readonly ConfigEntity[] | Promise<readonly ConfigEntity[]>);
|
|
23
|
+
readonly autoLoadEntities?: boolean | EntityDiscoveryOptions;
|
|
24
|
+
readonly schema?: {
|
|
25
|
+
readonly mode: "create" | "validate" | "validate-managed";
|
|
26
|
+
};
|
|
27
|
+
readonly migrations?: boolean | {
|
|
28
|
+
readonly directory?: string;
|
|
29
|
+
readonly snapshots?: string;
|
|
30
|
+
readonly format?: "ts" | "js" | "mts" | "mjs";
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface DuraxisConnectionsOptions {
|
|
34
|
+
readonly defaultConnection?: string;
|
|
35
|
+
readonly connections: Readonly<Record<string, DuraxisConnectionOptions>>;
|
|
36
|
+
}
|
|
37
|
+
export type DuraxisConfiguration = DuraxisConnectionOptions | DuraxisConnectionsOptions;
|
|
38
|
+
export declare function isDuraxisConfiguration(value: unknown): value is DuraxisConfiguration;
|
|
39
|
+
export declare function defineDuraxisConfig<const T extends DuraxisConfiguration>(config: T): T;
|
|
40
|
+
export declare function validateConnectionName(name: string): string;
|
|
41
|
+
export declare function selectDuraxisConnection(config: DuraxisConfiguration, name?: string): DuraxisConnectionOptions;
|
|
42
|
+
export declare function duraxisDriverContext(options: DuraxisConnectionOptions): DuraxisDriverContext;
|
|
43
|
+
export declare function normalizeDuraxisConnection(options: DuraxisConnectionOptions): {
|
|
44
|
+
name?: string;
|
|
45
|
+
driver: StorageDriver | ((context: DuraxisDriverContext) => StorageDriver | Promise<StorageDriver>);
|
|
46
|
+
location?: string | URL;
|
|
47
|
+
baseDirectory?: string;
|
|
48
|
+
cursorKeyEnv?: string;
|
|
49
|
+
closeStorage?: boolean;
|
|
50
|
+
entities?: readonly ConfigEntity[] | (() => readonly ConfigEntity[] | Promise<readonly ConfigEntity[]>);
|
|
51
|
+
autoLoadEntities?: boolean | EntityDiscoveryOptions;
|
|
52
|
+
context: DuraxisDriverContext;
|
|
53
|
+
database: {
|
|
54
|
+
cursorSigningKey?: Uint8Array;
|
|
55
|
+
resources?: Partial<import("./index.js").ResourceLimits>;
|
|
56
|
+
cursorEncryption?: import("./index.js").CursorKeyring;
|
|
57
|
+
accessScope?: {
|
|
58
|
+
readonly tenant: string;
|
|
59
|
+
readonly currentTenant: () => string | undefined;
|
|
60
|
+
};
|
|
61
|
+
namespace?: string | undefined;
|
|
62
|
+
};
|
|
63
|
+
schema: {
|
|
64
|
+
readonly mode: "create" | "validate" | "validate-managed";
|
|
65
|
+
};
|
|
66
|
+
migrations: {
|
|
67
|
+
directory: string;
|
|
68
|
+
snapshots: string;
|
|
69
|
+
format: "js" | "mjs" | "mts" | "ts";
|
|
70
|
+
} | undefined;
|
|
71
|
+
};
|
|
72
|
+
export declare function createDatabase(config: DuraxisConfiguration, name?: string): Promise<DuraxisDatabase>;
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let node_crypto = require("node:crypto");
|
|
3
3
|
let _duraxis_storage = require("@duraxis/storage");
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
let node_url = require("node:url");
|
|
4
7
|
// src/resources/resource-manager.ts
|
|
5
8
|
var DEFAULT_RESOURCE_LIMITS = Object.freeze({
|
|
6
9
|
maxQueuedOperations: 64,
|
|
@@ -1657,14 +1660,14 @@ function hasValue(value) {
|
|
|
1657
1660
|
}
|
|
1658
1661
|
function secondaryIndexBounds(index, options) {
|
|
1659
1662
|
const hasEquality = options.eq !== void 0;
|
|
1660
|
-
const
|
|
1663
|
+
const tupleBounds = [
|
|
1661
1664
|
options.gt,
|
|
1662
1665
|
options.gte,
|
|
1663
1666
|
options.lt,
|
|
1664
1667
|
options.lte
|
|
1665
1668
|
].filter((value) => value !== void 0);
|
|
1666
|
-
if (hasEquality && (options.prefix || options.range ||
|
|
1667
|
-
if (options.range &&
|
|
1669
|
+
if (hasEquality && (options.prefix || options.range || tupleBounds.length)) throw new Error("Equality cannot be combined with index ranges");
|
|
1670
|
+
if (options.range && tupleBounds.length) throw new Error("Use either range or tuple range shorthands");
|
|
1668
1671
|
const orders = indexFields(index).map((part) => part.order);
|
|
1669
1672
|
const encode = (values) => {
|
|
1670
1673
|
if (values.length == 0 || values.length > orders.length) throw new Error("Invalid compound key arity");
|
|
@@ -1680,13 +1683,13 @@ function secondaryIndexBounds(index, options) {
|
|
|
1680
1683
|
}
|
|
1681
1684
|
let prefix = options.prefix ?? [];
|
|
1682
1685
|
let range = options.range;
|
|
1683
|
-
if (
|
|
1686
|
+
if (tupleBounds.length) {
|
|
1684
1687
|
if (options.prefix) throw new Error("Tuple range shorthands already contain their prefix");
|
|
1685
|
-
const arity =
|
|
1686
|
-
if (arity == 0 ||
|
|
1687
|
-
prefix =
|
|
1688
|
+
const arity = tupleBounds[0].length;
|
|
1689
|
+
if (arity == 0 || tupleBounds.some((value) => value.length != arity)) throw new Error("Index range arity mismatch");
|
|
1690
|
+
prefix = tupleBounds[0].slice(0, -1);
|
|
1688
1691
|
const encodedPrefix = prefix.length ? encode(prefix) : /* @__PURE__ */ new Uint8Array();
|
|
1689
|
-
for (const value of
|
|
1692
|
+
for (const value of tupleBounds.slice(1)) {
|
|
1690
1693
|
const other = value.slice(0, -1);
|
|
1691
1694
|
if (compareBytes(encodedPrefix, other.length ? encode(other) : /* @__PURE__ */ new Uint8Array()) != 0) throw new Error("Compound range bounds must share an equality prefix");
|
|
1692
1695
|
}
|
|
@@ -2597,7 +2600,7 @@ function hmac(key, value) {
|
|
|
2597
2600
|
function queryHash(key, query) {
|
|
2598
2601
|
return hmac(key, canonical(shape(query)));
|
|
2599
2602
|
}
|
|
2600
|
-
function
|
|
2603
|
+
function cursorEnvelope(generation, schema, query, row, signingKey) {
|
|
2601
2604
|
if (query.collection != schema.name) throw new Error("Cursor collection mismatch");
|
|
2602
2605
|
const values = (query.orderBy ?? []).map(({ field }) => {
|
|
2603
2606
|
if (field == "$id") return row.id;
|
|
@@ -2607,46 +2610,13 @@ function encodeLegacyCursor(generation, schema, query, row, signingKey) {
|
|
|
2607
2610
|
if (value === null || typeof value == "string" || typeof value == "number" || typeof value == "boolean") return value;
|
|
2608
2611
|
throw new Error(`Cannot encode cursor value for ${field}`);
|
|
2609
2612
|
});
|
|
2610
|
-
|
|
2611
|
-
version:
|
|
2613
|
+
return {
|
|
2614
|
+
version: 3,
|
|
2612
2615
|
generation,
|
|
2613
2616
|
queryHash: queryHash(signingKey, query),
|
|
2614
2617
|
values,
|
|
2615
2618
|
id: row.id
|
|
2616
2619
|
};
|
|
2617
|
-
const payload = canonical(envelope);
|
|
2618
|
-
return Buffer.from(JSON.stringify({
|
|
2619
|
-
payload: envelope,
|
|
2620
|
-
signature: hmac(signingKey, payload)
|
|
2621
|
-
})).toString("base64url");
|
|
2622
|
-
}
|
|
2623
|
-
function decodeLegacyCursor(cursor, generation, schema, query, signingKey) {
|
|
2624
|
-
let parsed;
|
|
2625
|
-
try {
|
|
2626
|
-
parsed = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
2627
|
-
} catch {
|
|
2628
|
-
throw new Error("Invalid query cursor");
|
|
2629
|
-
}
|
|
2630
|
-
if (!parsed || typeof parsed != "object" || Array.isArray(parsed)) throw new Error("Invalid query cursor");
|
|
2631
|
-
const record = parsed;
|
|
2632
|
-
const envelope = record.payload;
|
|
2633
|
-
if (!envelope || envelope.version != 2 || typeof record.signature != "string") throw new Error("Invalid query cursor payload");
|
|
2634
|
-
const expectedSignature = hmac(signingKey, canonical(envelope));
|
|
2635
|
-
const actual = Buffer.from(record.signature, "hex");
|
|
2636
|
-
const expected = Buffer.from(expectedSignature, "hex");
|
|
2637
|
-
if (actual.length != expected.length || !(0, node_crypto.timingSafeEqual)(actual, expected)) throw new Error("Invalid query cursor signature");
|
|
2638
|
-
if (envelope.generation != generation) throw new Error("Query cursor snapshot generation is unavailable");
|
|
2639
|
-
if (envelope.queryHash != queryHash(signingKey, query) || query.collection != schema.name) throw new Error("Query cursor is incompatible with this query");
|
|
2640
|
-
const orderBy = query.orderBy ?? [];
|
|
2641
|
-
if (!Array.isArray(envelope.values) || envelope.values.length != orderBy.length || typeof envelope.id != "string") throw new Error("Invalid query cursor payload");
|
|
2642
|
-
return {
|
|
2643
|
-
id: envelope.id,
|
|
2644
|
-
values: envelope.values.map((item, index) => {
|
|
2645
|
-
if (item && typeof item == "object" && "missing" in item) return void 0;
|
|
2646
|
-
if (fieldDefinition(schema, orderBy[index].field)?.type == "date" && typeof item == "string") return new Date(item);
|
|
2647
|
-
return item;
|
|
2648
|
-
})
|
|
2649
|
-
};
|
|
2650
2620
|
}
|
|
2651
2621
|
function cursorKeys(value) {
|
|
2652
2622
|
if (value instanceof Uint8Array) {
|
|
@@ -2666,7 +2636,7 @@ function cursorKeys(value) {
|
|
|
2666
2636
|
function encodeQueryCursor(generation, schema, query, row, keys, namespace = "") {
|
|
2667
2637
|
const ring = cursorKeys(keys);
|
|
2668
2638
|
const key = ring.keys[ring.activeKeyId];
|
|
2669
|
-
const
|
|
2639
|
+
const envelope = cursorEnvelope(generation, schema, query, row, key);
|
|
2670
2640
|
const nonce = (0, node_crypto.randomBytes)(12);
|
|
2671
2641
|
const cipher = (0, node_crypto.createCipheriv)("aes-256-gcm", key, nonce);
|
|
2672
2642
|
cipher.setAAD(Buffer.from(canonical({
|
|
@@ -2675,7 +2645,7 @@ function encodeQueryCursor(generation, schema, query, row, keys, namespace = "")
|
|
|
2675
2645
|
namespace
|
|
2676
2646
|
})));
|
|
2677
2647
|
const payload = JSON.stringify({
|
|
2678
|
-
...
|
|
2648
|
+
...envelope,
|
|
2679
2649
|
version: 3,
|
|
2680
2650
|
namespace,
|
|
2681
2651
|
expiresAt: Date.now() + (ring.ttlMs ?? 9e5)
|
|
@@ -2701,10 +2671,7 @@ function decodeQueryCursor(cursor, generation, schema, query, keys, namespace =
|
|
|
2701
2671
|
throw new Error("Invalid query cursor");
|
|
2702
2672
|
}
|
|
2703
2673
|
if (!outer || typeof outer != "object" || Array.isArray(outer)) throw new Error("Invalid query cursor");
|
|
2704
|
-
if (outer.version !== 3)
|
|
2705
|
-
if (!namespace && ring.allowLegacySigned && ring.legacySigningKey) return decodeLegacyCursor(cursor, generation, schema, query, ring.legacySigningKey);
|
|
2706
|
-
throw new Error("Legacy signed cursors are disabled; restart pagination");
|
|
2707
|
-
}
|
|
2674
|
+
if (outer.version !== 3) throw new Error("Unsupported cursor format; restart pagination");
|
|
2708
2675
|
if (Object.keys(outer).sort().join(",") != "ciphertext,keyId,nonce,tag,version" || typeof outer.keyId != "string" || !Object.hasOwn(ring.keys, outer.keyId)) throw new Error("Invalid query cursor");
|
|
2709
2676
|
const binary = (value) => {
|
|
2710
2677
|
if (typeof value != "string" || !/^[A-Za-z0-9_-]+$/.test(value)) throw new Error("Invalid query cursor");
|
|
@@ -5219,6 +5186,160 @@ var ReferenceQueryExecutor = class {
|
|
|
5219
5186
|
return this.executor.page(context, query, new CollectionScan(query.collection));
|
|
5220
5187
|
}
|
|
5221
5188
|
};
|
|
5189
|
+
// src/config.ts
|
|
5190
|
+
var CONFIGURATION = Symbol.for("@duraxis/core/configuration");
|
|
5191
|
+
function isDuraxisConfiguration(value) {
|
|
5192
|
+
return typeof value === "object" && value !== null && Reflect.get(value, CONFIGURATION) === true;
|
|
5193
|
+
}
|
|
5194
|
+
function validateConnections(config) {
|
|
5195
|
+
if (!config.connections || typeof config.connections !== "object" || Array.isArray(config.connections)) throw new TypeError("Invalid Duraxis connections map");
|
|
5196
|
+
const prefixes = /* @__PURE__ */ new Set();
|
|
5197
|
+
for (const name of Object.keys(config.connections)) {
|
|
5198
|
+
validateConnectionName(name);
|
|
5199
|
+
const prefix = name.toUpperCase().replace(/-/g, "_");
|
|
5200
|
+
if (prefixes.has(prefix)) throw new Error("Connection names collide after environment variable normalization");
|
|
5201
|
+
prefixes.add(prefix);
|
|
5202
|
+
if (!config.connections[name] || typeof config.connections[name] !== "object") throw new TypeError(`Invalid Duraxis connection: ${name}`);
|
|
5203
|
+
const connection = config.connections[name];
|
|
5204
|
+
if (Object.keys(config.connections).length > 1 && connection.autoLoadEntities && (connection.autoLoadEntities === true || !connection.autoLoadEntities.directories?.length) && connection.entities === void 0) throw new Error(`Connection ${name} requires explicit discovery directories or entities in a multi-connection configuration`);
|
|
5205
|
+
}
|
|
5206
|
+
}
|
|
5207
|
+
function defineDuraxisConfig(config) {
|
|
5208
|
+
if (!config || typeof config !== "object") throw new TypeError("Invalid Duraxis configuration");
|
|
5209
|
+
if ("connections" in config) {
|
|
5210
|
+
validateConnections(config);
|
|
5211
|
+
const names = Object.keys(config.connections);
|
|
5212
|
+
if (!names.length) throw new Error("At least one Duraxis connection is required");
|
|
5213
|
+
for (const name of names) validateConnectionName(name);
|
|
5214
|
+
selectDuraxisConnection(config);
|
|
5215
|
+
} else if (!config || !config.driver) throw new Error("A Duraxis storage driver is required");
|
|
5216
|
+
Object.defineProperty(config, CONFIGURATION, { value: true });
|
|
5217
|
+
return config;
|
|
5218
|
+
}
|
|
5219
|
+
function validateConnectionName(name) {
|
|
5220
|
+
if (!/^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/.test(name) || [
|
|
5221
|
+
"constructor",
|
|
5222
|
+
"prototype",
|
|
5223
|
+
"__proto__"
|
|
5224
|
+
].includes(name)) throw new Error("Invalid Duraxis connection name");
|
|
5225
|
+
return name;
|
|
5226
|
+
}
|
|
5227
|
+
function selectDuraxisConnection(config, name) {
|
|
5228
|
+
if (!("connections" in config)) {
|
|
5229
|
+
if (name && name !== (config.name ?? "default")) throw new Error(`Unknown Duraxis connection: ${name}`);
|
|
5230
|
+
return config;
|
|
5231
|
+
}
|
|
5232
|
+
validateConnections(config);
|
|
5233
|
+
const selected = name ?? config.defaultConnection ?? Object.keys(config.connections).sort()[0];
|
|
5234
|
+
validateConnectionName(selected);
|
|
5235
|
+
if (!Object.hasOwn(config.connections, selected)) throw new Error(`Unknown Duraxis connection: ${selected}`);
|
|
5236
|
+
return {
|
|
5237
|
+
...config.connections[selected],
|
|
5238
|
+
name: selected
|
|
5239
|
+
};
|
|
5240
|
+
}
|
|
5241
|
+
function duraxisDriverContext(options) {
|
|
5242
|
+
const directory = options.location ? (0, node_path.dirname)(options.location instanceof URL || options.location.startsWith("file:") ? (0, node_url.fileURLToPath)(options.location) : (0, node_path.resolve)(options.location)) : (0, node_path.resolve)(options.baseDirectory ?? ".");
|
|
5243
|
+
let root = directory;
|
|
5244
|
+
if (options.baseDirectory) root = (0, node_path.resolve)(directory, options.baseDirectory);
|
|
5245
|
+
else while (!(0, node_fs.existsSync)((0, node_path.resolve)(root, "package.json")) && (0, node_path.dirname)(root) !== root) root = (0, node_path.dirname)(root);
|
|
5246
|
+
if ((0, node_path.dirname)(root) === root && !(0, node_fs.existsSync)((0, node_path.resolve)(root, "package.json"))) root = directory;
|
|
5247
|
+
const name = validateConnectionName(options.name ?? "default");
|
|
5248
|
+
const prefix = name === "default" ? "DURAXIS" : `DURAXIS_${name.toUpperCase().replace(/-/g, "_")}`;
|
|
5249
|
+
const env = Object.fromEntries([
|
|
5250
|
+
"NAMESPACE",
|
|
5251
|
+
"STORAGE_DIRECTORY",
|
|
5252
|
+
"S3_BUCKET",
|
|
5253
|
+
"S3_PREFIX",
|
|
5254
|
+
"S3_ENDPOINT"
|
|
5255
|
+
].map((key) => [key, process.env[`${prefix}_${key}`]]));
|
|
5256
|
+
return {
|
|
5257
|
+
root,
|
|
5258
|
+
directory,
|
|
5259
|
+
env,
|
|
5260
|
+
resolvePath: (path) => (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(root, path)
|
|
5261
|
+
};
|
|
5262
|
+
}
|
|
5263
|
+
function normalizeDuraxisConnection(options) {
|
|
5264
|
+
if (!options || typeof options.driver !== "function" && (typeof options.driver !== "object" || !options.driver)) throw new TypeError("Invalid Duraxis driver");
|
|
5265
|
+
if (options.migrations !== void 0 && typeof options.migrations !== "boolean" && (typeof options.migrations !== "object" || options.migrations === null || Array.isArray(options.migrations))) throw new TypeError("Invalid Duraxis migrations configuration");
|
|
5266
|
+
if (typeof options.migrations === "object") {
|
|
5267
|
+
for (const key of ["directory", "snapshots"]) if (options.migrations[key] !== void 0 && (typeof options.migrations[key] !== "string" || !options.migrations[key].trim())) throw new TypeError(`Invalid migration ${key}`);
|
|
5268
|
+
if (options.migrations.format !== void 0 && ![
|
|
5269
|
+
"ts",
|
|
5270
|
+
"js",
|
|
5271
|
+
"mts",
|
|
5272
|
+
"mjs"
|
|
5273
|
+
].includes(options.migrations.format)) throw new TypeError("Invalid migration format");
|
|
5274
|
+
}
|
|
5275
|
+
if (options.schema && ![
|
|
5276
|
+
"create",
|
|
5277
|
+
"validate",
|
|
5278
|
+
"validate-managed"
|
|
5279
|
+
].includes(options.schema.mode)) throw new TypeError("Invalid schema mode");
|
|
5280
|
+
if (options.autoLoadEntities !== void 0 && typeof options.autoLoadEntities !== "boolean" && (typeof options.autoLoadEntities !== "object" || !options.autoLoadEntities || Array.isArray(options.autoLoadEntities))) throw new TypeError("Invalid entity discovery options");
|
|
5281
|
+
if (typeof options.autoLoadEntities === "object" && options.autoLoadEntities.directories !== void 0 && (!Array.isArray(options.autoLoadEntities.directories) || !options.autoLoadEntities.directories.length || options.autoLoadEntities.directories.some((path) => typeof path !== "string" || !path.trim()))) throw new TypeError("Entity directories must be a nonempty array of paths");
|
|
5282
|
+
const context = duraxisDriverContext(options);
|
|
5283
|
+
if (options.migrations && options.schema?.mode === "create") throw new Error("Migrations require schema validation; automatic creation is disabled");
|
|
5284
|
+
const migrationOptions = typeof options.migrations === "object" ? options.migrations : {};
|
|
5285
|
+
const base = options.name && options.name !== "default" ? `duraxis/${validateConnectionName(options.name)}` : "duraxis";
|
|
5286
|
+
const database = {
|
|
5287
|
+
...options.name && options.name !== "default" ? { namespace: options.name } : {},
|
|
5288
|
+
...options.database,
|
|
5289
|
+
...context.env.NAMESPACE !== void 0 ? { namespace: context.env.NAMESPACE } : {}
|
|
5290
|
+
};
|
|
5291
|
+
if (options.cursorKeyEnv) {
|
|
5292
|
+
const key = process.env[options.cursorKeyEnv];
|
|
5293
|
+
if (!key || !/^[a-fA-F0-9]{64}$/.test(key)) throw new Error(`Provision ${options.cursorKeyEnv} as 32 secret bytes encoded as hex`);
|
|
5294
|
+
database.cursorEncryption = {
|
|
5295
|
+
activeKeyId: "configured",
|
|
5296
|
+
keys: { configured: Buffer.from(key, "hex") }
|
|
5297
|
+
};
|
|
5298
|
+
}
|
|
5299
|
+
return {
|
|
5300
|
+
...options,
|
|
5301
|
+
context,
|
|
5302
|
+
database,
|
|
5303
|
+
schema: options.schema ?? { mode: options.migrations ? "validate-managed" : "validate" },
|
|
5304
|
+
migrations: options.migrations ? {
|
|
5305
|
+
directory: context.resolvePath(migrationOptions.directory ?? `${base}/migrations`),
|
|
5306
|
+
snapshots: context.resolvePath(migrationOptions.snapshots ?? `${base}/snapshots`),
|
|
5307
|
+
format: migrationOptions.format ?? (options.location?.toString().match(/\.[cm]?js$/) ? "mjs" : "mts")
|
|
5308
|
+
} : void 0
|
|
5309
|
+
};
|
|
5310
|
+
}
|
|
5311
|
+
var ConfiguredDatabase = class extends DuraxisDatabase {
|
|
5312
|
+
owned;
|
|
5313
|
+
constructor(options, owned) {
|
|
5314
|
+
super(options);
|
|
5315
|
+
this.owned = owned;
|
|
5316
|
+
}
|
|
5317
|
+
close(options = {}) {
|
|
5318
|
+
return super.close({
|
|
5319
|
+
closeStorage: this.owned,
|
|
5320
|
+
...options
|
|
5321
|
+
});
|
|
5322
|
+
}
|
|
5323
|
+
};
|
|
5324
|
+
async function createDatabase(config, name) {
|
|
5325
|
+
const options = normalizeDuraxisConnection(selectDuraxisConnection(config, name));
|
|
5326
|
+
const owned = options.closeStorage ?? typeof options.driver === "function";
|
|
5327
|
+
const driver = typeof options.driver === "function" ? await options.driver(options.context) : options.driver;
|
|
5328
|
+
try {
|
|
5329
|
+
if (!driver || driver.capabilities?.boundedReads !== true) throw new Error("Duraxis storage requires verified bounded-read support");
|
|
5330
|
+
return new ConfiguredDatabase({
|
|
5331
|
+
...options.database,
|
|
5332
|
+
storage: driver
|
|
5333
|
+
}, owned);
|
|
5334
|
+
} catch (cause) {
|
|
5335
|
+
if (owned && driver?.close) try {
|
|
5336
|
+
await driver.close();
|
|
5337
|
+
} catch (cleanup) {
|
|
5338
|
+
throw new AggregateError([cause, cleanup], "Database initialization and cleanup failed");
|
|
5339
|
+
}
|
|
5340
|
+
throw cause;
|
|
5341
|
+
}
|
|
5342
|
+
}
|
|
5222
5343
|
exports.AccessPathEnumerator = AccessPathEnumerator;
|
|
5223
5344
|
exports.BPlusTree = BPlusTree;
|
|
5224
5345
|
exports.BPlusTreeNodeCache = BPlusTreeNodeCache;
|
|
@@ -5268,8 +5389,11 @@ exports.compareBytes = compareBytes;
|
|
|
5268
5389
|
exports.compareIndexScalars = compareIndexScalars;
|
|
5269
5390
|
exports.compareRows = compareRows;
|
|
5270
5391
|
exports.compositePrefixBounds = compositePrefixBounds;
|
|
5392
|
+
exports.createDatabase = createDatabase;
|
|
5271
5393
|
exports.decodeDocument = decodeDocument;
|
|
5272
5394
|
exports.decodeQueryCursor = decodeQueryCursor;
|
|
5395
|
+
exports.defineDuraxisConfig = defineDuraxisConfig;
|
|
5396
|
+
exports.duraxisDriverContext = duraxisDriverContext;
|
|
5273
5397
|
exports.encodeCompositeKey = encodeCompositeKey;
|
|
5274
5398
|
exports.encodeDocument = encodeDocument;
|
|
5275
5399
|
exports.encodeIdentifier = encodeIdentifier;
|
|
@@ -5284,15 +5408,19 @@ exports.indexFields = indexFields;
|
|
|
5284
5408
|
exports.indexId = indexId;
|
|
5285
5409
|
exports.indexRangeBounds = indexRangeBounds;
|
|
5286
5410
|
exports.indexedValue = indexedValue;
|
|
5411
|
+
exports.isDuraxisConfiguration = isDuraxisConfiguration;
|
|
5287
5412
|
exports.materialize = materialize;
|
|
5288
5413
|
exports.normalizeCollection = normalizeCollection;
|
|
5414
|
+
exports.normalizeDuraxisConnection = normalizeDuraxisConnection;
|
|
5289
5415
|
exports.parseRecordKey = parseRecordKey;
|
|
5290
5416
|
exports.prefixBounds = prefixBounds;
|
|
5291
5417
|
exports.prefixSuccessor = prefixSuccessor;
|
|
5292
5418
|
exports.recordKey = recordKey;
|
|
5293
5419
|
exports.resourceLimits = resourceLimits;
|
|
5294
5420
|
exports.secondaryIndexBounds = secondaryIndexBounds;
|
|
5421
|
+
exports.selectDuraxisConnection = selectDuraxisConnection;
|
|
5295
5422
|
exports.validateCollection = validateCollection;
|
|
5423
|
+
exports.validateConnectionName = validateConnectionName;
|
|
5296
5424
|
exports.validateDocument = validateDocument;
|
|
5297
5425
|
exports.validateQuery = validateQuery;
|
|
5298
5426
|
|