@push.rocks/smartdb 1.0.1 → 2.0.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.
Files changed (110) hide show
  1. package/.smartconfig.json +7 -4
  2. package/dist_rust/rustdb_linux_amd64 +0 -0
  3. package/dist_rust/rustdb_linux_arm64 +0 -0
  4. package/dist_ts/00_commitinfo_data.js +3 -3
  5. package/dist_ts/ts_local/classes.localsmartdb.d.ts +5 -5
  6. package/dist_ts/ts_local/classes.localsmartdb.js +5 -6
  7. package/dist_ts/ts_local/plugins.d.ts +1 -2
  8. package/dist_ts/ts_local/plugins.js +3 -3
  9. package/dist_ts/ts_smartdb/index.d.ts +1 -24
  10. package/dist_ts/ts_smartdb/index.js +4 -29
  11. package/dist_ts/ts_smartdb/plugins.d.ts +2 -10
  12. package/dist_ts/ts_smartdb/plugins.js +3 -13
  13. package/dist_ts/ts_smartdb/rust-db-bridge.d.ts +43 -0
  14. package/dist_ts/ts_smartdb/rust-db-bridge.js +98 -0
  15. package/dist_ts/ts_smartdb/server/SmartdbServer.d.ts +8 -37
  16. package/dist_ts/ts_smartdb/server/SmartdbServer.js +49 -204
  17. package/dist_ts/ts_smartdb/server/index.d.ts +0 -4
  18. package/dist_ts/ts_smartdb/server/index.js +1 -5
  19. package/license +3 -1
  20. package/package.json +9 -12
  21. package/readme.md +84 -171
  22. package/ts/00_commitinfo_data.ts +2 -2
  23. package/ts/ts_local/classes.localsmartdb.ts +5 -6
  24. package/ts/ts_local/plugins.ts +1 -3
  25. package/ts/ts_smartdb/index.ts +3 -41
  26. package/ts/ts_smartdb/plugins.ts +2 -15
  27. package/ts/ts_smartdb/rust-db-bridge.ts +138 -0
  28. package/ts/ts_smartdb/server/SmartdbServer.ts +53 -248
  29. package/ts/ts_smartdb/server/index.ts +0 -7
  30. package/dist_ts/ts_smartdb/engine/AggregationEngine.d.ts +0 -66
  31. package/dist_ts/ts_smartdb/engine/AggregationEngine.js +0 -189
  32. package/dist_ts/ts_smartdb/engine/IndexEngine.d.ts +0 -97
  33. package/dist_ts/ts_smartdb/engine/IndexEngine.js +0 -678
  34. package/dist_ts/ts_smartdb/engine/QueryEngine.d.ts +0 -54
  35. package/dist_ts/ts_smartdb/engine/QueryEngine.js +0 -271
  36. package/dist_ts/ts_smartdb/engine/QueryPlanner.d.ts +0 -64
  37. package/dist_ts/ts_smartdb/engine/QueryPlanner.js +0 -308
  38. package/dist_ts/ts_smartdb/engine/SessionEngine.d.ts +0 -117
  39. package/dist_ts/ts_smartdb/engine/SessionEngine.js +0 -232
  40. package/dist_ts/ts_smartdb/engine/TransactionEngine.d.ts +0 -85
  41. package/dist_ts/ts_smartdb/engine/TransactionEngine.js +0 -287
  42. package/dist_ts/ts_smartdb/engine/UpdateEngine.d.ts +0 -47
  43. package/dist_ts/ts_smartdb/engine/UpdateEngine.js +0 -461
  44. package/dist_ts/ts_smartdb/errors/SmartdbErrors.d.ts +0 -100
  45. package/dist_ts/ts_smartdb/errors/SmartdbErrors.js +0 -155
  46. package/dist_ts/ts_smartdb/server/CommandRouter.d.ts +0 -87
  47. package/dist_ts/ts_smartdb/server/CommandRouter.js +0 -222
  48. package/dist_ts/ts_smartdb/server/WireProtocol.d.ts +0 -117
  49. package/dist_ts/ts_smartdb/server/WireProtocol.js +0 -298
  50. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.d.ts +0 -100
  51. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.js +0 -668
  52. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.d.ts +0 -31
  53. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.js +0 -277
  54. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.d.ts +0 -8
  55. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.js +0 -95
  56. package/dist_ts/ts_smartdb/server/handlers/FindHandler.d.ts +0 -31
  57. package/dist_ts/ts_smartdb/server/handlers/FindHandler.js +0 -291
  58. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.d.ts +0 -11
  59. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.js +0 -62
  60. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.d.ts +0 -20
  61. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.js +0 -183
  62. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.d.ts +0 -8
  63. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.js +0 -79
  64. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.d.ts +0 -24
  65. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.js +0 -296
  66. package/dist_ts/ts_smartdb/server/handlers/index.d.ts +0 -8
  67. package/dist_ts/ts_smartdb/server/handlers/index.js +0 -10
  68. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.d.ts +0 -85
  69. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.js +0 -465
  70. package/dist_ts/ts_smartdb/storage/IStorageAdapter.d.ts +0 -145
  71. package/dist_ts/ts_smartdb/storage/IStorageAdapter.js +0 -2
  72. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.d.ts +0 -67
  73. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.js +0 -378
  74. package/dist_ts/ts_smartdb/storage/OpLog.d.ts +0 -93
  75. package/dist_ts/ts_smartdb/storage/OpLog.js +0 -221
  76. package/dist_ts/ts_smartdb/storage/WAL.d.ts +0 -117
  77. package/dist_ts/ts_smartdb/storage/WAL.js +0 -286
  78. package/dist_ts/ts_smartdb/types/interfaces.d.ts +0 -363
  79. package/dist_ts/ts_smartdb/types/interfaces.js +0 -2
  80. package/dist_ts/ts_smartdb/utils/checksum.d.ts +0 -30
  81. package/dist_ts/ts_smartdb/utils/checksum.js +0 -77
  82. package/dist_ts/ts_smartdb/utils/index.d.ts +0 -1
  83. package/dist_ts/ts_smartdb/utils/index.js +0 -2
  84. package/ts/ts_smartdb/engine/AggregationEngine.ts +0 -283
  85. package/ts/ts_smartdb/engine/IndexEngine.ts +0 -798
  86. package/ts/ts_smartdb/engine/QueryEngine.ts +0 -301
  87. package/ts/ts_smartdb/engine/QueryPlanner.ts +0 -393
  88. package/ts/ts_smartdb/engine/SessionEngine.ts +0 -292
  89. package/ts/ts_smartdb/engine/TransactionEngine.ts +0 -351
  90. package/ts/ts_smartdb/engine/UpdateEngine.ts +0 -506
  91. package/ts/ts_smartdb/errors/SmartdbErrors.ts +0 -181
  92. package/ts/ts_smartdb/server/CommandRouter.ts +0 -289
  93. package/ts/ts_smartdb/server/WireProtocol.ts +0 -416
  94. package/ts/ts_smartdb/server/handlers/AdminHandler.ts +0 -719
  95. package/ts/ts_smartdb/server/handlers/AggregateHandler.ts +0 -342
  96. package/ts/ts_smartdb/server/handlers/DeleteHandler.ts +0 -115
  97. package/ts/ts_smartdb/server/handlers/FindHandler.ts +0 -330
  98. package/ts/ts_smartdb/server/handlers/HelloHandler.ts +0 -78
  99. package/ts/ts_smartdb/server/handlers/IndexHandler.ts +0 -207
  100. package/ts/ts_smartdb/server/handlers/InsertHandler.ts +0 -97
  101. package/ts/ts_smartdb/server/handlers/UpdateHandler.ts +0 -344
  102. package/ts/ts_smartdb/server/handlers/index.ts +0 -10
  103. package/ts/ts_smartdb/storage/FileStorageAdapter.ts +0 -562
  104. package/ts/ts_smartdb/storage/IStorageAdapter.ts +0 -208
  105. package/ts/ts_smartdb/storage/MemoryStorageAdapter.ts +0 -455
  106. package/ts/ts_smartdb/storage/OpLog.ts +0 -282
  107. package/ts/ts_smartdb/storage/WAL.ts +0 -375
  108. package/ts/ts_smartdb/types/interfaces.ts +0 -433
  109. package/ts/ts_smartdb/utils/checksum.ts +0 -88
  110. package/ts/ts_smartdb/utils/index.ts +0 -1
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @push.rocks/smartdb
2
2
 
