fire2mongo 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.
- package/README.md +651 -0
- package/dist/index.d.mts +317 -0
- package/dist/index.d.ts +317 -0
- package/dist/index.js +576 -0
- package/dist/index.mjs +518 -0
- package/package.json +42 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { Db, Collection } from 'mongodb';
|
|
2
|
+
|
|
3
|
+
interface ConnectionConfig {
|
|
4
|
+
uri: string;
|
|
5
|
+
dbName: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Initialize the MongoDB connection. Call once at app startup.
|
|
9
|
+
* Safe to call multiple times — returns cached instance after first call.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* await initMongoDB({ uri: process.env.MONGODB_URI!, dbName: process.env.MONGODB_DB! });
|
|
13
|
+
*/
|
|
14
|
+
declare function initMongoDB(config: ConnectionConfig): Promise<Db>;
|
|
15
|
+
/**
|
|
16
|
+
* Get the cached Db instance.
|
|
17
|
+
* Throws if initMongoDB() has not been called.
|
|
18
|
+
*/
|
|
19
|
+
declare function getDb(): Db;
|
|
20
|
+
/**
|
|
21
|
+
* Close the MongoDB connection. Useful for graceful shutdown or tests.
|
|
22
|
+
*/
|
|
23
|
+
declare function closeMongoDB(): Promise<void>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Register a single Firebase collection name → MongoDB collection name.
|
|
27
|
+
* If mongoCollection is omitted, uses the same name as firebaseName.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* registerCollection('users');
|
|
31
|
+
* registerCollection('items', { mongoCollection: 'inventory_items' });
|
|
32
|
+
*/
|
|
33
|
+
declare function registerCollection(firebaseName: string, options?: {
|
|
34
|
+
mongoCollection?: string;
|
|
35
|
+
}): void;
|
|
36
|
+
/**
|
|
37
|
+
* Register multiple collections at once.
|
|
38
|
+
* Key = Firebase collection name, Value = MongoDB collection name.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* registerCollections({
|
|
42
|
+
* users: 'users',
|
|
43
|
+
* products: 'inventory_items',
|
|
44
|
+
* });
|
|
45
|
+
*/
|
|
46
|
+
declare function registerCollections(map: Record<string, string>): void;
|
|
47
|
+
/**
|
|
48
|
+
* Returns true if the collection is registered.
|
|
49
|
+
*/
|
|
50
|
+
declare function hasCollection(firebaseName: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves a Firebase collection name to a live MongoDB Collection.
|
|
53
|
+
* Throws with a helpful message if the collection is not registered.
|
|
54
|
+
*/
|
|
55
|
+
declare function getCollection(firebaseName: string): Collection;
|
|
56
|
+
/**
|
|
57
|
+
* Clear all registered collections. Useful for tests.
|
|
58
|
+
*/
|
|
59
|
+
declare function clearRegistry(): void;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* TypeScript interfaces mirroring the Firebase Firestore public API
|
|
63
|
+
*/
|
|
64
|
+
interface DocumentReference<T = any> {
|
|
65
|
+
id: string;
|
|
66
|
+
path: string;
|
|
67
|
+
collection: string;
|
|
68
|
+
_parentPath?: string;
|
|
69
|
+
}
|
|
70
|
+
interface CollectionReference<T = any> {
|
|
71
|
+
id: string;
|
|
72
|
+
path: string;
|
|
73
|
+
_mongoCollection: string;
|
|
74
|
+
_parentPath?: string;
|
|
75
|
+
}
|
|
76
|
+
interface Query<T = any> {
|
|
77
|
+
_collectionRef: CollectionReference<T>;
|
|
78
|
+
_filters: QueryConstraint[];
|
|
79
|
+
}
|
|
80
|
+
interface DocumentSnapshot<T = any> {
|
|
81
|
+
id: string;
|
|
82
|
+
exists(): boolean;
|
|
83
|
+
data(): T | undefined;
|
|
84
|
+
}
|
|
85
|
+
interface QueryDocumentSnapshot<T = any> {
|
|
86
|
+
id: string;
|
|
87
|
+
exists(): boolean;
|
|
88
|
+
data(): T;
|
|
89
|
+
}
|
|
90
|
+
interface QuerySnapshot<T = any> {
|
|
91
|
+
docs: QueryDocumentSnapshot<T>[];
|
|
92
|
+
size: number;
|
|
93
|
+
empty: boolean;
|
|
94
|
+
forEach(callback: (doc: QueryDocumentSnapshot<T>) => void): void;
|
|
95
|
+
}
|
|
96
|
+
type WhereFilterOp = '==' | '!=' | '<' | '<=' | '>' | '>=' | 'array-contains' | 'in' | 'not-in' | 'array-contains-any';
|
|
97
|
+
type OrderByDirection = 'asc' | 'desc';
|
|
98
|
+
interface QueryConstraint {
|
|
99
|
+
type: 'where' | 'orderBy' | 'limit' | 'offset' | 'and' | 'or';
|
|
100
|
+
field?: string;
|
|
101
|
+
operator?: WhereFilterOp;
|
|
102
|
+
value?: any;
|
|
103
|
+
direction?: OrderByDirection;
|
|
104
|
+
count?: number;
|
|
105
|
+
queries?: QueryConstraint[];
|
|
106
|
+
}
|
|
107
|
+
interface SetOptions {
|
|
108
|
+
merge?: boolean;
|
|
109
|
+
mergeFields?: string[];
|
|
110
|
+
}
|
|
111
|
+
type WithId<T> = T & {
|
|
112
|
+
id: string;
|
|
113
|
+
};
|
|
114
|
+
type WithOptionalId<T> = T & {
|
|
115
|
+
id?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type DocumentData = {
|
|
119
|
+
[field: string]: any;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Get a document reference.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* doc(db, 'users', userId)
|
|
126
|
+
* doc(db, 'orders', orderId, 'items', itemId) // subcollection
|
|
127
|
+
* doc(collectionRef) // auto-generate ID
|
|
128
|
+
*/
|
|
129
|
+
declare function doc<T = any>(dbOrCollection: any, collectionPath?: string, ...pathSegments: string[]): DocumentReference<T>;
|
|
130
|
+
/**
|
|
131
|
+
* Get a collection reference.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* collection(db, 'users')
|
|
135
|
+
* collection(db, 'orders', orderId, 'items') // subcollection → MongoDB: orders_items
|
|
136
|
+
*/
|
|
137
|
+
declare function collection<T = any>(_db: any, collectionPath: string, ...pathSegments: string[]): CollectionReference<T>;
|
|
138
|
+
/**
|
|
139
|
+
* Get a single document by reference.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const snap = await getDoc(doc(db, 'users', id));
|
|
143
|
+
* if (snap.exists()) { const user = snap.data(); }
|
|
144
|
+
*/
|
|
145
|
+
declare function getDoc<T = any>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
|
|
146
|
+
/**
|
|
147
|
+
* Get all documents matching a query or collection reference.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const snap = await getDocs(query(collection(db, 'users'), where('active', '==', true)));
|
|
151
|
+
* snap.forEach(doc => console.log(doc.data()));
|
|
152
|
+
*/
|
|
153
|
+
declare function getDocs<T = any>(queryOrRef: Query<T> | CollectionReference<T>): Promise<QuerySnapshot<T>>;
|
|
154
|
+
/**
|
|
155
|
+
* Create or overwrite a document.
|
|
156
|
+
* Pass `{ merge: true }` to do a partial update instead of overwrite.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* await setDoc(doc(db, 'users', id), userData);
|
|
160
|
+
* await setDoc(doc(db, 'users', id), { name: 'New' }, { merge: true });
|
|
161
|
+
*/
|
|
162
|
+
declare function setDoc<T = any>(reference: DocumentReference<T>, data: T, options?: SetOptions): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Add a new document with an auto-generated ID.
|
|
165
|
+
* Returns a DocumentReference for the new document.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* const ref = await addDoc(collection(db, 'orders'), orderData);
|
|
169
|
+
* console.log(ref.id);
|
|
170
|
+
*/
|
|
171
|
+
declare function addDoc<T = any>(reference: CollectionReference<T>, data: T): Promise<DocumentReference<T>>;
|
|
172
|
+
/**
|
|
173
|
+
* Partially update a document. Only provided fields are changed.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* await updateDoc(doc(db, 'users', id), { status: 'ACTIVE' });
|
|
177
|
+
* await updateDoc(ref, { tags: arrayUnion('premium') });
|
|
178
|
+
*/
|
|
179
|
+
declare function updateDoc<T = any>(reference: DocumentReference<T>, data: Partial<T>): Promise<void>;
|
|
180
|
+
/**
|
|
181
|
+
* Delete a document.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* await deleteDoc(doc(db, 'users', id));
|
|
185
|
+
*/
|
|
186
|
+
declare function deleteDoc<T = any>(reference: DocumentReference<T>): Promise<void>;
|
|
187
|
+
/** Build a query from a collection reference and constraints. */
|
|
188
|
+
declare function query<T = any>(collectionRef: CollectionReference<T>, ...constraints: QueryConstraint[]): Query<T>;
|
|
189
|
+
/** Filter by field value. */
|
|
190
|
+
declare function where(field: string, op: WhereFilterOp, value: any): QueryConstraint;
|
|
191
|
+
/** Explicit AND grouping. */
|
|
192
|
+
declare function and(...queries: QueryConstraint[]): QueryConstraint;
|
|
193
|
+
/** OR grouping — translates to MongoDB $or. */
|
|
194
|
+
declare function or(...queries: QueryConstraint[]): QueryConstraint;
|
|
195
|
+
/** Sort results. */
|
|
196
|
+
declare function orderBy(field: string, direction?: OrderByDirection): QueryConstraint;
|
|
197
|
+
/** Limit number of results. */
|
|
198
|
+
declare function limit(count: number): QueryConstraint;
|
|
199
|
+
/** Skip the first n results. */
|
|
200
|
+
declare function offset(count: number): QueryConstraint;
|
|
201
|
+
interface WriteBatch {
|
|
202
|
+
set<T>(ref: DocumentReference<T>, data: T, options?: SetOptions): WriteBatch;
|
|
203
|
+
update<T>(ref: DocumentReference<T>, data: Partial<T>): WriteBatch;
|
|
204
|
+
delete<T>(ref: DocumentReference<T>): WriteBatch;
|
|
205
|
+
commit(): Promise<void>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Batch multiple write operations. Not atomic — ops run in parallel.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* const batch = writeBatch(db);
|
|
212
|
+
* batch.set(ref1, data);
|
|
213
|
+
* batch.update(ref2, { status: 'DONE' });
|
|
214
|
+
* await batch.commit();
|
|
215
|
+
*/
|
|
216
|
+
declare function writeBatch(_db: any): WriteBatch;
|
|
217
|
+
interface Transaction {
|
|
218
|
+
get<T>(ref: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
|
|
219
|
+
set<T>(ref: DocumentReference<T>, data: T, options?: SetOptions): Transaction;
|
|
220
|
+
update<T>(ref: DocumentReference<T>, data: Partial<T>): Transaction;
|
|
221
|
+
delete<T>(ref: DocumentReference<T>): Transaction;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Best-effort transaction. Reads are immediate; writes are collected and run
|
|
225
|
+
* in parallel after the callback returns. Not true ACID — no rollback on failure.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* await runTransaction(db, async (tx) => {
|
|
229
|
+
* const snap = await tx.get(ref);
|
|
230
|
+
* tx.update(ref, { count: (snap.data()?.count ?? 0) + 1 });
|
|
231
|
+
* });
|
|
232
|
+
*/
|
|
233
|
+
declare function runTransaction<T>(_db: any, fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
234
|
+
/** Dummy db export for drop-in compatibility with firebase _db imports. */
|
|
235
|
+
declare function getFirestore(): any;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Firebase-compatible Timestamp class.
|
|
239
|
+
* Serializes as a JavaScript Date when stored in MongoDB.
|
|
240
|
+
*/
|
|
241
|
+
declare class Timestamp {
|
|
242
|
+
private readonly _seconds;
|
|
243
|
+
private readonly _nanoseconds;
|
|
244
|
+
constructor(_seconds: number, _nanoseconds: number);
|
|
245
|
+
get seconds(): number;
|
|
246
|
+
get nanoseconds(): number;
|
|
247
|
+
/** Current time */
|
|
248
|
+
static now(): Timestamp;
|
|
249
|
+
/** From a JavaScript Date */
|
|
250
|
+
static fromDate(date: Date): Timestamp;
|
|
251
|
+
/** From epoch milliseconds */
|
|
252
|
+
static fromMillis(ms: number): Timestamp;
|
|
253
|
+
/** Convert to JavaScript Date */
|
|
254
|
+
toDate(): Date;
|
|
255
|
+
/** Convert to epoch milliseconds */
|
|
256
|
+
toMillis(): number;
|
|
257
|
+
isEqual(other: Timestamp): boolean;
|
|
258
|
+
toString(): string;
|
|
259
|
+
/** MongoDB serialization — stored as Date */
|
|
260
|
+
toJSON(): Date;
|
|
261
|
+
valueOf(): Date;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Firebase-compatible FieldValue sentinel class.
|
|
266
|
+
* Used to express atomic array/increment operations in updateDoc / setDoc.
|
|
267
|
+
*/
|
|
268
|
+
declare class FieldValue {
|
|
269
|
+
readonly type: 'arrayUnion' | 'arrayRemove' | 'increment';
|
|
270
|
+
readonly elements: any[];
|
|
271
|
+
constructor(type: 'arrayUnion' | 'arrayRemove' | 'increment', elements: any[]);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Add elements to an array field (no duplicates).
|
|
275
|
+
* Translates to MongoDB $addToSet: { $each: [...] }
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* await updateDoc(ref, { tags: arrayUnion('premium', 'verified') });
|
|
279
|
+
*/
|
|
280
|
+
declare function arrayUnion(...elements: any[]): FieldValue;
|
|
281
|
+
/**
|
|
282
|
+
* Remove all instances of specified elements from an array field.
|
|
283
|
+
* Translates to MongoDB $pull: { $in: [...] }
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* await updateDoc(ref, { tags: arrayRemove('trial') });
|
|
287
|
+
*/
|
|
288
|
+
declare function arrayRemove(...elements: any[]): FieldValue;
|
|
289
|
+
/**
|
|
290
|
+
* Increment a numeric field by n.
|
|
291
|
+
* Translates to MongoDB $inc: { field: n }
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* await updateDoc(ref, { viewCount: increment(1) });
|
|
295
|
+
*/
|
|
296
|
+
declare function increment(n: number): FieldValue;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* fire2mongo
|
|
300
|
+
*
|
|
301
|
+
* Drop-in TypeScript replacement for firebase/firestore backed by MongoDB.
|
|
302
|
+
* Swap one import line — no other code changes required.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* // Before (Firebase)
|
|
306
|
+
* import { doc, getDoc, setDoc, collection, getDocs, query, where } from 'firebase/firestore';
|
|
307
|
+
* import { db } from '@/lib/firebase';
|
|
308
|
+
*
|
|
309
|
+
* // After (MongoDB)
|
|
310
|
+
* import { doc, getDoc, setDoc, collection, getDocs, query, where } from 'fire2mongo';
|
|
311
|
+
* import { db } from 'fire2mongo';
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** No-op db object — kept so existing `import { db } from 'firebase'` lines compile unchanged. */
|
|
315
|
+
declare const db: {};
|
|
316
|
+
|
|
317
|
+
export { type CollectionReference, type DocumentData, type DocumentReference, type DocumentSnapshot, FieldValue, type OrderByDirection, type Query, type QueryConstraint, type QueryDocumentSnapshot, type QuerySnapshot, type SetOptions, Timestamp, type Transaction, type WhereFilterOp, type WithId, type WithOptionalId, type WriteBatch, addDoc, and, arrayRemove, arrayUnion, clearRegistry, closeMongoDB, collection, db, deleteDoc, doc, getCollection, getDb, getDoc, getDocs, getFirestore, hasCollection, increment, initMongoDB, limit, offset, or, orderBy, query, registerCollection, registerCollections, runTransaction, setDoc, updateDoc, where, writeBatch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { Db, Collection } from 'mongodb';
|
|
2
|
+
|
|
3
|
+
interface ConnectionConfig {
|
|
4
|
+
uri: string;
|
|
5
|
+
dbName: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Initialize the MongoDB connection. Call once at app startup.
|
|
9
|
+
* Safe to call multiple times — returns cached instance after first call.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* await initMongoDB({ uri: process.env.MONGODB_URI!, dbName: process.env.MONGODB_DB! });
|
|
13
|
+
*/
|
|
14
|
+
declare function initMongoDB(config: ConnectionConfig): Promise<Db>;
|
|
15
|
+
/**
|
|
16
|
+
* Get the cached Db instance.
|
|
17
|
+
* Throws if initMongoDB() has not been called.
|
|
18
|
+
*/
|
|
19
|
+
declare function getDb(): Db;
|
|
20
|
+
/**
|
|
21
|
+
* Close the MongoDB connection. Useful for graceful shutdown or tests.
|
|
22
|
+
*/
|
|
23
|
+
declare function closeMongoDB(): Promise<void>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Register a single Firebase collection name → MongoDB collection name.
|
|
27
|
+
* If mongoCollection is omitted, uses the same name as firebaseName.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* registerCollection('users');
|
|
31
|
+
* registerCollection('items', { mongoCollection: 'inventory_items' });
|
|
32
|
+
*/
|
|
33
|
+
declare function registerCollection(firebaseName: string, options?: {
|
|
34
|
+
mongoCollection?: string;
|
|
35
|
+
}): void;
|
|
36
|
+
/**
|
|
37
|
+
* Register multiple collections at once.
|
|
38
|
+
* Key = Firebase collection name, Value = MongoDB collection name.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* registerCollections({
|
|
42
|
+
* users: 'users',
|
|
43
|
+
* products: 'inventory_items',
|
|
44
|
+
* });
|
|
45
|
+
*/
|
|
46
|
+
declare function registerCollections(map: Record<string, string>): void;
|
|
47
|
+
/**
|
|
48
|
+
* Returns true if the collection is registered.
|
|
49
|
+
*/
|
|
50
|
+
declare function hasCollection(firebaseName: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves a Firebase collection name to a live MongoDB Collection.
|
|
53
|
+
* Throws with a helpful message if the collection is not registered.
|
|
54
|
+
*/
|
|
55
|
+
declare function getCollection(firebaseName: string): Collection;
|
|
56
|
+
/**
|
|
57
|
+
* Clear all registered collections. Useful for tests.
|
|
58
|
+
*/
|
|
59
|
+
declare function clearRegistry(): void;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* TypeScript interfaces mirroring the Firebase Firestore public API
|
|
63
|
+
*/
|
|
64
|
+
interface DocumentReference<T = any> {
|
|
65
|
+
id: string;
|
|
66
|
+
path: string;
|
|
67
|
+
collection: string;
|
|
68
|
+
_parentPath?: string;
|
|
69
|
+
}
|
|
70
|
+
interface CollectionReference<T = any> {
|
|
71
|
+
id: string;
|
|
72
|
+
path: string;
|
|
73
|
+
_mongoCollection: string;
|
|
74
|
+
_parentPath?: string;
|
|
75
|
+
}
|
|
76
|
+
interface Query<T = any> {
|
|
77
|
+
_collectionRef: CollectionReference<T>;
|
|
78
|
+
_filters: QueryConstraint[];
|
|
79
|
+
}
|
|
80
|
+
interface DocumentSnapshot<T = any> {
|
|
81
|
+
id: string;
|
|
82
|
+
exists(): boolean;
|
|
83
|
+
data(): T | undefined;
|
|
84
|
+
}
|
|
85
|
+
interface QueryDocumentSnapshot<T = any> {
|
|
86
|
+
id: string;
|
|
87
|
+
exists(): boolean;
|
|
88
|
+
data(): T;
|
|
89
|
+
}
|
|
90
|
+
interface QuerySnapshot<T = any> {
|
|
91
|
+
docs: QueryDocumentSnapshot<T>[];
|
|
92
|
+
size: number;
|
|
93
|
+
empty: boolean;
|
|
94
|
+
forEach(callback: (doc: QueryDocumentSnapshot<T>) => void): void;
|
|
95
|
+
}
|
|
96
|
+
type WhereFilterOp = '==' | '!=' | '<' | '<=' | '>' | '>=' | 'array-contains' | 'in' | 'not-in' | 'array-contains-any';
|
|
97
|
+
type OrderByDirection = 'asc' | 'desc';
|
|
98
|
+
interface QueryConstraint {
|
|
99
|
+
type: 'where' | 'orderBy' | 'limit' | 'offset' | 'and' | 'or';
|
|
100
|
+
field?: string;
|
|
101
|
+
operator?: WhereFilterOp;
|
|
102
|
+
value?: any;
|
|
103
|
+
direction?: OrderByDirection;
|
|
104
|
+
count?: number;
|
|
105
|
+
queries?: QueryConstraint[];
|
|
106
|
+
}
|
|
107
|
+
interface SetOptions {
|
|
108
|
+
merge?: boolean;
|
|
109
|
+
mergeFields?: string[];
|
|
110
|
+
}
|
|
111
|
+
type WithId<T> = T & {
|
|
112
|
+
id: string;
|
|
113
|
+
};
|
|
114
|
+
type WithOptionalId<T> = T & {
|
|
115
|
+
id?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type DocumentData = {
|
|
119
|
+
[field: string]: any;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Get a document reference.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* doc(db, 'users', userId)
|
|
126
|
+
* doc(db, 'orders', orderId, 'items', itemId) // subcollection
|
|
127
|
+
* doc(collectionRef) // auto-generate ID
|
|
128
|
+
*/
|
|
129
|
+
declare function doc<T = any>(dbOrCollection: any, collectionPath?: string, ...pathSegments: string[]): DocumentReference<T>;
|
|
130
|
+
/**
|
|
131
|
+
* Get a collection reference.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* collection(db, 'users')
|
|
135
|
+
* collection(db, 'orders', orderId, 'items') // subcollection → MongoDB: orders_items
|
|
136
|
+
*/
|
|
137
|
+
declare function collection<T = any>(_db: any, collectionPath: string, ...pathSegments: string[]): CollectionReference<T>;
|
|
138
|
+
/**
|
|
139
|
+
* Get a single document by reference.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const snap = await getDoc(doc(db, 'users', id));
|
|
143
|
+
* if (snap.exists()) { const user = snap.data(); }
|
|
144
|
+
*/
|
|
145
|
+
declare function getDoc<T = any>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
|
|
146
|
+
/**
|
|
147
|
+
* Get all documents matching a query or collection reference.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const snap = await getDocs(query(collection(db, 'users'), where('active', '==', true)));
|
|
151
|
+
* snap.forEach(doc => console.log(doc.data()));
|
|
152
|
+
*/
|
|
153
|
+
declare function getDocs<T = any>(queryOrRef: Query<T> | CollectionReference<T>): Promise<QuerySnapshot<T>>;
|
|
154
|
+
/**
|
|
155
|
+
* Create or overwrite a document.
|
|
156
|
+
* Pass `{ merge: true }` to do a partial update instead of overwrite.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* await setDoc(doc(db, 'users', id), userData);
|
|
160
|
+
* await setDoc(doc(db, 'users', id), { name: 'New' }, { merge: true });
|
|
161
|
+
*/
|
|
162
|
+
declare function setDoc<T = any>(reference: DocumentReference<T>, data: T, options?: SetOptions): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Add a new document with an auto-generated ID.
|
|
165
|
+
* Returns a DocumentReference for the new document.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* const ref = await addDoc(collection(db, 'orders'), orderData);
|
|
169
|
+
* console.log(ref.id);
|
|
170
|
+
*/
|
|
171
|
+
declare function addDoc<T = any>(reference: CollectionReference<T>, data: T): Promise<DocumentReference<T>>;
|
|
172
|
+
/**
|
|
173
|
+
* Partially update a document. Only provided fields are changed.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* await updateDoc(doc(db, 'users', id), { status: 'ACTIVE' });
|
|
177
|
+
* await updateDoc(ref, { tags: arrayUnion('premium') });
|
|
178
|
+
*/
|
|
179
|
+
declare function updateDoc<T = any>(reference: DocumentReference<T>, data: Partial<T>): Promise<void>;
|
|
180
|
+
/**
|
|
181
|
+
* Delete a document.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* await deleteDoc(doc(db, 'users', id));
|
|
185
|
+
*/
|
|
186
|
+
declare function deleteDoc<T = any>(reference: DocumentReference<T>): Promise<void>;
|
|
187
|
+
/** Build a query from a collection reference and constraints. */
|
|
188
|
+
declare function query<T = any>(collectionRef: CollectionReference<T>, ...constraints: QueryConstraint[]): Query<T>;
|
|
189
|
+
/** Filter by field value. */
|
|
190
|
+
declare function where(field: string, op: WhereFilterOp, value: any): QueryConstraint;
|
|
191
|
+
/** Explicit AND grouping. */
|
|
192
|
+
declare function and(...queries: QueryConstraint[]): QueryConstraint;
|
|
193
|
+
/** OR grouping — translates to MongoDB $or. */
|
|
194
|
+
declare function or(...queries: QueryConstraint[]): QueryConstraint;
|
|
195
|
+
/** Sort results. */
|
|
196
|
+
declare function orderBy(field: string, direction?: OrderByDirection): QueryConstraint;
|
|
197
|
+
/** Limit number of results. */
|
|
198
|
+
declare function limit(count: number): QueryConstraint;
|
|
199
|
+
/** Skip the first n results. */
|
|
200
|
+
declare function offset(count: number): QueryConstraint;
|
|
201
|
+
interface WriteBatch {
|
|
202
|
+
set<T>(ref: DocumentReference<T>, data: T, options?: SetOptions): WriteBatch;
|
|
203
|
+
update<T>(ref: DocumentReference<T>, data: Partial<T>): WriteBatch;
|
|
204
|
+
delete<T>(ref: DocumentReference<T>): WriteBatch;
|
|
205
|
+
commit(): Promise<void>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Batch multiple write operations. Not atomic — ops run in parallel.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* const batch = writeBatch(db);
|
|
212
|
+
* batch.set(ref1, data);
|
|
213
|
+
* batch.update(ref2, { status: 'DONE' });
|
|
214
|
+
* await batch.commit();
|
|
215
|
+
*/
|
|
216
|
+
declare function writeBatch(_db: any): WriteBatch;
|
|
217
|
+
interface Transaction {
|
|
218
|
+
get<T>(ref: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
|
|
219
|
+
set<T>(ref: DocumentReference<T>, data: T, options?: SetOptions): Transaction;
|
|
220
|
+
update<T>(ref: DocumentReference<T>, data: Partial<T>): Transaction;
|
|
221
|
+
delete<T>(ref: DocumentReference<T>): Transaction;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Best-effort transaction. Reads are immediate; writes are collected and run
|
|
225
|
+
* in parallel after the callback returns. Not true ACID — no rollback on failure.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* await runTransaction(db, async (tx) => {
|
|
229
|
+
* const snap = await tx.get(ref);
|
|
230
|
+
* tx.update(ref, { count: (snap.data()?.count ?? 0) + 1 });
|
|
231
|
+
* });
|
|
232
|
+
*/
|
|
233
|
+
declare function runTransaction<T>(_db: any, fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
234
|
+
/** Dummy db export for drop-in compatibility with firebase _db imports. */
|
|
235
|
+
declare function getFirestore(): any;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Firebase-compatible Timestamp class.
|
|
239
|
+
* Serializes as a JavaScript Date when stored in MongoDB.
|
|
240
|
+
*/
|
|
241
|
+
declare class Timestamp {
|
|
242
|
+
private readonly _seconds;
|
|
243
|
+
private readonly _nanoseconds;
|
|
244
|
+
constructor(_seconds: number, _nanoseconds: number);
|
|
245
|
+
get seconds(): number;
|
|
246
|
+
get nanoseconds(): number;
|
|
247
|
+
/** Current time */
|
|
248
|
+
static now(): Timestamp;
|
|
249
|
+
/** From a JavaScript Date */
|
|
250
|
+
static fromDate(date: Date): Timestamp;
|
|
251
|
+
/** From epoch milliseconds */
|
|
252
|
+
static fromMillis(ms: number): Timestamp;
|
|
253
|
+
/** Convert to JavaScript Date */
|
|
254
|
+
toDate(): Date;
|
|
255
|
+
/** Convert to epoch milliseconds */
|
|
256
|
+
toMillis(): number;
|
|
257
|
+
isEqual(other: Timestamp): boolean;
|
|
258
|
+
toString(): string;
|
|
259
|
+
/** MongoDB serialization — stored as Date */
|
|
260
|
+
toJSON(): Date;
|
|
261
|
+
valueOf(): Date;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Firebase-compatible FieldValue sentinel class.
|
|
266
|
+
* Used to express atomic array/increment operations in updateDoc / setDoc.
|
|
267
|
+
*/
|
|
268
|
+
declare class FieldValue {
|
|
269
|
+
readonly type: 'arrayUnion' | 'arrayRemove' | 'increment';
|
|
270
|
+
readonly elements: any[];
|
|
271
|
+
constructor(type: 'arrayUnion' | 'arrayRemove' | 'increment', elements: any[]);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Add elements to an array field (no duplicates).
|
|
275
|
+
* Translates to MongoDB $addToSet: { $each: [...] }
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* await updateDoc(ref, { tags: arrayUnion('premium', 'verified') });
|
|
279
|
+
*/
|
|
280
|
+
declare function arrayUnion(...elements: any[]): FieldValue;
|
|
281
|
+
/**
|
|
282
|
+
* Remove all instances of specified elements from an array field.
|
|
283
|
+
* Translates to MongoDB $pull: { $in: [...] }
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* await updateDoc(ref, { tags: arrayRemove('trial') });
|
|
287
|
+
*/
|
|
288
|
+
declare function arrayRemove(...elements: any[]): FieldValue;
|
|
289
|
+
/**
|
|
290
|
+
* Increment a numeric field by n.
|
|
291
|
+
* Translates to MongoDB $inc: { field: n }
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* await updateDoc(ref, { viewCount: increment(1) });
|
|
295
|
+
*/
|
|
296
|
+
declare function increment(n: number): FieldValue;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* fire2mongo
|
|
300
|
+
*
|
|
301
|
+
* Drop-in TypeScript replacement for firebase/firestore backed by MongoDB.
|
|
302
|
+
* Swap one import line — no other code changes required.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* // Before (Firebase)
|
|
306
|
+
* import { doc, getDoc, setDoc, collection, getDocs, query, where } from 'firebase/firestore';
|
|
307
|
+
* import { db } from '@/lib/firebase';
|
|
308
|
+
*
|
|
309
|
+
* // After (MongoDB)
|
|
310
|
+
* import { doc, getDoc, setDoc, collection, getDocs, query, where } from 'fire2mongo';
|
|
311
|
+
* import { db } from 'fire2mongo';
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** No-op db object — kept so existing `import { db } from 'firebase'` lines compile unchanged. */
|
|
315
|
+
declare const db: {};
|
|
316
|
+
|
|
317
|
+
export { type CollectionReference, type DocumentData, type DocumentReference, type DocumentSnapshot, FieldValue, type OrderByDirection, type Query, type QueryConstraint, type QueryDocumentSnapshot, type QuerySnapshot, type SetOptions, Timestamp, type Transaction, type WhereFilterOp, type WithId, type WithOptionalId, type WriteBatch, addDoc, and, arrayRemove, arrayUnion, clearRegistry, closeMongoDB, collection, db, deleteDoc, doc, getCollection, getDb, getDoc, getDocs, getFirestore, hasCollection, increment, initMongoDB, limit, offset, or, orderBy, query, registerCollection, registerCollections, runTransaction, setDoc, updateDoc, where, writeBatch };
|