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
@@ -40,7 +40,7 @@ export const addCommitCreatedActivityFactory =
modelId: string
commit: CommitRecord
}) => {
const { commitId, input, streamId, userId, branchName, commit } = params
const { commitId, input, streamId, userId, branchName, commit, modelId } = params
await Promise.all([
saveActivity({
streamId,
@@ -53,7 +53,7 @@ export const addCommitCreatedActivityFactory =
commit: {
...input,
projectId: streamId,
modelId: params.modelId,
modelId,
versionId: commit.id
}
},
@@ -67,7 +67,7 @@ export const addCommitCreatedActivityFactory =
projectId: streamId,
projectVersionsUpdated: {
id: commit.id,
version: commit,
version: { ...commit, streamId },
type: ProjectVersionsUpdatedMessageType.Created,
modelId: null
}
@@ -123,7 +123,7 @@ export const addCommitUpdatedActivityFactory =
projectId: streamId,
projectVersionsUpdated: {
id: commitId,
version: newCommit,
version: { ...newCommit, streamId },
type: ProjectVersionsUpdatedMessageType.Updated,
modelId: null
}
@@ -162,7 +162,7 @@ export const addCommitMovedActivityFactory =
projectId: streamId,
projectVersionsUpdated: {
id: commitId,
version: commit,
version: { ...commit, streamId },
type: ProjectVersionsUpdatedMessageType.Updated,
modelId: null
}
@@ -1,12 +1,12 @@
import { Branch } from '@/modules/core/domain/branches/types'
import {
CommitWithBranchId,
CommitWithStreamBranchMetadata,
Commit,
CommitBranch,
CommitWithStreamId,
LegacyUserCommit,
LegacyStreamCommit
LegacyStreamCommit,
CommitWithStreamBranchId
} from '@/modules/core/domain/commits/types'
import {
CommitsDeleteInput,
@@ -54,7 +54,7 @@ export type GetSpecificBranchCommits = (
branchId: string
commitId: string
}[]
) => Promise<CommitWithBranchId[]>
) => Promise<CommitWithStreamBranchId[]>
export type StoreCommit = (
params: Omit<NullableKeysToOptional<Commit>, 'id' | 'createdAt'>
@@ -74,7 +74,7 @@ export type CreateCommitByBranchId = (
options?: Partial<{
notify: boolean
}>
) => Promise<Omit<CommitWithStreamBranchMetadata, 'branchName'>>
) => Promise<CommitWithStreamBranchId>
export type CreateCommitByBranchName = (
params: NullableKeysToOptional<{
@@ -109,7 +109,7 @@ export type InsertStreamCommits = (
export type UpdateCommitAndNotify = (
params: CommitUpdateInput | UpdateVersionInput,
userId: string
) => Promise<Omit<CommitWithStreamBranchMetadata, 'branchName'>>
) => Promise<CommitWithStreamBranchId>
export type GetCommitBranches = (commitIds: string[]) => Promise<CommitBranch[]>
@@ -166,7 +166,7 @@ export type GetUserAuthoredCommitCounts = (params: {
export type GetCommitsAndTheirBranchIds = (
commitIds: string[]
) => Promise<CommitWithBranchId[]>
) => Promise<CommitWithStreamBranchId[]>
export type GetBatchedStreamCommits = (
streamId: string,
@@ -203,7 +203,7 @@ export type PaginatedBranchCommitsParams = PaginatedBranchCommitsBaseParams & {
export type GetPaginatedBranchCommitsItems = (
params: PaginatedBranchCommitsParams
) => Promise<{
commits: Commit[]
commits: CommitWithStreamBranchId[]
cursor: string | null
}>
@@ -217,7 +217,7 @@ export type GetPaginatedBranchCommits = (
}
) => Promise<{
totalCount: number
items: Commit[]
items: CommitWithStreamBranchId[]
cursor: string | null
}>
@@ -14,9 +14,12 @@ export type CommitWithBranchId = Commit & {
export type CommitWithStreamId = Commit & { streamId: string }
export type BranchLatestCommit = CommitWithBranchId
export type CommitWithStreamBranchMetadata = Commit & {
export type CommitWithStreamBranchId = Commit & {
streamId: string
branchId: string
}
export type CommitWithStreamBranchMetadata = CommitWithStreamBranchId & {
branchName: string
}
@@ -31,6 +34,7 @@ export type LegacyUserCommit = {
parents: CommitRecord['parents']
createdAt: CommitRecord['createdAt']
branchName: BranchRecord['name']
branchId: BranchRecord['id']
streamId: StreamCommitRecord['streamId']
streamName: StreamRecord['name']
authorName: UserRecord['name']
@@ -47,6 +51,8 @@ export type LegacyStreamCommit = {
parents: CommitRecord['parents']
createdAt: CommitRecord['createdAt']
branchName: BranchRecord['name']
branchId: BranchRecord['id']
streamId: StreamCommitRecord['streamId']
authorName: UserRecord['name']
authorId: UserRecord['id']
authorAvatar: UserRecord['avatar']
@@ -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)
}
}
},
@@ -1,5 +1,5 @@
import {
CommitWithStreamBranchMetadata,
CommitWithStreamBranchId,
LegacyStreamCommit,
LegacyUserCommit
} from '@/modules/core/domain/commits/types'
@@ -44,7 +44,7 @@ export type ProjectGraphQLReturn = StreamGraphQLReturn
export type ModelGraphQLReturn = BranchRecord
export type VersionGraphQLReturn = Omit<CommitWithStreamBranchMetadata, 'branchName'>
export type VersionGraphQLReturn = CommitWithStreamBranchId
export type LimitedUserGraphQLReturn = Omit<
LimitedUser,
@@ -23,6 +23,7 @@ import {
import { Knex } from 'knex'
import { MaybeNullOrUndefined, Optional } from '@speckle/shared'
import {
CommitWithStreamBranchId,
CommitWithStreamBranchMetadata,
LegacyStreamCommit,
LegacyUserCommit
@@ -251,7 +252,7 @@ export const getCommitsAndTheirBranchIdsFactory =
return await tables
.commits(deps.db)
.select<Array<CommitRecord & { branchId: string }>>([
.select<Array<CommitWithStreamBranchId>>([
...Commits.cols,
BranchCommits.col.branchId
])
@@ -269,7 +270,7 @@ export const getSpecificBranchCommitsFactory =
const q = tables
.commits(deps.db)
.select<Array<Omit<CommitWithStreamBranchMetadata, 'branchName'>>>([
.select<Array<CommitWithStreamBranchId>>([
...Commits.cols,
knex.raw(`(array_agg(??))[1] as "branchId"`, [BranchCommits.col.branchId]),
knex.raw(`(array_agg(??))[1] as "streamId"`, [StreamCommits.col.streamId])
@@ -281,7 +282,7 @@ export const getSpecificBranchCommitsFactory =
.groupBy(Commits.col.id)
const queryResults = await q
const results: Array<CommitRecord & { branchId: string }> = []
const results: Array<CommitWithStreamBranchId> = []
for (const pair of pairs) {
const commit = queryResults.find(
@@ -297,9 +298,7 @@ export const getSpecificBranchCommitsFactory =
const getPaginatedBranchCommitsBaseQueryFactory =
(deps: { db: Knex }) =>
<T = Omit<CommitWithStreamBranchMetadata, 'branchName'>[]>(
params: PaginatedBranchCommitsBaseParams
) => {
<T = CommitWithStreamBranchId[]>(params: PaginatedBranchCommitsBaseParams) => {
const { branchId, filter } = params
const q = tables
@@ -183,7 +183,7 @@ type SubscriptionTypeMap = {
payload: {
projectVersionsUpdated: Merge<
ProjectVersionsUpdatedMessage,
{ version: Nullable<VersionGraphQLReturn> }
{ version: Nullable<Omit<VersionGraphQLReturn, 'branchId'>> }
>
projectId: string
}