Skip to content

Commit 2d76e4d

Browse files
committed
feat: add documentSnapshots method to FirestoreCollection and update tests
1 parent 9469502 commit 2d76e4d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default [
5555
'@typescript-eslint/no-unused-expressions': 'off',
5656
'multiline-comment-style': 'off',
5757
'@typescript-eslint/member-ordering': 'off',
58+
'@stylistic/arrow-parens': 'off',
5859
'max-len': 'off',
5960
'quote-props': ['error', 'consistent-as-needed'],
6061
'object-property-newline': 'off',

src/firestore/FirestoreCollection.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { FirestoreSnapshot } from './FirestoreSnapshot';
12
import type { Firestore } from 'firebase-admin/firestore';
23
import { FirestoreDocument } from './FirestoreDocument';
34
import type { FirestorePath } from './FirestorePath';
5+
import type { Flattable } from '@stevenkellner/typescript-common-functionality';
46

57
export class FirestoreCollection<
68
Documents extends Record<string, FirestoreDocument<any, any>>
@@ -11,6 +13,15 @@ export class FirestoreCollection<
1113
private readonly path: FirestorePath
1214
) {}
1315

16+
public async documentSnapshots(): Promise<FirestoreSnapshot<FirestoreDocument.ValuesOf<Documents[string]>>[]> {
17+
const collection = this.firestore.collection(this.path.fullPath);
18+
const documents = await collection.listDocuments();
19+
return Promise.all(documents.map(async document => {
20+
const snapshot = await document.get() as FirebaseFirestore.DocumentSnapshot<Flattable.Flatten<FirestoreDocument.ValuesOf<Documents[string]>>>;
21+
return new FirestoreSnapshot(snapshot);
22+
}));
23+
}
24+
1425
public document<Key extends keyof Documents & string>(key: Key): Documents[Key] {
1526
return new FirestoreDocument(this.firestore, this.path.appending(key)) as Documents[Key];
1627
}

test/firestore/firestore-admin.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export type FirestoreScheme = FirestoreDocument<never, {
1212
v10: boolean;
1313
}>;
1414
}>;
15+
col3: FirestoreCollection<{
16+
[key: string]: FirestoreDocument<{
17+
v11: string;
18+
}>;
19+
}>;
1520
}>;
1621
doc3: FirestoreDocument<never, {
1722
col2: FirestoreCollection<{
@@ -133,4 +138,13 @@ describe('Firestore admin', () => {
133138
const snapshot = await baseDocument.collection('baseCollection').document('doc4').snapshot();
134139
expect(snapshot.exists).toBeEqual(false);
135140
});
141+
142+
it('should get document snapshots', async () => {
143+
await baseDocument.collection('baseCollection').document('doc1').collection('col3').document('d1').set({ v11: 'value1' });
144+
await baseDocument.collection('baseCollection').document('doc1').collection('col3').document('d2').set({ v11: 'value2' });
145+
await baseDocument.collection('baseCollection').document('doc1').collection('col3').document('d3').set({ v11: 'value3' });
146+
const snapshots = await baseDocument.collection('baseCollection').document('doc1').collection('col3').documentSnapshots();
147+
expect(snapshots.length).toBeEqual(3);
148+
expect(snapshots.map(s => s.data.v11)).toHaveSameMembers(['value1', 'value2', 'value3']);
149+
});
136150
});

0 commit comments

Comments
 (0)