Skip to content

Commit

Permalink
feat(mongodb): Include virtuals on document responses
Browse files Browse the repository at this point in the history
  • Loading branch information
marian2js authored and doug-martin committed Oct 16, 2020
1 parent 0402184 commit bc407a0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export class TestReference {

@prop({ required: true })
name!: string;

getOutputData(): TestReference {
return {
...this,
id: this.id,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ export class TestEntity {
@prop({ ref: TestReference })
testReference?: Ref<TestReference>;

getInputData() {
getInputData(): Partial<TestEntity> {
return {
stringType: this.stringType,
boolType: this.boolType,
numberType: this.numberType,
dateType: this.dateType,
};
}

getOutputData(): TestEntity {
return {
...this,
id: this.id,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('TypegooseQueryService', () => {
it('should support eq operator', async () => {
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.query({ filter: { stringType: { eq: 'foo1' } } });
return expect(queryResult).toEqual([TEST_ENTITIES[0]]);
return expect(queryResult).toEqual([TEST_ENTITIES[0].getOutputData()]);
});

it('should support neq operator', async () => {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('TypegooseQueryService', () => {
it('should support in operator', async () => {
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.query({ filter: { numberType: { in: [1, 2, 3] } } });
return expect(queryResult).toEqual([TEST_ENTITIES[0], TEST_ENTITIES[1], TEST_ENTITIES[2]]);
return expect(queryResult).toEqual(TEST_ENTITIES.slice(0, 3).map((e) => e.getOutputData()));
});

it('should support notIn operator', async () => {
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('TypegooseQueryService', () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const found = await queryService.findById(entity.id.toString());
expect(found).toEqual(entity);
expect(found).toEqual(entity.getOutputData());
});

it('return undefined if not found', async () => {
Expand All @@ -153,7 +153,7 @@ describe('TypegooseQueryService', () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const found = await queryService.getById(entity.id.toString());
expect(found).toEqual(entity);
expect(found).toEqual(entity.getOutputData());
});

it('return undefined if not found', () => {
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('TypegooseQueryService', () => {
it('remove the entity', async () => {
const queryService = moduleRef.get(TestEntityService);
const deleted = await queryService.deleteOne(TEST_ENTITIES[0].id.toString());
expect(deleted).toEqual(TEST_ENTITIES[0]);
expect(deleted).toEqual(TEST_ENTITIES[0].getOutputData());
});

it('call fail if the entity is not found', async () => {
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('TypegooseQueryService', () => {
const queryService = moduleRef.get(TestEntityService);
const updated = await queryService.updateOne(TEST_ENTITIES[0].id.toString(), { stringType: 'updated' });
expect(updated).toEqual({
...TEST_ENTITIES[0],
...TEST_ENTITIES[0].getOutputData(),
stringType: 'updated',
});
});
Expand Down Expand Up @@ -287,7 +287,7 @@ describe('TypegooseQueryService', () => {
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.findRelation(TestReference, 'testReference', [entity]);

expect(queryResult.values().next().value).toEqual(TEST_REFERENCES[0]);
expect(queryResult.values().next().value).toEqual(TEST_REFERENCES[0].getOutputData());
});

it('should return undefined select if no results are found.', async () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/query-typegoose/src/services/typegoose-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
})),
},
).exec();
return entities.map((doc) => doc.toObject() as Entity);
return entities.map((doc) => doc.toObject({ virtuals: true }) as Entity);
}

aggregate(filter: Filter<Entity>, aggregate: AggregateQuery<Entity>): Promise<AggregateResponse<Entity>> {
Expand All @@ -144,7 +144,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
*/
async findById(id: string): Promise<Entity | undefined> {
const doc = await this.Model.findById(id);
return doc?.toObject() as Entity;
return doc?.toObject({ virtuals: true }) as Entity;
}

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
async createOne<C extends DeepPartial<Entity>>(record: C): Promise<Entity> {
const doc = new this.Model(record);
await doc.save(record);
return doc.toObject() as Entity;
return doc.toObject({ virtuals: true }) as Entity;
}

/**
Expand Down Expand Up @@ -213,7 +213,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
this.ensureIdIsNotPresent(update);
const doc = await this.Model.findByIdAndUpdate(id, update as UpdateQuery<new () => Entity>, { new: true });
if (doc) {
return doc.toObject() as Entity;
return doc.toObject({ virtuals: true }) as Entity;
}
throw new NotFoundException(`Unable to find ${this.Model.modelName} with id: ${id}`);
}
Expand Down Expand Up @@ -254,7 +254,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
async deleteOne(id: string | number): Promise<Entity> {
const doc = await this.Model.findByIdAndDelete(id);
if (doc) {
return doc.toObject() as Entity;
return doc.toObject({ virtuals: true }) as Entity;
}
throw new NotFoundException(`Unable to find ${this.Model.modelName} with id: ${id}`);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
const referenceId = curr[relationName as keyof Entity];
if (referenceId) {
const relationDoc = await relationModel.findOne(referenceId);
map.set(curr, relationDoc?.toObject());
map.set(curr, relationDoc?.toObject({ virtuals: true }));
}
return map;
}, Promise.resolve(new Map<Entity, Relation | undefined>()));
Expand Down

0 comments on commit bc407a0

Please sign in to comment.