From 04b03cc3412efff79ea3354987cfd999db051056 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Tue, 8 Oct 2024 14:11:12 +0300 Subject: [PATCH] chore(server): IoC 30 - getCommitStream(s)Factory --- .../modules/core/domain/streams/operations.ts | 15 +++- .../modules/core/domain/streams/types.ts | 5 ++ .../modules/core/graph/resolvers/commits.js | 5 +- .../modules/core/graph/resolvers/versions.ts | 3 +- packages/server/modules/core/loaders.ts | 5 +- .../modules/core/repositories/streams.ts | 90 ++++++++++--------- .../core/services/commit/management.ts | 9 +- .../server/modules/core/tests/commits.spec.js | 5 +- 8 files changed, 83 insertions(+), 54 deletions(-) diff --git a/packages/server/modules/core/domain/streams/operations.ts b/packages/server/modules/core/domain/streams/operations.ts index 4ab040bef..880f83a8a 100644 --- a/packages/server/modules/core/domain/streams/operations.ts +++ b/packages/server/modules/core/domain/streams/operations.ts @@ -1,4 +1,7 @@ -import { StreamWithOptionalRole } from '@/modules/core/domain/streams/types' +import { + StreamWithCommitId, + StreamWithOptionalRole +} from '@/modules/core/domain/streams/types' import { Optional } from '@speckle/shared' import { Knex } from 'knex' @@ -19,3 +22,13 @@ export type GetStream = ( trx: Knex.Transaction }> ) => Promise> + +export type GetCommitStreams = (params: { + commitIds: string[] + userId?: string +}) => Promise + +export type GetCommitStream = (params: { + commitId: string + userId?: string +}) => Promise> diff --git a/packages/server/modules/core/domain/streams/types.ts b/packages/server/modules/core/domain/streams/types.ts index 825b475da..e27876ea0 100644 --- a/packages/server/modules/core/domain/streams/types.ts +++ b/packages/server/modules/core/domain/streams/types.ts @@ -10,3 +10,8 @@ export type StreamWithOptionalRole = Stream & { */ role?: StreamRoles } + +export type StreamWithCommitId = + StreamType & { + commitId: string + } diff --git a/packages/server/modules/core/graph/resolvers/commits.js b/packages/server/modules/core/graph/resolvers/commits.js index 842b7b69f..424e1e261 100644 --- a/packages/server/modules/core/graph/resolvers/commits.js +++ b/packages/server/modules/core/graph/resolvers/commits.js @@ -57,9 +57,9 @@ const { const { db } = require('@/db/knex') const { markCommitStreamUpdated, - getCommitStream, getStreamFactory, - getStreamsFactory + getStreamsFactory, + getCommitStreamFactory } = require('@/modules/core/repositories/streams') const { markCommitBranchUpdatedFactory, @@ -81,6 +81,7 @@ const COMMIT_CREATED = CommitPubsubEvents.CommitCreated const COMMIT_UPDATED = CommitPubsubEvents.CommitUpdated const COMMIT_DELETED = CommitPubsubEvents.CommitDeleted +const getCommitStream = getCommitStreamFactory({ db }) const getStream = getStreamFactory({ db }) const getStreams = getStreamsFactory({ db }) const deleteCommitAndNotify = deleteCommitAndNotifyFactory({ diff --git a/packages/server/modules/core/graph/resolvers/versions.ts b/packages/server/modules/core/graph/resolvers/versions.ts index 4c9a94cad..114cc1386 100644 --- a/packages/server/modules/core/graph/resolvers/versions.ts +++ b/packages/server/modules/core/graph/resolvers/versions.ts @@ -40,7 +40,7 @@ import { markCommitBranchUpdatedFactory } from '@/modules/core/repositories/branches' import { - getCommitStream, + getCommitStreamFactory, getStreamFactory, getStreamsFactory, markCommitStreamUpdated @@ -53,6 +53,7 @@ import { } from '@/modules/activitystream/services/commitActivity' import { getObjectFactory } from '@/modules/core/repositories/objects' +const getCommitStream = getCommitStreamFactory({ db }) const getStream = getStreamFactory({ db }) const getStreams = getStreamsFactory({ db }) const getObject = getObjectFactory({ db }) diff --git a/packages/server/modules/core/loaders.ts b/packages/server/modules/core/loaders.ts index 9ff4e97eb..6a46c03c1 100644 --- a/packages/server/modules/core/loaders.ts +++ b/packages/server/modules/core/loaders.ts @@ -5,10 +5,10 @@ import { getOwnedFavoritesCountByUserIds, getStreamRoles, getStreamsSourceApps, - getCommitStreams, StreamWithCommitId, getUserStreamCounts, - getStreamsFactory + getStreamsFactory, + getCommitStreamsFactory } from '@/modules/core/repositories/streams' import { UserWithOptionalRole, getUsers } from '@/modules/core/repositories/users' import { keyBy } from 'lodash' @@ -117,6 +117,7 @@ const getCommitBranches = getCommitBranchesFactory({ db }) const getStreamCommitCounts = getStreamCommitCountsFactory({ db }) const getUserStreamCommitCounts = getUserStreamCommitCountsFactory({ db }) const getUserAuthoredCommitCounts = getUserAuthoredCommitCountsFactory({ db }) +const getCommitStreams = getCommitStreamsFactory({ db }) /** * TODO: Lazy load DataLoaders to reduce memory usage diff --git a/packages/server/modules/core/repositories/streams.ts b/packages/server/modules/core/repositories/streams.ts index bd1f1aaf0..7a775ee23 100644 --- a/packages/server/modules/core/repositories/streams.ts +++ b/packages/server/modules/core/repositories/streams.ts @@ -25,6 +25,7 @@ import { Roles, StreamRoles } from '@/modules/core/helpers/mainConstants' import { LimitedUserRecord, StreamAclRecord, + StreamCommitRecord, StreamFavoriteRecord, StreamRecord, UserWithRole @@ -66,13 +67,22 @@ import { GetRolesByUserId, UpsertProjectRole } from '@/modules/core/domain/projects/operations' -import { StreamWithOptionalRole } from '@/modules/core/domain/streams/types' -import { GetStream, GetStreams } from '@/modules/core/domain/streams/operations' -export type { StreamWithOptionalRole } +import { + StreamWithCommitId, + StreamWithOptionalRole +} from '@/modules/core/domain/streams/types' +import { + GetCommitStream, + GetCommitStreams, + GetStream, + GetStreams +} from '@/modules/core/domain/streams/operations' +export type { StreamWithOptionalRole, StreamWithCommitId } const tables = { streams: (db: Knex) => db(Streams.name), - streamAcl: (db: Knex) => db(StreamAcl.name) + streamAcl: (db: Knex) => db(StreamAcl.name), + streamCommits: (db: Knex) => db(StreamCommits.name) } /** @@ -190,48 +200,48 @@ export const getProjectFactory = return project } -export type StreamWithCommitId = StreamWithOptionalRole & { commitId: string } +export const getCommitStreamsFactory = + (deps: { db: Knex }): GetCommitStreams => + async (params: { commitIds: string[]; userId?: string }) => { + const { commitIds, userId } = params + if (!commitIds?.length) return [] -export async function getCommitStreams(params: { - commitIds: string[] - userId?: string -}) { - const { commitIds, userId } = params - if (!commitIds?.length) return [] + const q = tables + .streamCommits(deps.db) + .select>([...Streams.cols, StreamCommits.col.commitId]) + .innerJoin(Streams.name, Streams.col.id, StreamCommits.col.streamId) + .whereIn(StreamCommits.col.commitId, commitIds) - const q = StreamCommits.knex() - .select>([...Streams.cols, StreamCommits.col.commitId]) - .innerJoin(Streams.name, Streams.col.id, StreamCommits.col.streamId) - .whereIn(StreamCommits.col.commitId, commitIds) + if (userId) { + q.select([ + // Getting first role from grouped results + knex.raw(`(array_agg("stream_acl"."role"))[1] as role`) + ]) + q.leftJoin(StreamAcl.name, function () { + this.on(StreamAcl.col.resourceId, Streams.col.id).andOnVal( + StreamAcl.col.userId, + userId + ) + }) + q.groupBy(Streams.col.id, StreamCommits.col.commitId) + } - if (userId) { - q.select([ - // Getting first role from grouped results - knex.raw(`(array_agg("stream_acl"."role"))[1] as role`) - ]) - q.leftJoin(StreamAcl.name, function () { - this.on(StreamAcl.col.resourceId, Streams.col.id).andOnVal( - StreamAcl.col.userId, - userId - ) - }) - q.groupBy(Streams.col.id, StreamCommits.col.commitId) + const results = await q + return results } - const results = await q - return results -} +export const getCommitStreamFactory = + (deps: { db: Knex }): GetCommitStream => + async (params: { commitId: string; userId?: string }) => { + const { commitId } = params + if (!commitId) throw new InvalidArgumentError('Invalid commit ID') -export async function getCommitStream(params: { commitId: string; userId?: string }) { - const { commitId } = params - if (!commitId) throw new InvalidArgumentError('Invalid commit ID') - - const results = await getCommitStreams({ - commitIds: [commitId], - userId: params.userId - }) - return >results[0] -} + const results = await getCommitStreamsFactory(deps)({ + commitIds: [commitId], + userId: params.userId + }) + return >results[0] + } /** * Get base query for finding or counting user favorited streams diff --git a/packages/server/modules/core/services/commit/management.ts b/packages/server/modules/core/services/commit/management.ts index 1f41ee9a3..a48acead8 100644 --- a/packages/server/modules/core/services/commit/management.ts +++ b/packages/server/modules/core/services/commit/management.ts @@ -25,7 +25,7 @@ import { UpdateCommitAndNotify } from '@/modules/core/domain/commits/operations' import { GetObject } from '@/modules/core/domain/objects/operations' -import { GetStream } from '@/modules/core/domain/streams/operations' +import { GetCommitStream, GetStream } from '@/modules/core/domain/streams/operations' import { CommitCreateError, CommitDeleteError, @@ -44,10 +44,7 @@ import { } from '@/modules/core/graph/generated/graphql' import { CommitRecord } from '@/modules/core/helpers/types' import { getCommitFactory } from '@/modules/core/repositories/commits' -import { - getCommitStream, - markCommitStreamUpdated -} from '@/modules/core/repositories/streams' +import { markCommitStreamUpdated } from '@/modules/core/repositories/streams' import { ensureError, MaybeNullOrUndefined, Nullable, Roles } from '@speckle/shared' import { has } from 'lodash' @@ -254,7 +251,7 @@ export const updateCommitAndNotifyFactory = (deps: { getCommit: GetCommit getStream: GetStream - getCommitStream: typeof getCommitStream + getCommitStream: GetCommitStream getStreamBranchByName: GetStreamBranchByName getCommitBranch: GetCommitBranch switchCommitBranch: SwitchCommitBranch diff --git a/packages/server/modules/core/tests/commits.spec.js b/packages/server/modules/core/tests/commits.spec.js index 238caa564..bc5a0f20e 100644 --- a/packages/server/modules/core/tests/commits.spec.js +++ b/packages/server/modules/core/tests/commits.spec.js @@ -46,8 +46,8 @@ const { } = require('@/modules/core/services/commit/management') const { markCommitStreamUpdated, - getCommitStream, - getStreamFactory + getStreamFactory, + getCommitStreamFactory } = require('@/modules/core/repositories/streams') const { addCommitDeletedActivity, @@ -57,6 +57,7 @@ const { const { VersionsEmitter } = require('@/modules/core/events/versionsEmitter') const { getObjectFactory } = require('@/modules/core/repositories/objects') +const getCommitStream = getCommitStreamFactory({ db }) const getStream = getStreamFactory({ db }) const createBranch = createBranchFactory({ db }) const createBranchAndNotify = createBranchAndNotifyFactory({