3
- A pure TypeScript MongoDB wire-protocol-compatible database server. Zero binary dependencies, instant startup, pluggable storage — use the official MongoDB driver and it just works.
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.
4
4
 
5
5
  ## Install
6
6
 
@@ -16,22 +16,67 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
16
16
 
17
17
  ## What It Does
18
18
 
19
- `@push.rocks/smartdb` is a **real database server** written entirely in TypeScript that speaks the MongoDB binary wire protocol. Connect with the official `mongodb` Node.js driver — no mocks, no stubs, no MongoDB binary required.
19
+ `@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.
20
20
 
21
21
  ### Why SmartDB?
22
22
 
23
- | | SmartDB | Real MongoDB |
23
+ | | SmartDB | External DB Server |
24
24
  |---|---|---|
25
- | **Startup time** | ~5ms | ~2-5s |
26
- | **Binary download** | None | ~200MB |
27
- | **Node.js only** | | |
25
+ | **Startup time** | ~30ms | ~2-5s |
26
+ | **Binary download** | Bundled (~7MB) | ~200MB+ |
27
+ | **Install** | `pnpm add` | System package / Docker |
28
28
  | **Persistence** | Memory or file-based | Full disk engine |
29
- | **Perfect for** | Unit tests, CI/CD, prototyping, local dev | Production |
29
+ | **Perfect for** | Unit tests, CI/CD, prototyping, local dev, embedded | Production at scale |
30
30
 
31
31
  ### Two Ways to Use It
32
32
 
33
33
  - 🏗️ **`SmartdbServer`** — Full control. Configure port, host, storage backend, Unix sockets. Great for test fixtures or custom setups.
34
- - 🎯 **`LocalSmartDb`** — Zero-config convenience. Give it a folder path, get a persistent MongoDB-compatible database over a Unix socket. Done.
34
+ - 🎯 **`LocalSmartDb`** — Zero-config convenience. Give it a folder path, get a persistent database over a Unix socket. Done.
35
+
36
+ ### Architecture: TypeScript + Rust 🦀
37
+
38
+ SmartDB uses the same **sidecar binary** pattern as [@push.rocks/smartproxy](https://code.foss.global/push.rocks/smartproxy):
39
+
40
+ ```
41
+ ┌──────────────────────────────────────────────────────────┐
42
+ │ Your Application │
43
+ │ (TypeScript / Node.js) │
44
+ │ ┌─────────────────┐ ┌───────────────────────────┐ │
45
+ │ │ SmartdbServer │────▶│ RustDbBridge (IPC) │ │
46
+ │ │ or LocalSmartDb │ │ @push.rocks/smartrust │ │
47
+ │ └─────────────────┘ └───────────┬───────────────┘ │
48
+ └──────────────────────────────────────┼───────────────────┘
49
+ │ spawn + JSON IPC
50
+
51
+ ┌──────────────────────────────────────────────────────────┐
52
+ │ rustdb binary 🦀 │
53
+ │ │
54
+ │ ┌──────────────┐ ┌──────────────┐ ┌───────────────┐ │
55
+ │ │ Wire Protocol│→ │Command Router│→ │ Handlers │ │
56
+ │ │ (OP_MSG) │ │ (40 cmds) │ │ Find,Insert.. │ │
57
+ │ └──────────────┘ └──────────────┘ └───────┬───────┘ │
58
+ │ │ │
59
+ │ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌──────┴──────┐ │
60
+ │ │ Query │ │ Update │ │Aggregation│ │ Index │ │
61
+ │ │ Matcher │ │ Engine │ │ Engine │ │ Engine │ │
62
+ │ └─────────┘ └────────┘ └───────────┘ └─────────────┘ │
63
+ │ │
64
+ │ ┌──────────────────┐ ┌──────────────────┐ │
65
+ │ │ MemoryStorage │ │ FileStorage │ │
66
+ │ └──────────────────┘ └──────────────────┘ │
67
+ └──────────────────────────────────────────────────────────┘
68
+
69
+ │ TCP / Unix Socket (wire protocol)
70
+
71
+ ┌─────────────┴────────────────────────────────────────────┐
72
+ │ MongoClient (mongodb npm driver) │
73
+ │ Connects directly to Rust binary │
74
+ └──────────────────────────────────────────────────────────┘
75
+ ```
76
+
77
+ 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**.
78
+
79
+ ---
35
80
 
36
81
  ## Quick Start
37
82
 
@@ -47,11 +92,11 @@ import { MongoClient } from 'mongodb';
47
92
  const db = new LocalSmartDb({ folderPath: './my-data' });
48
93
  const { connectionUri } = await db.start();
49
94
 
50
- // Connect with the standard MongoDB driver
95
+ // Connect with the standard driver
51
96
  const client = new MongoClient(connectionUri, { directConnection: true });
52
97
  await client.connect();
53
98
 
54
- // Use exactly like MongoDB
99
+ // Use it like any wire-protocol-compatible database
55
100
  const users = client.db('myapp').collection('users');
56
101
  await users.insertOne({ name: 'Alice', email: 'alice@example.com' });
57
102
  const user = await users.findOne({ name: 'Alice' });
@@ -89,7 +134,7 @@ await server.stop();
89
134
 
90
135
  ### SmartdbServer
91
136
 
92
- The core server class. Speaks MongoDB wire protocol over TCP or Unix sockets.
137
+ The core server class. Manages the Rust database engine and exposes connection details.
93
138
 
94
139
  #### Constructor Options (`ISmartdbServerOptions`)
95
140
 
@@ -123,15 +168,13 @@ const server = new SmartdbServer({
123
168
 
124
169
  | Method / Property | Type | Description |
125
170
  |---|---|---|
126
- | `start()` | `Promise<void>` | Start the server |
171
+ | `start()` | `Promise<void>` | Start the server (spawns Rust binary) |
127
172
  | `stop()` | `Promise<void>` | Stop the server and clean up |
128
- | `getConnectionUri()` | `string` | Get the MongoDB connection URI |
173
+ | `getConnectionUri()` | `string` | Get the `mongodb://` connection URI |
129
174
  | `running` | `boolean` | Whether the server is currently running |
