@push.rocks/smartmongo 2.0.12 → 2.1.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.
- package/dist_ts/00_commitinfo_data.js +2 -2
- package/dist_ts/congodb/congodb.plugins.d.ts +10 -0
- package/dist_ts/congodb/congodb.plugins.js +14 -0
- package/dist_ts/congodb/engine/AggregationEngine.d.ts +66 -0
- package/dist_ts/congodb/engine/AggregationEngine.js +189 -0
- package/dist_ts/congodb/engine/IndexEngine.d.ts +77 -0
- package/dist_ts/congodb/engine/IndexEngine.js +376 -0
- package/dist_ts/congodb/engine/QueryEngine.d.ts +54 -0
- package/dist_ts/congodb/engine/QueryEngine.js +271 -0
- package/dist_ts/congodb/engine/TransactionEngine.d.ts +85 -0
- package/dist_ts/congodb/engine/TransactionEngine.js +287 -0
- package/dist_ts/congodb/engine/UpdateEngine.d.ts +47 -0
- package/dist_ts/congodb/engine/UpdateEngine.js +461 -0
- package/dist_ts/congodb/errors/CongoErrors.d.ts +100 -0
- package/dist_ts/congodb/errors/CongoErrors.js +155 -0
- package/dist_ts/congodb/index.d.ts +19 -0
- package/dist_ts/congodb/index.js +26 -0
- package/dist_ts/congodb/server/CommandRouter.d.ts +51 -0
- package/dist_ts/congodb/server/CommandRouter.js +132 -0
- package/dist_ts/congodb/server/CongoServer.d.ts +95 -0
- package/dist_ts/congodb/server/CongoServer.js +227 -0
- package/dist_ts/congodb/server/WireProtocol.d.ts +117 -0
- package/dist_ts/congodb/server/WireProtocol.js +298 -0
- package/dist_ts/congodb/server/handlers/AdminHandler.d.ts +100 -0
- package/dist_ts/congodb/server/handlers/AdminHandler.js +568 -0
- package/dist_ts/congodb/server/handlers/AggregateHandler.d.ts +31 -0
- package/dist_ts/congodb/server/handlers/AggregateHandler.js +277 -0
- package/dist_ts/congodb/server/handlers/DeleteHandler.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/DeleteHandler.js +83 -0
- package/dist_ts/congodb/server/handlers/FindHandler.d.ts +31 -0
- package/dist_ts/congodb/server/handlers/FindHandler.js +261 -0
- package/dist_ts/congodb/server/handlers/HelloHandler.d.ts +11 -0
- package/dist_ts/congodb/server/handlers/HelloHandler.js +62 -0
- package/dist_ts/congodb/server/handlers/IndexHandler.d.ts +20 -0
- package/dist_ts/congodb/server/handlers/IndexHandler.js +183 -0
- package/dist_ts/congodb/server/handlers/InsertHandler.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/InsertHandler.js +76 -0
- package/dist_ts/congodb/server/handlers/UpdateHandler.d.ts +24 -0
- package/dist_ts/congodb/server/handlers/UpdateHandler.js +270 -0
- package/dist_ts/congodb/server/handlers/index.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/index.js +10 -0
- package/dist_ts/congodb/server/index.d.ts +6 -0
- package/dist_ts/congodb/server/index.js +7 -0
- package/dist_ts/congodb/storage/FileStorageAdapter.d.ts +61 -0
- package/dist_ts/congodb/storage/FileStorageAdapter.js +396 -0
- package/dist_ts/congodb/storage/IStorageAdapter.d.ts +140 -0
- package/dist_ts/congodb/storage/IStorageAdapter.js +2 -0
- package/dist_ts/congodb/storage/MemoryStorageAdapter.d.ts +66 -0
- package/dist_ts/congodb/storage/MemoryStorageAdapter.js +367 -0
- package/dist_ts/congodb/storage/OpLog.d.ts +93 -0
- package/dist_ts/congodb/storage/OpLog.js +221 -0
- package/dist_ts/congodb/types/interfaces.d.ts +363 -0
- package/dist_ts/congodb/types/interfaces.js +2 -0
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +8 -6
- package/dist_ts/smartmongo.plugins.d.ts +1 -1
- package/dist_ts/smartmongo.plugins.js +2 -2
- package/npmextra.json +17 -7
- package/package.json +20 -12
- package/readme.hints.md +89 -1
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/congodb/congodb.plugins.ts +17 -0
- package/ts/congodb/engine/AggregationEngine.ts +283 -0
- package/ts/congodb/engine/IndexEngine.ts +479 -0
- package/ts/congodb/engine/QueryEngine.ts +301 -0
- package/ts/congodb/engine/TransactionEngine.ts +351 -0
- package/ts/congodb/engine/UpdateEngine.ts +506 -0
- package/ts/congodb/errors/CongoErrors.ts +181 -0
- package/ts/congodb/index.ts +37 -0
- package/ts/congodb/server/CommandRouter.ts +180 -0
- package/ts/congodb/server/CongoServer.ts +298 -0
- package/ts/congodb/server/WireProtocol.ts +416 -0
- package/ts/congodb/server/handlers/AdminHandler.ts +614 -0
- package/ts/congodb/server/handlers/AggregateHandler.ts +342 -0
- package/ts/congodb/server/handlers/DeleteHandler.ts +100 -0
- package/ts/congodb/server/handlers/FindHandler.ts +301 -0
- package/ts/congodb/server/handlers/HelloHandler.ts +78 -0
- package/ts/congodb/server/handlers/IndexHandler.ts +207 -0
- package/ts/congodb/server/handlers/InsertHandler.ts +91 -0
- package/ts/congodb/server/handlers/UpdateHandler.ts +315 -0
- package/ts/congodb/server/handlers/index.ts +10 -0
- package/ts/congodb/server/index.ts +10 -0
- package/ts/congodb/storage/FileStorageAdapter.ts +479 -0
- package/ts/congodb/storage/IStorageAdapter.ts +202 -0
- package/ts/congodb/storage/MemoryStorageAdapter.ts +443 -0
- package/ts/congodb/storage/OpLog.ts +282 -0
- package/ts/congodb/types/interfaces.ts +433 -0
- package/ts/index.ts +3 -0
- package/ts/smartmongo.plugins.ts +1 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import * as plugins from '../congodb.plugins.js';
|
|
2
|
+
import type { IStorageAdapter } from './IStorageAdapter.js';
|
|
3
|
+
import type { IOpLogEntry, Document, IResumeToken, ChangeStreamOperationType } from '../types/interfaces.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Operation Log for tracking all mutations
|
|
7
|
+
* Used primarily for change stream support
|
|
8
|
+
*/
|
|
9
|
+
export class OpLog {
|
|
10
|
+
private storage: IStorageAdapter;
|
|
11
|
+
private counter = 0;
|
|
12
|
+
private listeners: Array<(entry: IOpLogEntry) => void> = [];
|
|
13
|
+
|
|
14
|
+
constructor(storage: IStorageAdapter) {
|
|
15
|
+
this.storage = storage;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Generate a new timestamp for oplog entries
|
|
20
|
+
*/
|
|
21
|
+
generateTimestamp(): plugins.bson.Timestamp {
|
|
22
|
+
this.counter++;
|
|
23
|
+
return new plugins.bson.Timestamp({ t: Math.floor(Date.now() / 1000), i: this.counter });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Generate a resume token from a timestamp
|
|
28
|
+
*/
|
|
29
|
+
generateResumeToken(ts: plugins.bson.Timestamp): IResumeToken {
|
|
30
|
+
// Create a resume token similar to MongoDB's format
|
|
31
|
+
// It's a base64-encoded BSON document containing the timestamp
|
|
32
|
+
const tokenData = {
|
|
33
|
+
_data: Buffer.from(JSON.stringify({
|
|
34
|
+
ts: { t: ts.high, i: ts.low },
|
|
35
|
+
version: 1,
|
|
36
|
+
})).toString('base64'),
|
|
37
|
+
};
|
|
38
|
+
return tokenData;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Parse a resume token to get the timestamp
|
|
43
|
+
*/
|
|
44
|
+
parseResumeToken(token: IResumeToken): plugins.bson.Timestamp {
|
|
45
|
+
try {
|
|
46
|
+
const data = JSON.parse(Buffer.from(token._data, 'base64').toString('utf-8'));
|
|
47
|
+
return new plugins.bson.Timestamp({ t: data.ts.t, i: data.ts.i });
|
|
48
|
+
} catch {
|
|
49
|
+
throw new Error('Invalid resume token');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Record an insert operation
|
|
55
|
+
*/
|
|
56
|
+
async recordInsert(
|
|
57
|
+
dbName: string,
|
|
58
|
+
collName: string,
|
|
59
|
+
document: Document,
|
|
60
|
+
txnInfo?: { txnNumber?: number; lsid?: { id: plugins.bson.Binary } }
|
|
61
|
+
): Promise<IOpLogEntry> {
|
|
62
|
+
const entry: IOpLogEntry = {
|
|
63
|
+
ts: this.generateTimestamp(),
|
|
64
|
+
op: 'i',
|
|
65
|
+
ns: `${dbName}.${collName}`,
|
|
66
|
+
o: document,
|
|
67
|
+
...txnInfo,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
await this.storage.appendOpLog(entry);
|
|
71
|
+
this.notifyListeners(entry);
|
|
72
|
+
return entry;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Record an update operation
|
|
77
|
+
*/
|
|
78
|
+
async recordUpdate(
|
|
79
|
+
dbName: string,
|
|
80
|
+
collName: string,
|
|
81
|
+
filter: Document,
|
|
82
|
+
update: Document,
|
|
83
|
+
txnInfo?: { txnNumber?: number; lsid?: { id: plugins.bson.Binary } }
|
|
84
|
+
): Promise<IOpLogEntry> {
|
|
85
|
+
const entry: IOpLogEntry = {
|
|
86
|
+
ts: this.generateTimestamp(),
|
|
87
|
+
op: 'u',
|
|
88
|
+
ns: `${dbName}.${collName}`,
|
|
89
|
+
o: update,
|
|
90
|
+
o2: filter,
|
|
91
|
+
...txnInfo,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
await this.storage.appendOpLog(entry);
|
|
95
|
+
this.notifyListeners(entry);
|
|
96
|
+
return entry;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Record a delete operation
|
|
101
|
+
*/
|
|
102
|
+
async recordDelete(
|
|
103
|
+
dbName: string,
|
|
104
|
+
collName: string,
|
|
105
|
+
filter: Document,
|
|
106
|
+
txnInfo?: { txnNumber?: number; lsid?: { id: plugins.bson.Binary } }
|
|
107
|
+
): Promise<IOpLogEntry> {
|
|
108
|
+
const entry: IOpLogEntry = {
|
|
109
|
+
ts: this.generateTimestamp(),
|
|
110
|
+
op: 'd',
|
|
111
|
+
ns: `${dbName}.${collName}`,
|
|
112
|
+
o: filter,
|
|
113
|
+
...txnInfo,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
await this.storage.appendOpLog(entry);
|
|
117
|
+
this.notifyListeners(entry);
|
|
118
|
+
return entry;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Record a command (drop, rename, etc.)
|
|
123
|
+
*/
|
|
124
|
+
async recordCommand(
|
|
125
|
+
dbName: string,
|
|
126
|
+
command: Document
|
|
127
|
+
): Promise<IOpLogEntry> {
|
|
128
|
+
const entry: IOpLogEntry = {
|
|
129
|
+
ts: this.generateTimestamp(),
|
|
130
|
+
op: 'c',
|
|
131
|
+
ns: `${dbName}.$cmd`,
|
|
132
|
+
o: command,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
await this.storage.appendOpLog(entry);
|
|
136
|
+
this.notifyListeners(entry);
|
|
137
|
+
return entry;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Get oplog entries after a timestamp
|
|
142
|
+
*/
|
|
143
|
+
async getEntriesAfter(ts: plugins.bson.Timestamp, limit?: number): Promise<IOpLogEntry[]> {
|
|
144
|
+
return this.storage.getOpLogAfter(ts, limit);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Get the latest timestamp
|
|
149
|
+
*/
|
|
150
|
+
async getLatestTimestamp(): Promise<plugins.bson.Timestamp | null> {
|
|
151
|
+
return this.storage.getLatestOpLogTimestamp();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Subscribe to oplog changes (for change streams)
|
|
156
|
+
*/
|
|
157
|
+
subscribe(listener: (entry: IOpLogEntry) => void): () => void {
|
|
158
|
+
this.listeners.push(listener);
|
|
159
|
+
return () => {
|
|
160
|
+
const idx = this.listeners.indexOf(listener);
|
|
161
|
+
if (idx >= 0) {
|
|
162
|
+
this.listeners.splice(idx, 1);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Notify all listeners of a new entry
|
|
169
|
+
*/
|
|
170
|
+
private notifyListeners(entry: IOpLogEntry): void {
|
|
171
|
+
for (const listener of this.listeners) {
|
|
172
|
+
try {
|
|
173
|
+
listener(entry);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.error('Error in oplog listener:', error);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Convert an oplog entry to a change stream document
|
|
182
|
+
*/
|
|
183
|
+
opLogEntryToChangeEvent(
|
|
184
|
+
entry: IOpLogEntry,
|
|
185
|
+
fullDocument?: Document,
|
|
186
|
+
fullDocumentBeforeChange?: Document
|
|
187
|
+
): {
|
|
188
|
+
_id: IResumeToken;
|
|
189
|
+
operationType: ChangeStreamOperationType;
|
|
190
|
+
fullDocument?: Document;
|
|
191
|
+
fullDocumentBeforeChange?: Document;
|
|
192
|
+
ns: { db: string; coll?: string };
|
|
193
|
+
documentKey?: { _id: plugins.bson.ObjectId };
|
|
194
|
+
updateDescription?: {
|
|
195
|
+
updatedFields?: Document;
|
|
196
|
+
removedFields?: string[];
|
|
197
|
+
};
|
|
198
|
+
clusterTime: plugins.bson.Timestamp;
|
|
199
|
+
} {
|
|
200
|
+
const [db, coll] = entry.ns.split('.');
|
|
201
|
+
const resumeToken = this.generateResumeToken(entry.ts);
|
|
202
|
+
|
|
203
|
+
const baseEvent = {
|
|
204
|
+
_id: resumeToken,
|
|
205
|
+
ns: { db, coll: coll === '$cmd' ? undefined : coll },
|
|
206
|
+
clusterTime: entry.ts,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
switch (entry.op) {
|
|
210
|
+
case 'i':
|
|
211
|
+
return {
|
|
212
|
+
...baseEvent,
|
|
213
|
+
operationType: 'insert' as ChangeStreamOperationType,
|
|
214
|
+
fullDocument: fullDocument || entry.o,
|
|
215
|
+
documentKey: entry.o._id ? { _id: entry.o._id } : undefined,
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
case 'u':
|
|
219
|
+
const updateEvent: any = {
|
|
220
|
+
...baseEvent,
|
|
221
|
+
operationType: 'update' as ChangeStreamOperationType,
|
|
222
|
+
documentKey: entry.o2?._id ? { _id: entry.o2._id } : undefined,
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
if (fullDocument) {
|
|
226
|
+
updateEvent.fullDocument = fullDocument;
|
|
227
|
+
}
|
|
228
|
+
if (fullDocumentBeforeChange) {
|
|
229
|
+
updateEvent.fullDocumentBeforeChange = fullDocumentBeforeChange;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Parse update description
|
|
233
|
+
if (entry.o.$set || entry.o.$unset) {
|
|
234
|
+
updateEvent.updateDescription = {
|
|
235
|
+
updatedFields: entry.o.$set || {},
|
|
236
|
+
removedFields: entry.o.$unset ? Object.keys(entry.o.$unset) : [],
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return updateEvent;
|
|
241
|
+
|
|
242
|
+
case 'd':
|
|
243
|
+
return {
|
|
244
|
+
...baseEvent,
|
|
245
|
+
operationType: 'delete' as ChangeStreamOperationType,
|
|
246
|
+
documentKey: entry.o._id ? { _id: entry.o._id } : undefined,
|
|
247
|
+
fullDocumentBeforeChange,
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
case 'c':
|
|
251
|
+
if (entry.o.drop) {
|
|
252
|
+
return {
|
|
253
|
+
...baseEvent,
|
|
254
|
+
operationType: 'drop' as ChangeStreamOperationType,
|
|
255
|
+
ns: { db, coll: entry.o.drop },
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
if (entry.o.dropDatabase) {
|
|
259
|
+
return {
|
|
260
|
+
...baseEvent,
|
|
261
|
+
operationType: 'dropDatabase' as ChangeStreamOperationType,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (entry.o.renameCollection) {
|
|
265
|
+
return {
|
|
266
|
+
...baseEvent,
|
|
267
|
+
operationType: 'rename' as ChangeStreamOperationType,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
...baseEvent,
|
|
272
|
+
operationType: 'invalidate' as ChangeStreamOperationType,
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
default:
|
|
276
|
+
return {
|
|
277
|
+
...baseEvent,
|
|
278
|
+
operationType: 'invalidate' as ChangeStreamOperationType,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import type * as plugins from '../congodb.plugins.js';
|
|
2
|
+
|
|
3
|
+
// ============================================================================
|
|
4
|
+
// Document Types
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
export type Document = Record<string, any>;
|
|
8
|
+
|
|
9
|
+
export interface WithId<TSchema> {
|
|
10
|
+
_id: plugins.bson.ObjectId;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Client Options
|
|
15
|
+
// ============================================================================
|
|
16
|
+
|
|
17
|
+
export interface ICongoClientOptions {
|
|
18
|
+
/** Storage adapter type: 'memory' or 'file' */
|
|
19
|
+
storageType?: 'memory' | 'file';
|
|
20
|
+
/** Path for file-based storage */
|
|
21
|
+
storagePath?: string;
|
|
22
|
+
/** Enable persistence for memory adapter */
|
|
23
|
+
persist?: boolean;
|
|
24
|
+
/** Path for persistence file when using memory adapter */
|
|
25
|
+
persistPath?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ============================================================================
|
|
29
|
+
// Connection String Parsing
|
|
30
|
+
// ============================================================================
|
|
31
|
+
|
|
32
|
+
export interface IParsedConnectionString {
|
|
33
|
+
protocol: 'congo';
|
|
34
|
+
storageType: 'memory' | 'file';
|
|
35
|
+
options: {
|
|
36
|
+
persist?: string;
|
|
37
|
+
path?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// CRUD Operation Options
|
|
43
|
+
// ============================================================================
|
|
44
|
+
|
|
45
|
+
export interface IInsertOneOptions {
|
|
46
|
+
/** Session for transaction support */
|
|
47
|
+
session?: IClientSession;
|
|
48
|
+
/** Custom write concern */
|
|
49
|
+
writeConcern?: IWriteConcern;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface IInsertManyOptions extends IInsertOneOptions {
|
|
53
|
+
/** If true, inserts are ordered and stop on first error */
|
|
54
|
+
ordered?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface IFindOptions<TSchema = Document> {
|
|
58
|
+
/** Projection to apply */
|
|
59
|
+
projection?: Partial<Record<keyof TSchema | string, 0 | 1 | boolean>>;
|
|
60
|
+
/** Sort specification */
|
|
61
|
+
sort?: ISortSpecification;
|
|
62
|
+
/** Number of documents to skip */
|
|
63
|
+
skip?: number;
|
|
64
|
+
/** Maximum number of documents to return */
|
|
65
|
+
limit?: number;
|
|
66
|
+
/** Session for transaction support */
|
|
67
|
+
session?: IClientSession;
|
|
68
|
+
/** Hint for index usage */
|
|
69
|
+
hint?: string | Document;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface IUpdateOptions {
|
|
73
|
+
/** Create document if it doesn't exist */
|
|
74
|
+
upsert?: boolean;
|
|
75
|
+
/** Session for transaction support */
|
|
76
|
+
session?: IClientSession;
|
|
77
|
+
/** Array filters for positional updates */
|
|
78
|
+
arrayFilters?: Document[];
|
|
79
|
+
/** Custom write concern */
|
|
80
|
+
writeConcern?: IWriteConcern;
|
|
81
|
+
/** Hint for index usage */
|
|
82
|
+
hint?: string | Document;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface IReplaceOptions extends IUpdateOptions {}
|
|
86
|
+
|
|
87
|
+
export interface IDeleteOptions {
|
|
88
|
+
/** Session for transaction support */
|
|
89
|
+
session?: IClientSession;
|
|
90
|
+
/** Custom write concern */
|
|
91
|
+
writeConcern?: IWriteConcern;
|
|
92
|
+
/** Hint for index usage */
|
|
93
|
+
hint?: string | Document;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface IFindOneAndUpdateOptions extends IUpdateOptions {
|
|
97
|
+
/** Return the document before or after the update */
|
|
98
|
+
returnDocument?: 'before' | 'after';
|
|
99
|
+
/** Projection to apply */
|
|
100
|
+
projection?: Document;
|
|
101
|
+
/** Sort specification to determine which document to modify */
|
|
102
|
+
sort?: ISortSpecification;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface IFindOneAndReplaceOptions extends IFindOneAndUpdateOptions {}
|
|
106
|
+
|
|
107
|
+
export interface IFindOneAndDeleteOptions {
|
|
108
|
+
/** Projection to apply */
|
|
109
|
+
projection?: Document;
|
|
110
|
+
/** Sort specification to determine which document to delete */
|
|
111
|
+
sort?: ISortSpecification;
|
|
112
|
+
/** Session for transaction support */
|
|
113
|
+
session?: IClientSession;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ============================================================================
|
|
117
|
+
// CRUD Results
|
|
118
|
+
// ============================================================================
|
|
119
|
+
|
|
120
|
+
export interface IInsertOneResult {
|
|
121
|
+
acknowledged: boolean;
|
|
122
|
+
insertedId: plugins.bson.ObjectId;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface IInsertManyResult {
|
|
126
|
+
acknowledged: boolean;
|
|
127
|
+
insertedCount: number;
|
|
128
|
+
insertedIds: Record<number, plugins.bson.ObjectId>;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface IUpdateResult {
|
|
132
|
+
acknowledged: boolean;
|
|
133
|
+
matchedCount: number;
|
|
134
|
+
modifiedCount: number;
|
|
135
|
+
upsertedCount: number;
|
|
136
|
+
upsertedId: plugins.bson.ObjectId | null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface IDeleteResult {
|
|
140
|
+
acknowledged: boolean;
|
|
141
|
+
deletedCount: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface IModifyResult<TSchema> {
|
|
145
|
+
value: TSchema | null;
|
|
146
|
+
ok: 1 | 0;
|
|
147
|
+
lastErrorObject?: {
|
|
148
|
+
n: number;
|
|
149
|
+
updatedExisting?: boolean;
|
|
150
|
+
upserted?: plugins.bson.ObjectId;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ============================================================================
|
|
155
|
+
// Sort and Index Types
|
|
156
|
+
// ============================================================================
|
|
157
|
+
|
|
158
|
+
export type ISortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending';
|
|
159
|
+
|
|
160
|
+
export type ISortSpecification = Record<string, ISortDirection> | [string, ISortDirection][];
|
|
161
|
+
|
|
162
|
+
export interface IIndexSpecification {
|
|
163
|
+
key: Record<string, 1 | -1 | 'text' | '2dsphere'>;
|
|
164
|
+
name?: string;
|
|
165
|
+
unique?: boolean;
|
|
166
|
+
sparse?: boolean;
|
|
167
|
+
expireAfterSeconds?: number;
|
|
168
|
+
background?: boolean;
|
|
169
|
+
partialFilterExpression?: Document;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface IIndexInfo {
|
|
173
|
+
v: number;
|
|
174
|
+
key: Record<string, 1 | -1 | string>;
|
|
175
|
+
name: string;
|
|
176
|
+
unique?: boolean;
|
|
177
|
+
sparse?: boolean;
|
|
178
|
+
expireAfterSeconds?: number;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface ICreateIndexOptions {
|
|
182
|
+
unique?: boolean;
|
|
183
|
+
sparse?: boolean;
|
|
184
|
+
expireAfterSeconds?: number;
|
|
185
|
+
name?: string;
|
|
186
|
+
background?: boolean;
|
|
187
|
+
partialFilterExpression?: Document;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ============================================================================
|
|
191
|
+
// Write Concern
|
|
192
|
+
// ============================================================================
|
|
193
|
+
|
|
194
|
+
export interface IWriteConcern {
|
|
195
|
+
w?: number | 'majority';
|
|
196
|
+
j?: boolean;
|
|
197
|
+
wtimeout?: number;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ============================================================================
|
|
201
|
+
// Aggregation Types
|
|
202
|
+
// ============================================================================
|
|
203
|
+
|
|
204
|
+
export interface IAggregateOptions {
|
|
205
|
+
/** Allow disk use for large aggregations */
|
|
206
|
+
allowDiskUse?: boolean;
|
|
207
|
+
/** Maximum time in ms */
|
|
208
|
+
maxTimeMS?: number;
|
|
209
|
+
/** Session for transaction support */
|
|
210
|
+
session?: IClientSession;
|
|
211
|
+
/** Batch size for cursor */
|
|
212
|
+
batchSize?: number;
|
|
213
|
+
/** Collation settings */
|
|
214
|
+
collation?: ICollation;
|
|
215
|
+
/** Hint for index usage */
|
|
216
|
+
hint?: string | Document;
|
|
217
|
+
/** Comment for profiling */
|
|
218
|
+
comment?: string;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface ICollation {
|
|
222
|
+
locale: string;
|
|
223
|
+
caseLevel?: boolean;
|
|
224
|
+
caseFirst?: string;
|
|
225
|
+
strength?: number;
|
|
226
|
+
numericOrdering?: boolean;
|
|
227
|
+
alternate?: string;
|
|
228
|
+
maxVariable?: string;
|
|
229
|
+
backwards?: boolean;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// ============================================================================
|
|
233
|
+
// Change Stream Types
|
|
234
|
+
// ============================================================================
|
|
235
|
+
|
|
236
|
+
export interface IChangeStreamOptions {
|
|
237
|
+
/** Resume after this token */
|
|
238
|
+
resumeAfter?: IResumeToken;
|
|
239
|
+
/** Start at this operation time */
|
|
240
|
+
startAtOperationTime?: plugins.bson.Timestamp;
|
|
241
|
+
/** Start after this token */
|
|
242
|
+
startAfter?: IResumeToken;
|
|
243
|
+
/** Full document lookup mode */
|
|
244
|
+
fullDocument?: 'default' | 'updateLookup' | 'whenAvailable' | 'required';
|
|
245
|
+
/** Full document before change */
|
|
246
|
+
fullDocumentBeforeChange?: 'off' | 'whenAvailable' | 'required';
|
|
247
|
+
/** Batch size */
|
|
248
|
+
batchSize?: number;
|
|
249
|
+
/** Maximum await time in ms */
|
|
250
|
+
maxAwaitTimeMS?: number;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface IResumeToken {
|
|
254
|
+
_data: string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export type ChangeStreamOperationType =
|
|
258
|
+
| 'insert'
|
|
259
|
+
| 'update'
|
|
260
|
+
| 'replace'
|
|
261
|
+
| 'delete'
|
|
262
|
+
| 'drop'
|
|
263
|
+
| 'rename'
|
|
264
|
+
| 'dropDatabase'
|
|
265
|
+
| 'invalidate';
|
|
266
|
+
|
|
267
|
+
export interface IChangeStreamDocument<TSchema = Document> {
|
|
268
|
+
_id: IResumeToken;
|
|
269
|
+
operationType: ChangeStreamOperationType;
|
|
270
|
+
fullDocument?: TSchema;
|
|
271
|
+
fullDocumentBeforeChange?: TSchema;
|
|
272
|
+
ns: {
|
|
273
|
+
db: string;
|
|
274
|
+
coll?: string;
|
|
275
|
+
};
|
|
276
|
+
documentKey?: { _id: plugins.bson.ObjectId };
|
|
277
|
+
updateDescription?: {
|
|
278
|
+
updatedFields?: Document;
|
|
279
|
+
removedFields?: string[];
|
|
280
|
+
truncatedArrays?: Array<{ field: string; newSize: number }>;
|
|
281
|
+
};
|
|
282
|
+
clusterTime?: plugins.bson.Timestamp;
|
|
283
|
+
txnNumber?: number;
|
|
284
|
+
lsid?: { id: plugins.bson.Binary; uid: plugins.bson.Binary };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ============================================================================
|
|
288
|
+
// Transaction Types
|
|
289
|
+
// ============================================================================
|
|
290
|
+
|
|
291
|
+
export interface IClientSession {
|
|
292
|
+
id: { id: plugins.bson.Binary };
|
|
293
|
+
inTransaction(): boolean;
|
|
294
|
+
startTransaction(options?: ITransactionOptions): void;
|
|
295
|
+
commitTransaction(): Promise<void>;
|
|
296
|
+
abortTransaction(): Promise<void>;
|
|
297
|
+
withTransaction<T>(fn: () => Promise<T>, options?: ITransactionOptions): Promise<T>;
|
|
298
|
+
endSession(): Promise<void>;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export interface ITransactionOptions {
|
|
302
|
+
readConcern?: IReadConcern;
|
|
303
|
+
writeConcern?: IWriteConcern;
|
|
304
|
+
readPreference?: string;
|
|
305
|
+
maxCommitTimeMS?: number;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface IReadConcern {
|
|
309
|
+
level: 'local' | 'available' | 'majority' | 'linearizable' | 'snapshot';
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ============================================================================
|
|
313
|
+
// Bulk Operation Types
|
|
314
|
+
// ============================================================================
|
|
315
|
+
|
|
316
|
+
export interface IBulkWriteOptions {
|
|
317
|
+
ordered?: boolean;
|
|
318
|
+
session?: IClientSession;
|
|
319
|
+
writeConcern?: IWriteConcern;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface IBulkWriteOperation<TSchema = Document> {
|
|
323
|
+
insertOne?: { document: TSchema };
|
|
324
|
+
updateOne?: { filter: Document; update: Document; upsert?: boolean; arrayFilters?: Document[]; hint?: Document | string };
|
|
325
|
+
updateMany?: { filter: Document; update: Document; upsert?: boolean; arrayFilters?: Document[]; hint?: Document | string };
|
|
326
|
+
replaceOne?: { filter: Document; replacement: TSchema; upsert?: boolean; hint?: Document | string };
|
|
327
|
+
deleteOne?: { filter: Document; hint?: Document | string };
|
|
328
|
+
deleteMany?: { filter: Document; hint?: Document | string };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export interface IBulkWriteResult {
|
|
332
|
+
acknowledged: boolean;
|
|
333
|
+
insertedCount: number;
|
|
334
|
+
matchedCount: number;
|
|
335
|
+
modifiedCount: number;
|
|
336
|
+
deletedCount: number;
|
|
337
|
+
upsertedCount: number;
|
|
338
|
+
insertedIds: Record<number, plugins.bson.ObjectId>;
|
|
339
|
+
upsertedIds: Record<number, plugins.bson.ObjectId>;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ============================================================================
|
|
343
|
+
// Storage Types
|
|
344
|
+
// ============================================================================
|
|
345
|
+
|
|
346
|
+
export interface IStoredDocument extends Document {
|
|
347
|
+
_id: plugins.bson.ObjectId;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export interface IOpLogEntry {
|
|
351
|
+
ts: plugins.bson.Timestamp;
|
|
352
|
+
op: 'i' | 'u' | 'd' | 'c' | 'n';
|
|
353
|
+
ns: string;
|
|
354
|
+
o: Document;
|
|
355
|
+
o2?: Document;
|
|
356
|
+
txnNumber?: number;
|
|
357
|
+
lsid?: { id: plugins.bson.Binary };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// ============================================================================
|
|
361
|
+
// Admin Types
|
|
362
|
+
// ============================================================================
|
|
363
|
+
|
|
364
|
+
export interface IDatabaseInfo {
|
|
365
|
+
name: string;
|
|
366
|
+
sizeOnDisk: number;
|
|
367
|
+
empty: boolean;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export interface ICollectionInfo {
|
|
371
|
+
name: string;
|
|
372
|
+
type: 'collection' | 'view';
|
|
373
|
+
options: Document;
|
|
374
|
+
info: {
|
|
375
|
+
readOnly: boolean;
|
|
376
|
+
uuid?: plugins.bson.Binary;
|
|
377
|
+
};
|
|
378
|
+
idIndex?: IIndexInfo;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export interface IServerStatus {
|
|
382
|
+
host: string;
|
|
383
|
+
version: string;
|
|
384
|
+
process: string;
|
|
385
|
+
pid: number;
|
|
386
|
+
uptime: number;
|
|
387
|
+
uptimeMillis: number;
|
|
388
|
+
uptimeEstimate: number;
|
|
389
|
+
localTime: Date;
|
|
390
|
+
mem: {
|
|
391
|
+
resident: number;
|
|
392
|
+
virtual: number;
|
|
393
|
+
};
|
|
394
|
+
connections: {
|
|
395
|
+
current: number;
|
|
396
|
+
available: number;
|
|
397
|
+
totalCreated: number;
|
|
398
|
+
};
|
|
399
|
+
ok: 1;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export interface ICollectionStats {
|
|
403
|
+
ns: string;
|
|
404
|
+
count: number;
|
|
405
|
+
size: number;
|
|
406
|
+
avgObjSize: number;
|
|
407
|
+
storageSize: number;
|
|
408
|
+
totalIndexSize: number;
|
|
409
|
+
indexSizes: Record<string, number>;
|
|
410
|
+
nindexes: number;
|
|
411
|
+
ok: 1;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ============================================================================
|
|
415
|
+
// Count Types
|
|
416
|
+
// ============================================================================
|
|
417
|
+
|
|
418
|
+
export interface ICountDocumentsOptions {
|
|
419
|
+
skip?: number;
|
|
420
|
+
limit?: number;
|
|
421
|
+
session?: IClientSession;
|
|
422
|
+
hint?: string | Document;
|
|
423
|
+
maxTimeMS?: number;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export interface IEstimatedDocumentCountOptions {
|
|
427
|
+
maxTimeMS?: number;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export interface IDistinctOptions {
|
|
431
|
+
session?: IClientSession;
|
|
432
|
+
maxTimeMS?: number;
|
|
433
|
+
}
|
package/ts/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { commitinfo } from './00_commitinfo_data.js';
|
|
2
2
|
import * as plugins from './smartmongo.plugins.js';
|
|
3
3
|
|
|
4
|
+
// Export CongoDB module
|
|
5
|
+
export * as congodb from './congodb/index.js';
|
|
6
|
+
|
|
4
7
|
export class SmartMongo {
|
|
5
8
|
// STATIC
|
|
6
9
|
public static async createAndStart(replCountArg: number = 1) {
|