Skip to content

Commit

Permalink
refactor(userSchema): unique index definitions using `partialFilterEx…
Browse files Browse the repository at this point in the history
…pression` instead of `sparse`
  • Loading branch information
danny-avila committed Feb 16, 2024
1 parent fe0ef2c commit 8ec3ea9
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions api/models/schema/userSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

0 comments on commit 8ec3ea9

Please sign in to comment.