diff --git a/src/AbstractFirestoreRepository.ts b/src/AbstractFirestoreRepository.ts index be8bd809..bacc4fc0 100644 --- a/src/AbstractFirestoreRepository.ts +++ b/src/AbstractFirestoreRepository.ts @@ -104,7 +104,7 @@ export abstract class AbstractFirestoreRepository // tslint:disable-next-line:no-unnecessary-type-assertion const entity = plainToClass( this.colMetadata.entity, - this.transformFirestoreTypes(doc.data() as T) + {id:doc.id, ...this.transformFirestoreTypes(doc.data() as T)} ) as T; this.initializeSubCollections(entity); diff --git a/src/BaseFirestoreRepository.spec.ts b/src/BaseFirestoreRepository.spec.ts index cbe3beb8..9ddcbef1 100644 --- a/src/BaseFirestoreRepository.spec.ts +++ b/src/BaseFirestoreRepository.spec.ts @@ -309,7 +309,7 @@ describe('BaseFirestoreRepository', () => { try { await bandRepository.update(band); } catch (error) { - expect(error[0].constraints.isEmail).to.equal('Invalid email!') + expect(error[0].constraints.isEmail).to.equal('Invalid email!'); } }); @@ -701,4 +701,28 @@ describe('BaseFirestoreRepository', () => { }); }); }); + + describe('fetching documents created w/o id inside object', () => { + let docId: string = null; + beforeEach(async () => { + const bandWithoutId = new Band(); + docId = (await firestore.collection(bandRepository.collectionPath).add(bandWithoutId)).id; + }); + + it('Get by id - entity should contain id', async () => { + const band = await bandRepository.findById(docId); + expect(band).to.have.property('id'); + expect(band.id).to.equal(docId); + }); + + it('Get list - all entities should contain id', async () => { + const bands = await bandRepository.find(); + for (const b of bands) { + expect(b.id).not.to.be.undefined; + } + + const possibleDocWithoutId = bands.find(band => band.id === docId); + expect(possibleDocWithoutId).not.to.be.undefined; + }); + }); });