@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
@@ -1,283 +0,0 @@
1
- import * as plugins from '../plugins.js';
2
- import type { Document, IStoredDocument, IAggregateOptions } from '../types/interfaces.js';
3
-
4
- // Import mingo Aggregator
5
- import { Aggregator } from 'mingo';
6
-
7
- /**
8
- * Aggregation engine using mingo for MongoDB-compatible aggregation pipeline execution
9
- */
10
- export class AggregationEngine {
11
- /**
12
- * Execute an aggregation pipeline on a collection of documents
13
- */
14
- static aggregate(
15
- documents: IStoredDocument[],
16
- pipeline: Document[],
17
- options?: IAggregateOptions
18
- ): Document[] {
19
- if (!pipeline || pipeline.length === 0) {
20
- return documents;
21
- }
22
-
23
- // Create mingo aggregator with the pipeline
24
- const aggregator = new Aggregator(pipeline, {
25
- collation: options?.collation as any,
26
- });
27
-
28
- // Run the aggregation
29
- const result = aggregator.run(documents);
30
-
31
- return Array.isArray(result) ? result : [];
32
- }
33
-
34
- /**
35
- * Execute aggregation and return an iterator for lazy evaluation
36
- */
37
- static *aggregateIterator(
38
- documents: IStoredDocument[],
39
- pipeline: Document[],
40
- options?: IAggregateOptions
41
- ): Generator<Document> {
42
- const aggregator = new Aggregator(pipeline, {
43
- collation: options?.collation as any,
44
- });
45
-
46
- // Get the cursor from mingo
47
- const cursor = aggregator.stream(documents);
48
-
49
- for (const doc of cursor) {
50
- yield doc as Document;
51
- }
52
- }
53
-
54
- /**
55
- * Execute a $lookup stage manually (for cross-collection lookups)
56
- * This is used when the lookup references another collection in the same database
57
- */
58
- static executeLookup(
59
- documents: IStoredDocument[],
60
- lookupSpec: {
61
- from: string;
62
- localField: string;
63
- foreignField: string;
64
- as: string;
65
- },
66
- foreignCollection: IStoredDocument[]
67
- ): Document[] {
68
- const { localField, foreignField, as } = lookupSpec;
69
-
70
- return documents.map(doc => {
71
- const localValue = this.getNestedValue(doc, localField);
72
- const matches = foreignCollection.filter(foreignDoc => {
73
- const foreignValue = this.getNestedValue(foreignDoc, foreignField);
74
- return this.valuesMatch(localValue, foreignValue);
75
- });
76
-
77
- return {
78
- ...doc,
79
- [as]: matches,
80
- };
81
- });
82
- }
83
-
84
- /**
85
- * Execute a $graphLookup stage manually
86
- */
87
- static executeGraphLookup(
88
- documents: IStoredDocument[],
89
- graphLookupSpec: {
90
- from: string;
91
- startWith: string | Document;
92
- connectFromField: string;
93
- connectToField: string;
94
- as: string;
95
- maxDepth?: number;
96
- depthField?: string;
97
- restrictSearchWithMatch?: Document;
98
- },
99
- foreignCollection: IStoredDocument[]
100
- ): Document[] {
101
- const {
102
- startWith,
103
- connectFromField,
104
- connectToField,
105
- as,
106
- maxDepth = 10,
107
- depthField,
108
- restrictSearchWithMatch,
109
- } = graphLookupSpec;
110
-
111
- return documents.map(doc => {
112
- const startValue = typeof startWith === 'string' && startWith.startsWith('$')
113
- ? this.getNestedValue(doc, startWith.slice(1))
114
- : startWith;
115
-
116
- const results: Document[] = [];
117
- const visited = new Set<string>();
118
- const queue: Array<{ value: any; depth: number }> = [];
119
-
120
- // Initialize with start value(s)
121
- const startValues = Array.isArray(startValue) ? startValue : [startValue];
122
- for (const val of startValues) {
123
- queue.push({ value: val, depth: 0 });
124
- }
125
-
126
- while (queue.length > 0) {
127
- const { value, depth } = queue.shift()!;
128
- if (depth > maxDepth) continue;
129
-
130
- const valueKey = JSON.stringify(value);
131
- if (visited.has(valueKey)) continue;
132
- visited.add(valueKey);
133
-
134
- // Find matching documents
135
- for (const foreignDoc of foreignCollection) {
136
- const foreignValue = this.getNestedValue(foreignDoc, connectToField);
137
-
138
- if (this.valuesMatch(value, foreignValue)) {
139
- // Check restrictSearchWithMatch
140
- if (restrictSearchWithMatch) {
141
- const matchQuery = new plugins.mingo.Query(restrictSearchWithMatch);
142
- if (!matchQuery.test(foreignDoc)) continue;
143
- }
144
-
145
- const resultDoc = depthField
146
- ? { ...foreignDoc, [depthField]: depth }
147
- : { ...foreignDoc };
148
-
149
- // Avoid duplicates in results
150
- const docKey = foreignDoc._id.toHexString();
151
- if (!results.some(r => r._id?.toHexString?.() === docKey)) {
152
- results.push(resultDoc);
153
-
154
- // Add connected values to queue
155
- const nextValue = this.getNestedValue(foreignDoc, connectFromField);
156
- if (nextValue !== undefined) {
157
- const nextValues = Array.isArray(nextValue) ? nextValue : [nextValue];
158
- for (const nv of nextValues) {
159
- queue.push({ value: nv, depth: depth + 1 });
160
- }
161
- }
162
- }
163
- }
164
- }
165
- }
166
-
167
- return {
168
- ...doc,
169
- [as]: results,
170
- };
171
- });
172
- }
173
-
174
- /**
175
- * Execute a $facet stage manually
176
- */
177
- static executeFacet(
178
- documents: IStoredDocument[],
179
- facetSpec: Record<string, Document[]>
180
- ): Document {
181
- const result: Document = {};
182
-
183
- for (const [facetName, pipeline] of Object.entries(facetSpec)) {
184
- result[facetName] = this.aggregate(documents, pipeline);
185
- }
186
-
187
- return result;
188
- }
189
-
190
- /**
191
- * Execute a $unionWith stage
192
- */
193
- static executeUnionWith(
194
- documents: IStoredDocument[],
195
- otherDocuments: IStoredDocument[],
196
- pipeline?: Document[]
197
- ): Document[] {
198
- let unionDocs: Document[] = otherDocuments;
199
- if (pipeline && pipeline.length > 0) {
200
- unionDocs = this.aggregate(otherDocuments, pipeline);
201
- }
202
- return [...documents, ...unionDocs];
203
- }
204
-
205
- /**
206
- * Execute a $merge stage (output to another collection)
207
- * Returns the documents that would be inserted/updated
208
- */
209
- static prepareMerge(
210
- documents: Document[],
211
- mergeSpec: {
212
- into: string;
213
- on?: string | string[];
214
- whenMatched?: 'replace' | 'keepExisting' | 'merge' | 'fail' | Document[];
215
- whenNotMatched?: 'insert' | 'discard' | 'fail';
216
- }
217
- ): {
218
- toInsert: Document[];
219
- toUpdate: Array<{ filter: Document; update: Document }>;
220
- onField: string | string[];
221
- whenMatched: string | Document[];
222
- whenNotMatched: string;
223
- } {
224
- const onField = mergeSpec.on || '_id';
225
- const whenMatched = mergeSpec.whenMatched || 'merge';
226
- const whenNotMatched = mergeSpec.whenNotMatched || 'insert';
227
-
228
- return {
229
- toInsert: [],
230
- toUpdate: [],
231
- onField,
232
- whenMatched,
233
- whenNotMatched,
234
- };
235
- }
236
-
237
- // ============================================================================
238
- // Helper Methods
239
- // ============================================================================
240
-
241
- private static getNestedValue(obj: any, path: string): any {
242
- const parts = path.split('.');
243
- let current = obj;
244
-
245
- for (const part of parts) {
246
- if (current === null || current === undefined) {
247
- return undefined;
248
- }
249
- current = current[part];
250
- }
251
-
252
- return current;
253
- }
254
-
255
- private static valuesMatch(a: any, b: any): boolean {
256
- if (a === b) return true;
257
-
258
- // Handle ObjectId comparison
259
- if (a instanceof plugins.bson.ObjectId && b instanceof plugins.bson.ObjectId) {
260
- return a.equals(b);
261
- }
262
-
263
- // Handle array contains check
264
- if (Array.isArray(a)) {
265
- return a.some(item => this.valuesMatch(item, b));
266
- }
267
- if (Array.isArray(b)) {
268
- return b.some(item => this.valuesMatch(a, item));
269
- }
270
-
271
- // Handle Date comparison
272
- if (a instanceof Date && b instanceof Date) {
273
- return a.getTime() === b.getTime();
274
- }
275
-
276
- // Handle object comparison
277
- if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
278
- return JSON.stringify(a) === JSON.stringify(b);
279
- }
280
-
281
- return false;
282
- }
283
- }