use read replica for selecting library items, labels and highlights

This commit is contained in:
Hongbo Wu 2024-06-27 19:02:15 +08:00
parent fb3d2eada6
commit 2ed480bb49
9 changed files with 83 additions and 39 deletions

View file

@ -26,6 +26,7 @@ export const aiSummarize = async (jobData: AISummarizeJobData) => {
.findById(jobData.libraryItemId),
{
uid: jobData.userId,
replicationMode: 'replica',
}
)
if (!libraryItem || libraryItem.state !== LibraryItemState.Succeeded) {

View file

@ -22,6 +22,7 @@ export const explainText = async (
tx.withRepository(libraryItemRepository).findById(libraryItemId),
{
uid: userId,
replicationMode: 'replica',
}
)

View file

@ -183,6 +183,7 @@ export const userDigestEligible = async (uid: string): Promise<boolean> => {
},
{
uid,
replicationMode: 'replica',
}
)
@ -194,6 +195,7 @@ export const userDigestEligible = async (uid: string): Promise<boolean> => {
},
{
uid,
replicationMode: 'replica',
}
)

View file

@ -23,10 +23,14 @@ export type HighlightEvent = Merge<
export const batchGetHighlightsFromLibraryItemIds = async (
libraryItemIds: readonly string[]
): Promise<Highlight[][]> => {
const highlights = await authTrx(async (tx) =>
tx.getRepository(Highlight).find({
where: { libraryItem: { id: In(libraryItemIds as string[]) } },
})
const highlights = await authTrx(
async (tx) =>
tx.getRepository(Highlight).find({
where: { libraryItem: { id: In(libraryItemIds as string[]) } },
}),
{
replicationMode: 'replica',
}
)
return libraryItemIds.map((libraryItemId) =>
@ -261,6 +265,7 @@ export const findHighlightById = async (
},
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -280,6 +285,7 @@ export const findHighlightsByLibraryItemId = async (
}),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -329,6 +335,7 @@ export const searchHighlights = async (
},
{
uid: userId,
replicationMode: 'replica',
}
)
}

View file

