@push.rocks/smartdb 2.0.0 → 2.3.1
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/.smartconfig.json +11 -0
- package/dist_rust/rustdb_linux_amd64 +0 -0
- package/dist_rust/rustdb_linux_arm64 +0 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +2 -0
- package/dist_ts/index.js +3 -1
- package/dist_ts/ts_local/classes.localsmartdb.js +7 -4
- package/dist_ts/ts_migration/classes.storagemigrator.d.ts +24 -0
- package/dist_ts/ts_migration/classes.storagemigrator.js +75 -0
- package/dist_ts/ts_migration/index.d.ts +1 -0
- package/dist_ts/ts_migration/index.js +2 -0
- package/dist_ts/ts_migration/migrators/v0_to_v1.d.ts +9 -0
- package/dist_ts/ts_migration/migrators/v0_to_v1.js +225 -0
- package/dist_ts/ts_smartdb/index.d.ts +1 -0
- package/dist_ts/ts_smartdb/rust-db-bridge.d.ts +80 -1
- package/dist_ts/ts_smartdb/rust-db-bridge.js +17 -2
- package/dist_ts/ts_smartdb/server/SmartdbServer.d.ts +31 -0
- package/dist_ts/ts_smartdb/server/SmartdbServer.js +47 -5
- package/dist_ts_debugserver/bundled.d.ts +4 -0
- package/dist_ts_debugserver/bundled.js +12 -0
- package/dist_ts_debugserver/classes.debugserver.d.ts +36 -0
- package/dist_ts_debugserver/classes.debugserver.js +95 -0
- package/dist_ts_debugserver/index.d.ts +2 -0
- package/dist_ts_debugserver/index.js +2 -0
- package/dist_ts_debugserver/plugins.d.ts +2 -0
- package/dist_ts_debugserver/plugins.js +3 -0
- package/dist_ts_debugui/index.d.ts +2 -0
- package/dist_ts_debugui/index.js +2 -0
- package/dist_ts_debugui/plugins.d.ts +1 -0
- package/dist_ts_debugui/plugins.js +2 -0
- package/dist_ts_debugui/smartdb-debugui.d.ts +62 -0
- package/dist_ts_debugui/smartdb-debugui.js +1132 -0
- package/package.json +9 -4
- package/readme.md +161 -42
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +14 -0
- package/ts/ts_local/classes.localsmartdb.ts +5 -0
- package/ts/ts_migration/classes.storagemigrator.ts +93 -0
- package/ts/ts_migration/index.ts +1 -0
- package/ts/ts_migration/migrators/v0_to_v1.ts +253 -0
- package/ts/ts_smartdb/index.ts +11 -0
- package/ts/ts_smartdb/rust-db-bridge.ts +127 -3
- package/ts/ts_smartdb/server/SmartdbServer.ts +71 -0
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@push.rocks/smartdb",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A MongoDB-compatible embedded database server with wire protocol support, backed by a high-performance Rust engine.",
|
|
6
6
|
"exports": {
|
|
7
|
-
".": "./dist_ts/index.js"
|
|
7
|
+
".": "./dist_ts/index.js",
|
|
8
|
+
"./debugui": "./dist_ts_debugui/index.js",
|
|
9
|
+
"./debugserver": "./dist_ts_debugserver/index.js"
|
|
8
10
|
},
|
|
9
11
|
"type": "module",
|
|
10
12
|
"author": "Task Venture Capital GmbH",
|
|
@@ -12,7 +14,7 @@
|
|
|
12
14
|
"scripts": {
|
|
13
15
|
"test:before": "(tsrust)",
|
|
14
16
|
"test": "(tstest test/. --verbose --logfile --timeout 60)",
|
|
15
|
-
"build": "(tsbuild tsfolders) && (tsrust)",
|
|
17
|
+
"build": "(tsbundle) && (tsbuild tsfolders) && (tsrust)",
|
|
16
18
|
"buildDocs": "tsdoc"
|
|
17
19
|
},
|
|
18
20
|
"devDependencies": {
|
|
@@ -25,7 +27,10 @@
|
|
|
25
27
|
"mongodb": "^7.1.1"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
|
-
"@
|
|
30
|
+
"@api.global/typedserver": "^8.0.0",
|
|
31
|
+
"@design.estate/dees-element": "^2.0.0",
|
|
32
|
+
"@push.rocks/smartrust": "^1.3.2",
|
|
33
|
+
"bson": "^7.2.0"
|
|
29
34
|
},
|
|
30
35
|
"browserslist": [
|
|
31
36
|
"last 1 chrome versions"
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @push.rocks/smartdb
|
|
2
2
|
|
|
3
|
-
A MongoDB-compatible embedded database server powered by Rust 🦀⚡ — use the official `mongodb` driver and it just works. No binary downloads, instant startup, zero config.
|
|
3
|
+
A MongoDB-compatible embedded database server powered by Rust 🦀⚡ — use the official `mongodb` driver and it just works. No binary downloads, instant startup, zero config. Features a built-in **operation log** with **point-in-time revert** and a web-based **debug dashboard**.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -14,6 +14,8 @@ npm install @push.rocks/smartdb
|
|
|
14
14
|
|
|
15
15
|
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
|
16
16
|
|
|
17
|
+
---
|
|
18
|
+
|
|
17
19
|
## What It Does
|
|
18
20
|
|
|
19
21
|
`@push.rocks/smartdb` is a **real database server** that speaks the wire protocol used by MongoDB drivers. The core engine is written in Rust for high performance, with a thin TypeScript orchestration layer. Connect with the standard `mongodb` Node.js driver — no mocks, no stubs, no external binaries required.
|
|
@@ -26,52 +28,55 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
|
|
|
26
28
|
| **Binary download** | Bundled (~7MB) | ~200MB+ |
|
|
27
29
|
| **Install** | `pnpm add` | System package / Docker |
|
|
28
30
|
| **Persistence** | Memory or file-based | Full disk engine |
|
|
31
|
+
| **Debug UI** | Built-in 🖥️ | External tooling |
|
|
32
|
+
| **Point-in-time revert** | Built-in ⏪ | Requires oplog tailing |
|
|
29
33
|
| **Perfect for** | Unit tests, CI/CD, prototyping, local dev, embedded | Production at scale |
|
|
30
34
|
|
|
31
|
-
###
|
|
35
|
+
### Three Ways to Use It
|
|
32
36
|
|
|
33
|
-
- 🏗️ **`SmartdbServer`** — Full control. Configure port, host, storage backend, Unix sockets. Great for test fixtures or custom setups.
|
|
34
37
|
- 🎯 **`LocalSmartDb`** — Zero-config convenience. Give it a folder path, get a persistent database over a Unix socket. Done.
|
|
38
|
+
- 🏗️ **`SmartdbServer`** — Full control. Configure port, host, storage backend, Unix sockets. Great for test fixtures or custom setups.
|
|
39
|
+
- 🖥️ **`SmartdbDebugServer`** — Launch a web dashboard to visually browse collections, inspect the operation log, and revert to any point in time.
|
|
35
40
|
|
|
36
41
|
### Architecture: TypeScript + Rust 🦀
|
|
37
42
|
|
|
38
|
-
SmartDB uses
|
|
43
|
+
SmartDB uses a **sidecar binary** pattern — TypeScript handles lifecycle, Rust handles all database operations:
|
|
39
44
|
|
|
40
45
|
```
|
|
41
|
-
|
|
42
|
-
│ Your Application
|
|
43
|
-
│ (TypeScript / Node.js)
|
|
44
|
-
│ ┌─────────────────┐ ┌───────────────────────────┐
|
|
45
|
-
│ │ SmartdbServer │────▶│ RustDbBridge (IPC) │
|
|
46
|
-
│ │ or LocalSmartDb │ │ @push.rocks/smartrust │
|
|
47
|
-
│ └─────────────────┘ └───────────┬───────────────┘
|
|
48
|
-
|
|
46
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
47
|
+
│ Your Application │
|
|
48
|
+
│ (TypeScript / Node.js) │
|
|
49
|
+
│ ┌─────────────────┐ ┌───────────────────────────┐ │
|
|
50
|
+
│ │ SmartdbServer │────▶│ RustDbBridge (IPC) │ │
|
|
51
|
+
│ │ or LocalSmartDb │ │ @push.rocks/smartrust │ │
|
|
52
|
+
│ └─────────────────┘ └───────────┬───────────────┘ │
|
|
53
|
+
└──────────────────────────────────────┼───────────────────────┘
|
|
49
54
|
│ spawn + JSON IPC
|
|
50
55
|
▼
|
|
51
|
-
|
|
52
|
-
│ rustdb binary 🦀
|
|
53
|
-
│
|
|
54
|
-
│ ┌──────────────┐ ┌──────────────┐ ┌───────────────┐
|
|
55
|
-
│ │ Wire Protocol│→ │Command Router│→ │ Handlers │
|
|
56
|
-
│ │ (OP_MSG) │ │ (40 cmds)
|
|
57
|
-
│ └──────────────┘ └──────────────┘ └───────┬───────┘
|
|
58
|
-
│ │
|
|
59
|
-
│ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌──────┴──────┐
|
|
60
|
-
│ │ Query │ │ Update │ │Aggregation│ │ Index │
|
|
61
|
-
│ │ Matcher │ │ Engine │ │ Engine │ │ Engine │
|
|
62
|
-
│ └─────────┘ └────────┘ └───────────┘ └─────────────┘
|
|
63
|
-
│
|
|
64
|
-
│ ┌──────────────────┐ ┌──────────────────┐
|
|
65
|
-
│ │ MemoryStorage │ │ FileStorage │
|
|
66
|
-
│ └──────────────────┘ └──────────────────┘
|
|
67
|
-
|
|
56
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
57
|
+
│ rustdb binary 🦀 │
|
|
58
|
+
│ │
|
|
59
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌───────────────┐ │
|
|
60
|
+
│ │ Wire Protocol│→ │Command Router│→ │ Handlers │ │
|
|
61
|
+
│ │ (OP_MSG) │ │ (40+ cmds) │ │ Find,Insert.. │ │
|
|
62
|
+
│ └──────────────┘ └──────────────┘ └───────┬───────┘ │
|
|
63
|
+
│ │ │
|
|
64
|
+
│ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌──────┴──────┐ │
|
|
65
|
+
│ │ Query │ │ Update │ │Aggregation│ │ Index │ │
|
|
66
|
+
│ │ Matcher │ │ Engine │ │ Engine │ │ Engine │ │
|
|
67
|
+
│ └─────────┘ └────────┘ └───────────┘ └─────────────┘ │
|
|
68
|
+
│ │
|
|
69
|
+
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────┐ │
|
|
70
|
+
│ │ MemoryStorage │ │ FileStorage │ │ OpLog │ │
|
|
71
|
+
│ └──────────────────┘ └──────────────────┘ └──────────┘ │
|
|
72
|
+
└──────────────────────────────────────────────────────────────┘
|
|
68
73
|
▲
|
|
69
74
|
│ TCP / Unix Socket (wire protocol)
|
|
70
75
|
│
|
|
71
|
-
|
|
72
|
-
│ MongoClient (mongodb npm driver)
|
|
73
|
-
│ Connects directly to Rust binary
|
|
74
|
-
|
|
76
|
+
┌─────────────┴────────────────────────────────────────────────┐
|
|
77
|
+
│ MongoClient (mongodb npm driver) │
|
|
78
|
+
│ Connects directly to Rust binary │
|
|
79
|
+
└──────────────────────────────────────────────────────────────┘
|
|
75
80
|
```
|
|
76
81
|
|
|
77
82
|
The TypeScript layer handles **lifecycle only** (start/stop/configure via IPC). All database operations flow directly from the `MongoClient` to the Rust binary over TCP or Unix sockets — **zero per-query IPC overhead**.
|
|
@@ -128,6 +133,87 @@ await client.close();
|
|
|
128
133
|
await server.stop();
|
|
129
134
|
```
|
|
130
135
|
|
|
136
|
+
### Option 3: Debug Server (Visual Dashboard) 🖥️
|
|
137
|
+
|
|
138
|
+
Launch a web-based dashboard to inspect your database in real time:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { SmartdbServer } from '@push.rocks/smartdb';
|
|
142
|
+
import { SmartdbDebugServer } from '@push.rocks/smartdb/debugserver';
|
|
143
|
+
|
|
144
|
+
const server = new SmartdbServer({ storage: 'memory' });
|
|
145
|
+
await server.start();
|
|
146
|
+
|
|
147
|
+
const debugServer = new SmartdbDebugServer(server, { port: 4000 });
|
|
148
|
+
await debugServer.start();
|
|
149
|
+
// Open http://localhost:4000 in your browser 🚀
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The debug dashboard gives you:
|
|
153
|
+
- 📊 **Dashboard** — server status, uptime, database/collection counts, operation breakdown
|
|
154
|
+
- 📁 **Collection Browser** — browse databases, collections, and documents interactively
|
|
155
|
+
- 📝 **OpLog Timeline** — every insert, update, and delete with expandable field-level diffs
|
|
156
|
+
- ⏪ **Point-in-Time Revert** — select any oplog sequence, preview what will be undone, and execute
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## 📝 Operation Log & Point-in-Time Revert
|
|
161
|
+
|
|
162
|
+
Every write operation (insert, update, delete) is automatically recorded in an in-memory **operation log (OpLog)** with full before/after document snapshots. This enables:
|
|
163
|
+
|
|
164
|
+
- **Change tracking** — see exactly what changed, when, and in which collection
|
|
165
|
+
- **Field-level diffs** — compare previous and new document states
|
|
166
|
+
- **Point-in-time revert** — undo operations back to any sequence number
|
|
167
|
+
- **Dry-run preview** — see what would be reverted before executing
|
|
168
|
+
|
|
169
|
+
### Programmatic OpLog API
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { SmartdbServer } from '@push.rocks/smartdb';
|
|
173
|
+
|
|
174
|
+
const server = new SmartdbServer({ port: 27017 });
|
|
175
|
+
await server.start();
|
|
176
|
+
|
|
177
|
+
// ... perform some CRUD operations via MongoClient ...
|
|
178
|
+
|
|
179
|
+
// Get oplog entries
|
|
180
|
+
const oplog = await server.getOpLog({ limit: 50 });
|
|
181
|
+
console.log(oplog.entries);
|
|
182
|
+
// [{ seq: 1, op: 'insert', db: 'myapp', collection: 'users', document: {...}, previousDocument: null }, ...]
|
|
183
|
+
|
|
184
|
+
// Get aggregate stats
|
|
185
|
+
const stats = await server.getOpLogStats();
|
|
186
|
+
console.log(stats);
|
|
187
|
+
// { currentSeq: 42, totalEntries: 42, entriesByOp: { insert: 20, update: 15, delete: 7 } }
|
|
188
|
+
|
|
189
|
+
// Preview a revert (dry run)
|
|
190
|
+
const preview = await server.revertToSeq(30, true);
|
|
191
|
+
console.log(`Would undo ${preview.reverted} operations`);
|
|
192
|
+
|
|
193
|
+
// Execute the revert — undoes all operations after seq 30
|
|
194
|
+
const result = await server.revertToSeq(30, false);
|
|
195
|
+
console.log(`Reverted ${result.reverted} operations`);
|
|
196
|
+
|
|
197
|
+
// Browse collections programmatically
|
|
198
|
+
const collections = await server.getCollections();
|
|
199
|
+
const docs = await server.getDocuments('myapp', 'users', 50, 0);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### OpLog Entry Structure
|
|
203
|
+
|
|
204
|
+
Each entry contains:
|
|
205
|
+
|
|
206
|
+
| Field | Type | Description |
|
|
207
|
+
|---|---|---|
|
|
208
|
+
| `seq` | `number` | Monotonically increasing sequence number |
|
|
209
|
+
| `timestampMs` | `number` | Unix timestamp in milliseconds |
|
|
210
|
+
| `op` | `'insert' \| 'update' \| 'delete'` | Operation type |
|
|
211
|
+
| `db` | `string` | Database name |
|
|
212
|
+
| `collection` | `string` | Collection name |
|
|
213
|
+
| `documentId` | `string` | Document `_id` as hex string |
|
|
214
|
+
| `document` | `object \| null` | New document state (null for deletes) |
|
|
215
|
+
| `previousDocument` | `object \| null` | Previous document state (null for inserts) |
|
|
216
|
+
|
|
131
217
|
---
|
|
132
218
|
|
|
133
219
|
## API Reference
|
|
@@ -175,6 +261,12 @@ const server = new SmartdbServer({
|
|
|
175
261
|
| `port` | `number` | Configured port (TCP mode) |
|
|
176
262
|
| `host` | `string` | Configured host (TCP mode) |
|
|
177
263
|
| `socketPath` | `string \| undefined` | Socket path (socket mode) |
|
|
264
|
+
| `getMetrics()` | `Promise<ISmartDbMetrics>` | Server metrics (db/collection counts, uptime) |
|
|
265
|
+
| `getOpLog(params?)` | `Promise<IOpLogResult>` | Query oplog entries with optional filters |
|
|
266
|
+
| `getOpLogStats()` | `Promise<IOpLogStats>` | Aggregate oplog statistics |
|
|
267
|
+
| `revertToSeq(seq, dryRun?)` | `Promise<IRevertResult>` | Revert to a specific oplog sequence |
|
|
268
|
+
| `getCollections(db?)` | `Promise<ICollectionInfo[]>` | List all collections with counts |
|
|
269
|
+
| `getDocuments(db, coll, limit?, skip?)` | `Promise<IDocumentsResult>` | Browse documents with pagination |
|
|
178
270
|
|
|
179
271
|
### LocalSmartDb
|
|
180
272
|
|
|
@@ -202,13 +294,34 @@ const db = new LocalSmartDb({
|
|
|
202
294
|
| `getServer()` | `SmartdbServer` | Access the underlying server |
|
|
203
295
|
| `running` | `boolean` | Whether the server is running |
|
|
204
296
|
|
|
205
|
-
|
|
297
|
+
### SmartdbDebugServer
|
|
298
|
+
|
|
299
|
+
Web-based debug dashboard served via `@api.global/typedserver`. Import from the `debugserver` subpath:
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { SmartdbDebugServer } from '@push.rocks/smartdb/debugserver';
|
|
303
|
+
|
|
304
|
+
const debugServer = new SmartdbDebugServer(server, { port: 4000 });
|
|
305
|
+
await debugServer.start();
|
|
306
|
+
// Dashboard at http://localhost:4000
|
|
307
|
+
|
|
308
|
+
await debugServer.stop();
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
The UI is bundled as base64-encoded content (via `@git.zone/tsbundle`) and served from memory — no static file directory needed.
|
|
312
|
+
|
|
313
|
+
### SmartdbDebugUi (Web Component)
|
|
314
|
+
|
|
315
|
+
For embedding the debug UI directly into your own web application, import the `<smartdb-debugui>` web component:
|
|
206
316
|
|
|
207
317
|
```typescript
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
318
|
+
import { SmartdbDebugUi } from '@push.rocks/smartdb/debugui';
|
|
319
|
+
|
|
320
|
+
// In your HTML/lit template:
|
|
321
|
+
// <smartdb-debugui .server=${mySmartdbServer}></smartdb-debugui>
|
|
322
|
+
//
|
|
323
|
+
// Or in HTTP mode (when served by SmartdbDebugServer):
|
|
324
|
+
// <smartdb-debugui apiBaseUrl=""></smartdb-debugui>
|
|
212
325
|
```
|
|
213
326
|
|
|
214
327
|
---
|
|
@@ -377,10 +490,10 @@ The Rust engine is organized as a Cargo workspace with 8 focused crates:
|
|
|
377
490
|
| `rustdb-config` | Server configuration types (serde, camelCase JSON) |
|
|
378
491
|
| `rustdb-wire` | Wire protocol parser/encoder (OP_MSG, OP_QUERY, OP_REPLY) |
|
|
379
492
|
| `rustdb-query` | Query matcher, update engine, aggregation, sort, projection |
|
|
380
|
-
| `rustdb-storage` | Storage backends (memory, file)
|
|
493
|
+
| `rustdb-storage` | Storage backends (memory, file), OpLog with point-in-time replay |
|
|
381
494
|
| `rustdb-index` | B-tree/hash indexes, query planner (IXSCAN/COLLSCAN) |
|
|
382
495
|
| `rustdb-txn` | Transaction + session management with snapshot isolation |
|
|
383
|
-
| `rustdb-commands` | 40 command handlers wiring everything together |
|
|
496
|
+
| `rustdb-commands` | 40+ command handlers wiring everything together |
|
|
384
497
|
|
|
385
498
|
Cross-compiled for `linux_amd64` and `linux_arm64` via [@git.zone/tsrust](https://www.npmjs.com/package/@git.zone/tsrust).
|
|
386
499
|
|
|
@@ -410,6 +523,12 @@ tap.test('should insert and find', async () => {
|
|
|
410
523
|
expect(item?.price).toEqual(9.99);
|
|
411
524
|
});
|
|
412
525
|
|
|
526
|
+
tap.test('should track changes in oplog', async () => {
|
|
527
|
+
const oplog = await server.getOpLog();
|
|
528
|
+
expect(oplog.entries.length).toBeGreaterThan(0);
|
|
529
|
+
expect(oplog.entries[0].op).toEqual('insert');
|
|
530
|
+
});
|
|
531
|
+
|
|
413
532
|
tap.test('teardown', async () => {
|
|
414
533
|
await client.close();
|
|
415
534
|
await server.stop();
|
|
@@ -422,7 +541,7 @@ export default tap.start();
|
|
|
422
541
|
|
|
423
542
|
## License and Legal Information
|
|
424
543
|
|
|
425
|
-
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [
|
|
544
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
|
426
545
|
|
|
427
546
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
428
547
|
|
|
@@ -434,7 +553,7 @@ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark G
|
|
|
434
553
|
|
|
435
554
|
### Company Information
|
|
436
555
|
|
|
437
|
-
Task Venture Capital GmbH
|
|
556
|
+
Task Venture Capital GmbH
|
|
438
557
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
439
558
|
|
|
440
559
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
package/ts/00_commitinfo_data.ts
CHANGED
package/ts/index.ts
CHANGED
|
@@ -7,5 +7,19 @@ export * from './ts_smartdb/index.js';
|
|
|
7
7
|
export { LocalSmartDb } from './ts_local/index.js';
|
|
8
8
|
export type { ILocalSmartDbOptions, ILocalSmartDbConnectionInfo } from './ts_local/index.js';
|
|
9
9
|
|
|
10
|
+
// Export migration
|
|
11
|
+
export { StorageMigrator } from './ts_migration/index.js';
|
|
12
|
+
|
|
10
13
|
// Export commitinfo
|
|
11
14
|
export { commitinfo };
|
|
15
|
+
|
|
16
|
+
// Re-export oplog / debug types for convenience
|
|
17
|
+
export type {
|
|
18
|
+
IOpLogEntry,
|
|
19
|
+
IOpLogResult,
|
|
20
|
+
IOpLogStats,
|
|
21
|
+
IRevertResult,
|
|
22
|
+
ICollectionInfo,
|
|
23
|
+
IDocumentsResult,
|
|
24
|
+
ISmartDbMetrics,
|
|
25
|
+
} from './ts_smartdb/index.js';
|
|
@@ -2,6 +2,7 @@ import * as crypto from 'crypto';
|
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import * as os from 'os';
|
|
4
4
|
import { SmartdbServer } from '../ts_smartdb/index.js';
|
|
5
|
+
import { StorageMigrator } from '../ts_migration/index.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Connection information returned by LocalSmartDb.start()
|
|
@@ -73,6 +74,10 @@ export class LocalSmartDb {
|
|
|
73
74
|
throw new Error('LocalSmartDb is already running');
|
|
74
75
|
}
|
|
75
76
|
|
|
77
|
+
// Run storage migration before starting the Rust engine
|
|
78
|
+
const migrator = new StorageMigrator(this.options.folderPath);
|
|
79
|
+
await migrator.run();
|
|
80
|
+
|
|
76
81
|
// Use provided socket path or generate one
|
|
77
82
|
this.generatedSocketPath = this.options.socketPath ?? this.generateSocketPath();
|
|
78
83
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { migrateV0ToV1 } from './migrators/v0_to_v1.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Detected storage format version.
|
|
7
|
+
* - v0: Legacy JSON format ({db}/{coll}.json files)
|
|
8
|
+
* - v1: Bitcask binary format ({db}/{coll}/data.rdb directories)
|
|
9
|
+
*/
|
|
10
|
+
type TStorageVersion = 0 | 1;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* StorageMigrator — runs before the Rust engine starts.
|
|
14
|
+
*
|
|
15
|
+
* Detects the current storage format version and runs the appropriate
|
|
16
|
+
* migration chain. The Rust engine only knows the current format (v1).
|
|
17
|
+
*
|
|
18
|
+
* Migration is safe: original files are never modified or deleted.
|
|
19
|
+
* On success, a console hint is printed about which old files can be removed.
|
|
20
|
+
*/
|
|
21
|
+
export class StorageMigrator {
|
|
22
|
+
private storagePath: string;
|
|
23
|
+
|
|
24
|
+
constructor(storagePath: string) {
|
|
25
|
+
this.storagePath = storagePath;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Run any needed migrations. Safe to call even if storage is already current.
|
|
30
|
+
*/
|
|
31
|
+
async run(): Promise<void> {
|
|
32
|
+
if (!fs.existsSync(this.storagePath)) {
|
|
33
|
+
return; // No data yet — nothing to migrate
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const version = this.detectVersion();
|
|
37
|
+
|
|
38
|
+
if (version === 1) {
|
|
39
|
+
return; // Already current
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (version === 0) {
|
|
43
|
+
console.log(`[smartdb] Detected v0 (JSON) storage format at ${this.storagePath}`);
|
|
44
|
+
console.log(`[smartdb] Running migration v0 → v1 (Bitcask binary format)...`);
|
|
45
|
+
|
|
46
|
+
const deletableFiles = await migrateV0ToV1(this.storagePath);
|
|
47
|
+
|
|
48
|
+
if (deletableFiles.length > 0) {
|
|
49
|
+
console.log(`[smartdb] Migration v0 → v1 complete.`);
|
|
50
|
+
console.log(`[smartdb] The following old files can be safely deleted:`);
|
|
51
|
+
for (const f of deletableFiles) {
|
|
52
|
+
console.log(`[smartdb] ${f}`);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
console.log(`[smartdb] Migration v0 → v1 complete. No old files to clean up.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Detect the storage format version by inspecting the directory structure.
|
|
62
|
+
*
|
|
63
|
+
* v0: {db}/{coll}.json files exist
|
|
64
|
+
* v1: {db}/{coll}/data.rdb directories exist
|
|
65
|
+
*/
|
|
66
|
+
private detectVersion(): TStorageVersion {
|
|
67
|
+
const entries = fs.readdirSync(this.storagePath, { withFileTypes: true });
|
|
68
|
+
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (!entry.isDirectory()) continue;
|
|
71
|
+
|
|
72
|
+
const dbDir = path.join(this.storagePath, entry.name);
|
|
73
|
+
const dbEntries = fs.readdirSync(dbDir, { withFileTypes: true });
|
|
74
|
+
|
|
75
|
+
for (const dbEntry of dbEntries) {
|
|
76
|
+
// v1: subdirectory with data.rdb
|
|
77
|
+
if (dbEntry.isDirectory()) {
|
|
78
|
+
const dataRdb = path.join(dbDir, dbEntry.name, 'data.rdb');
|
|
79
|
+
if (fs.existsSync(dataRdb)) {
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// v0: .json file (not .indexes.json)
|
|
84
|
+
if (dbEntry.isFile() && dbEntry.name.endsWith('.json') && !dbEntry.name.endsWith('.indexes.json')) {
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Empty or unrecognized — treat as v1 (fresh start)
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { StorageMigrator } from './classes.storagemigrator.js';
|