Skip to content

Commit

Permalink
perf(core): update username index (#4614)
Browse files Browse the repository at this point in the history
* perf(core): update username index

* fix: index filter expression
  • Loading branch information
dolcalmi authored Oct 18, 2024
1 parent 7c32599 commit 69c2045
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions core/api/src/services/mongoose/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { caseInsensitiveRegex, parseRepositoryError } from "./utils"
import { parseRepositoryError } from "./utils"

import { AccountStatus } from "@/domain/accounts"
import {
Expand Down Expand Up @@ -44,7 +44,9 @@ export const AccountsRepository = (): IAccountsRepository => {
username: Username,
): Promise<Account | RepositoryError> => {
try {
const result = await Account.findOne({ username: caseInsensitiveRegex(username) })
const result = await Account.findOne({ username })
.collation({ locale: "en", strength: 2 })
.hint({ username: 1 })
if (!result) {
return new CouldNotFindAccountFromUsernameError(username)
}
Expand Down
9 changes: 7 additions & 2 deletions core/api/src/services/mongoose/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,18 @@ const AccountSchema = new Schema<AccountRecord>(

username: {
type: String,
match: [UsernameRegex, "Username can only have alphabets, numbers and underscores"],
match: [
UsernameRegex,
"Username can only have alphabets, numbers, and underscores",
],
minlength: 3,
maxlength: 50,
index: {
unique: true,
collation: { locale: "en", strength: 2 },
partialFilterExpression: { username: { $type: "string" } },
partialFilterExpression: {
username: { $type: "string", $exists: true },
},
},
},
contactEnabled: {
Expand Down

0 comments on commit 69c2045

Please sign in to comment.