This package provides additional utility functions for mongoose like Paginator, MongoParamId decorator and much more
- Create a
.npmrc
in the root folder and add the following lines.
//npm.pkg.github.com/:_authToken=TOKEN
@goapptiv:registry=https://npm.pkg.github.com/
-
Create a personal token with read:packages permission and replace the
TOKEN
with your personal token in the above mentioned file. -
Install the package using the following command
npm install @goapptiv/mongoose-helper-nestjs
This decorator helps to validate the param which is of type Mongo Object Id
import { MongoIdParam } from '@goapptiv/mongoose-helper-nestjs';
/**
* find a customer by id
*/
@Get('id/:id')
@AuthGuard(Role.ADMIN, Role.HQ_MANAGER)
@HqGuard(HqGuardPath.PARAMS, 'id')
async findById(
@MongoIdParam('id') id: string, // <------
@Query('imageUrls') imageUrls: string,
): Promise<ResponseSuccess | ResponseError> {
const withImages = imageUrls === 'true' ? true : false;
const customer = await this.customerService.findById(
new MongooseTypes.ObjectId(id),
withImages,
);
return RestResponse.success(customer);
}
import { GaBaseRepository } from '@goapptiv/mongoose-helper-nestjs';
export class CustomerRepository extends GaBaseRepository<Customer> {
...
fetch(
filters: FetchFiltersDAO,
paginateOptions: PaginateOptions,
): Promise<PaginateResult<Customer>> {
return this.aggregatePaginate(aggregateQuery, paginateOptions);
}
}