Skip to content

Commit

Permalink
fix: fetching entity w/o id within firestore doc
Browse files Browse the repository at this point in the history
  • Loading branch information
skalashnyk committed Nov 19, 2019
1 parent 9305194 commit fed065a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/AbstractFirestoreRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export abstract class AbstractFirestoreRepository<T extends IEntity>
// 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);
Expand Down
26 changes: 25 additions & 1 deletion src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
});

Expand Down Expand Up @@ -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;
});
});
});

0 comments on commit fed065a

Please sign in to comment.