@quereus/store 0.3.6 → 0.3.7
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/README.md +2 -2
- package/dist/src/common/ddl-generator.d.ts +15 -0
- package/dist/src/common/ddl-generator.d.ts.map +1 -0
- package/dist/src/common/ddl-generator.js +131 -0
- package/dist/src/common/ddl-generator.js.map +1 -0
- package/dist/src/common/encoding.d.ts +65 -0
- package/dist/src/common/encoding.d.ts.map +1 -0
- package/dist/src/common/encoding.js +339 -0
- package/dist/src/common/encoding.js.map +1 -0
- package/dist/src/common/events.d.ts +119 -0
- package/dist/src/common/events.d.ts.map +1 -0
- package/dist/src/common/events.js +158 -0
- package/dist/src/common/events.js.map +1 -0
- package/dist/src/common/index.d.ts +15 -0
- package/dist/src/common/index.d.ts.map +1 -0
- package/dist/src/common/index.js +23 -0
- package/dist/src/common/index.js.map +1 -0
- package/dist/src/common/key-builder.d.ts +58 -0
- package/dist/src/common/key-builder.d.ts.map +1 -0
- package/dist/src/common/key-builder.js +130 -0
- package/dist/src/common/key-builder.js.map +1 -0
- package/dist/src/common/kv-store.d.ts +150 -0
- package/dist/src/common/kv-store.d.ts.map +1 -0
- package/dist/src/common/kv-store.js +6 -0
- package/dist/src/common/kv-store.js.map +1 -0
- package/dist/src/common/memory-store.d.ts +37 -0
- package/dist/src/common/memory-store.d.ts.map +1 -0
- package/dist/src/common/memory-store.js +146 -0
- package/dist/src/common/memory-store.js.map +1 -0
- package/dist/src/common/serialization.d.ts +41 -0
- package/dist/src/common/serialization.d.ts.map +1 -0
- package/dist/src/common/serialization.js +111 -0
- package/dist/src/common/serialization.js.map +1 -0
- package/dist/src/common/store-connection.d.ts +34 -0
- package/dist/src/common/store-connection.d.ts.map +1 -0
- package/dist/src/common/store-connection.js +55 -0
- package/dist/src/common/store-connection.js.map +1 -0
- package/dist/src/common/store-module.d.ts +111 -0
- package/dist/src/common/store-module.d.ts.map +1 -0
- package/dist/src/common/store-module.js +331 -0
- package/dist/src/common/store-module.js.map +1 -0
- package/dist/src/common/store-table.d.ts +115 -0
- package/dist/src/common/store-table.d.ts.map +1 -0
- package/dist/src/common/store-table.js +472 -0
- package/dist/src/common/store-table.js.map +1 -0
- package/dist/src/common/transaction.d.ts +57 -0
- package/dist/src/common/transaction.d.ts.map +1 -0
- package/dist/src/common/transaction.js +156 -0
- package/dist/src/common/transaction.js.map +1 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +18 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction coordinator for virtual table modules.
|
|
3
|
+
*
|
|
4
|
+
* Manages a shared WriteBatch across all tables in a transaction,
|
|
5
|
+
* providing multi-table atomicity.
|
|
6
|
+
*/
|
|
7
|
+
import { QuereusError, StatusCode } from '@quereus/quereus';
|
|
8
|
+
/**
|
|
9
|
+
* Coordinates transactions across multiple tables.
|
|
10
|
+
*
|
|
11
|
+
* All mutations within a transaction are buffered in a shared WriteBatch.
|
|
12
|
+
* On commit, the batch is written atomically and events are fired.
|
|
13
|
+
* On rollback, the batch and events are discarded.
|
|
14
|
+
*/
|
|
15
|
+
export class TransactionCoordinator {
|
|
16
|
+
store;
|
|
17
|
+
eventEmitter;
|
|
18
|
+
// Transaction state
|
|
19
|
+
inTransaction = false;
|
|
20
|
+
pendingOps = [];
|
|
21
|
+
pendingEvents = [];
|
|
22
|
+
savepoints = new Map();
|
|
23
|
+
callbacks = [];
|
|
24
|
+
constructor(store, eventEmitter) {
|
|
25
|
+
this.store = store;
|
|
26
|
+
this.eventEmitter = eventEmitter;
|
|
27
|
+
}
|
|
28
|
+
/** Register callbacks for transaction lifecycle events. */
|
|
29
|
+
registerCallbacks(callbacks) {
|
|
30
|
+
this.callbacks.push(callbacks);
|
|
31
|
+
}
|
|
32
|
+
/** Check if a transaction is active. */
|
|
33
|
+
isInTransaction() {
|
|
34
|
+
return this.inTransaction;
|
|
35
|
+
}
|
|
36
|
+
/** Begin a transaction. */
|
|
37
|
+
begin() {
|
|
38
|
+
if (this.inTransaction) {
|
|
39
|
+
// Already in transaction - no-op (matches SQLite semantics)
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.inTransaction = true;
|
|
43
|
+
this.pendingOps = [];
|
|
44
|
+
this.pendingEvents = [];
|
|
45
|
+
this.savepoints.clear();
|
|
46
|
+
}
|
|
47
|
+
/** Queue a put operation. */
|
|
48
|
+
put(key, value) {
|
|
49
|
+
if (!this.inTransaction) {
|
|
50
|
+
throw new QuereusError('Cannot queue operation outside transaction', StatusCode.MISUSE);
|
|
51
|
+
}
|
|
52
|
+
this.pendingOps.push({ type: 'put', key, value });
|
|
53
|
+
}
|
|
54
|
+
/** Queue a delete operation. */
|
|
55
|
+
delete(key) {
|
|
56
|
+
if (!this.inTransaction) {
|
|
57
|
+
throw new QuereusError('Cannot queue operation outside transaction', StatusCode.MISUSE);
|
|
58
|
+
}
|
|
59
|
+
this.pendingOps.push({ type: 'delete', key });
|
|
60
|
+
}
|
|
61
|
+
/** Queue a data change event (fired on commit). */
|
|
62
|
+
queueEvent(event) {
|
|
63
|
+
if (!this.inTransaction) {
|
|
64
|
+
// If not in transaction, emit immediately
|
|
65
|
+
this.eventEmitter?.emitDataChange(event);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.pendingEvents.push(event);
|
|
69
|
+
}
|
|
70
|
+
/** Commit the transaction. */
|
|
71
|
+
async commit() {
|
|
72
|
+
if (!this.inTransaction) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
// Write all pending operations atomically
|
|
77
|
+
if (this.pendingOps.length > 0) {
|
|
78
|
+
const batch = this.store.batch();
|
|
79
|
+
for (const op of this.pendingOps) {
|
|
80
|
+
if (op.type === 'put') {
|
|
81
|
+
batch.put(op.key, op.value);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
batch.delete(op.key);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
await batch.write();
|
|
88
|
+
}
|
|
89
|
+
// Fire all pending events
|
|
90
|
+
for (const event of this.pendingEvents) {
|
|
91
|
+
this.eventEmitter?.emitDataChange(event);
|
|
92
|
+
}
|
|
93
|
+
// Notify callbacks
|
|
94
|
+
for (const cb of this.callbacks) {
|
|
95
|
+
cb.onCommit();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
this.clearTransaction();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Rollback the transaction. */
|
|
103
|
+
rollback() {
|
|
104
|
+
if (!this.inTransaction) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Notify callbacks
|
|
108
|
+
for (const cb of this.callbacks) {
|
|
109
|
+
cb.onRollback();
|
|
110
|
+
}
|
|
111
|
+
this.clearTransaction();
|
|
112
|
+
}
|
|
113
|
+
/** Create a savepoint. */
|
|
114
|
+
createSavepoint(index) {
|
|
115
|
+
if (!this.inTransaction) {
|
|
116
|
+
// Start implicit transaction
|
|
117
|
+
this.begin();
|
|
118
|
+
}
|
|
119
|
+
this.savepoints.set(index, {
|
|
120
|
+
opIndex: this.pendingOps.length,
|
|
121
|
+
eventIndex: this.pendingEvents.length,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/** Release a savepoint (no-op, just removes from map). */
|
|
125
|
+
releaseSavepoint(index) {
|
|
126
|
+
this.savepoints.delete(index);
|
|
127
|
+
}
|
|
128
|
+
/** Rollback to a savepoint. */
|
|
129
|
+
rollbackToSavepoint(index) {
|
|
130
|
+
const savepoint = this.savepoints.get(index);
|
|
131
|
+
if (!savepoint) {
|
|
132
|
+
throw new QuereusError(`Savepoint ${index} not found`, StatusCode.NOTFOUND);
|
|
133
|
+
}
|
|
134
|
+
// Truncate operations and events back to savepoint
|
|
135
|
+
this.pendingOps = this.pendingOps.slice(0, savepoint.opIndex);
|
|
136
|
+
this.pendingEvents = this.pendingEvents.slice(0, savepoint.eventIndex);
|
|
137
|
+
// Remove this savepoint and any created after it
|
|
138
|
+
for (const [idx] of this.savepoints) {
|
|
139
|
+
if (idx >= index) {
|
|
140
|
+
this.savepoints.delete(idx);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** Clear all transaction state. */
|
|
145
|
+
clearTransaction() {
|
|
146
|
+
this.inTransaction = false;
|
|
147
|
+
this.pendingOps = [];
|
|
148
|
+
this.pendingEvents = [];
|
|
149
|
+
this.savepoints.clear();
|
|
150
|
+
}
|
|
151
|
+
/** Get the underlying store for direct reads. */
|
|
152
|
+
getStore() {
|
|
153
|
+
return this.store;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../../src/common/transaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAuB5D;;;;;;GAMG;AACH,MAAM,OAAO,sBAAsB;IACzB,KAAK,CAAU;IACf,YAAY,CAAqB;IAEzC,oBAAoB;IACZ,aAAa,GAAG,KAAK,CAAC;IACtB,UAAU,GAAgB,EAAE,CAAC;IAC7B,aAAa,GAAsB,EAAE,CAAC;IACtC,UAAU,GAA2B,IAAI,GAAG,EAAE,CAAC;IAC/C,SAAS,GAA2B,EAAE,CAAC;IAE/C,YAAY,KAAc,EAAE,YAAgC;QAC1D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED,2DAA2D;IAC3D,iBAAiB,CAAC,SAA+B;QAC/C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IAED,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AK,CAAC;QAC3B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IAED,iDAAiD;IACjD,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract Store Module for Quereus
|
|
3
|
+
*
|
|
4
|
+
* Provides the generic StoreModule and KVStore abstractions for persistent storage.
|
|
5
|
+
* Concrete implementations (LevelDB, IndexedDB) are in separate plugin packages:
|
|
6
|
+
* - @quereus/plugin-leveldb (Node.js)
|
|
7
|
+
* - @quereus/plugin-indexeddb (Browser)
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { StoreModule, type KVStoreProvider } from '@quereus/store';
|
|
11
|
+
*
|
|
12
|
+
* const provider: KVStoreProvider = createYourProvider();
|
|
13
|
+
* const module = new StoreModule(provider);
|
|
14
|
+
* db.registerVtabModule('store', module);
|
|
15
|
+
*/
|
|
16
|
+
export * from './common/index.js';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract Store Module for Quereus
|
|
3
|
+
*
|
|
4
|
+
* Provides the generic StoreModule and KVStore abstractions for persistent storage.
|
|
5
|
+
* Concrete implementations (LevelDB, IndexedDB) are in separate plugin packages:
|
|
6
|
+
* - @quereus/plugin-leveldb (Node.js)
|
|
7
|
+
* - @quereus/plugin-indexeddb (Browser)
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { StoreModule, type KVStoreProvider } from '@quereus/store';
|
|
11
|
+
*
|
|
12
|
+
* const provider: KVStoreProvider = createYourProvider();
|
|
13
|
+
* const module = new StoreModule(provider);
|
|
14
|
+
* db.registerVtabModule('store', module);
|
|
15
|
+
*/
|
|
16
|
+
// Export all common utilities and abstractions
|
|
17
|
+
export * from './common/index.js';
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,+CAA+C;AAC/C,cAAc,mBAAmB,CAAC"}
|