Skip to content

Commit

Permalink
feat(mongodb): Allow to customize mongoose document options
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 bc407a0 commit 46db24a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('TypegooseQueryService', () => {

class TestEntityService extends TypegooseQueryService<TestEntity> {
constructor(@InjectModel(TestEntity) readonly model: ReturnModelType<typeof TestEntity>) {
super(model);
super(model, { documentToObjectOptions: { virtuals: true } });
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/query-typegoose/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { TypegooseQueryService } from './services';
export { TypegooseQueryService, TypegooseQueryServiceOpts } from './services';
export { NestjsQueryTypegooseModule } from './module';
37 changes: 21 additions & 16 deletions packages/query-typegoose/src/services/typegoose-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
QueryService,
UpdateManyResponse,
} from '@nestjs-query/core';
import { Logger, NotFoundException } from '@nestjs/common';
import { FilterQuery, UpdateQuery } from 'mongoose';
import { NotFoundException } from '@nestjs/common';
import { DocumentToObjectOptions, FilterQuery, UpdateQuery } from 'mongoose';
import { ReturnModelType } from '@typegoose/typegoose';
import escapeRegExp from 'lodash.escaperegexp';

export interface TypegooseQueryServiceOpts {
documentToObjectOptions?: DocumentToObjectOptions;
}

const mongoOperatorMapper: { [k: string]: string } = {
eq: '$eq',
neq: '$ne',
Expand All @@ -28,25 +32,27 @@ const mongoOperatorMapper: { [k: string]: string } = {
};

/**
* Base class for all query services that use a `typeorm` Repository.
* Base class for all query services that use Typegoose.
*
* @example
*
* ```ts
* @QueryService(TodoItemEntity)
* export class TodoItemService extends TypeOrmQueryService<TodoItemEntity> {
* export class TodoItemService extends TypegooseQueryService<TodoItemEntity> {
* constructor(
* @InjectRepository(TodoItemEntity) repo: Repository<TodoItemEntity>,
* @InjectModel(TodoItemEntity) model: ReturnModelType<typeof TodoItemEntity>,
* ) {
* super(repo);
* super(model);
* }
* }
* ```
*/
export class TypegooseQueryService<Entity> implements QueryService<Entity> {
protected readonly logger = new Logger(TypegooseQueryService.name);
protected readonly documentToObjectOptions: DocumentToObjectOptions;

constructor(readonly Model: ReturnModelType<new () => Entity>) {}
constructor(readonly Model: ReturnModelType<new () => Entity>, opts?: TypegooseQueryServiceOpts) {
this.documentToObjectOptions = opts?.documentToObjectOptions || { virtuals: true };
}

protected buildExpression(filter: Filter<Entity>): FilterQuery<new () => Entity> {
return Object.entries(filter).reduce((prev: FilterQuery<new () => Entity>, [key, value]) => {
Expand Down Expand Up @@ -81,7 +87,6 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
$regex: regExp,
};
}
this.logger.error(`Operator ${fieldKey} not supported yet`);
return prevCondition;
},
{},
Expand All @@ -93,7 +98,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
}, {});
}

private getSchemaKey(key: string) {
private getSchemaKey(key: string): string {
return key === 'id' ? '_id' : key;
}

Expand Down Expand Up @@ -122,7 +127,7 @@ export class TypegooseQueryService<Entity> implements QueryService<Entity> {
})),
},
).exec();
return entities.map((doc) => doc.toObject({ virtuals: true }) as Entity);
return entities.map((doc) => doc.toObject(this.documentToObjectOptions) as Entity);
}

aggregate(filter: Filter<Entity>, aggregate: AggregateQuery<Entity>): Promise<AggregateResponse<Entity>> {
Expand All @@ -144,7 +149,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({ virtuals: true }) as Entity;
return doc?.toObject(this.documentToObjectOptions) as Entity;
}

/**
Expand Down Expand Up @@ -180,7 +185,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({ virtuals: true }) as Entity;
return doc.toObject(this.documentToObjectOptions) as Entity;
}

/**
Expand Down Expand Up @@ -213,7 +218,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({ virtuals: true }) as Entity;
return doc.toObject(this.documentToObjectOptions) as Entity;
}
throw new NotFoundException(`Unable to find ${this.Model.modelName} with id: ${id}`);
}
Expand Down Expand Up @@ -254,7 +259,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({ virtuals: true }) as Entity;
return doc.toObject(this.documentToObjectOptions) as Entity;
}
throw new NotFoundException(`Unable to find ${this.Model.modelName} with id: ${id}`);
}
Expand Down Expand Up @@ -364,7 +369,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({ virtuals: true }));
map.set(curr, relationDoc?.toObject(this.documentToObjectOptions));
}
return map;
}, Promise.resolve(new Map<Entity, Relation | undefined>()));
Expand Down

0 comments on commit 46db24a

Please sign in to comment.