@semics-tech/mongolite 1.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 (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +179 -0
  3. package/dist/adapters/browser.d.ts +97 -0
  4. package/dist/adapters/browser.js +103 -0
  5. package/dist/adapters/browser.js.map +1 -0
  6. package/dist/adapters/cloudflare.d.ts +106 -0
  7. package/dist/adapters/cloudflare.js +94 -0
  8. package/dist/adapters/cloudflare.js.map +1 -0
  9. package/dist/bin/mongolite-debug.d.ts +2 -0
  10. package/dist/bin/mongolite-debug.js +115 -0
  11. package/dist/bin/mongolite-debug.js.map +1 -0
  12. package/dist/changeStream.d.ts +114 -0
  13. package/dist/changeStream.js +338 -0
  14. package/dist/changeStream.js.map +1 -0
  15. package/dist/cloudflare.d.ts +39 -0
  16. package/dist/cloudflare.js +39 -0
  17. package/dist/cloudflare.js.map +1 -0
  18. package/dist/collection.d.ts +274 -0
  19. package/dist/collection.js +1706 -0
  20. package/dist/collection.js.map +1 -0
  21. package/dist/cursors/findCursor.d.ts +101 -0
  22. package/dist/cursors/findCursor.js +757 -0
  23. package/dist/cursors/findCursor.js.map +1 -0
  24. package/dist/db.d.ts +116 -0
  25. package/dist/db.js +254 -0
  26. package/dist/db.js.map +1 -0
  27. package/dist/debugger/query-debugger.d.ts +22 -0
  28. package/dist/debugger/query-debugger.js +258 -0
  29. package/dist/debugger/query-debugger.js.map +1 -0
  30. package/dist/index.d.ts +30 -0
  31. package/dist/index.js +40 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/mongo-client.d.ts +56 -0
  34. package/dist/mongo-client.js +55 -0
  35. package/dist/mongo-client.js.map +1 -0
  36. package/dist/types.d.ts +318 -0
  37. package/dist/types.js +2 -0
  38. package/dist/types.js.map +1 -0
  39. package/dist/utils/indexing.d.ts +4 -0
  40. package/dist/utils/indexing.js +43 -0
  41. package/dist/utils/indexing.js.map +1 -0
  42. package/dist/utils/projection.d.ts +16 -0
  43. package/dist/utils/projection.js +98 -0
  44. package/dist/utils/projection.js.map +1 -0
  45. package/dist/utils/queryOperators.d.ts +16 -0
  46. package/dist/utils/queryOperators.js +138 -0
  47. package/dist/utils/queryOperators.js.map +1 -0
  48. package/package.json +92 -0
@@ -0,0 +1,274 @@
1
+ import type { IDatabaseAdapter } from './db.js';
2
+ import { DocumentWithId, Filter, UpdateFilter, InsertOneResult, InsertManyResult, UpdateResult, DeleteResult, Projection, IndexSpecification, CreateIndexOptions, CreateIndexResult, DropIndexResult, IndexInfo, FindOneAndUpdateOptions, FindOneAndDeleteOptions, FindOneAndReplaceOptions, ReplaceOptions, AggregationPipeline } from './types.js';
3
+ import { FindCursor } from './cursors/findCursor.js';
4
+ import { ChangeStream, ChangeStreamOptions } from './changeStream.js';
5
+ /**
6
+ * MongoLiteCollection provides methods to interact with a specific SQLite table
7
+ * as if it were a MongoDB collection.
8
+ */
9
+ export declare class MongoLiteCollection<T extends DocumentWithId> {
10
+ private db;
11
+ readonly name: string;
12
+ private readonly options;
13
+ constructor(db: IDatabaseAdapter, name: string, options?: {
14
+ verbose?: boolean;
15
+ });
16
+ /**
17
+ * Ensures the SQLite table for this collection exists, creating it if necessary.
18
+ * The table will have an `_id` column (indexed) and a `data` column for JSON.
19
+ * @private
20
+ */
21
+ ensureTable(): Promise<void>;
22
+ /**
23
+ * Inserts a single document into the collection.
24
+ * If `_id` is not provided, a UUID will be generated.
25
+ * @param doc The document to insert.
26
+ * @returns {Promise<InsertOneResult>} An object containing the outcome of the insert operation.
27
+ */
28
+ insertOne(doc: Omit<T, '_id'> & {
29
+ _id?: string;
30
+ }): Promise<InsertOneResult>;
31
+ /**
32
+ * Inserts multiple documents into the collection.
33
+ * If any document does not have an `_id`, a UUID will be generated.
34
+ * Uses batch insert with transactions for improved performance.
35
+ * @param docs An array of documents to insert.
36
+ * @returns {Promise<InsertManyResult>} An object containing the outcome of all insert operations.
37
+ * */
38
+ insertMany(docs: (Omit<T, '_id'> & {
39
+ _id?: string;
40
+ })[]): Promise<InsertManyResult>;
41
+ /**
42
+ * Inserts a batch of documents using a single transaction for optimal performance.
43
+ * @private
44
+ */
45
+ private insertBatch;
46
+ /**
47
+ * Finds a single document matching the filter.
48
+ * @param filter The query criteria.
49
+ * @param projection Optional. Specifies the fields to return.
50
+ * @returns {Promise<T | null>} The found document or `null`.
51
+ * @remarks When using a projection, some fields may be undefined at runtime despite the
52
+ * return type being `T`. This provides better ergonomics for the common case where no
53
+ * projection is used.
54
+ */
55
+ findOne(filter: Filter<T>, projection?: Projection<T>): Promise<T | null>;
56
+ /**
57
+ * Finds multiple documents matching the filter and returns a cursor.
58
+ * @param filter The query criteria.
59
+ * @returns {FindCursor<T>} A `FindCursor` instance.
60
+ */
61
+ find(filter?: Filter<T>): FindCursor<T>;
62
+ /**
63
+ * Updates a single document matching the filter.
64
+ * @param filter The selection criteria for the update.
65
+ * @param update The modifications to apply.
66
+ * @returns {Promise<UpdateResult>} An object describing the outcome.
67
+ */
68
+ updateOne(filter: Filter<T>, update: UpdateFilter<T>, options?: {
69
+ upsert?: boolean;
70
+ }): Promise<UpdateResult>;
71
+ /**
72
+ * Updates multiple documents matching the filter.
73
+ * @param filter The selection criteria for the update.
74
+ * @param update The modifications to apply.
75
+ * @returns {Promise<UpdateResult>} An object describing the outcome.
76
+ */
77
+ updateMany(filter: Filter<T>, update: UpdateFilter<T>): Promise<UpdateResult>;
78
+ /**
79
+ * Creates an index on the specified field(s) of the collection.
80
+ *
81
+ * @param fieldOrSpec The field or specification to create the index on.
82
+ * @param options Options for creating the index.
83
+ * @returns A promise that resolves to the result of the operation.
84
+ *
85
+ * @example
86
+ * // Create a simple index on the "name" field
87
+ * collection.createIndex({ name: 1 });
88
+ *
89
+ * // Create a unique index on the "email" field
90
+ * collection.createIndex({ email: 1 }, { unique: true });
91
+ *
92
+ * // Create a compound index on multiple fields
93
+ * collection.createIndex({ name: 1, age: -1 });
94
+ */
95
+ createIndex(fieldOrSpec: string | IndexSpecification, options?: CreateIndexOptions): Promise<CreateIndexResult>;
96
+ /**
97
+ * Generate a MongoDB-like index name from an index specification.
98
+ * Format: collectionName_field1_1_field2_-1
99
+ */
100
+ private generateIndexName;
101
+ /**
102
+ * Lists all indexes on the collection.
103
+ *
104
+ * @returns A promise that resolves to an object with a toArray method
105
+ * that returns the list of indexes.
106
+ */
107
+ listIndexes(): {
108
+ toArray: () => Promise<IndexInfo[]>;
109
+ };
110
+ /**
111
+ * Drops a specified index from the collection.
112
+ *
113
+ * @param indexName The name of the index to drop.
114
+ * @returns A promise that resolves to the result of the operation.
115
+ */
116
+ dropIndex(indexName: string): Promise<DropIndexResult>;
117
+ /**
118
+ * Drops all indexes from the collection, except for the index on _id.
119
+ *
120
+ * @returns A promise that resolves to the result of the operation.
121
+ */
122
+ dropIndexes(): Promise<{
123
+ acknowledged: boolean;
124
+ droppedCount: number;
125
+ }>;
126
+ /**
127
+ * Prepends '$.' to the path for SQLite JSON functions.
128
+ * @private
129
+ * @param path Field path, e.g., 'address.city'
130
+ * @returns JSON path string for SQLite, e.g., '$.address.city'
131
+ */
132
+ private parseJsonPath;
133
+ private setNestedValue;
134
+ private unsetNestedValue;
135
+ private getNestedValue;
136
+ /**
137
+ * Deletes a single document matching the filter.
138
+ * @param filter The criteria to select the document to delete.
139
+ * @returns {Promise<DeleteResult>} An object describing the outcome.
140
+ */
141
+ deleteOne(filter: Filter<T>): Promise<DeleteResult>;
142
+ /**
143
+ * Deletes multiple documents matching the filter.
144
+ * @param filter The criteria to select documents to delete.
145
+ * @returns {Promise<DeleteResult>} An object describing the outcome.
146
+ */
147
+ deleteMany(filter: Filter<T>): Promise<DeleteResult>;
148
+ /**
149
+ * Counts the number of documents matching the filter.
150
+ * @param filter The criteria to select documents to count.
151
+ * @returns {Promise<number>} The count of matching documents.
152
+ */
153
+ countDocuments(filter?: Filter<T>): Promise<number>;
154
+ /**
155
+ * Returns the number of documents in the collection.
156
+ * This is an estimate and may not reflect the exact count in concurrent environments.
157
+ * @returns {Promise<number>} The estimated count.
158
+ */
159
+ estimatedDocumentCount(): Promise<number>;
160
+ /**
161
+ * Finds a single document matching the filter, applies the update, and returns the document.
162
+ * @param filter The selection criteria.
163
+ * @param update The modifications to apply.
164
+ * @param options Options including returnDocument ('before'|'after') and upsert.
165
+ * @returns {Promise<T | null>} The document before or after the update, or null.
166
+ */
167
+ findOneAndUpdate(filter: Filter<T>, update: UpdateFilter<T>, options?: FindOneAndUpdateOptions<T>): Promise<T | null>;
168
+ /**
169
+ * Finds a single document matching the filter, deletes it, and returns it.
170
+ * @param filter The selection criteria.
171
+ * @param options Options including field projection.
172
+ * @returns {Promise<T | null>} The deleted document or null.
173
+ */
174
+ findOneAndDelete(filter: Filter<T>, options?: FindOneAndDeleteOptions<T>): Promise<T | null>;
175
+ /**
176
+ * Finds a single document matching the filter and replaces it entirely.
177
+ * @param filter The selection criteria.
178
+ * @param replacement The replacement document (replaces all fields except _id).
179
+ * @param options Options including returnDocument, upsert, and projection.
180
+ * @returns {Promise<T | null>} The document before or after the replacement, or null.
181
+ */
182
+ findOneAndReplace(filter: Filter<T>, replacement: Omit<T, '_id'>, options?: FindOneAndReplaceOptions<T>): Promise<T | null>;
183
+ /**
184
+ * Replaces a single document matching the filter.
185
+ * @param filter The selection criteria.
186
+ * @param replacement The replacement document (replaces all fields except _id).
187
+ * @param options Options including upsert.
188
+ * @returns {Promise<UpdateResult>} An object describing the outcome.
189
+ */
190
+ replaceOne(filter: Filter<T>, replacement: Omit<T, '_id'>, options?: ReplaceOptions): Promise<UpdateResult>;
191
+ /**
192
+ * Returns distinct values for a field across all documents matching the filter.
193
+ * @param field The field to find distinct values for.
194
+ * @param filter Optional filter to narrow the documents.
195
+ * @returns {Promise<unknown[]>} An array of distinct values.
196
+ */
197
+ distinct(field: string, filter?: Filter<T>): Promise<unknown[]>;
198
+ /**
199
+ * Drops the entire collection (table) from the database.
200
+ * @returns {Promise<void>}
201
+ */
202
+ drop(): Promise<void>;
203
+ /**
204
+ * Executes an aggregation pipeline on the collection.
205
+ * Supports: $match, $project, $sort, $limit, $skip, $count, $group, $unwind, $addFields.
206
+ * @param pipeline An array of pipeline stage documents.
207
+ * @returns An object with a toArray() method that returns the aggregation result.
208
+ */
209
+ aggregate(pipeline: AggregationPipeline): {
210
+ toArray: () => Promise<Record<string, unknown>[]>;
211
+ };
212
+ /**
213
+ * Runs an aggregation pipeline and returns results.
214
+ * @private
215
+ */
216
+ private runAggregationPipeline;
217
+ /**
218
+ * Applies a $group pipeline stage.
219
+ * @private
220
+ */
221
+ private applyGroupStage;
222
+ /**
223
+ * Applies a $project stage to a document.
224
+ * @private
225
+ */
226
+ private applyAggregateProjection;
227
+ /**
228
+ * Checks if a document matches a filter (for in-memory aggregation stages).
229
+ * Uses the same logic as FindCursor but applied in JavaScript.
230
+ * @private
231
+ */
232
+ private matchesFilter;
233
+ /**
234
+ * Checks if a value matches a set of query operators (for in-memory filtering).
235
+ * @private
236
+ */
237
+ private matchesOperators;
238
+ /**
239
+ * Opens a change stream to watch for changes on this collection.
240
+ * Returns a ChangeStream that emits events when documents are inserted, updated, or deleted.
241
+ *
242
+ * @param options Options for the change stream
243
+ * @returns A ChangeStream instance
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * const changeStream = collection.watch();
248
+ *
249
+ * changeStream.on('change', (change) => {
250
+ * console.log('Change detected:', change);
251
+ * });
252
+ *
253
+ * // Or use async iteration
254
+ * for await (const change of changeStream) {
255
+ * console.log('Change detected:', change);
256
+ * }
257
+ *
258
+ * // Close the change stream when done
259
+ * changeStream.close();
260
+ * ```
261
+ */
262
+ watch(options?: ChangeStreamOptions<T>): ChangeStream<T>;
263
+ /**
264
+ * Retries an operation with exponential backoff when SQLITE_BUSY errors occur.
265
+ * @param operation The operation function to retry.
266
+ * @param operationDescription A description of the operation for logging purposes.
267
+ * @param maxRetries The maximum number of retry attempts (default: 5).
268
+ * @param initialDelayMs The initial delay before the first retry in milliseconds (default: 100).
269
+ * @param maxDelayMs The maximum delay between retries in milliseconds (default: 10000).
270
+ * @returns The result of the operation once successful.
271
+ * @private
272
+ */
273
+ private retryWithBackoff;
274
+ }