firestore-snapshot-utils
v2.2.1
Published
[](https://github.com/ericvera/firestore-snapshot-utils/blob/master/LICENSE) [)
// Run your database operations...
// After operation
const afterDocs = await getDBSnapshot(firestore.collection('users'))
// Compare snapshots
const changes = getDBSnapshotChanges(beforeDocs, afterDocs, {
// Mask sensitive fields
users: ['id', 'createdAt'],
})
// Generate readable diff
console.log(getDiffFromDBSnapshotChanges(changes))📚 API Reference
getDBSnapshot
function getDBSnapshot(
queries: Query | Query[],
): Promise<QueryDocumentSnapshot[]>Gets documents from one or more Firestore queries as a flat array.
Example:
// Single collection
const docs = await getDBSnapshot(firestore.collection('users'))
// Multiple collections
const docs = await getDBSnapshot([
firestore.collection('users'),
firestore.collection('products'),
])getDBSnapshotChanges
function getDBSnapshotChanges(
beforeDocs: QueryDocumentSnapshot[],
afterDocs: QueryDocumentSnapshot[],
maskKeys: Record<string, string[]> = {},
debugOptions: { logTimestamps?: boolean } = {},
): DBSnapshotChangesCompares two document sets and identifies what changed.
Example:
const changes = getDBSnapshotChanges(beforeDocs, afterDocs, {
users: ['id', 'createdAt'], // Mask these fields
products: ['updatedAt'],
})getDiffFromDBSnapshotChanges
function getDiffFromDBSnapshotChanges(changes: DBSnapshotChanges): stringCreates a human-readable diff from database changes.
Example with Jest:
expect(getDiffFromDBSnapshotChanges(changes)).toMatchInlineSnapshot()normalizeData
function normalizeData<T = unknown>(
data: T,
options?: { logTimestamps?: boolean },
): TNormalizes Firestore Timestamp and Buffer objects in any data structure for deterministic testing.
Example:
import { Timestamp } from 'firebase-admin/firestore'
const testData = {
createdAt: new Timestamp(1234567890, 0),
user: {
lastLogin: new Timestamp(1234567891, 0),
},
}
const normalized = normalizeData(testData)
// Use in tests for stable snapshots
expect(normalizeData(actualData)).toMatchInlineSnapshot(`
Object {
"createdAt": "/Timestamp 0000/",
"user": Object {
"lastLogin": "/Timestamp 0001/",
},
}
`)🧪 Testing Example
describe('User profile update', () => {
it('should update user data correctly', async () => {
// Before state
const beforeDocs = await getDBSnapshot(
firestore.collection('users').where('id', '==', userId),
)
// Run operation
await updateUserProfile(userId, { name: 'New Name' })
// After state
const afterDocs = await getDBSnapshot(
firestore.collection('users').where('id', '==', userId),
)
// Compare with masked timestamps
const changes = getDBSnapshotChanges(beforeDocs, afterDocs, {
users: ['updatedAt'],
})
// Verify against snapshot
expect(getDiffFromDBSnapshotChanges(changes)).toMatchInlineSnapshot(`
"DB DIFF
--------------------------------
MODIFIED (path: users/[ID])
--------------------------------
- Expected
+ Received
Object {
- "name": "Old Name",
+ "name": "New Name",
}"
`)
})
})📝 Notes
- Timestamps are automatically normalized for consistent comparisons
- Buffer objects are converted to base64url strings for reliable diff generation
- The
normalizeDatafunction can be used standalone to normalize test data with Timestamps and Buffers
🤝 AI Disclosure
This library's documentation has been enhanced with AI assistance. All code and functionality has been carefully designed and tested by humans.
📄 License
MIT
