@duraxis/core 0.1.0 → 0.2.2
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 +24 -0
- package/dist/catalog/collection-schema.d.ts +11 -0
- package/dist/commit/commit-coordinator.d.ts +1 -0
- package/dist/database/duraxis-database.d.ts +1 -0
- package/dist/errors/foreign-key.error.d.ts +15 -0
- package/dist/index.cjs +237 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +233 -12
- package/dist/index.js.map +1 -1
- package/dist/relations/foreign-key-manager.d.ts +15 -0
- package/dist/transactions/transaction.d.ts +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
# @duraxis/core changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8c357b5: fix: padronize the published versions
|
|
8
|
+
- 8c357b5: fix: padronize the published versions
|
|
9
|
+
- Updated dependencies [8c357b5]
|
|
10
|
+
- Updated dependencies [8c357b5]
|
|
11
|
+
- @duraxis/storage@0.1.2
|
|
12
|
+
|
|
13
|
+
## 0.2.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- e6facc7: fix: padronize the published versions
|
|
18
|
+
- Updated dependencies [e6facc7]
|
|
19
|
+
- @duraxis/storage@0.1.1
|
|
20
|
+
|
|
21
|
+
## 0.2.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- 6123078: Add transactional foreign-key integrity, referential actions, ORM relationship decorators, batched relation loading, and ManyToMany association operations.
|
|
26
|
+
|
|
3
27
|
This file is maintained by Changesets. Future releases will add versioned
|
|
4
28
|
entries automatically.
|
|
@@ -16,11 +16,22 @@ export interface SecondaryIndexDefinition {
|
|
|
16
16
|
readonly fields?: readonly IndexFieldDefinition[];
|
|
17
17
|
readonly field?: string;
|
|
18
18
|
}
|
|
19
|
+
export type ReferentialAction = "RESTRICT" | "CASCADE" | "SET_NULL";
|
|
20
|
+
export interface ForeignKeyDefinition {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly field: string;
|
|
23
|
+
readonly references: {
|
|
24
|
+
readonly collection: string;
|
|
25
|
+
readonly field: "$id";
|
|
26
|
+
};
|
|
27
|
+
readonly onDelete: ReferentialAction;
|
|
28
|
+
}
|
|
19
29
|
export interface CollectionDefinition {
|
|
20
30
|
readonly name: string;
|
|
21
31
|
readonly fields?: Readonly<Record<string, FieldDefinition>>;
|
|
22
32
|
readonly allowAdditionalFields?: boolean;
|
|
23
33
|
readonly indexes: readonly SecondaryIndexDefinition[];
|
|
34
|
+
readonly foreignKeys?: readonly ForeignKeyDefinition[];
|
|
24
35
|
}
|
|
25
36
|
export type DocumentRecord = Record<string, unknown>;
|
|
26
37
|
export declare const RECORD_PREFIX = "@duraxis/records/";
|
|
@@ -6,6 +6,7 @@ export declare class CommitCoordinator {
|
|
|
6
6
|
private readonly keys;
|
|
7
7
|
private readonly primary;
|
|
8
8
|
private readonly secondary;
|
|
9
|
+
private readonly foreignKeys;
|
|
9
10
|
constructor(storage: StorageDriver, keys?: StorageKeyManager);
|
|
10
11
|
commit(transaction: Transaction): Promise<void>;
|
|
11
12
|
}
|
|
@@ -48,6 +48,7 @@ export declare class DuraxisDatabase {
|
|
|
48
48
|
transaction<T>(callback: (tx: Transaction) => Promise<T> | T): Promise<T>;
|
|
49
49
|
snapshot<T>(callback: (reader: SnapshotReader) => Promise<T> | T): Promise<T>;
|
|
50
50
|
defineCollection(schema: CollectionDefinition): Promise<void>;
|
|
51
|
+
defineCollections(schemas: readonly CollectionDefinition[]): Promise<void>;
|
|
51
52
|
getCollectionDefinitions(): Promise<readonly CollectionDefinition[]>;
|
|
52
53
|
addIndex(collection: string, index: SecondaryIndexDefinition): Promise<void>;
|
|
53
54
|
removeIndex(collection: string, indexName: string): Promise<void>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class ForeignKeyViolationError extends Error {
|
|
2
|
+
readonly collection: string;
|
|
3
|
+
readonly constraint: string;
|
|
4
|
+
readonly reference: string;
|
|
5
|
+
constructor(message: string, collection: string, constraint: string, reference: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class ReferencedRecordNotFoundError extends ForeignKeyViolationError {
|
|
8
|
+
constructor(collection: string, constraint: string, reference: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class ReferencedRecordRestrictedError extends ForeignKeyViolationError {
|
|
11
|
+
constructor(collection: string, constraint: string, reference: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class InvalidForeignKeyDefinitionError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -70,6 +70,37 @@ var StorageKeyManager = class {
|
|
|
70
70
|
if (!this.indexNodePattern.test(key)) throw new Error("Invalid or foreign Duraxis index node reference");
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
|
+
// src/errors/foreign-key.error.ts
|
|
74
|
+
var ForeignKeyViolationError = class extends Error {
|
|
75
|
+
collection;
|
|
76
|
+
constraint;
|
|
77
|
+
reference;
|
|
78
|
+
constructor(message, collection, constraint, reference) {
|
|
79
|
+
super(message);
|
|
80
|
+
this.collection = collection;
|
|
81
|
+
this.constraint = constraint;
|
|
82
|
+
this.reference = reference;
|
|
83
|
+
this.name = "ForeignKeyViolationError";
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var ReferencedRecordNotFoundError = class extends ForeignKeyViolationError {
|
|
87
|
+
constructor(collection, constraint, reference) {
|
|
88
|
+
super(`Referenced record not found for ${collection}/${constraint}: ${reference}`, collection, constraint, reference);
|
|
89
|
+
this.name = "ReferencedRecordNotFoundError";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var ReferencedRecordRestrictedError = class extends ForeignKeyViolationError {
|
|
93
|
+
constructor(collection, constraint, reference) {
|
|
94
|
+
super(`Referenced record is still used by ${collection}/${constraint}: ${reference}`, collection, constraint, reference);
|
|
95
|
+
this.name = "ReferencedRecordRestrictedError";
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var InvalidForeignKeyDefinitionError = class extends Error {
|
|
99
|
+
constructor(message) {
|
|
100
|
+
super(message);
|
|
101
|
+
this.name = "InvalidForeignKeyDefinitionError";
|
|
102
|
+
}
|
|
103
|
+
};
|
|
73
104
|
// src/catalog/collection-schema.ts
|
|
74
105
|
var NAME = /^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/;
|
|
75
106
|
var RECORD_PREFIX = "@duraxis/records/";
|
|
@@ -93,18 +124,42 @@ function normalizeCollection(schema) {
|
|
|
93
124
|
required: field.required ?? false,
|
|
94
125
|
nullable: field.nullable ?? false
|
|
95
126
|
}])) : void 0;
|
|
127
|
+
const foreignKeys = [...schema.foreignKeys ?? []].sort((left, right) => left.name.localeCompare(right.name)).map((foreignKey) => ({
|
|
128
|
+
name: foreignKey.name,
|
|
129
|
+
field: foreignKey.field,
|
|
130
|
+
references: {
|
|
131
|
+
collection: foreignKey.references.collection,
|
|
132
|
+
field: "$id"
|
|
133
|
+
},
|
|
134
|
+
onDelete: foreignKey.onDelete
|
|
135
|
+
}));
|
|
136
|
+
const indexes = schema.indexes.map((index) => ({
|
|
137
|
+
name: index.name,
|
|
138
|
+
unique: index.unique,
|
|
139
|
+
fields: indexFields(index).map((part) => ({
|
|
140
|
+
field: part.field,
|
|
141
|
+
order: part.order
|
|
142
|
+
}))
|
|
143
|
+
}));
|
|
144
|
+
for (const foreignKey of foreignKeys) {
|
|
145
|
+
if (indexes.some((index) => index.fields.length == 1 && index.fields[0].field == foreignKey.field)) continue;
|
|
146
|
+
const name = `fk_${foreignKey.name}`;
|
|
147
|
+
if (indexes.find((index) => index.name == name)) throw new Error(`Foreign-key index name collision: ${schema.name}/${name}`);
|
|
148
|
+
indexes.push({
|
|
149
|
+
name,
|
|
150
|
+
unique: false,
|
|
151
|
+
fields: [{
|
|
152
|
+
field: foreignKey.field,
|
|
153
|
+
order: "ASC"
|
|
154
|
+
}]
|
|
155
|
+
});
|
|
156
|
+
}
|
|
96
157
|
return {
|
|
97
158
|
name: schema.name,
|
|
98
159
|
...fields == null ? {} : { fields },
|
|
99
160
|
allowAdditionalFields: schema.allowAdditionalFields ?? true,
|
|
100
|
-
indexes
|
|
101
|
-
|
|
102
|
-
unique: index.unique,
|
|
103
|
-
fields: indexFields(index).map((part) => ({
|
|
104
|
-
field: part.field,
|
|
105
|
-
order: part.order
|
|
106
|
-
}))
|
|
107
|
-
}))
|
|
161
|
+
indexes,
|
|
162
|
+
...foreignKeys.length == 0 ? {} : { foreignKeys }
|
|
108
163
|
};
|
|
109
164
|
}
|
|
110
165
|
function validateCollection(schema) {
|
|
@@ -142,6 +197,24 @@ function validateCollection(schema) {
|
|
|
142
197
|
if (fields && !Object.hasOwn(fields, part.field)) throw new Error(`Unknown indexed field: ${part.field}`);
|
|
143
198
|
}
|
|
144
199
|
}
|
|
200
|
+
if (schema.foreignKeys != null && !Array.isArray(schema.foreignKeys)) throw new InvalidForeignKeyDefinitionError("Foreign keys must be an array");
|
|
201
|
+
const foreignKeyNames = /* @__PURE__ */ new Set();
|
|
202
|
+
const foreignKeyFields = /* @__PURE__ */ new Set();
|
|
203
|
+
for (const foreignKey of schema.foreignKeys ?? []) {
|
|
204
|
+
if (!foreignKey || !isValidSchemaName(foreignKey.name) || foreignKey.name.length > 61 || !isValidSchemaName(foreignKey.field) || !foreignKey.references || !isValidSchemaName(foreignKey.references.collection) || foreignKey.references.field != "$id" || ![
|
|
205
|
+
"RESTRICT",
|
|
206
|
+
"CASCADE",
|
|
207
|
+
"SET_NULL"
|
|
208
|
+
].includes(foreignKey.onDelete)) throw new InvalidForeignKeyDefinitionError(`Invalid foreign key definition: ${foreignKey?.name ?? "unknown"}`);
|
|
209
|
+
if (foreignKeyNames.has(foreignKey.name)) throw new InvalidForeignKeyDefinitionError(`Duplicate foreign key: ${foreignKey.name}`);
|
|
210
|
+
if (foreignKeyFields.has(foreignKey.field)) throw new InvalidForeignKeyDefinitionError(`Duplicate foreign key field: ${foreignKey.field}`);
|
|
211
|
+
foreignKeyNames.add(foreignKey.name);
|
|
212
|
+
foreignKeyFields.add(foreignKey.field);
|
|
213
|
+
const definition = fields?.[foreignKey.field];
|
|
214
|
+
if (!definition) throw new InvalidForeignKeyDefinitionError(`Unknown foreign key field: ${foreignKey.field}`);
|
|
215
|
+
if (definition.type != "string") throw new InvalidForeignKeyDefinitionError(`Foreign key ${foreignKey.name} must use a string field`);
|
|
216
|
+
if (foreignKey.onDelete == "SET_NULL" && !definition.nullable) throw new InvalidForeignKeyDefinitionError(`SET_NULL foreign key ${foreignKey.name} must be nullable`);
|
|
217
|
+
}
|
|
145
218
|
}
|
|
146
219
|
function normalizeFieldValue(name, definition, value) {
|
|
147
220
|
if (value === null) {
|
|
@@ -1408,17 +1481,146 @@ var SecondaryIndexManager = class {
|
|
|
1408
1481
|
return ids;
|
|
1409
1482
|
}
|
|
1410
1483
|
};
|
|
1484
|
+
// src/relations/foreign-key-manager.ts
|
|
1485
|
+
var MAX_CASCADE_OPERATIONS = 1e5;
|
|
1486
|
+
var ForeignKeyManager = class {
|
|
1487
|
+
#primary;
|
|
1488
|
+
#secondary;
|
|
1489
|
+
constructor(storage, keys = new StorageKeyManager()) {
|
|
1490
|
+
this.#primary = new PrimaryIndex(storage, keys);
|
|
1491
|
+
this.#secondary = new SecondaryIndexManager(storage, keys);
|
|
1492
|
+
}
|
|
1493
|
+
async prepare(snapshot, sourceWrites, sourceCatalog) {
|
|
1494
|
+
const catalog = this.validateCatalog(sourceCatalog);
|
|
1495
|
+
const writes = new Map(sourceWrites);
|
|
1496
|
+
const incoming = this.incoming(catalog);
|
|
1497
|
+
const queue = [];
|
|
1498
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1499
|
+
for (const [key, operation] of writes) {
|
|
1500
|
+
const parsed = parseRecordKey(key);
|
|
1501
|
+
if (parsed && operation.type == "delete") queue.push(parsed);
|
|
1502
|
+
}
|
|
1503
|
+
for (let offset = 0; offset < queue.length; offset++) {
|
|
1504
|
+
if (visited.size > MAX_CASCADE_OPERATIONS) throw new Error("Foreign-key cascade operation limit exceeded");
|
|
1505
|
+
const parent = queue[offset];
|
|
1506
|
+
const parentKey = recordKey(parent.collection, parent.id);
|
|
1507
|
+
if (visited.has(parentKey)) continue;
|
|
1508
|
+
visited.add(parentKey);
|
|
1509
|
+
for (const relation of incoming.get(parent.collection) ?? []) {
|
|
1510
|
+
const childIds = await this.childIds(snapshot, writes, relation, parent.id);
|
|
1511
|
+
for (const childId of childIds) {
|
|
1512
|
+
const childKey = recordKey(relation.collection, childId);
|
|
1513
|
+
const document = await this.finalDocument(snapshot, writes, relation.collection, childId);
|
|
1514
|
+
if (!document || document[relation.foreignKey.field] !== parent.id) continue;
|
|
1515
|
+
switch (relation.foreignKey.onDelete) {
|
|
1516
|
+
case "RESTRICT": throw new ReferencedRecordRestrictedError(relation.collection, relation.foreignKey.name, `${parent.collection}/${parent.id}`);
|
|
1517
|
+
case "CASCADE":
|
|
1518
|
+
writes.set(childKey, { type: "delete" });
|
|
1519
|
+
queue.push({
|
|
1520
|
+
collection: relation.collection,
|
|
1521
|
+
id: childId
|
|
1522
|
+
});
|
|
1523
|
+
break;
|
|
1524
|
+
case "SET_NULL": {
|
|
1525
|
+
const next = {
|
|
1526
|
+
...document,
|
|
1527
|
+
[relation.foreignKey.field]: null
|
|
1528
|
+
};
|
|
1529
|
+
writes.set(childKey, {
|
|
1530
|
+
type: "put",
|
|
1531
|
+
data: encodeDocument(validateDocument(relation.schema, next))
|
|
1532
|
+
});
|
|
1533
|
+
break;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
for (const [key, operation] of writes) {
|
|
1540
|
+
const parsed = parseRecordKey(key);
|
|
1541
|
+
if (!parsed || operation.type != "put") continue;
|
|
1542
|
+
const schema = catalog.get(parsed.collection);
|
|
1543
|
+
if (!schema) throw new InvalidForeignKeyDefinitionError(`Unknown collection: ${parsed.collection}`);
|
|
1544
|
+
const document = decodeDocument(operation.data);
|
|
1545
|
+
for (const foreignKey of schema.foreignKeys ?? []) {
|
|
1546
|
+
const definition = fieldDefinition(schema, foreignKey.field);
|
|
1547
|
+
const value = document[foreignKey.field];
|
|
1548
|
+
if (value == null) {
|
|
1549
|
+
if (value === null && definition.nullable) continue;
|
|
1550
|
+
if (value === void 0 && !definition.required) continue;
|
|
1551
|
+
throw new ReferencedRecordNotFoundError(parsed.collection, foreignKey.name, String(value));
|
|
1552
|
+
}
|
|
1553
|
+
if (typeof value != "string" || !await this.finalRecordExists(snapshot, writes, foreignKey.references.collection, value)) throw new ReferencedRecordNotFoundError(parsed.collection, foreignKey.name, `${foreignKey.references.collection}/${String(value)}`);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
return writes;
|
|
1557
|
+
}
|
|
1558
|
+
validateCatalog(source) {
|
|
1559
|
+
const catalog = /* @__PURE__ */ new Map();
|
|
1560
|
+
try {
|
|
1561
|
+
for (const [name, definition] of source) catalog.set(name, normalizeCollection(definition));
|
|
1562
|
+
} catch (cause) {
|
|
1563
|
+
throw new InvalidForeignKeyDefinitionError(cause instanceof Error ? cause.message : "Invalid foreign key definition");
|
|
1564
|
+
}
|
|
1565
|
+
for (const [collection, schema] of catalog) for (const foreignKey of schema.foreignKeys ?? []) if (!catalog.has(foreignKey.references.collection)) throw new InvalidForeignKeyDefinitionError(`Foreign key ${collection}/${foreignKey.name} references unknown collection ${foreignKey.references.collection}`);
|
|
1566
|
+
return catalog;
|
|
1567
|
+
}
|
|
1568
|
+
incoming(catalog) {
|
|
1569
|
+
const result = /* @__PURE__ */ new Map();
|
|
1570
|
+
for (const [collection, schema] of catalog) for (const foreignKey of schema.foreignKeys ?? []) {
|
|
1571
|
+
const index = schema.indexes.find((candidate) => {
|
|
1572
|
+
const parts = indexFields(candidate);
|
|
1573
|
+
return parts.length == 1 && parts[0].field == foreignKey.field;
|
|
1574
|
+
});
|
|
1575
|
+
if (!index) throw new InvalidForeignKeyDefinitionError(`Foreign key ${collection}/${foreignKey.name} has no supporting index`);
|
|
1576
|
+
const values = result.get(foreignKey.references.collection) ?? [];
|
|
1577
|
+
values.push({
|
|
1578
|
+
collection,
|
|
1579
|
+
schema,
|
|
1580
|
+
foreignKey,
|
|
1581
|
+
index: index.name
|
|
1582
|
+
});
|
|
1583
|
+
result.set(foreignKey.references.collection, values);
|
|
1584
|
+
}
|
|
1585
|
+
for (const values of result.values()) values.sort((left, right) => `${left.collection}/${left.foreignKey.name}`.localeCompare(`${right.collection}/${right.foreignKey.name}`));
|
|
1586
|
+
return result;
|
|
1587
|
+
}
|
|
1588
|
+
async childIds(snapshot, writes, relation, parentId) {
|
|
1589
|
+
const result = /* @__PURE__ */ new Set();
|
|
1590
|
+
if (snapshot.manifest.catalog.get(relation.collection)?.indexes.some((index) => index.name == relation.index)) for await (const entry of this.#secondary.scanIds(snapshot.manifest, relation.collection, relation.index, { eq: parentId })) result.add(entry.id);
|
|
1591
|
+
for (const [key, operation] of writes) {
|
|
1592
|
+
const parsed = parseRecordKey(key);
|
|
1593
|
+
if (!parsed || parsed.collection != relation.collection) continue;
|
|
1594
|
+
if (operation.type == "delete") result.delete(parsed.id);
|
|
1595
|
+
else if (decodeDocument(operation.data)[relation.foreignKey.field] === parentId) result.add(parsed.id);
|
|
1596
|
+
else result.delete(parsed.id);
|
|
1597
|
+
}
|
|
1598
|
+
return result;
|
|
1599
|
+
}
|
|
1600
|
+
async finalRecordExists(snapshot, writes, collection, id) {
|
|
1601
|
+
return await this.finalDocument(snapshot, writes, collection, id) != null;
|
|
1602
|
+
}
|
|
1603
|
+
async finalDocument(snapshot, writes, collection, id) {
|
|
1604
|
+
const key = recordKey(collection, id);
|
|
1605
|
+
const pending = writes.get(key);
|
|
1606
|
+
if (pending) return pending.type == "delete" ? null : decodeDocument(pending.data);
|
|
1607
|
+
const stored = await this.#primary.read(snapshot.manifest, key);
|
|
1608
|
+
return stored == null ? null : decodeDocument(stored);
|
|
1609
|
+
}
|
|
1610
|
+
};
|
|
1411
1611
|
// src/commit/commit-coordinator.ts
|
|
1412
1612
|
var CommitCoordinator = class {
|
|
1413
1613
|
storage;
|
|
1414
1614
|
keys;
|
|
1415
1615
|
primary;
|
|
1416
1616
|
secondary;
|
|
1617
|
+
foreignKeys;
|
|
1417
1618
|
constructor(storage, keys = new StorageKeyManager()) {
|
|
1418
1619
|
this.storage = storage;
|
|
1419
1620
|
this.keys = keys;
|
|
1420
1621
|
this.primary = new PrimaryIndex(storage, keys);
|
|
1421
1622
|
this.secondary = new SecondaryIndexManager(storage, keys);
|
|
1623
|
+
this.foreignKeys = new ForeignKeyManager(storage, keys);
|
|
1422
1624
|
}
|
|
1423
1625
|
async commit(transaction) {
|
|
1424
1626
|
const prepared = transaction.prepareForCommit();
|
|
@@ -1429,8 +1631,9 @@ var CommitCoordinator = class {
|
|
|
1429
1631
|
const transactionId = (0, node_crypto.randomUUID)();
|
|
1430
1632
|
const catalog = new Map(prepared.snapshot.manifest.catalog);
|
|
1431
1633
|
for (const [name, schema] of prepared.schemaChanges) catalog.set(name, schema);
|
|
1634
|
+
const writes = await this.foreignKeys.prepare(prepared.snapshot, prepared.writes, catalog);
|
|
1432
1635
|
const changes = /* @__PURE__ */ new Map();
|
|
1433
|
-
for (const [key, operation] of
|
|
1636
|
+
for (const [key, operation] of writes) {
|
|
1434
1637
|
if (operation.type == "delete") {
|
|
1435
1638
|
changes.set(key, null);
|
|
1436
1639
|
continue;
|
|
@@ -1440,7 +1643,7 @@ var CommitCoordinator = class {
|
|
|
1440
1643
|
changes.set(key, ref);
|
|
1441
1644
|
}
|
|
1442
1645
|
const primaryRoot = await this.primary.prepare(prepared.snapshot.manifest.primaryRoot, changes);
|
|
1443
|
-
const indexRoots = await this.secondary.prepare(prepared.snapshot.manifest, primaryRoot,
|
|
1646
|
+
const indexRoots = await this.secondary.prepare(prepared.snapshot.manifest, primaryRoot, writes, catalog);
|
|
1444
1647
|
const commitBytes = encodeCommitRecord({
|
|
1445
1648
|
format: 1,
|
|
1446
1649
|
transactionId,
|
|
@@ -2873,6 +3076,16 @@ var Transaction = class {
|
|
|
2873
3076
|
if (this.collection(definition.name)) throw new Error(`Collection already exists: ${definition.name}`);
|
|
2874
3077
|
this.schemas.set(definition.name, cloneSchema(definition));
|
|
2875
3078
|
}
|
|
3079
|
+
defineCollections(definitions) {
|
|
3080
|
+
this.assertActive();
|
|
3081
|
+
const names = /* @__PURE__ */ new Set();
|
|
3082
|
+
for (const definition of definitions) {
|
|
3083
|
+
validateCollection(definition);
|
|
3084
|
+
if (names.has(definition.name) || this.collection(definition.name)) throw new Error(`Collection already exists: ${definition.name}`);
|
|
3085
|
+
names.add(definition.name);
|
|
3086
|
+
}
|
|
3087
|
+
for (const definition of definitions) this.schemas.set(definition.name, cloneSchema(definition));
|
|
3088
|
+
}
|
|
2876
3089
|
addIndex(collection, index) {
|
|
2877
3090
|
this.assertActive();
|
|
2878
3091
|
const current = this.requireCollection(collection);
|
|
@@ -3393,10 +3606,15 @@ function immutableCollection(definition) {
|
|
|
3393
3606
|
...index,
|
|
3394
3607
|
fields: Object.freeze((index.fields ?? []).map((field) => Object.freeze({ ...field })))
|
|
3395
3608
|
})));
|
|
3609
|
+
const foreignKeys = Object.freeze((normalized.foreignKeys ?? []).map((foreignKey) => Object.freeze({
|
|
3610
|
+
...foreignKey,
|
|
3611
|
+
references: Object.freeze({ ...foreignKey.references })
|
|
3612
|
+
})));
|
|
3396
3613
|
return Object.freeze({
|
|
3397
3614
|
...normalized,
|
|
3398
3615
|
...fields == null ? {} : { fields },
|
|
3399
|
-
indexes
|
|
3616
|
+
indexes,
|
|
3617
|
+
...foreignKeys.length == 0 ? {} : { foreignKeys }
|
|
3400
3618
|
});
|
|
3401
3619
|
}
|
|
3402
3620
|
var DuraxisDatabase = class {
|
|
@@ -3471,6 +3689,9 @@ var DuraxisDatabase = class {
|
|
|
3471
3689
|
tx.defineCollection(schema);
|
|
3472
3690
|
});
|
|
3473
3691
|
}
|
|
3692
|
+
async defineCollections(schemas) {
|
|
3693
|
+
await this.transaction((tx) => tx.defineCollections(schemas));
|
|
3694
|
+
}
|
|
3474
3695
|
async getCollectionDefinitions() {
|
|
3475
3696
|
const snapshot = await this.acquireSnapshot();
|
|
3476
3697
|
try {
|
|
@@ -3774,8 +3995,11 @@ exports.CommitOutcomeUnknownError = CommitOutcomeUnknownError;
|
|
|
3774
3995
|
exports.Difference = Difference;
|
|
3775
3996
|
exports.DuraxisDatabase = DuraxisDatabase;
|
|
3776
3997
|
exports.Filter = Filter;
|
|
3998
|
+
exports.ForeignKeyManager = ForeignKeyManager;
|
|
3999
|
+
exports.ForeignKeyViolationError = ForeignKeyViolationError;
|
|
3777
4000
|
exports.GarbageCollector = GarbageCollector;
|
|
3778
4001
|
exports.Intersection = Intersection;
|
|
4002
|
+
exports.InvalidForeignKeyDefinitionError = InvalidForeignKeyDefinitionError;
|
|
3779
4003
|
exports.Limit = Limit;
|
|
3780
4004
|
exports.ManifestReachabilityScanner = ManifestReachabilityScanner;
|
|
3781
4005
|
exports.ManifestSnapshotReader = ManifestSnapshotReader;
|
|
@@ -3789,6 +4013,8 @@ exports.QueryExecutor = QueryExecutor;
|
|
|
3789
4013
|
exports.QueryMemoryLimitError = QueryMemoryLimitError;
|
|
3790
4014
|
exports.RecoveryManager = RecoveryManager;
|
|
3791
4015
|
exports.ReferenceQueryExecutor = ReferenceQueryExecutor;
|
|
4016
|
+
exports.ReferencedRecordNotFoundError = ReferencedRecordNotFoundError;
|
|
4017
|
+
exports.ReferencedRecordRestrictedError = ReferencedRecordRestrictedError;
|
|
3792
4018
|
exports.RuleBasedPlanner = RuleBasedPlanner;
|
|
3793
4019
|
exports.SecondaryIndexManager = SecondaryIndexManager;
|
|
3794
4020
|
exports.SecondaryIndexRangeScan = SecondaryIndexRangeScan;
|