@push.rocks/smartdb 1.0.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.
Files changed (112) hide show
  1. package/.smartconfig.json +38 -0
  2. package/dist_ts/00_commitinfo_data.d.ts +8 -0
  3. package/dist_ts/00_commitinfo_data.js +9 -0
  4. package/dist_ts/index.d.ts +5 -0
  5. package/dist_ts/index.js +8 -0
  6. package/dist_ts/ts_local/classes.localsmartdb.d.ts +78 -0
  7. package/dist_ts/ts_local/classes.localsmartdb.js +115 -0
  8. package/dist_ts/ts_local/index.d.ts +2 -0
  9. package/dist_ts/ts_local/index.js +2 -0
  10. package/dist_ts/ts_local/plugins.d.ts +2 -0
  11. package/dist_ts/ts_local/plugins.js +3 -0
  12. package/dist_ts/ts_smartdb/engine/AggregationEngine.d.ts +66 -0
  13. package/dist_ts/ts_smartdb/engine/AggregationEngine.js +189 -0
  14. package/dist_ts/ts_smartdb/engine/IndexEngine.d.ts +97 -0
  15. package/dist_ts/ts_smartdb/engine/IndexEngine.js +678 -0
  16. package/dist_ts/ts_smartdb/engine/QueryEngine.d.ts +54 -0
  17. package/dist_ts/ts_smartdb/engine/QueryEngine.js +271 -0
  18. package/dist_ts/ts_smartdb/engine/QueryPlanner.d.ts +64 -0
  19. package/dist_ts/ts_smartdb/engine/QueryPlanner.js +308 -0
  20. package/dist_ts/ts_smartdb/engine/SessionEngine.d.ts +117 -0
  21. package/dist_ts/ts_smartdb/engine/SessionEngine.js +232 -0
  22. package/dist_ts/ts_smartdb/engine/TransactionEngine.d.ts +85 -0
  23. package/dist_ts/ts_smartdb/engine/TransactionEngine.js +287 -0
  24. package/dist_ts/ts_smartdb/engine/UpdateEngine.d.ts +47 -0
  25. package/dist_ts/ts_smartdb/engine/UpdateEngine.js +461 -0
  26. package/dist_ts/ts_smartdb/errors/SmartdbErrors.d.ts +100 -0
  27. package/dist_ts/ts_smartdb/errors/SmartdbErrors.js +155 -0
  28. package/dist_ts/ts_smartdb/index.d.ts +26 -0
  29. package/dist_ts/ts_smartdb/index.js +31 -0
  30. package/dist_ts/ts_smartdb/plugins.d.ts +10 -0
  31. package/dist_ts/ts_smartdb/plugins.js +14 -0
  32. package/dist_ts/ts_smartdb/server/CommandRouter.d.ts +87 -0
  33. package/dist_ts/ts_smartdb/server/CommandRouter.js +222 -0
  34. package/dist_ts/ts_smartdb/server/SmartdbServer.d.ts +102 -0
  35. package/dist_ts/ts_smartdb/server/SmartdbServer.js +279 -0
  36. package/dist_ts/ts_smartdb/server/WireProtocol.d.ts +117 -0
  37. package/dist_ts/ts_smartdb/server/WireProtocol.js +298 -0
  38. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.d.ts +100 -0
  39. package/dist_ts/ts_smartdb/server/handlers/AdminHandler.js +668 -0
  40. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.d.ts +31 -0
  41. package/dist_ts/ts_smartdb/server/handlers/AggregateHandler.js +277 -0
  42. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.d.ts +8 -0
  43. package/dist_ts/ts_smartdb/server/handlers/DeleteHandler.js +95 -0
  44. package/dist_ts/ts_smartdb/server/handlers/FindHandler.d.ts +31 -0
  45. package/dist_ts/ts_smartdb/server/handlers/FindHandler.js +291 -0
  46. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.d.ts +11 -0
  47. package/dist_ts/ts_smartdb/server/handlers/HelloHandler.js +62 -0
  48. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.d.ts +20 -0
  49. package/dist_ts/ts_smartdb/server/handlers/IndexHandler.js +183 -0
  50. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.d.ts +8 -0
  51. package/dist_ts/ts_smartdb/server/handlers/InsertHandler.js +79 -0
  52. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.d.ts +24 -0
  53. package/dist_ts/ts_smartdb/server/handlers/UpdateHandler.js +296 -0
  54. package/dist_ts/ts_smartdb/server/handlers/index.d.ts +8 -0
  55. package/dist_ts/ts_smartdb/server/handlers/index.js +10 -0
  56. package/dist_ts/ts_smartdb/server/index.d.ts +6 -0
  57. package/dist_ts/ts_smartdb/server/index.js +7 -0
  58. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.d.ts +85 -0
  59. package/dist_ts/ts_smartdb/storage/FileStorageAdapter.js +465 -0
  60. package/dist_ts/ts_smartdb/storage/IStorageAdapter.d.ts +145 -0
  61. package/dist_ts/ts_smartdb/storage/IStorageAdapter.js +2 -0
  62. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.d.ts +67 -0
  63. package/dist_ts/ts_smartdb/storage/MemoryStorageAdapter.js +378 -0
  64. package/dist_ts/ts_smartdb/storage/OpLog.d.ts +93 -0
  65. package/dist_ts/ts_smartdb/storage/OpLog.js +221 -0
  66. package/dist_ts/ts_smartdb/storage/WAL.d.ts +117 -0
  67. package/dist_ts/ts_smartdb/storage/WAL.js +286 -0
  68. package/dist_ts/ts_smartdb/types/interfaces.d.ts +363 -0
  69. package/dist_ts/ts_smartdb/types/interfaces.js +2 -0
  70. package/dist_ts/ts_smartdb/utils/checksum.d.ts +30 -0
  71. package/dist_ts/ts_smartdb/utils/checksum.js +77 -0
  72. package/dist_ts/ts_smartdb/utils/index.d.ts +1 -0
  73. package/dist_ts/ts_smartdb/utils/index.js +2 -0
  74. package/license +19 -0
  75. package/package.json +69 -0
  76. package/readme.md +529 -0
  77. package/ts/00_commitinfo_data.ts +8 -0
  78. package/ts/index.ts +11 -0
  79. package/ts/ts_local/classes.localsmartdb.ts +143 -0
  80. package/ts/ts_local/index.ts +2 -0
  81. package/ts/ts_local/plugins.ts +3 -0
  82. package/ts/ts_smartdb/engine/AggregationEngine.ts +283 -0
  83. package/ts/ts_smartdb/engine/IndexEngine.ts +798 -0
  84. package/ts/ts_smartdb/engine/QueryEngine.ts +301 -0
  85. package/ts/ts_smartdb/engine/QueryPlanner.ts +393 -0
  86. package/ts/ts_smartdb/engine/SessionEngine.ts +292 -0
  87. package/ts/ts_smartdb/engine/TransactionEngine.ts +351 -0
  88. package/ts/ts_smartdb/engine/UpdateEngine.ts +506 -0
  89. package/ts/ts_smartdb/errors/SmartdbErrors.ts +181 -0
  90. package/ts/ts_smartdb/index.ts +46 -0
  91. package/ts/ts_smartdb/plugins.ts +17 -0
  92. package/ts/ts_smartdb/server/CommandRouter.ts +289 -0
  93. package/ts/ts_smartdb/server/SmartdbServer.ts +354 -0
  94. package/ts/ts_smartdb/server/WireProtocol.ts +416 -0
  95. package/ts/ts_smartdb/server/handlers/AdminHandler.ts +719 -0
  96. package/ts/ts_smartdb/server/handlers/AggregateHandler.ts +342 -0
  97. package/ts/ts_smartdb/server/handlers/DeleteHandler.ts +115 -0
  98. package/ts/ts_smartdb/server/handlers/FindHandler.ts +330 -0
  99. package/ts/ts_smartdb/server/handlers/HelloHandler.ts +78 -0
  100. package/ts/ts_smartdb/server/handlers/IndexHandler.ts +207 -0
  101. package/ts/ts_smartdb/server/handlers/InsertHandler.ts +97 -0
  102. package/ts/ts_smartdb/server/handlers/UpdateHandler.ts +344 -0
  103. package/ts/ts_smartdb/server/handlers/index.ts +10 -0
  104. package/ts/ts_smartdb/server/index.ts +10 -0
  105. package/ts/ts_smartdb/storage/FileStorageAdapter.ts +562 -0
  106. package/ts/ts_smartdb/storage/IStorageAdapter.ts +208 -0
  107. package/ts/ts_smartdb/storage/MemoryStorageAdapter.ts +455 -0
  108. package/ts/ts_smartdb/storage/OpLog.ts +282 -0
  109. package/ts/ts_smartdb/storage/WAL.ts +375 -0
  110. package/ts/ts_smartdb/types/interfaces.ts +433 -0
  111. package/ts/ts_smartdb/utils/checksum.ts +88 -0
  112. package/ts/ts_smartdb/utils/index.ts +1 -0
