firestore-batch-updater 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.
@@ -0,0 +1,316 @@
1
+ import { WhereFilterOp, Firestore } from 'firebase-admin/firestore';
2
+
3
+ /**
4
+ * Firestore Batch Updater Type Definitions
5
+ */
6
+
7
+ /**
8
+ * Progress information during batch operations
9
+ */
10
+ interface ProgressInfo {
11
+ current: number;
12
+ total: number;
13
+ percentage: number;
14
+ }
15
+ /**
16
+ * Options for update operations
17
+ */
18
+ interface UpdateOptions {
19
+ /**
20
+ * Callback function for progress updates
21
+ * @param progress - Current progress information
22
+ */
23
+ onProgress?: (progress: ProgressInfo) => void;
24
+ /**
25
+ * Log file generation options
26
+ */
27
+ log?: LogOptions;
28
+ /**
29
+ * Batch size for pagination (optional)
30
+ * When set, documents are processed in batches to prevent memory issues with large collections
31
+ * When not set, all documents are loaded at once
32
+ */
33
+ batchSize?: number;
34
+ }
35
+ /**
36
+ * Result of batch update operation
37
+ */
38
+ interface UpdateResult {
39
+ successCount: number;
40
+ failureCount: number;
41
+ totalCount: number;
42
+ failedDocIds?: string[];
43
+ }
44
+ /**
45
+ * Document snapshot showing before/after state
46
+ */
47
+ interface DocumentSnapshot {
48
+ id: string;
49
+ before: Record<string, any>;
50
+ after: Record<string, any>;
51
+ }
52
+ /**
53
+ * Preview result before executing update
54
+ */
55
+ interface PreviewResult {
56
+ affectedCount: number;
57
+ samples: DocumentSnapshot[];
58
+ affectedFields: string[];
59
+ }
60
+ /**
61
+ * Where clause condition
62
+ */
63
+ interface WhereCondition {
64
+ field: string;
65
+ operator: WhereFilterOp;
66
+ value: any;
67
+ }
68
+ /**
69
+ * Field value result
70
+ */
71
+ interface FieldValue {
72
+ id: string;
73
+ value: any;
74
+ }
75
+ /**
76
+ * Input for creating a single document
77
+ */
78
+ interface CreateDocumentInput {
79
+ id?: string;
80
+ data: Record<string, any>;
81
+ }
82
+ /**
83
+ * Options for create operations
84
+ */
85
+ interface CreateOptions {
86
+ /**
87
+ * Callback function for progress updates
88
+ * @param progress - Current progress information
89
+ */
90
+ onProgress?: (progress: ProgressInfo) => void;
91
+ /**
92
+ * Log file generation options
93
+ */
94
+ log?: LogOptions;
95
+ }
96
+ /**
97
+ * Result of batch create operation
98
+ */
99
+ interface CreateResult {
100
+ successCount: number;
101
+ failureCount: number;
102
+ totalCount: number;
103
+ createdIds: string[];
104
+ failedDocIds?: string[];
105
+ }
106
+ /**
107
+ * Options for upsert operations
108
+ */
109
+ interface UpsertOptions {
110
+ /**
111
+ * Callback function for progress updates
112
+ * @param progress - Current progress information
113
+ */
114
+ onProgress?: (progress: ProgressInfo) => void;
115
+ /**
116
+ * Log file generation options
117
+ */
118
+ log?: LogOptions;
119
+ /**
120
+ * Batch size for pagination (optional)
121
+ * When set, documents are processed in batches to prevent memory issues with large collections
122
+ * When not set, all documents are loaded at once
123
+ */
124
+ batchSize?: number;
125
+ }
126
+ /**
127
+ * Result of batch upsert operation
128
+ */
129
+ interface UpsertResult {
130
+ successCount: number;
131
+ failureCount: number;
132
+ totalCount: number;
133
+ failedDocIds?: string[];
134
+ }
135
+ /**
136
+ * Log options for batch operations
137
+ */
138
+ interface LogOptions {
139
+ enabled: boolean;
140
+ path?: string;
141
+ filename?: string;
142
+ }
143
+ /**
144
+ * Log entry for a single document operation
145
+ */
146
+ interface LogEntry {
147
+ timestamp: string;
148
+ documentId: string;
149
+ status: "success" | "failure";
150
+ error?: string;
151
+ }
152
+ /**
153
+ * Complete log data for an operation
154
+ */
155
+ interface OperationLog {
156
+ operation: "update" | "create" | "upsert";
157
+ collection: string;
158
+ startedAt: string;
159
+ completedAt: string;
160
+ conditions?: WhereCondition[];
161
+ updateData?: Record<string, any>;
162
+ summary: {
163
+ totalCount: number;
164
+ successCount: number;
165
+ failureCount: number;
166
+ };
167
+ entries: LogEntry[];
168
+ }
169
+
170
+ /**
171
+ * BatchUpdater - Core class for batch operations on Firestore
172
+ */
173
+
174
+ /**
175
+ * BatchUpdater class for efficient batch operations
176
+ */
177
+ declare class BatchUpdater {
178
+ private firestore;
179
+ private collectionPath?;
180
+ private conditions;
181
+ /**
182
+ * Create a new BatchUpdater instance
183
+ * @param firestore - Initialized Firestore instance from firebase-admin
184
+ */
185
+ constructor(firestore: Firestore);
186
+ /**
187
+ * Select a collection to operate on
188
+ * @param path - Collection path
189
+ * @returns This instance for chaining
190
+ */
191
+ collection(path: string): this;
192
+ /**
193
+ * Add a where condition to filter documents
194
+ * @param field - Field path
195
+ * @param operator - Comparison operator
196
+ * @param value - Value to compare
197
+ * @returns This instance for chaining
198
+ */
199
+ where(field: string, operator: WhereFilterOp, value: any): this;
200
+ /**
201
+ * Preview changes before executing update
202
+ * @param updateData - Data to update
203
+ * @returns Preview result with affected count and samples
204
+ */
205
+ preview(updateData: Record<string, any>): Promise<PreviewResult>;
206
+ /**
207
+ * Execute batch update operation
208
+ * @param updateData - Data to update
209
+ * @param options - Update options (e.g., progress callback, log options, batchSize for pagination)
210
+ * @returns Update result with success/failure counts and optional log file path
211
+ */
212
+ update(updateData: Record<string, any>, options?: UpdateOptions): Promise<UpdateResult & {
213
+ logFilePath?: string;
214
+ }>;
215
+ /**
216
+ * Get specific field values from matching documents
217
+ * @param fieldPath - Field path to retrieve
218
+ * @returns Array of field values with document IDs
219
+ */
220
+ getFields(fieldPath: string): Promise<FieldValue[]>;
221
+ /**
222
+ * Create multiple documents in batch
223
+ * @param documents - Array of documents to create
224
+ * @param options - Create options (e.g., progress callback, log options)
225
+ * @returns Create result with success/failure counts, created IDs, and optional log file path
226
+ */
227
+ create(documents: CreateDocumentInput[], options?: CreateOptions): Promise<CreateResult & {
228
+ logFilePath?: string;
229
+ }>;
230
+ /**
231
+ * Upsert documents matching query conditions
232
+ * Updates existing documents or creates them if they don't exist
233
+ * @param updateData - Data to set/merge
234
+ * @param options - Upsert options (e.g., progress callback, log options, batchSize for pagination)
235
+ * @returns Upsert result with success/failure counts and optional log file path
236
+ */
237
+ upsert(updateData: Record<string, any>, options?: UpsertOptions): Promise<UpsertResult & {
238
+ logFilePath?: string;
239
+ }>;
240
+ /**
241
+ * Validate that collection is set
242
+ * @private
243
+ */
244
+ private validateSetup;
245
+ /**
246
+ * Build Firestore query with all conditions
247
+ * @private
248
+ */
249
+ private buildQuery;
250
+ /**
251
+ * Get nested value from object using dot notation
252
+ * @private
253
+ */
254
+ private getNestedValue;
255
+ }
256
+
257
+ /**
258
+ * Logger utility for Firestore Batch Updater
259
+ */
260
+
261
+ /**
262
+ * Format operation log to string
263
+ */
264
+ declare function formatOperationLog(log: OperationLog): string;
265
+ /**
266
+ * Write operation log to file
267
+ */
268
+ declare function writeOperationLog(log: OperationLog, options: LogOptions): string;
269
+ /**
270
+ * Create a log collector for tracking operation entries
271
+ */
272
+ declare function createLogCollector(operation: "update" | "create" | "upsert", collection: string, conditions?: WhereCondition[], updateData?: Record<string, any>): {
273
+ addEntry: (documentId: string, status: "success" | "failure", error?: string) => void;
274
+ finalize: (options: LogOptions) => string;
275
+ getLog: () => OperationLog;
276
+ };
277
+
278
+ /**
279
+ * Utility functions for Firestore Batch Updater
280
+ */
281
+
282
+ /**
283
+ * Calculate progress information
284
+ * @param current - Number of documents processed so far
285
+ * @param total - Total number of documents to process
286
+ * @returns Progress information with percentage
287
+ */
288
+ declare function calculateProgress(current: number, total: number): ProgressInfo;
289
+ /**
290
+ * Extract field names from update data
291
+ * @param updateData - Data to be updated
292
+ * @returns Array of field names that will be affected
293
+ */
294
+ declare function getAffectedFields(updateData: Record<string, any>): string[];
295
+ /**
296
+ * Merge update data with existing document data
297
+ * @param existingData - Current document data
298
+ * @param updateData - Data to update
299
+ * @returns Merged data (shallow merge)
300
+ */
301
+ declare function mergeUpdateData(existingData: Record<string, any>, updateData: Record<string, any>): Record<string, any>;
302
+ /**
303
+ * Check if a value is a valid update data object
304
+ * @param value - Value to check
305
+ * @returns True if value is a valid update data object
306
+ */
307
+ declare function isValidUpdateData(value: any): value is Record<string, any>;
308
+ /**
309
+ * Format error message for failed operations
310
+ * @param error - Error object
311
+ * @param context - Additional context (e.g., document ID)
312
+ * @returns Formatted error message
313
+ */
314
+ declare function formatError(error: unknown, context?: string): string;
315
+
316
+ export { BatchUpdater, type CreateDocumentInput, type CreateOptions, type CreateResult, type DocumentSnapshot, type FieldValue, type LogEntry, type LogOptions, type OperationLog, type PreviewResult, type ProgressInfo, type UpdateOptions, type UpdateResult, type UpsertOptions, type UpsertResult, type WhereCondition, calculateProgress, createLogCollector, formatError, formatOperationLog, getAffectedFields, isValidUpdateData, mergeUpdateData, writeOperationLog };
@@ -0,0 +1,316 @@
1
+ import { WhereFilterOp, Firestore } from 'firebase-admin/firestore';
2
+
3
+ /**
4
+ * Firestore Batch Updater Type Definitions
5
+ */
6
+
7
+ /**
8
+ * Progress information during batch operations
9
+ */
10
+ interface ProgressInfo {
11
+ current: number;
12
+ total: number;
13
+ percentage: number;
14
+ }
15
+ /**
16
+ * Options for update operations
17
+ */
18
+ interface UpdateOptions {
19
+ /**
20
+ * Callback function for progress updates
21
+ * @param progress - Current progress information
22
+ */
23
+ onProgress?: (progress: ProgressInfo) => void;
24
+ /**
25
+ * Log file generation options
26
+ */
27
+ log?: LogOptions;
28
+ /**
29
+ * Batch size for pagination (optional)
30
+ * When set, documents are processed in batches to prevent memory issues with large collections
31
+ * When not set, all documents are loaded at once
32
+ */
33
+ batchSize?: number;
34
+ }
35
+ /**
36
+ * Result of batch update operation
37
+ */
38
+ interface UpdateResult {
39
+ successCount: number;
40
+ failureCount: number;
41
+ totalCount: number;
42
+ failedDocIds?: string[];
43
+ }
44
+ /**
45
+ * Document snapshot showing before/after state
46
+ */
47
+ interface DocumentSnapshot {
48
+ id: string;
49
+ before: Record<string, any>;
50
+ after: Record<string, any>;
51
+ }
52
+ /**
53
+ * Preview result before executing update
54
+ */
55
+ interface PreviewResult {
56
+ affectedCount: number;
57
+ samples: DocumentSnapshot[];
58
+ affectedFields: string[];
59
+ }
60
+ /**
61
+ * Where clause condition
62
+ */
63
+ interface WhereCondition {
64
+ field: string;
65
+ operator: WhereFilterOp;
66
+ value: any;
67
+ }
68
+ /**
69
+ * Field value result
70
+ */
71
+ interface FieldValue {
72
+ id: string;
73
+ value: any;
74
+ }
75
+ /**
76
+ * Input for creating a single document
77
+ */
78
+ interface CreateDocumentInput {
79
+ id?: string;
80
+ data: Record<string, any>;
81
+ }
82
+ /**
83
+ * Options for create operations
84
+ */
85
+ interface CreateOptions {
86
+ /**
87
+ * Callback function for progress updates
88
+ * @param progress - Current progress information
89
+ */
90
+ onProgress?: (progress: ProgressInfo) => void;
91
+ /**
92
+ * Log file generation options
93
+ */
94
+ log?: LogOptions;
95
+ }
96
+ /**
97
+ * Result of batch create operation
98
+ */
99
+ interface CreateResult {
100
+ successCount: number;
101
+ failureCount: number;
102
+ totalCount: number;
103
+ createdIds: string[];
104
+ failedDocIds?: string[];
105
+ }
106
+ /**
107
+ * Options for upsert operations
108
+ */
109
+ interface UpsertOptions {
110
+ /**
111
+ * Callback function for progress updates
112
+ * @param progress - Current progress information
113
+ */
114
+ onProgress?: (progress: ProgressInfo) => void;
115
+ /**
116
+ * Log file generation options
117
+ */
118
+ log?: LogOptions;
119
+ /**
120
+ * Batch size for pagination (optional)
121
+ * When set, documents are processed in batches to prevent memory issues with large collections
122
+ * When not set, all documents are loaded at once
123
+ */
124
+ batchSize?: number;
125
+ }
126
+ /**
127
+ * Result of batch upsert operation
128
+ */
129
+ interface UpsertResult {
130
+ successCount: number;
131
+ failureCount: number;
132
+ totalCount: number;
133
+ failedDocIds?: string[];
134
+ }
135
+ /**
136
+ * Log options for batch operations
137
+ */
138
+ interface LogOptions {
139
+ enabled: boolean;
140
+ path?: string;
141
+ filename?: string;
142
+ }
143
+ /**
144
+ * Log entry for a single document operation
145
+ */
146
+ interface LogEntry {
147
+ timestamp: string;
148
+ documentId: string;
149
+ status: "success" | "failure";
150
+ error?: string;
151
+ }
152
+ /**
153
+ * Complete log data for an operation
154
+ */
155
+ interface OperationLog {
156
+ operation: "update" | "create" | "upsert";
157
+ collection: string;
158
+ startedAt: string;
159
+ completedAt: string;
160
+ conditions?: WhereCondition[];
161
+ updateData?: Record<string, any>;
162
+ summary: {
163
+ totalCount: number;
164
+ successCount: number;
165
+ failureCount: number;
166
+ };
167
+ entries: LogEntry[];
168
+ }
169
+
170
+ /**
171
+ * BatchUpdater - Core class for batch operations on Firestore
172
+ */
173
+
174
+ /**
175
+ * BatchUpdater class for efficient batch operations
176
+ */
177
+ declare class BatchUpdater {
178
+ private firestore;
179
+ private collectionPath?;
180
+ private conditions;
181
+ /**
182
+ * Create a new BatchUpdater instance
183
+ * @param firestore - Initialized Firestore instance from firebase-admin
184
+ */
185
+ constructor(firestore: Firestore);
186
+ /**
187
+ * Select a collection to operate on
188
+ * @param path - Collection path
189
+ * @returns This instance for chaining
190
+ */
191
+ collection(path: string): this;
192
+ /**
193
+ * Add a where condition to filter documents
194
+ * @param field - Field path
195
+ * @param operator - Comparison operator
196
+ * @param value - Value to compare
197
+ * @returns This instance for chaining
198
+ */
199
+ where(field: string, operator: WhereFilterOp, value: any): this;
200
+ /**
201
+ * Preview changes before executing update
202
+ * @param updateData - Data to update
203
+ * @returns Preview result with affected count and samples
204
+ */
205
+ preview(updateData: Record<string, any>): Promise<PreviewResult>;
206
+ /**
207
+ * Execute batch update operation
208
+ * @param updateData - Data to update
209
+ * @param options - Update options (e.g., progress callback, log options, batchSize for pagination)
210
+ * @returns Update result with success/failure counts and optional log file path
211
+ */
212
+ update(updateData: Record<string, any>, options?: UpdateOptions): Promise<UpdateResult & {
213
+ logFilePath?: string;
214
+ }>;
215
+ /**
216
+ * Get specific field values from matching documents
217
+ * @param fieldPath - Field path to retrieve
218
+ * @returns Array of field values with document IDs
219
+ */
220
+ getFields(fieldPath: string): Promise<FieldValue[]>;
221
+ /**
222
+ * Create multiple documents in batch
223
+ * @param documents - Array of documents to create
224
+ * @param options - Create options (e.g., progress callback, log options)
225
+ * @returns Create result with success/failure counts, created IDs, and optional log file path
226
+ */
227
+ create(documents: CreateDocumentInput[], options?: CreateOptions): Promise<CreateResult & {
228
+ logFilePath?: string;
229
+ }>;
230
+ /**
231
+ * Upsert documents matching query conditions
232
+ * Updates existing documents or creates them if they don't exist
233
+ * @param updateData - Data to set/merge
234
+ * @param options - Upsert options (e.g., progress callback, log options, batchSize for pagination)
235
+ * @returns Upsert result with success/failure counts and optional log file path
236
+ */
237
+ upsert(updateData: Record<string, any>, options?: UpsertOptions): Promise<UpsertResult & {
238
+ logFilePath?: string;
239
+ }>;
240
+ /**
241
+ * Validate that collection is set
242
+ * @private
243
+ */
244
+ private validateSetup;
245
+ /**
246
+ * Build Firestore query with all conditions
247
+ * @private
248
+ */
249
+ private buildQuery;
250
+ /**
251
+ * Get nested value from object using dot notation
252
+ * @private
253
+ */
254
+ private getNestedValue;
255
+ }
256
+
257
+ /**
258
+ * Logger utility for Firestore Batch Updater
259
+ */
260
+
261
+ /**
262
+ * Format operation log to string
263
+ */
264
+ declare function formatOperationLog(log: OperationLog): string;
265
+ /**
266
+ * Write operation log to file
267
+ */
268
+ declare function writeOperationLog(log: OperationLog, options: LogOptions): string;
269
+ /**
270
+ * Create a log collector for tracking operation entries
271
+ */
272
+ declare function createLogCollector(operation: "update" | "create" | "upsert", collection: string, conditions?: WhereCondition[], updateData?: Record<string, any>): {
273
+ addEntry: (documentId: string, status: "success" | "failure", error?: string) => void;
274
+ finalize: (options: LogOptions) => string;
275
+ getLog: () => OperationLog;
276
+ };
277
+
278
+ /**
279
+ * Utility functions for Firestore Batch Updater
280
+ */
281
+
282
+ /**
283
+ * Calculate progress information
284
+ * @param current - Number of documents processed so far
285
+ * @param total - Total number of documents to process
286
+ * @returns Progress information with percentage
287
+ */
288
+ declare function calculateProgress(current: number, total: number): ProgressInfo;
289
+ /**
290
+ * Extract field names from update data
291
+ * @param updateData - Data to be updated
292
+ * @returns Array of field names that will be affected
293
+ */
294
+ declare function getAffectedFields(updateData: Record<string, any>): string[];
295
+ /**
296
+ * Merge update data with existing document data
297
+ * @param existingData - Current document data
298
+ * @param updateData - Data to update
299
+ * @returns Merged data (shallow merge)
300
+ */
301
+ declare function mergeUpdateData(existingData: Record<string, any>, updateData: Record<string, any>): Record<string, any>;
302
+ /**
303
+ * Check if a value is a valid update data object
304
+ * @param value - Value to check
305
+ * @returns True if value is a valid update data object
306
+ */
307
+ declare function isValidUpdateData(value: any): value is Record<string, any>;
308
+ /**
309
+ * Format error message for failed operations
310
+ * @param error - Error object
311
+ * @param context - Additional context (e.g., document ID)
312
+ * @returns Formatted error message
313
+ */
314
+ declare function formatError(error: unknown, context?: string): string;
315
+
316
+ export { BatchUpdater, type CreateDocumentInput, type CreateOptions, type CreateResult, type DocumentSnapshot, type FieldValue, type LogEntry, type LogOptions, type OperationLog, type PreviewResult, type ProgressInfo, type UpdateOptions, type UpdateResult, type UpsertOptions, type UpsertResult, type WhereCondition, calculateProgress, createLogCollector, formatError, formatOperationLog, getAffectedFields, isValidUpdateData, mergeUpdateData, writeOperationLog };