Merge pull request #3202 from specklesystems/fabians/core-ioc-30

chore(server): IoC 30 - getCommitStream(s)Factory
This commit is contained in:
Alessandro Magionami
2024-10-09 10:06:42 +02:00
committed by GitHub
8 changed files with 83 additions and 54 deletions
@@ -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<Optional<StreamWithOptionalRole>>
export type GetCommitStreams = (params: {
commitIds: string[]
userId?: string
}) => Promise<StreamWithCommitId[]>
export type GetCommitStream = (params: {
commitId: string
userId?: string
}) => Promise<Optional<StreamWithCommitId>>
@@ -10,3 +10,8 @@ export type StreamWithOptionalRole = Stream & {
*/
role?: StreamRoles
}
export type StreamWithCommitId<StreamType extends Stream = StreamWithOptionalRole> =
StreamType & {
commitId: string
}
@@ -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({
@@ -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 })
+3 -2
View File
@@ -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
@@ -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<StreamRecord>(Streams.name),
streamAcl: (db: Knex) => db<StreamAclRecord>(StreamAcl.name)
streamAcl: (db: Knex) => db<StreamAclRecord>(StreamAcl.name),
streamCommits: (db: Knex) => db<StreamCommitRecord>(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<Array<StreamWithCommitId>>([...Streams.cols, StreamCommits.col.commitId])
.innerJoin(Streams.name, Streams.col.id, StreamCommits.col.streamId)
.whereIn(StreamCommits.col.commitId, commitIds)
const q = StreamCommits.knex()
.select<Array<StreamWithCommitId>>([...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 <Optional<StreamWithCommitId>>results[0]
}
const results = await getCommitStreamsFactory(deps)({
commitIds: [commitId],
userId: params.userId
})
return <Optional<StreamWithCommitId>>results[0]
}
/**
* Get base query for finding or counting user favorited streams
@@ -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
@@ -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({