package/readme.md ADDED
@@ -0,0 +1,529 @@
1
+ # @push.rocks/smartdb
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. ⚡
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @push.rocks/smartdb
9
+ # or
10
+ npm install @push.rocks/smartdb
11
+ ```
12
+
13
+ ## Issue Reporting and Security
14
+
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
+
17
+ ## What It Does
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.
20
+
21
+ ### Why SmartDB?
22
+
23
+ | | SmartDB | Real MongoDB |
24
+ |---|---|---|
25
+ | **Startup time** | ~5ms | ~2-5s |
26
+ | **Binary download** | None | ~200MB |
27
+ | **Node.js only** | ✅ | ❌ |
28
+ | **Persistence** | Memory or file-based | Full disk engine |
29
+ | **Perfect for** | Unit tests, CI/CD, prototyping, local dev | Production |
30
+
31
+ ### Two Ways to Use It
32
+
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.
35
+
36
+ ## Quick Start
37
+
38
+ ### Option 1: LocalSmartDb (Zero Config) 🎯
39
+
40
+ The fastest way to get a persistent local database:
41
+
42
+ ```typescript
43
+ import { LocalSmartDb } from '@push.rocks/smartdb';
44
+ import { MongoClient } from 'mongodb';
45
+
46
+ // Point it at a folder — that's it
47
+ const db = new LocalSmartDb({ folderPath: './my-data' });
48
+ const { connectionUri } = await db.start();
49
+
50
+ // Connect with the standard MongoDB driver
51
+ const client = new MongoClient(connectionUri, { directConnection: true });
52
+ await client.connect();
53
+
54
+ // Use exactly like MongoDB
55
+ const users = client.db('myapp').collection('users');
56
+ await users.insertOne({ name: 'Alice', email: 'alice@example.com' });
57
+ const user = await users.findOne({ name: 'Alice' });
58
+ console.log(user); // { _id: ObjectId(...), name: 'Alice', email: 'alice@example.com' }
59
+
60
+ // Data persists to disk automatically — survives restarts!
61
+ await client.close();
62
+ await db.stop();
63
+ ```
64
+
65
+ ### Option 2: SmartdbServer (Full Control) 🏗️
66
+
67
+ ```typescript
68
+ import { SmartdbServer } from '@push.rocks/smartdb';
69
+ import { MongoClient } from 'mongodb';
70
+
71
+ // TCP mode
72
+ const server = new SmartdbServer({ port: 27017 });
73
+ await server.start();
74
+
75
+ const client = new MongoClient('mongodb://127.0.0.1:27017');
76
+ await client.connect();
77
+
78
+ const db = client.db('myapp');
79
+ await db.collection('users').insertOne({ name: 'Alice', age: 30 });
80
+ const user = await db.collection('users').findOne({ name: 'Alice' });
81
+
82
+ await client.close();
83
+ await server.stop();
84
+ ```
85
+
86
+ ---
87
+
88
+ ## API Reference
89
+
90
+ ### SmartdbServer
91
+
92
+ The core server class. Speaks MongoDB wire protocol over TCP or Unix sockets.
93
+
94
+ #### Constructor Options (`ISmartdbServerOptions`)
95
+
96
+ ```typescript
97
+ import { SmartdbServer } from '@push.rocks/smartdb';
98
+
99
+ // TCP mode (default)
100
+ const server = new SmartdbServer({
101
+ port: 27017, // Default: 27017
102
+ host: '127.0.0.1', // Default: 127.0.0.1
103
+ storage: 'memory', // 'memory' or 'file' (default: 'memory')
104
+ storagePath: './data', // Required when storage is 'file'
105
+ });
106
+
107
+ // Unix socket mode — no port conflicts!
108
+ const server = new SmartdbServer({
109
+ socketPath: '/tmp/smartdb.sock',
110
+ storage: 'file',
111
+ storagePath: './data',
112
+ });
113
+
114
+ // Memory storage with periodic persistence
115
+ const server = new SmartdbServer({
116
+ storage: 'memory',
117
+ persistPath: './data/snapshot.json',
118
+ persistIntervalMs: 30000, // Save every 30s
119
+ });
120
+ ```
121
+
122
+ #### Methods & Properties
123
+
124
+ | Method / Property | Type | Description |
125
+ |---|---|---|
126
+ | `start()` | `Promise<void>` | Start the server |
127
+ | `stop()` | `Promise<void>` | Stop the server and clean up |
128
+ | `getConnectionUri()` | `string` | Get the MongoDB connection URI |
129
+ | `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 |
135
+
136
+ ### LocalSmartDb
137
+
138
+ Zero-config wrapper around SmartdbServer. Uses Unix sockets and file-based persistence.
139
+
140
+ #### Constructor Options (`ILocalSmartDbOptions`)
141
+
142
+ ```typescript
143
+ import { LocalSmartDb } from '@push.rocks/smartdb';
144
+
145
+ const db = new LocalSmartDb({
146
+ folderPath: './data', // Required: data storage directory
147
+ socketPath: '/tmp/custom.sock', // Optional: custom socket (default: auto-generated)
148
+ });
149
+ ```
150
+
151
+ #### Methods & Properties
152
+
153
+ | Method / Property | Type | Description |
154
+ |---|---|---|
155
+ | `start()` | `Promise<ILocalSmartDbConnectionInfo>` | Start and return connection info |
156
+ | `stop()` | `Promise<void>` | Stop the server |
157
+ | `getConnectionInfo()` | `ILocalSmartDbConnectionInfo` | Get current connection info |
158
+ | `getConnectionUri()` | `string` | Get the MongoDB URI |
159
+ | `getServer()` | `SmartdbServer` | Access the underlying server |
160
+ | `running` | `boolean` | Whether the server is running |
161
+
162
+ #### Connection Info (`ILocalSmartDbConnectionInfo`)
163
+
164
+ ```typescript
165
+ interface ILocalSmartDbConnectionInfo {
166
+ socketPath: string; // e.g., /tmp/smartdb-abc123.sock
167
+ connectionUri: string; // e.g., mongodb://%2Ftmp%2Fsmartdb-abc123.sock
168
+ }
169
+ ```
170
+
171
+ ---
172
+
173
+ ## Supported MongoDB Operations
174
+
175
+ SmartDB supports the core MongoDB operations through the wire protocol. Use the standard `mongodb` driver — these all work:
176
+
177
+ ### CRUD
178
+
179
+ ```typescript
180
+ // Insert
181
+ await collection.insertOne({ name: 'Bob' });
182
+ await collection.insertMany([{ a: 1 }, { a: 2 }]);
183
+
184
+ // Find
185
+ const doc = await collection.findOne({ name: 'Bob' });
186
+ const docs = await collection.find({ age: { $gte: 18 } }).toArray();
187
+
188
+ // Update
189
+ await collection.updateOne({ name: 'Bob' }, { $set: { age: 25 } });
190
+ await collection.updateMany({ active: false }, { $set: { archived: true } });
191
+
192
+ // Delete
193
+ await collection.deleteOne({ name: 'Bob' });
194
+ await collection.deleteMany({ archived: true });
195
+
196
+ // Replace
197
+ await collection.replaceOne({ _id: id }, { name: 'New Bob', age: 30 });
198
+
199
+ // Find and Modify
200
+ await collection.findOneAndUpdate({ name: 'Bob' }, { $inc: { visits: 1 } }, { returnDocument: 'after' });
201
+ await collection.findOneAndDelete({ expired: true });
202
+ await collection.findOneAndReplace({ _id: id }, { name: 'Replaced' }, { returnDocument: 'after' });
203
+ ```
204
+
205
+ ### Query Operators
206
+
207
+ ```typescript
208
+ // Comparison
209
+ { age: { $eq: 25 } } { age: { $ne: 25 } }
210
+ { age: { $gt: 18 } } { age: { $lt: 65 } }
211
+ { age: { $gte: 18 } } { age: { $lte: 65 } }
212
+ { status: { $in: ['active', 'pending'] } }
213
+ { status: { $nin: ['deleted'] } }
214
+
215
+ // Logical
216
+ { $and: [{ age: { $gte: 18 } }, { active: true }] }
217
+ { $or: [{ status: 'active' }, { admin: true }] }
218
+ { $not: { status: 'deleted' } }
219
+
220
+ // Element
221
+ { email: { $exists: true } }
222
+ { type: { $type: 'string' } }
223
+
224
+ // Array
225
+ { tags: { $all: ['mongodb', 'database'] } }
226
+ { scores: { $elemMatch: { $gte: 80, $lt: 90 } } }
227
+ { tags: { $size: 3 } }
228
+
229
+ // Regex
230
+ { name: { $regex: /^Al/i } }
231
+ ```
232
+
233
+ ### Update Operators
234
+
235
+ ```typescript
236
+ { $set: { name: 'New Name' } }
237
+ { $unset: { tempField: '' } }
238
+ { $inc: { count: 1 } }
239
+ { $mul: { price: 1.1 } }
240
+ { $min: { low: 50 } } { $max: { high: 100 } }
241
+ { $push: { tags: 'new' } } { $pull: { tags: 'old' } }
242
+ { $addToSet: { tags: 'unique' } }
243
+ { $pop: { queue: 1 } } // Remove last
244
+ { $pop: { queue: -1 } } // Remove first
245
+ { $rename: { old: 'new' } }
246
+ { $currentDate: { lastModified: true } }
247
+ ```
248
+
249
+ ### Aggregation Pipeline
250
+
251
+ ```typescript
252
+ const results = await collection.aggregate([
253
+ { $match: { status: 'active' } },
254
+ { $group: { _id: '$category', total: { $sum: '$amount' } } },
255
+ { $sort: { total: -1 } },
256
+ { $limit: 10 },
257
+ { $project: { category: '$_id', total: 1, _id: 0 } },
258
+ ]).toArray();
259
+ ```
260
+
261
+ **Supported stages:** `$match`, `$project`, `$group`, `$sort`, `$limit`, `$skip`, `$unwind`, `$lookup`, `$addFields`, `$count`, `$facet`, `$replaceRoot`, `$set`, `$unset`
262
+
263
+ **Group accumulators:** `$sum`, `$avg`, `$min`, `$max`, `$first`, `$last`, `$push`, `$addToSet`, `$count`
264
+
265
+ ### Indexes
266
+
267
+ ```typescript
268
+ await collection.createIndex({ email: 1 }, { unique: true });
269
+ await collection.createIndex({ name: 1, age: -1 }); // compound
270
+ await collection.createIndex({ field: 1 }, { sparse: true });
271
+ const indexes = await collection.listIndexes().toArray();
272
+ await collection.dropIndex('email_1');
273
+ await collection.dropIndexes(); // drop all except _id
274
+ ```
275
+
276
+ ### Database & Admin
277
+
278
+ ```typescript
279
+ await db.listCollections().toArray();
280
+ await db.createCollection('new');
281
+ await db.dropCollection('old');
282
+ await db.dropDatabase();
283
+ await db.stats();
284
+
285
+ const admin = client.db().admin();
286
+ await admin.listDatabases();
287
+ await admin.ping();
288
+ await admin.serverStatus();
289
+ ```
290
+
291
+ ### Bulk Operations
292
+
293
+ ```typescript
294
+ const result = await collection.bulkWrite([
295
+ { insertOne: { document: { name: 'Bulk1' } } },
296
+ { updateOne: { filter: { name: 'X' }, update: { $set: { bulk: true } } } },
297
+ { deleteOne: { filter: { name: 'Expired' } } },
298
+ ]);
299
+ ```
300
+
301
+ ### Count & Distinct
302
+
303
+ ```typescript
304
+ const count = await collection.countDocuments({ status: 'active' });
305
+ const estimated = await collection.estimatedDocumentCount();
306
+ const names = await collection.distinct('name');
307
+ ```
308
+
309
+ ---
310
+
311
+ ## Wire Protocol Commands
312
+
313
+ | Category | Commands |
314
+ |---|---|
315
+ | **Handshake** | `hello`, `isMaster`, `ismaster` |
316
+ | **CRUD** | `find`, `insert`, `update`, `delete`, `findAndModify`, `getMore`, `killCursors` |
317
+ | **Aggregation** | `aggregate`, `count`, `distinct` |
318
+ | **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` |
322
+
323
+ Compatible with MongoDB wire protocol versions 0–21 (MongoDB 3.6 through 7.0 drivers).
324
+
325
+ ---
326
+
327
+ ## Architecture
328
+
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
+ ```
365
+
366
+ ### Key Components
367
+
368
+ | Component | What It Does |
369
+ |---|---|
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';
411
+
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
+ ```
455
+
456
+ ---
457
+
458
+ ## Testing Examples
459
+
460
+ ### Unit Tests with @git.zone/tstest
461
+
462
+ ```typescript
463
+ import { expect, tap } from '@git.zone/tstest/tapbundle';
464
+ import { SmartdbServer } from '@push.rocks/smartdb';
465
+ import { MongoClient } from 'mongodb';
466
+
467
+ let server: SmartdbServer;
468
+ let client: MongoClient;
469
+
470
+ tap.test('setup', async () => {
471
+ server = new SmartdbServer({ port: 27117 });
472
+ await server.start();
473
+ client = new MongoClient('mongodb://127.0.0.1:27117', { directConnection: true });
474
+ await client.connect();
475
+ });
476
+
477
+ tap.test('should insert and find', async () => {
478
+ const col = client.db('test').collection('items');
479
+ await col.insertOne({ name: 'Widget', price: 9.99 });
480
+ const item = await col.findOne({ name: 'Widget' });
481
+ expect(item?.price).toEqual(9.99);
482
+ });
483
+
484
+ tap.test('teardown', async () => {
485
+ await client.close();
486
+ await server.stop();
487
+ });
488
+
489
+ export default tap.start();
490
+ ```
491
+
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
+ ```
509
+
510
+ ## License and Legal Information
511
+
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.
513
+
514
+ **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
+
516
+ ### Trademarks
517
+
518
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
519
+
520
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
521
+
522
+ ### Company Information
523
+
524
+ Task Venture Capital GmbH
525
+ Registered at District Court Bremen HRB 35230 HB, Germany
526
+
527
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
528
+
529
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
+ */
4
+ export const commitinfo = {
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.'
8
+ }
package/ts/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { commitinfo } from './00_commitinfo_data.js';
2
+
3
+ // Export core SmartDB module
4
+ export * from './ts_smartdb/index.js';
5
+
6
+ // Export LocalSmartDb
7
+ export { LocalSmartDb } from './ts_local/index.js';
8
+ export type { ILocalSmartDbOptions, ILocalSmartDbConnectionInfo } from './ts_local/index.js';
9
+
10
+ // Export commitinfo
11
+ export { commitinfo };
@@ -0,0 +1,143 @@
1
+ import * as plugins from './plugins.js';
2
+ import * as crypto from 'crypto';
3
+ import * as path from 'path';
4
+ import * as os from 'os';
5
+ import { SmartdbServer } from '../ts_smartdb/index.js';
6
+
7
+ /**
8
+ * Connection information returned by LocalSmartDb.start()
9
+ */
10
+ export interface ILocalSmartDbConnectionInfo {
11
+ /** The Unix socket file path */
12
+ socketPath: string;
13
+ /** MongoDB connection URI ready for MongoClient */
14
+ connectionUri: string;
15
+ }
16
+
17
+ export interface ILocalSmartDbOptions {
18
+ /** Required: where to store data */
19
+ folderPath: string;
20
+ /** Optional: custom socket path (default: auto-generated in /tmp) */
21
+ socketPath?: string;
22
+ }
23
+
24
+ /**
25
+ * LocalSmartDb - Lightweight local MongoDB-compatible database using Unix sockets
26
+ *
27
+ * 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.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import { LocalSmartDb } from '@push.rocks/smartdb';
34
+ * import { MongoClient } from 'mongodb';
35
+ *
36
+ * const db = new LocalSmartDb({ folderPath: './data' });
37
+ * const { connectionUri } = await db.start();
38
+ *
39
+ * // Connect with your own MongoDB client
40
+ * const client = new MongoClient(connectionUri, { directConnection: true });
41
+ * await client.connect();
42
+ *
43
+ * // Use the MongoDB client
44
+ * const collection = client.db('mydb').collection('users');
45
+ * await collection.insertOne({ name: 'Alice' });
46
+ *
47
+ * // When done
48
+ * await client.close();
49
+ * await db.stop();
50
+ * ```
51
+ */
52
+ export class LocalSmartDb {
53
+ private options: ILocalSmartDbOptions;
54
+ private server: SmartdbServer | null = null;
55
+ private generatedSocketPath: string | null = null;
56
+
57
+ constructor(options: ILocalSmartDbOptions) {
58
+ this.options = options;
59
+ }
60
+
61
+ /**
62
+ * Generate a unique socket path in /tmp
63
+ */
64
+ private generateSocketPath(): string {
65
+ const randomId = crypto.randomBytes(8).toString('hex');
66
+ return path.join(os.tmpdir(), `smartdb-${randomId}.sock`);
67
+ }
68
+
69
+ /**
70
+ * Start the local SmartDB server and return connection info
71
+ */
72
+ async start(): Promise<ILocalSmartDbConnectionInfo> {
73
+ if (this.server) {
74
+ throw new Error('LocalSmartDb is already running');
75
+ }
76
+
77
+ // Use provided socket path or generate one
78
+ this.generatedSocketPath = this.options.socketPath ?? this.generateSocketPath();
79
+
80
+ this.server = new SmartdbServer({
81
+ socketPath: this.generatedSocketPath,
82
+ storage: 'file',
83
+ storagePath: this.options.folderPath,
84
+ });
85
+ await this.server.start();
86
+
87
+ return {
88
+ socketPath: this.generatedSocketPath,
89
+ connectionUri: this.server.getConnectionUri(),
90
+ };
91
+ }
92
+
93
+ /**
94
+ * Get connection info (throws if not started)
95
+ */
96
+ getConnectionInfo(): ILocalSmartDbConnectionInfo {
97
+ if (!this.server || !this.generatedSocketPath) {
98
+ throw new Error('LocalSmartDb is not running. Call start() first.');
99
+ }
100
+ return {
101
+ socketPath: this.generatedSocketPath,
102
+ connectionUri: this.server.getConnectionUri(),
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Get the underlying SmartdbServer instance (throws if not started)
108
+ */
109
+ getServer(): SmartdbServer {
110
+ if (!this.server) {
111
+ throw new Error('LocalSmartDb is not running. Call start() first.');
112
+ }
113
+ return this.server;
114
+ }
115
+
116
+ /**
117
+ * Get the connection URI
118
+ */
119
+ getConnectionUri(): string {
120
+ if (!this.server) {
121
+ throw new Error('LocalSmartDb is not running. Call start() first.');
122
+ }
123
+ return this.server.getConnectionUri();
124
+ }
125
+
126
+ /**
127
+ * Check if the server is running
128
+ */
129
+ get running(): boolean {
130
+ return this.server !== null && this.server.running;
131
+ }
132
+
133
+ /**
134
+ * Stop the local SmartDB server
135
+ */
136
+ async stop(): Promise<void> {
137
+ if (this.server) {
138
+ await this.server.stop();
139
+ this.server = null;
140
+ this.generatedSocketPath = null;
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,2 @@
1
+ export { LocalSmartDb } from './classes.localsmartdb.js';
2
+ export type { ILocalSmartDbOptions, ILocalSmartDbConnectionInfo } from './classes.localsmartdb.js';
@@ -0,0 +1,3 @@
1
+ import * as smartpromise from '@push.rocks/smartpromise';
2
+
3
+ export { smartpromise };