Skip to content

Commit

Permalink
feat(stats): change useMaster to useReplica in user model
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Apr 15, 2021
1 parent aed159b commit adb4f9c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ export const User = <UserTypeStatic>sequelize.define(
}
},
/**
* Use the master database for read queries. ALWAYS ENABLE THIS.
* Use the replica database for read queries.
*/
useMasterDb() {
useReplica() {
return {
useMaster: true,
useMaster: false,
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class StatisticsRepository implements interfaces.StatisticsRepository {

if (userCount == null) {
// Allow use of read replica
userCount = await User.unscoped().count()
userCount = await User.scope('useReplica').count()
this.trySetCache(USER_COUNT_KEY, userCount.toString())
}

Expand Down
16 changes: 6 additions & 10 deletions src/server/repositories/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,29 @@ export class UserRepository implements UserRepositoryInterface {
public findById: (userId: number) => Promise<StorableUser | null> = async (
userId,
) => {
return this.userMapper.persistenceToDto(
await User.scope('useMasterDb').findByPk(userId),
)
return this.userMapper.persistenceToDto(await User.findByPk(userId))
}

public findByEmail: (email: string) => Promise<StorableUser | null> = async (
email,
) => {
return this.userMapper.persistenceToDto(
await User.scope('useMasterDb').findOne({ where: { email } }),
await User.findOne({ where: { email } }),
)
}

public findOrCreateWithEmail: (email: string) => Promise<StorableUser> = (
email,
) => {
return User.scope('useMasterDb')
.findOrCreate({ where: { email } })
.then(([user, _]) => user)
return User.findOrCreate({ where: { email } }).then(([user, _]) => user)
}

public findOneUrlForUser: (
userId: number,
shortUrl: string,
) => Promise<StorableUrl | null> = async (userId, shortUrl) => {
const user = await User.scope([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['includeShortUrl', shortUrl],
},
Expand All @@ -82,7 +78,7 @@ export class UserRepository implements UserRepositoryInterface {
shortUrl: string,
) => Promise<StorableUser | null> = async (shortUrl) => {
const user = await User.scope([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['includeShortUrl', shortUrl],
},
Expand All @@ -96,7 +92,7 @@ export class UserRepository implements UserRepositoryInterface {
) => Promise<UrlsPaginated> = async (conditions) => {
const notFoundMessage = 'Urls not found'
const userCountAndArray = await User.scope([
{ method: ['useMasterDb'] },
{ method: ['defaultScope'] },
{
method: ['urlsWithQueryConditions', conditions],
},
Expand Down

0 comments on commit adb4f9c

Please sign in to comment.