refactor(userSchema): unique index definitions using partialFilterExpression instead of sparse

This commit is contained in:
Danny Avila 2024-02-16 10:05:48 -05:00
parent 18edd2660b
commit a1c7110a94
No known key found for this signature in database
GPG key ID: 72EC5F6AC04820A6

View file

@ -51,28 +51,18 @@ const userSchema = mongoose.Schema(
},
googleId: {
type: String,
unique: true,
sparse: true,
},
facebookId: {
type: String,
unique: true,
sparse: true,
},
openidId: {
type: String,
unique: true,
sparse: true,
},
githubId: {
type: String,
unique: true,
sparse: true,
},
discordId: {
type: String,
unique: true,
sparse: true,
},
plugins: {
type: Array,
@ -85,4 +75,25 @@ const userSchema = mongoose.Schema(
{ timestamps: true },
);
userSchema.index(
{ googleId: 1 },
{ unique: true, partialFilterExpression: { googleId: { $type: 'string' } } },
);
userSchema.index(
{ facebookId: 1 },
{ unique: true, partialFilterExpression: { facebookId: { $type: 'string' } } },
);
userSchema.index(
{ openidId: 1 },
{ unique: true, partialFilterExpression: { openidId: { $type: 'string' } } },
);
userSchema.index(
{ githubId: 1 },
{ unique: true, partialFilterExpression: { githubId: { $type: 'string' } } },
);
userSchema.index(
{ discordId: 1 },
{ unique: true, partialFilterExpression: { discordId: { $type: 'string' } } },
);
module.exports = userSchema;