From 1c19f67dd2d451aedcc4a74836cf01c662c52d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= Date: Mon, 11 Nov 2024 06:09:53 +0100 Subject: [PATCH] feat(models): support streamId, branchId in models --- .../activitystream/services/commitActivity.ts | 10 +++---- .../modules/core/domain/commits/operations.ts | 16 +++++----- .../modules/core/domain/commits/types.ts | 8 ++++- .../modules/core/graph/dataloaders/index.ts | 7 +++-- .../modules/core/graph/resolvers/models.ts | 29 ++++++++++++++----- .../server/modules/core/helpers/graphTypes.ts | 4 +-- .../modules/core/repositories/commits.ts | 11 ++++--- .../modules/shared/utils/subscriptions.ts | 2 +- 8 files changed, 55 insertions(+), 32 deletions(-) diff --git a/packages/server/modules/activitystream/services/commitActivity.ts b/packages/server/modules/activitystream/services/commitActivity.ts index bb880a46e..026db3560 100644 --- a/packages/server/modules/activitystream/services/commitActivity.ts +++ b/packages/server/modules/activitystream/services/commitActivity.ts @@ -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 } diff --git a/packages/server/modules/core/domain/commits/operations.ts b/packages/server/modules/core/domain/commits/operations.ts index 56f4de478..589561d83 100644 --- a/packages/server/modules/core/domain/commits/operations.ts +++ b/packages/server/modules/core/domain/commits/operations.ts @@ -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 +) => Promise export type StoreCommit = ( params: Omit, 'id' | 'createdAt'> @@ -74,7 +74,7 @@ export type CreateCommitByBranchId = ( options?: Partial<{ notify: boolean }> -) => Promise> +) => Promise export type CreateCommitByBranchName = ( params: NullableKeysToOptional<{ @@ -109,7 +109,7 @@ export type InsertStreamCommits = ( export type UpdateCommitAndNotify = ( params: CommitUpdateInput | UpdateVersionInput, userId: string -) => Promise> +) => Promise export type GetCommitBranches = (commitIds: string[]) => Promise @@ -166,7 +166,7 @@ export type GetUserAuthoredCommitCounts = (params: { export type GetCommitsAndTheirBranchIds = ( commitIds: string[] -) => Promise +) => Promise 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 }> diff --git a/packages/server/modules/core/domain/commits/types.ts b/packages/server/modules/core/domain/commits/types.ts index c32d4d9c7..158e8a478 100644 --- a/packages/server/modules/core/domain/commits/types.ts +++ b/packages/server/modules/core/domain/commits/types.ts @@ -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'] diff --git a/packages/server/modules/core/graph/dataloaders/index.ts b/packages/server/modules/core/graph/dataloaders/index.ts index 0db7d5b90..9976276ef 100644 --- a/packages/server/modules/core/graph/dataloaders/index.ts +++ b/packages/server/modules/core/graph/dataloaders/index.ts @@ -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 {} @@ -369,7 +372,7 @@ const dataLoadersDefinition = defineRequestDataloaders( }), getBranchCommit: createLoader< { branchId: string; commitId: string }, - Nullable, + Nullable, string >( async (idPairs) => { diff --git a/packages/server/modules/core/graph/resolvers/models.ts b/packages/server/modules/core/graph/resolvers/models.ts index bef995d3a..5e764b035 100644 --- a/packages/server/modules/core/graph/resolvers/models.ts +++ b/packages/server/modules/core/graph/resolvers/models.ts @@ -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) } } }, diff --git a/packages/server/modules/core/helpers/graphTypes.ts b/packages/server/modules/core/helpers/graphTypes.ts index 4e0b0507c..e0dd6551b 100644 --- a/packages/server/modules/core/helpers/graphTypes.ts +++ b/packages/server/modules/core/helpers/graphTypes.ts @@ -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 +export type VersionGraphQLReturn = CommitWithStreamBranchId export type LimitedUserGraphQLReturn = Omit< LimitedUser, diff --git a/packages/server/modules/core/repositories/commits.ts b/packages/server/modules/core/repositories/commits.ts index ad357c929..3d73c7d2c 100644 --- a/packages/server/modules/core/repositories/commits.ts +++ b/packages/server/modules/core/repositories/commits.ts @@ -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>([ + .select>([ ...Commits.cols, BranchCommits.col.branchId ]) @@ -269,7 +270,7 @@ export const getSpecificBranchCommitsFactory = const q = tables .commits(deps.db) - .select>>([ + .select>([ ...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 = [] + const results: Array = [] for (const pair of pairs) { const commit = queryResults.find( @@ -297,9 +298,7 @@ export const getSpecificBranchCommitsFactory = const getPaginatedBranchCommitsBaseQueryFactory = (deps: { db: Knex }) => - []>( - params: PaginatedBranchCommitsBaseParams - ) => { + (params: PaginatedBranchCommitsBaseParams) => { const { branchId, filter } = params const q = tables diff --git a/packages/server/modules/shared/utils/subscriptions.ts b/packages/server/modules/shared/utils/subscriptions.ts index 41ce47789..a2c7f3e54 100644 --- a/packages/server/modules/shared/utils/subscriptions.ts +++ b/packages/server/modules/shared/utils/subscriptions.ts @@ -183,7 +183,7 @@ type SubscriptionTypeMap = { payload: { projectVersionsUpdated: Merge< ProjectVersionsUpdatedMessage, - { version: Nullable } + { version: Nullable> } > projectId: string }