feat(models): support streamId, branchId in models

This commit is contained in:
Gergő Jedlicska
2024-11-11 06:09:53 +01:00
parent 7fb3a97ee5
commit 1c19f67dd2
8 changed files with 55 additions and 32 deletions
@@ -89,7 +89,10 @@ import {
getUsersFactory,
UserWithOptionalRole
} from '@/modules/core/repositories/users'
import { CommitWithStreamBranchMetadata } from '@/modules/core/domain/commits/types'
import {
CommitWithStreamBranchId,
CommitWithStreamBranchMetadata
} from '@/modules/core/domain/commits/types'
declare module '@/modules/core/loaders' {
interface ModularizedDataLoaders extends ReturnType<typeof dataLoadersDefinition> {}
@@ -369,7 +372,7 @@ const dataLoadersDefinition = defineRequestDataloaders(
}),
getBranchCommit: createLoader<
{ branchId: string; commitId: string },
Nullable<CommitRecord>,
Nullable<CommitWithStreamBranchId>,
string
>(
async (idPairs) => {
@@ -61,19 +61,34 @@ import {
} from '@/modules/core/repositories/streams'
import { ModelsEmitter } from '@/modules/core/events/modelsEmitter'
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'
import {
getProjectDbClient,
getRegisteredRegionClients
} from '@/modules/multiregion/dbSelector'
export = {
User: {
async versions(parent, args, ctx) {
const authoredOnly = args.authoredOnly
const regionClients = await getRegisteredRegionClients()
const allLoaders = [
ctx.loaders,
...Object.values(regionClients).map((db) => ctx.loaders.forRegion({ db }))
]
let counts: number[]
if (authoredOnly) {
counts = await Promise.all(
allLoaders.map((loader) =>
loader.users.getAuthoredCommitCount.load(parent.id)
)
)
} else {
counts = await Promise.all(
allLoaders.map((loader) => loader.users.getStreamCommitCount.load(parent.id))
)
}
return {
totalCount: authoredOnly
? // TODO: make one dataloader for region and sum
await ctx.loaders
.forRegion({ db: regionDB })
.users.getAuthoredCommitCount.load(parent.id)
: await ctx.loaders.users.getStreamCommitCount.load(parent.id)
totalCount: counts.reduce((acc, curr) => acc + curr, 0)
}
}
},