Skip to content

Commit ef4cf80

Browse files
committed
feature: replace mongoose lean() on exec() and improve serialize interceptor
1 parent ba21335 commit ef4cf80

File tree

9 files changed

+117
-63
lines changed

9 files changed

+117
-63
lines changed

generators/auth/templates/mongodb/jwt/src/interceptors/serialization.interceptor.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import {
66
} from '@nestjs/common';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import _ from 'lodash';
910
import { getSerializeType } from '@decorators/serialization.decorator';
1011

12+
const getSerializer = (entity: any) => (data: any) => Object.assign(entity, data);
13+
1114
@Injectable()
1215
export default class SerializeInterceptor implements NestInterceptor {
1316
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1417
return next.handle().pipe(
1518
map((args) => {
1619
const SerializeType = getSerializeType(context.getHandler());
17-
const entity = new SerializeType();
18-
if (Array.isArray(args)) {
19-
return Object.assign(entity, { data: args });
20-
} else {
21-
return Object.assign(entity, args);
20+
const serializer = getSerializer(new SerializeType());
21+
22+
if (_.isArray(args)) {
23+
if (args && args[0] && args[0].toJSON) {
24+
return serializer({ data: args.map((doc) => doc.toJSON()) });
25+
}
26+
27+
return serializer({ data: args });
2228
}
29+
30+
if (args && args.toJSON) {
31+
return serializer(args.toJSON());
32+
}
33+
34+
return serializer(args);
2335
}),
2436
);
2537
}

generators/auth/templates/mongodb/jwt/src/modules/v1/users/users.repository.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,41 @@ export default class UsersRepository {
1717
verified: false,
1818
});
1919

20-
return newUser.toObject();
20+
return newUser.toJSON();
2121
}
2222

2323
public async getUnverifiedUserByEmail(email: string): Promise<User | null> {
2424
return this.usersModel.findOne({
2525
email,
2626
verified: false,
27-
}).lean();
27+
}).exec();
2828
}
2929

3030
public async getVerifiedUserByEmail(email: string): Promise<User | null> {
3131
return this.usersModel.findOne({
3232
email,
3333
verified: true,
34-
}).lean();
34+
}).exec();
3535
}
3636

3737
public async getById(id: Types.ObjectId): Promise<User | null> {
3838
return this.usersModel.findOne({
3939
_id: id,
40-
}, { password: 0 }).lean();
40+
}, { password: 0 }).exec();
4141
}
4242

4343
public async getVerifiedUserById(id: Types.ObjectId): Promise<User | null> {
4444
return this.usersModel.findOne({
4545
_id: id,
4646
verified: true,
47-
}, { password: 0 }).lean();
47+
}, { password: 0 }).exec();
4848
}
4949

5050
public async getUnverifiedUserById(id: Types.ObjectId): Promise<User | null> {
5151
return this.usersModel.findOne({
5252
_id: id,
5353
verified: false,
54-
}, { password: 0 }).lean();
54+
}, { password: 0 }).exec();
5555
}
5656

5757
public async updateById(id: Types.ObjectId, data: UpdateUserDto): Promise<User | null> {
@@ -60,14 +60,14 @@ export default class UsersRepository {
6060
{
6161
$set: data,
6262
},
63-
).lean();
63+
).exec();
6464
}
6565

6666
public getAll() {
67-
return this.usersModel.find().lean();
67+
return this.usersModel.find().exec();
6868
}
6969

7070
public getVerifiedUsers() {
71-
return this.usersModel.find({ verified: true }).lean();
71+
return this.usersModel.find({ verified: true }).exec();
7272
}
7373
}

generators/auth/templates/mongodb/oauth2/src/interceptors/serialization.interceptor.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import {
66
} from '@nestjs/common';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import _ from 'lodash';
910
import { getSerializeType } from '@decorators/serialization.decorator';
1011