130
- | `port` | `number` | Bound port (TCP mode) |
131
- | `host` | `string` | Bound host (TCP mode) |
132
- | `socketPath` | `string` | Socket path (socket mode) |
133
- | `getUptime()` | `number` | Seconds since start |
134
- | `getConnectionCount()` | `number` | Active client connections |
175
+ | `port` | `number` | Configured port (TCP mode) |
176
+ | `host` | `string` | Configured host (TCP mode) |
177
+ | `socketPath` | `string \| undefined` | Socket path (socket mode) |
135
178
 
136
179
  ### LocalSmartDb
137
180
 
@@ -155,7 +198,7 @@ const db = new LocalSmartDb({
155
198
  | `start()` | `Promise<ILocalSmartDbConnectionInfo>` | Start and return connection info |
156
199
  | `stop()` | `Promise<void>` | Stop the server |
157
200
  | `getConnectionInfo()` | `ILocalSmartDbConnectionInfo` | Get current connection info |
158
- | `getConnectionUri()` | `string` | Get the MongoDB URI |
201
+ | `getConnectionUri()` | `string` | Get the connection URI |
159
202
  | `getServer()` | `SmartdbServer` | Access the underlying server |
160
203
  | `running` | `boolean` | Whether the server is running |
161
204
 
@@ -170,9 +213,9 @@ interface ILocalSmartDbConnectionInfo {
170
213
 
171
214
  ---
172
215
 
173
- ## Supported MongoDB Operations
216
+ ## Supported Operations
174
217
 
175
- SmartDB supports the core MongoDB operations through the wire protocol. Use the standard `mongodb` driver — these all work:
218
+ SmartDB supports the core operations through the wire protocol. Use the standard `mongodb` driver — these all work:
176
219
 
177
220
  ### CRUD
178
221
 
@@ -258,7 +301,7 @@ const results = await collection.aggregate([
258
301
  ]).toArray();
259
302
  ```
260
303
 
261
- **Supported stages:** `$match`, `$project`, `$group`, `$sort`, `$limit`, `$skip`, `$unwind`, `$lookup`, `$addFields`, `$count`, `$facet`, `$replaceRoot`, `$set`, `$unset`
304
+ **Supported stages:** `$match`, `$project`, `$group`, `$sort`, `$limit`, `$skip`, `$unwind`, `$lookup`, `$addFields`, `$count`, `$facet`, `$replaceRoot`, `$set`, `$unionWith`, `$out`, `$merge`
262
305
 
263
306
  **Group accumulators:** `$sum`, `$avg`, `$min`, `$max`, `$first`, `$last`, `$push`, `$addToSet`, `$count`
264
307
 
@@ -316,148 +359,34 @@ const names = await collection.distinct('name');
316
359
  | **CRUD** | `find`, `insert`, `update`, `delete`, `findAndModify`, `getMore`, `killCursors` |
317
360
  | **Aggregation** | `aggregate`, `count`, `distinct` |
318
361
  | **Indexes** | `createIndexes`, `dropIndexes`, `listIndexes` |
319
- | **Transactions** | `startTransaction`, `commitTransaction`, `abortTransaction` |
320
- | **Sessions** | `startSession`, `endSessions`, `refreshSessions` |
321
- | **Admin** | `ping`, `listDatabases`, `listCollections`, `drop`, `dropDatabase`, `create`, `serverStatus`, `buildInfo`, `dbStats`, `collStats`, `connectionStatus`, `currentOp`, `collMod`, `renameCollection` |
362
+ | **Sessions** | `startSession`, `endSessions` |
363
+ | **Transactions** | `commitTransaction`, `abortTransaction` |
364
+ | **Admin** | `ping`, `listDatabases`, `listCollections`, `drop`, `dropDatabase`, `create`, `serverStatus`, `buildInfo`, `dbStats`, `collStats`, `connectionStatus`, `currentOp`, `renameCollection` |
322
365
 
323
- Compatible with MongoDB wire protocol versions 0–21 (MongoDB 3.6 through 7.0 drivers).
366
+ Compatible with wire protocol versions 0–21 (driver versions 3.6 through 7.0).
324
367
 
325
368
  ---
326
369
 
327
- ## Architecture
370
+ ## Rust Crate Architecture 🦀
328
371
 
329
- ```
330
- ┌─────────────────────────────────────────────────────────────┐
331
- │ Official MongoDB Driver │
332
- │ (mongodb npm) │
333
- └─────────────────────────┬───────────────────────────────────┘
334
- │ TCP / Unix Socket + OP_MSG / BSON
335
-
336
- ┌─────────────────────────────────────────────────────────────┐
337
- │ SmartdbServer │
338
- │ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
339
- │ │ WireProtocol │→ │CommandRouter │→ │ Handlers │ │
340
- │ │ (OP_MSG) │ │ │ │ (Find, Insert..) │ │
341
- │ └──────────────┘ └──────────────┘ └──────────────────┘ │
342
- └─────────────────────────┬───────────────────────────────────┘
343
-
344
-
345
- ┌─────────────────────────────────────────────────────────────┐
346
- │ Engines │
347
- │ ┌─────────┐ ┌────────┐ ┌───────────┐ ┌───────┐ ┌───────┐ │
348
- │ │ Query │ │ Update │ │Aggregation│ │ Index │ │Session│ │
349
- │ │ Planner │ │ Engine │ │ Engine │ │Engine │ │Engine │ │
350
- │ └─────────┘ └────────┘ └───────────┘ └───────┘ └───────┘ │
351
- │ ┌──────────────────────┐ │
352
- │ │ Transaction Engine │ │
353
- │ └──────────────────────┘ │
354
- └─────────────────────────┬───────────────────────────────────┘
355
-
356
-
357
- ┌─────────────────────────────────────────────────────────────┐
358
- │ Storage Layer │
359
- │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────┐ │
360
- │ │ MemoryStorage │ │ FileStorage │ │ WAL │ │
361
- │ │ │ │ (+ Checksums) │ │ │ │
362
- │ └──────────────────┘ └──────────────────┘ └──────────┘ │
363
- └─────────────────────────────────────────────────────────────┘
364
- ```
372
+ The Rust engine is organized as a Cargo workspace with 8 focused crates:
365
373
 
366
- ### Key Components
367
-
368
- | Component | What It Does |
374
+ | Crate | Purpose |
369
375
  |---|---|
370
- | **WireProtocol** | Parses/encodes MongoDB OP_MSG binary frames |
371
- | **CommandRouter** | Routes parsed commands to the right handler |
372
- | **QueryPlanner** | Picks COLLSCAN vs IXSCAN based on available indexes |
373
- | **QueryEngine** | Filter matching powered by [mingo](https://github.com/kofrasa/mingo) |
374
- | **UpdateEngine** | Processes `$set`, `$inc`, `$push`, and all update operators |
375
- | **AggregationEngine** | Runs aggregation pipelines via mingo |
376
- | **IndexEngine** | B-tree (range) and hash (equality) indexes |
377
- | **TransactionEngine** | ACID transactions with snapshot isolation |
378
- | **SessionEngine** | Client session tracking with automatic timeouts |
379
- | **WAL** | Write-ahead logging with CRC32 checksums for crash recovery |
380
-
381
- ---
382
-
383
- ## Advanced Usage
384
-
385
- ### Storage Adapters
386
-
387
- ```typescript
388
- import { SmartdbServer } from '@push.rocks/smartdb';
389
-
390
- // In-memory (default) — fast, data lost on stop
391
- const server = new SmartdbServer({ storage: 'memory' });
392
-
393
- // In-memory with periodic persistence
394
- const server = new SmartdbServer({
395
- storage: 'memory',
396
- persistPath: './data/snapshot.json',
397
- persistIntervalMs: 30000,
398
- });
399
-
400
- // File-based — persistent storage with CRC32 checksums
401
- const server = new SmartdbServer({
402
- storage: 'file',
403
- storagePath: './data/smartdb',
404
- });
405
- ```
406
-
407
- ### Query Planner (Debugging)
408
-
409
- ```typescript
410
- import { QueryPlanner, IndexEngine, MemoryStorageAdapter } from '@push.rocks/smartdb';
376
+ | `rustdb` | Binary entry point: TCP/Unix listener, management IPC, CLI |
377
+ | `rustdb-config` | Server configuration types (serde, camelCase JSON) |
378
+ | `rustdb-wire` | Wire protocol parser/encoder (OP_MSG, OP_QUERY, OP_REPLY) |
379
+ | `rustdb-query` | Query matcher, update engine, aggregation, sort, projection |
380
+ | `rustdb-storage` | Storage backends (memory, file) + WAL + OpLog |
381
+ | `rustdb-index` | B-tree/hash indexes, query planner (IXSCAN/COLLSCAN) |
382
+ | `rustdb-txn` | Transaction + session management with snapshot isolation |
383
+ | `rustdb-commands` | 40 command handlers wiring everything together |
411
384
 
412
- const storage = new MemoryStorageAdapter();
413
- await storage.initialize();
414
- const indexEngine = new IndexEngine('mydb', 'mycoll', storage);
415
- const planner = new QueryPlanner(indexEngine);
416
-
417
- const plan = await planner.plan({ age: { $gte: 18 } });
418
- console.log(plan);
419
- // { type: 'IXSCAN_RANGE', indexName: 'age_1', selectivity: 0.3, usesRange: true, ... }
420
-
421
- const explain = await planner.explain({ age: 18 });
422
- // Returns winning plan, rejected plans, and detailed analysis
423
- ```
424
-
425
- ### Data Integrity Checksums
426
-
427
- ```typescript
428
- import { calculateDocumentChecksum, addChecksum, verifyChecksum, removeChecksum } from '@push.rocks/smartdb';
429
-
430
- const doc = { name: 'Alice', age: 30 };
431
-
432
- const protected = addChecksum(doc); // Adds _checksum field
433
- const valid = verifyChecksum(protected); // true
434
- protected.age = 31; // Tamper!
435
- const still = verifyChecksum(protected); // false
436
- const clean = removeChecksum(protected); // Removes _checksum
437
- ```
438
-
439
- ### Write-Ahead Logging
440
-
441
- ```typescript
442
- import { WAL } from '@push.rocks/smartdb';
443
-
444
- const wal = new WAL('./data/wal.log', { checkpointInterval: 100 });
445
- await wal.initialize();
446
-
447
- // Entries include: LSN, timestamp, operation, BSON data, CRC32 checksum
448
- const lsn = await wal.logInsert('mydb', 'users', doc);
449
- const entries = wal.getEntriesAfter(lastCheckpoint);
450
- const recovered = wal.recoverDocument(entry);
451
-
452
- await wal.checkpoint();
453
- await wal.close();
454
- ```
385
+ Cross-compiled for `linux_amd64` and `linux_arm64` via [@git.zone/tsrust](https://www.npmjs.com/package/@git.zone/tsrust).
455
386
 
456
387
  ---
457
388
 
458
- ## Testing Examples
459
-
460
- ### Unit Tests with @git.zone/tstest
389
+ ## Testing Example
461
390
 
462
391
  ```typescript
463
392
  import { expect, tap } from '@git.zone/tstest/tapbundle';
@@ -489,27 +418,11 @@ tap.test('teardown', async () => {
489
418
  export default tap.start();
490
419
  ```
491
420
 
492
- ### With LocalSmartDb (Persistent Tests)
493
-
494
- ```typescript
495
- import { LocalSmartDb } from '@push.rocks/smartdb';
496
- import { MongoClient } from 'mongodb';
497
-
498
- const db = new LocalSmartDb({ folderPath: './test-data' });
499
- const { connectionUri } = await db.start();
500
-
501
- const client = new MongoClient(connectionUri, { directConnection: true });
502
- await client.connect();
503
-
504
- // Tests here — data persists between test runs!
505
-
506
- await client.close();
507
- await db.stop();
508
- ```
421
+ ---
509
422
 
510
423
  ## License and Legal Information
511
424
 
512
- This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
425
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](./license) file.
513
426
 
514
427
  **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.
515
428
 
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartdb',
6
- version: '1.0.1',
7
- description: 'A pure TypeScript MongoDB wire-protocol-compatible database server with pluggable storage, indexing, transactions, and zero external binary dependencies.'
6
+ version: '2.0.0',
7
+ description: 'A MongoDB-compatible embedded database server with wire protocol support, backed by a high-performance Rust engine.'
8
8
  }
@@ -1,4 +1,3 @@
1
- import * as plugins from './plugins.js';
2
1
  import * as crypto from 'crypto';
3
2
  import * as path from 'path';
4
3
  import * as os from 'os';
@@ -10,7 +9,7 @@ import { SmartdbServer } from '../ts_smartdb/index.js';
10
9
  export interface ILocalSmartDbConnectionInfo {
11
10
  /** The Unix socket file path */
12
11
  socketPath: string;
13
- /** MongoDB connection URI ready for MongoClient */
12
+ /** Connection URI (mongodb:// scheme) ready for MongoClient */
14
13
  connectionUri: string;
15
14
  }
16
15
 
@@ -25,8 +24,8 @@ export interface ILocalSmartDbOptions {
25
24
  * LocalSmartDb - Lightweight local MongoDB-compatible database using Unix sockets
26
25
  *
27
26
  * This class wraps SmartdbServer and provides a simple interface for
28
- * starting a local file-based MongoDB-compatible server. Returns connection
29
- * info that you can use with your own MongoDB driver instance.
27
+ * starting a local file-based database server. Returns connection
28
+ * info that you can use with any compatible driver instance.
30
29
  *
31
30
  * @example
32
31
  * ```typescript
@@ -36,11 +35,11 @@ export interface ILocalSmartDbOptions {
36
35
  * const db = new LocalSmartDb({ folderPath: './data' });
37
36
  * const { connectionUri } = await db.start();
38
37
  *
39
- * // Connect with your own MongoDB client
38
+ * // Connect with the driver
40
39
  * const client = new MongoClient(connectionUri, { directConnection: true });
41
40
  * await client.connect();
42
41
  *
43
- * // Use the MongoDB client
42
+ * // Use the client
44
43
  * const collection = client.db('mydb').collection('users');
45
44
  * await collection.insertOne({ name: 'Alice' });
46
45
  *
@@ -1,3 +1 @@
1
- import * as smartpromise from '@push.rocks/smartpromise';
2
-
3
- export { smartpromise };
1
+ // Local module plugins - currently no external dependencies needed
@@ -1,46 +1,8 @@
1
- // SmartDB - MongoDB Wire Protocol compatible in-memory database server
2
- // Use the official MongoDB driver to connect to SmartdbServer
3
-
4
- // Re-export plugins for external use
5
- import * as plugins from './plugins.js';
6
- export { plugins };
7
-
8
- // Export BSON types for convenience
9
- export { ObjectId, Binary, Timestamp, Long, Decimal128, UUID } from 'bson';
10
-
11
- // Export all types
12
- export * from './types/interfaces.js';
13
-
14
- // Export errors
15
- export * from './errors/SmartdbErrors.js';
16
-
17
- // Export storage adapters
18
- export type { IStorageAdapter } from './storage/IStorageAdapter.js';
19
- export { MemoryStorageAdapter } from './storage/MemoryStorageAdapter.js';
20
- export { FileStorageAdapter } from './storage/FileStorageAdapter.js';
21
- export { OpLog } from './storage/OpLog.js';
22
- export { WAL } from './storage/WAL.js';
23
- export type { IWalEntry, TWalOperation } from './storage/WAL.js';
24
-
25
- // Export engines
26
- export { QueryEngine } from './engine/QueryEngine.js';
27
- export { UpdateEngine } from './engine/UpdateEngine.js';
28
- export { AggregationEngine } from './engine/AggregationEngine.js';
29
- export { IndexEngine } from './engine/IndexEngine.js';
30
- export { TransactionEngine } from './engine/TransactionEngine.js';
31
- export { QueryPlanner } from './engine/QueryPlanner.js';
32
- export type { IQueryPlan, TQueryPlanType } from './engine/QueryPlanner.js';
33
- export { SessionEngine } from './engine/SessionEngine.js';
34
- export type { ISession, ISessionEngineOptions } from './engine/SessionEngine.js';
1
+ // SmartDB - Wire protocol compatible embedded database server
35
2
 
36
3
  // Export server (the main entry point for using SmartDB)
37
4
  export { SmartdbServer } from './server/SmartdbServer.js';
38
5
  export type { ISmartdbServerOptions } from './server/SmartdbServer.js';
39
6
 
40
- // Export wire protocol utilities (for advanced usage)
41
- export { WireProtocol } from './server/WireProtocol.js';
42
- export { CommandRouter } from './server/CommandRouter.js';
43
- export type { ICommandHandler, IHandlerContext, ICursorState } from './server/CommandRouter.js';
44
-
45
- // Export utilities
46
- export * from './utils/checksum.js';
7
+ // Export bridge for advanced usage
8
+ export { RustDbBridge } from './rust-db-bridge.js';
@@ -1,17 +1,4 @@
1
1
  // @push.rocks scope
2
- import * as smartfs from '@push.rocks/smartfs';
3
- import * as smartpath from '@push.rocks/smartpath';
4
- import * as smartpromise from '@push.rocks/smartpromise';
5
- import * as smartrx from '@push.rocks/smartrx';
2
+ import * as smartrust from '@push.rocks/smartrust';
6
3
 
7
- export { smartfs, smartpath, smartpromise, smartrx };
8
-
9
- // thirdparty
10
- import * as bson from 'bson';
11
- import * as mingo from 'mingo';
12
-
13
- export { bson, mingo };
14
-
15
- // Re-export commonly used mingo classes
16
- export { Query } from 'mingo';
17
- export { Aggregator } from 'mingo';
4
+ export { smartrust };
@@ -0,0 +1,138 @@
1
+ import * as plugins from './plugins.js';
2
+ import * as path from 'path';
3
+ import * as url from 'url';
4
+ import { EventEmitter } from 'events';
5
+
6
+ /**
7
+ * Type-safe command definitions for the RustDb IPC protocol.
8
+ */
9
+ type TSmartDbCommands = {
10
+ start: { params: { config: ISmartDbRustConfig }; result: { connectionUri: string } };
11
+ stop: { params: Record<string, never>; result: void };
12
+ getStatus: { params: Record<string, never>; result: { running: boolean } };
13
+ getMetrics: { params: Record<string, never>; result: any };
14
+ };
15
+
16
+ /**
17
+ * Configuration sent to the Rust binary on start.
18
+ */
19
+ interface ISmartDbRustConfig {
20
+ port?: number;
21
+ host?: string;
22
+ socketPath?: string;
23
+ storage: 'memory' | 'file';
24
+ storagePath?: string;
25
+ persistPath?: string;
26
+ persistIntervalMs?: number;
27
+ }
28
+
29
+ /**
30
+ * Get the package root directory using import.meta.url.
31
+ * This file is at ts/ts_smartdb/, so package root is 2 levels up.
32
+ */
33
+ function getPackageRoot(): string {
34
+ const thisDir = path.dirname(url.fileURLToPath(import.meta.url));
35
+ return path.resolve(thisDir, '..', '..');
36
+ }
37
+
38
+ /**
39
+ * Map Node.js process.platform/process.arch to tsrust's friendly name suffix.
40
+ * tsrust names cross-compiled binaries as: rustdb_linux_amd64, rustdb_linux_arm64, etc.
41
+ */
42
+ function getTsrustPlatformSuffix(): string | null {
43
+ const archMap: Record<string, string> = { x64: 'amd64', arm64: 'arm64' };
44
+ const osMap: Record<string, string> = { linux: 'linux', darwin: 'macos' };
45
+ const os = osMap[process.platform];
46
+ const arch = archMap[process.arch];
47
+ if (os && arch) {
48
+ return `${os}_${arch}`;
49
+ }
50
+ return null;
51
+ }
52
+
53
+ /**
54
+ * Build local search paths for the Rust binary, including dist_rust/ candidates
55
+ * (built by tsrust) and local development build paths.
56
+ */
57
+ function buildLocalPaths(): string[] {
58
+ const packageRoot = getPackageRoot();
59
+ const suffix = getTsrustPlatformSuffix();
60
+ const paths: string[] = [];
61
+
62
+ // dist_rust/ candidates (tsrust cross-compiled output)
63
+ if (suffix) {
64
+ paths.push(path.join(packageRoot, 'dist_rust', `rustdb_${suffix}`));
65
+ }
66
+ paths.push(path.join(packageRoot, 'dist_rust', 'rustdb'));
67
+
68
+ // Local dev build paths
69
+ paths.push(path.resolve(process.cwd(), 'rust', 'target', 'release', 'rustdb'));
70
+ paths.push(path.resolve(process.cwd(), 'rust', 'target', 'debug', 'rustdb'));
71
+
72
+ return paths;
73
+ }
74
+
75
+ /**
76
+ * Bridge between TypeScript SmartdbServer and the Rust binary.
77
+ * Wraps @push.rocks/smartrust's RustBridge with type-safe command definitions.
78
+ */
79
+ export class RustDbBridge extends EventEmitter {
80
+ private bridge: plugins.smartrust.RustBridge<TSmartDbCommands>;
81
+
82
+ constructor() {
83
+ super();
84
+
85
+ this.bridge = new plugins.smartrust.RustBridge<TSmartDbCommands>({
86
+ binaryName: 'rustdb',
87
+ envVarName: 'SMARTDB_RUST_BINARY',
88
+ platformPackagePrefix: '@push.rocks/smartdb',
89
+ localPaths: buildLocalPaths(),
90
+ maxPayloadSize: 10 * 1024 * 1024, // 10 MB
91
+ });
92
+
93
+ // Forward events from the inner bridge
94
+ this.bridge.on('exit', (code: number | null, signal: string | null) => {
95
+ this.emit('exit', code, signal);
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Spawn the Rust binary in management mode.
101
+ * Returns true if the binary was found and spawned successfully.
102
+ */
103
+ public async spawn(): Promise<boolean> {
104
+ return this.bridge.spawn();
105
+ }
106
+
107
+ /**
108
+ * Kill the Rust process and clean up.
109
+ */
110
+ public kill(): void {
111
+ this.bridge.kill();
112
+ }
113
+
114
+ /**
115
+ * Whether the bridge is currently running.
116
+ */
117
+ public get running(): boolean {
118
+ return this.bridge.running;
119
+ }
120
+
121
+ // --- Convenience methods for each management command ---
122
+
123
+ public async startDb(config: ISmartDbRustConfig): Promise<{ connectionUri: string }> {
124
+ return await this.bridge.sendCommand('start', { config }) as { connectionUri: string };
125
+ }
126
+
127
+ public async stopDb(): Promise<void> {
128
+ await this.bridge.sendCommand('stop', {} as Record<string, never>);
129
+ }
130
+
131
+ public async getStatus(): Promise<{ running: boolean }> {
132
+ return await this.bridge.sendCommand('getStatus', {} as Record<string, never>) as { running: boolean };
133
+ }
134
+
135
+ public async getMetrics(): Promise<any> {
136
+ return this.bridge.sendCommand('getMetrics', {} as Record<string, never>);
137
+ }
138
+ }