@ -4,12 +4,16 @@ import { authTrx } from '../repository'
export const batchGetPublicItems = async (
ids: readonly string[]
): Promise<Array<PublicItem | undefined>> => {
const publicItems = await authTrx(async (tx) =>
tx
.getRepository(PublicItem)
.createQueryBuilder('public_item')
.where('public_item.id IN (:...ids)', { ids })
.getMany()
const publicItems = await authTrx(
async (tx) =>
tx
.getRepository(PublicItem)
.createQueryBuilder('public_item')
.where('public_item.id IN (:...ids)', { ids })
.getMany(),
{
replicationMode: 'replica',
}
)
return ids.map((id) => publicItems.find((pi) => pi.id === id))
@ -50,6 +54,7 @@ export const findUnseenPublicItems = async (
.getMany(),
{
uid: userId,
replicationMode: 'replica',
}
)
}

View file

@ -25,11 +25,15 @@ export type LabelEvent = Merge<
export const batchGetLabelsFromLibraryItemIds = async (
libraryItemIds: readonly string[]
): Promise<Label[][]> => {
const labels = await authTrx(async (tx) =>
tx.getRepository(EntityLabel).find({
where: { libraryItemId: In(libraryItemIds as string[]) },
relations: ['label'],
})
const labels = await authTrx(
async (tx) =>
tx.getRepository(EntityLabel).find({
where: { libraryItemId: In(libraryItemIds as string[]) },
relations: ['label'],
}),
{
replicationMode: 'replica',
}
)
return libraryItemIds.map((libraryItemId) =>
@ -42,11 +46,15 @@ export const batchGetLabelsFromLibraryItemIds = async (
export const batchGetLabelsFromHighlightIds = async (
highlightIds: readonly string[]
): Promise<Label[][]> => {
const labels = await authTrx(async (tx) =>
tx.getRepository(EntityLabel).find({
where: { highlightId: In(highlightIds as string[]) },
relations: ['label'],
})
const labels = await authTrx(
async (tx) =>
tx.getRepository(EntityLabel).find({
where: { highlightId: In(highlightIds as string[]) },
relations: ['label'],
}),
{
replicationMode: 'replica',
}
)
return highlightIds.map((highlightId) =>
@ -270,6 +278,7 @@ export const findLabelsByIds = async (
},
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -357,6 +366,7 @@ export const findLabelsByUserId = async (userId: string): Promise<Label[]> => {
}),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -369,6 +379,7 @@ export const findLabelById = async (id: string, userId: string) => {
.findOneBy({ id, user: { id: userId } }),
{
uid: userId,
replicationMode: 'replica',
}
)
}

View file

@ -138,13 +138,17 @@ export const batchGetLibraryItems = async (ids: readonly string[]) => {
const select = getColumns(libraryItemRepository).filter(
(select) => ['originalContent', 'readableContent'].indexOf(select) === -1
)
const items = await authTrx(async (tx) =>
tx.getRepository(LibraryItem).find({
select,
where: {
id: In(ids as string[]),
},
})
const items = await authTrx(
async (tx) =>
tx.getRepository(LibraryItem).find({
select,
where: {
id: In(ids as string[]),
},
}),
{
replicationMode: 'replica',
}
)
return ids.map((id) => items.find((item) => item.id === id) || undefined)
@ -709,6 +713,7 @@ export const countLibraryItems = async (args: SearchArgs, userId: string) => {
async (tx) => createSearchQueryBuilder(args, userId, tx).getCount(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -732,6 +737,7 @@ export const searchLibraryItems = async (
.getMany(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -778,6 +784,7 @@ export const findRecentLibraryItems = async (
.getMany(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -803,6 +810,7 @@ export const findLibraryItemsByIds = async (
.getMany(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -832,6 +840,7 @@ export const findLibraryItemById = async (
}),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -855,6 +864,7 @@ export const findLibraryItemByUrl = async (
.getOne(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -1153,17 +1163,21 @@ export const findLibraryItemsByPrefix = async (
): Promise<LibraryItem[]> => {
const prefixWildcard = `${prefix}%`
return authTrx(async (tx) =>
tx
.createQueryBuilder(LibraryItem, 'library_item')
.where('library_item.user_id = :userId', { userId })
.andWhere(
'(library_item.title ILIKE :prefix OR library_item.site_name ILIKE :prefix)',
{ prefix: prefixWildcard }
)
.orderBy('library_item.savedAt', 'DESC')
.limit(limit)
.getMany()
return authTrx(
async (tx) =>
tx
.createQueryBuilder(LibraryItem, 'library_item')
.where('library_item.user_id = :userId', { userId })
.andWhere(
'(library_item.title ILIKE :prefix OR library_item.site_name ILIKE :prefix)',
{ prefix: prefixWildcard }
)
.orderBy('library_item.savedAt', 'DESC')
.limit(limit)
.getMany(),
{
replicationMode: 'replica',
}
)
}
@ -1184,6 +1198,7 @@ export const countBySavedAt = async (
.getCount(),
{
uid: userId,
replicationMode: 'replica',
}
)
}
@ -1424,6 +1439,7 @@ export const findLibraryItemIdsByLabelId = async (
},
{
uid: userId,
replicationMode: 'replica',
}
)
}

View file

@ -1,6 +1,6 @@
import { Profile } from '../entity/profile'
import { User } from '../entity/user'
import { authTrx, getRepository } from '../repository'
import { getRepository } from '../repository'
export const findProfile = async (user: User): Promise<Profile | null> => {
return getRepository(Profile).findOneBy({ user: { id: user.id } })

View file

@ -60,6 +60,7 @@ export const findReceivedEmailById = async (id: string, userId: string) => {
t.getRepository(ReceivedEmail).findOneBy({ id, user: { id: userId } }),
{
uid: userId,
replicationMode: 'replica',
}
)
}