12+
const getSerializer = (entity: any) => (data: any) => Object.assign(entity, data);
13+
1114
@Injectable()
1215
export default class SerializeInterceptor implements NestInterceptor {
1316
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1417
return next.handle().pipe(
1518
map((args) => {
1619
const SerializeType = getSerializeType(context.getHandler());
17-
const entity = new SerializeType();
18-
if (Array.isArray(args)) {
19-
return Object.assign(entity, { data: args });
20-
} else {
21-
return Object.assign(entity, args);
20+
const serializer = getSerializer(new SerializeType());
21+
22+
if (_.isArray(args)) {
23+
if (args && args[0] && args[0].toJSON) {
24+
return serializer({ data: args.map((doc) => doc.toJSON()) });
25+
}
26+
27+
return serializer({ data: args });
2228
}
29+
30+
if (args && args.toJSON) {
31+
return serializer(args.toJSON());
32+
}
33+
34+
return serializer(args);
2335
}),
2436
);
2537
}

generators/auth/templates/mongodb/oauth2/src/modules/v1/users/users.repository.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,47 @@ export default class UsersRepository {
1111
public async create(user: User): Promise<User> {
1212
const newUser = await this.usersModel.create(user);
1313

14-
return newUser.toObject();
14+
return newUser.toJSON();
1515
}
1616

1717
public async getByEmail(email: string): Promise<User | null> {
1818
return this.usersModel.findOne({
1919
email,
20-
}, { password: 0 }).lean();
20+
}, { password: 0 }).exec();
2121
}
2222

2323
public async getVerifiedUserById(id: Types.ObjectId): Promise<User | null> {
2424
return this.usersModel.findOne({
2525
_id: id,
2626
verified: true,
27-
}, { password: 0 }).lean();
27+
}, { password: 0 }).exec();
2828
}
2929

3030
public async getById(id: Types.ObjectId): Promise<User | null> {
3131
return this.usersModel.findOne({
3232
_id: id,
33-
}, { password: 0 }).lean();
33+
}, { password: 0 }).exec();
3434
}
3535

3636
public async getVerifiedUserByEmail(email: string) {
3737
return this.usersModel.findOne({
3838
email,
3939
verified: true,
40-
}).lean();
40+
}).exec();
4141
}
4242

4343
public async getUnverifiedUserByEmail(email: string) {
4444
return this.usersModel.findOne({
4545
email,
4646
verified: false,
47-
}).lean();
47+
}).exec();
4848
}
4949

5050
public getAll(): Query<UserDocument[], UserDocument> {
51-
return this.usersModel.find().lean();
51+
return this.usersModel.find().exec();
5252
}
5353

5454
public getVerifiedUsers(): Query<UserDocument[], UserDocument> {
55-
return this.usersModel.find({ verified: true }).lean();
55+
return this.usersModel.find({ verified: true }).exec();
5656
}
5757
}

generators/auth/templates/mongodb/passportLocal/src/modules/v1/users/users.repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ export default class UsersRepository {
2424
return this.userModel.findOne({
2525
email,
2626
verified: true,
27-
});
27+
}).exec();
2828
}
2929

3030
public async getVerifiedUserById(id: Types.ObjectId): Promise<User | null> {
3131
return this.userModel.findOne({
3232
_id: id,
3333
verified: true,
34-
});
34+
}).exec();
3535
}
3636

3737
public async getAll(): Promise<User[] | []> {
38-
return this.userModel.find({}, { password: false }).lean();
38+
return this.userModel.find({}, { password: false }).exec();
3939
}
4040
}

generators/auth/templates/mysql/jwt/src/interceptors/serialization.interceptor.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import {
66
} from '@nestjs/common';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import _ from 'lodash';
910
import { getSerializeType } from '@decorators/serialization.decorator';
1011

