Skip to content

Commit 895a794

Browse files
committed
feat: add transform schema to json
1 parent d63703b commit 895a794

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/common/base/base.service.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
12
import { Model } from 'mongoose';
23
import { IQuery } from '../interfaces/query.interface';
34

@@ -19,16 +20,19 @@ export abstract class BaseService<T> implements IBaseService<T> {
1920

2021
async findMany(query: IQuery): Promise<IFindMany<T>> {
2122
const [total, docs] = await Promise.all([
22-
this.model.countDocuments(query.filter).limit(query.limit),
23+
this.model.countDocuments(query.filter),
2324
this.model
2425
.find(query.filter)
2526
.limit(query.limit)
2627
.skip(query.skip)
27-
.sort(query.sort),
28+
.sort(query.sort)
29+
.exec(),
2830
]);
2931

32+
// @ts-ignore
33+
const docsToJson = docs.map((doc) => doc?.toJSON());
3034
return {
31-
docs,
35+
docs: docsToJson,
3236
total,
3337
limit: query.limit,
3438
page: query.skip / query.limit + 1,
@@ -37,6 +41,8 @@ export abstract class BaseService<T> implements IBaseService<T> {
3741
}
3842

3943
async findOne(id: number): Promise<T | null> {
40-
return this.model.findOne({ id }).lean();
44+
const found = await this.model.findOne({ id });
45+
// @ts-ignore
46+
return found.toJSON();
4147
}
4248
}

0 commit comments

Comments
 (0)