Skip to content

Commit dc21b89

Browse files
committed
fix: phone email employeeId null unique index
1 parent f7ce9bc commit dc21b89

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
mongodb:
3-
image: mongo
3+
image: mongo:8
44
ports:
55
- 27017:27017
66
redis:

openapi.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"hash": "9c7b53ed249543d1b5994078e776a3a52c540309332df65c188b206f250d095f",
2+
"hash": "3585f972f863d385615370bca5855850c404d7b5d1e8aff5b58464e237b49f00",
33
"openapi": "3.0.0",
44
"paths": {
55
"/hello": {
@@ -3818,6 +3818,7 @@
38183818
"in": "query",
38193819
"description": "邮箱",
38203820
"schema": {
3821+
"nullable": true,
38213822
"type": "string"
38223823
}
38233824
},
@@ -4107,6 +4108,7 @@
41074108
"in": "query",
41084109
"description": "邮箱",
41094110
"schema": {
4111+
"nullable": true,
41104112
"type": "string"
41114113
}
41124114
},
@@ -4765,6 +4767,7 @@
47654767
"in": "query",
47664768
"description": "邮箱",
47674769
"schema": {
4770+
"nullable": true,
47684771
"type": "string"
47694772
}
47704773
},
@@ -5654,6 +5657,7 @@
56545657
},
56555658
"email": {
56565659
"type": "string",
5660+
"nullable": true,
56575661
"description": "邮箱"
56585662
},
56595663
"name": {
@@ -5738,6 +5742,7 @@
57385742
},
57395743
"employeeId": {
57405744
"type": "string",
5745+
"nullable": true,
57415746
"description": "员工编号"
57425747
},
57435748
"permissions": {
@@ -7177,6 +7182,7 @@
71777182
},
71787183
"email": {
71797184
"type": "string",
7185+
"nullable": true,
71807186
"description": "邮箱"
71817187
},
71827188
"name": {
@@ -7238,6 +7244,7 @@
72387244
},
72397245
"employeeId": {
72407246
"type": "string",
7247+
"nullable": true,
72417248
"description": "员工编号"
72427249
},
72437250
"active": {
@@ -7282,6 +7289,7 @@
72827289
},
72837290
"email": {
72847291
"type": "string",
7292+
"nullable": true,
72857293
"description": "邮箱"
72867294
},
72877295
"name": {
@@ -7366,6 +7374,7 @@
73667374
},
73677375
"employeeId": {
73687376
"type": "string",
7377+
"nullable": true,
73697378
"description": "员工编号"
73707379
},
73717380
"permissions": {
@@ -7748,6 +7757,7 @@
77487757
},
77497758
"email": {
77507759
"type": "string",
7760+
"nullable": true,
77517761
"description": "邮箱"
77527762
},
77537763
"groups": {

src/user/entities/user.entity.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export class UserDoc {
5151
@IsOptional()
5252
@IsString()
5353
@IsEmail()
54-
@Prop({ unique: true, sparse: true })
55-
email?: string;
54+
@Prop()
55+
email?: string | null;
5656

5757
/**
5858
* 姓名
@@ -173,7 +173,7 @@ export class UserDoc {
173173
@IsOptional()
174174
@IsString()
175175
@IsMobilePhone('zh-CN') // 中国大陆地区手机号
176-
@Prop({ unique: true, sparse: true })
176+
@Prop()
177177
phone?: string | null;
178178

179179
/**
@@ -220,8 +220,8 @@ export class UserDoc {
220220
*/
221221
@IsOptional()
222222
@IsString()
223-
@Prop({ unique: true, sparse: true })
224-
employeeId?: string;
223+
@Prop()
224+
employeeId?: string | null;
225225

226226
/**
227227
* 权限
@@ -289,3 +289,17 @@ export type UserDocument = User & Document;
289289
UserSchema.virtual('hasPassword').get(function (): boolean {
290290
return !!this.password;
291291
});
292+
293+
// add partial unique indexes to allow multiple nulls
294+
UserSchema.index(
295+
{ email: 1 },
296+
{ unique: true, partialFilterExpression: { email: { $type: 'string' } } }
297+
);
298+
UserSchema.index(
299+
{ phone: 1 },
300+
{ unique: true, partialFilterExpression: { phone: { $type: 'string' } } }
301+
);
302+
UserSchema.index(
303+
{ employeeId: 1 },
304+
{ unique: true, partialFilterExpression: { employeeId: { $type: 'string' } } }
305+
);

0 commit comments

Comments
 (0)