12+
const getSerializer = (entity: any) => (data: any) => Object.assign(entity, data);
13+
1114
@Injectable()
1215
export default class SerializeInterceptor implements NestInterceptor {
1316
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1417
return next.handle().pipe(
1518
map((args) => {
1619
const SerializeType = getSerializeType(context.getHandler());
17-
const entity = new SerializeType();
18-
if (Array.isArray(args)) {
19-
return Object.assign(entity, { data: args });
20-
} else {
21-
return Object.assign(entity, args);
20+
const serializer = getSerializer(new SerializeType());
21+
22+
if (_.isArray(args)) {
23+
if (args && args[0] && args[0].toJSON) {
24+
return serializer({ data: args.map((doc) => doc.toJSON()) });
25+
}
26+
27+
return serializer({ data: args });
2228
}
29+
30+
if (args && args.toJSON) {
31+
return serializer(args.toJSON());
32+
}
33+
34+
return serializer(args);
2335
}),
2436
);
2537
}

generators/auth/templates/mysql/oauth2/src/interceptors/serialization.interceptor.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import {
66
} from '@nestjs/common';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import _ from 'lodash';
910
import { getSerializeType } from '@decorators/serialization.decorator';
1011

12+
const getSerializer = (entity: any) => (data: any) => Object.assign(entity, data);
13+
1114
@Injectable()
1215
export default class SerializeInterceptor implements NestInterceptor {
1316
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1417
return next.handle().pipe(
1518
map((args) => {
1619
const SerializeType = getSerializeType(context.getHandler());
17-
const entity = new SerializeType();
18-
if (Array.isArray(args)) {
19-
return Object.assign(entity, { data: args });
20-
} else {
21-
return Object.assign(entity, args);
20+
const serializer = getSerializer(new SerializeType());
21+
22+
if (_.isArray(args)) {
23+
if (args && args[0] && args[0].toJSON) {
24+
return serializer({ data: args.map((doc) => doc.toJSON()) });
25+
}
26+
27+
return serializer({ data: args });
2228
}
29+
30+
if (args && args.toJSON) {
31+
return serializer(args.toJSON());
32+
}
33+
34+
return serializer(args);
2335
}),
2436
);
2537
}

generators/auth/templates/postgresql/jwt/src/interceptors/serialization.interceptor.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@ import {
66
} from '@nestjs/common';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import _ from 'lodash';
910
import { getSerializeType } from '@decorators/serialization.decorator';
1011

12+
const getSerializer = (entity: any) => (data: any) => Object.assign(entity, data);
13+
1114
@Injectable()
1215
export default class SerializeInterceptor implements NestInterceptor {
1316
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1417
return next.handle().pipe(
1518
map((args) => {
1619
const SerializeType = getSerializeType(context.getHandler());
17-
const entity = new SerializeType();
18-
if (Array.isArray(args)) {
19-
return Object.assign(entity, { data: args });
20-
} else {
21-
return Object.assign(entity, args);
20+
const serializer = getSerializer(new SerializeType());
21+
22+
if (_.isArray(args)) {
23+
if (args && args[0] && args[0].toJSON) {
24+
return serializer({ data: args.map((doc) => doc.toJSON()) });
25+
}
26+
27+
return serializer({ data: args });
2228
}
29+
30+
if (args && args.toJSON) {
31+
return serializer(args.toJSON());
32+
}
33+
34+
return serializer(args);
2335
}),
2436
);
2537
}

generators/mailer/templates/mongodb/passportLocal/src/modules/v1/users/users.repository.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,38 @@ export default class UsersRepository {
2222
}
2323

2424
public async getByEmail(email: string): Promise<User | null> {
25-
const foundUser: User | null = await this.userModel.findOne({
25+
return this.userModel.findOne({
2626
email,
27-
});
28-
return foundUser || null;
27+
}).exec();
2928
}
3029

3130
public async getById(id: Types.ObjectId): Promise<User | null> {
32-
const foundUser: User | null = await this.userModel.findOne({
31+
return this.userModel.findOne({
3332
_id: id,
34-
});
35-
return foundUser || null;
33+
}).exec();
3634
}
3735

3836
public async getAll(): Promise<User[] | []> {
39-
const foundUsers: User[] | [] = await this.userModel.find({}, { password: false }).lean();
37+
const foundUsers: User[] | [] = await this.userModel.find({}, { password: false }).exec();
4038

4139
return foundUsers.length > 0 ? foundUsers : [];
4240
}
4341

44-
public async getVerifiedUserByEmail(email: string): Promise<User | null> {
45-
const foundUser: User | null = await this.userModel.findOne({
42+
public getVerifiedUserByEmail(email: string): Promise<User | null> {
43+
return this.userModel.findOne({
4644
email,
4745
verified: true,
48-
});
49-
50-
return foundUser || null;
46+
}).exec();
5147
}
5248

53-
public async getVerifiedUserById(id: Types.ObjectId): Promise<User | null> {
54-
const foundUser: User | null = await this.userModel.findOne({
49+
public getVerifiedUserById(id: Types.ObjectId): Promise<User | null> {
50+
return this.userModel.findOne({
5551
_id: id,
5652
verified: true,
57-
});
58-
59-
return foundUser || null;
53+
}).exec();
6054
}
6155

6256
public findOneAndUpdate(_id: Types.ObjectId, fieldForUpdate: IUpdateUser) {
63-
return this.userModel.findByIdAndUpdate({ _id }, fieldForUpdate);
57+
return this.userModel.findByIdAndUpdate({ _id }, fieldForUpdate).exec();
6458
}
6559
}

0 commit comments

Comments
 (0)