Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(server): core IoC 25 - getObjectCommitsWithStreamIdsFactory #3190

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/server/modules/core/domain/commits/operations.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Branch } from '@/modules/core/domain/branches/types'
import {
BranchCommit,
CommitWithBranchId,
CommitWithStreamBranchMetadata,
Commit,
CommitBranch
CommitBranch,
CommitWithStreamId
} from '@/modules/core/domain/commits/types'
import {
CommitsMoveInput,
Expand Down Expand Up @@ -43,7 +44,7 @@ export type GetSpecificBranchCommits = (
branchId: string
commitId: string
}[]
) => Promise<BranchCommit[]>
) => Promise<CommitWithBranchId[]>

export type StoreCommit = (
params: Omit<Commit, 'id' | 'createdAt'> & {
Expand Down Expand Up @@ -157,7 +158,7 @@ export type GetUserAuthoredCommitCounts = (params: {

export type GetCommitsAndTheirBranchIds = (
commitIds: string[]
) => Promise<BranchCommit[]>
) => Promise<CommitWithBranchId[]>

export type GetBatchedStreamCommits = (
streamId: string,
Expand Down Expand Up @@ -221,3 +222,10 @@ export type ValidateAndBatchMoveCommits = (
params: CommitsMoveInput | MoveVersionsInput,
userId: string
) => Promise<Branch>

export type GetObjectCommitsWithStreamIds = (
objectIds: string[],
options?: {
streamIds?: string[]
}
) => Promise<CommitWithStreamId[]>
5 changes: 3 additions & 2 deletions packages/server/modules/core/domain/commits/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Branch } from '@/modules/core/domain/branches/types'
import { CommitRecord } from '@/modules/core/helpers/types'

export type Commit = CommitRecord
export type BranchLatestCommit = Commit & {
export type CommitWithBranchId = Commit & {
branchId: string
}
export type BranchCommit = BranchLatestCommit
export type CommitWithStreamId = Commit & { streamId: string }
export type BranchLatestCommit = CommitWithBranchId

export type CommitWithStreamBranchMetadata = Commit & {
streamId: string
Expand Down
54 changes: 29 additions & 25 deletions packages/server/modules/core/repositories/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
GetBatchedStreamCommits,
GetBatchedBranchCommits,
InsertCommits,
GetObjectCommitsWithStreamIds,
PaginatedBranchCommitsBaseParams,
PaginatedBranchCommitsParams,
GetPaginatedBranchCommitsItems,
Expand Down Expand Up @@ -402,32 +403,35 @@ export const createCommitFactory =
return item
}

export async function getObjectCommitsWithStreamIds(
objectIds: string[],
options?: {
/**
* Optionally also filter by stream ids
*/
streamIds?: string[]
}
) {
if (!objectIds?.length) return []
const { streamIds } = options || {}

const q = Commits.knex()
.select<Array<CommitRecord & { streamId: string }>>([
...Commits.cols,
StreamCommits.col.streamId
])
.whereIn(Commits.col.referencedObject, objectIds)
.innerJoin(StreamCommits.name, StreamCommits.col.commitId, Commits.col.id)

if (streamIds?.length) {
q.whereIn(StreamCommits.col.streamId, streamIds)
}
export const getObjectCommitsWithStreamIdsFactory =
(deps: { db: Knex }): GetObjectCommitsWithStreamIds =>
async (
objectIds: string[],
options?: {
/**
* Optionally also filter by stream ids
*/
streamIds?: string[]
}
) => {
if (!objectIds?.length) return []
const { streamIds } = options || {}

return await q
}
const q = tables
.commits(deps.db)
.select<Array<CommitRecord & { streamId: string }>>([
...Commits.cols,
StreamCommits.col.streamId
])
.whereIn(Commits.col.referencedObject, objectIds)
.innerJoin(StreamCommits.name, StreamCommits.col.commitId, Commits.col.id)

if (streamIds?.length) {
q.whereIn(StreamCommits.col.streamId, streamIds)
}

return await q
}

export const getAllBranchCommitsFactory =
(deps: { db: Knex }): GetAllBranchCommits =>
Expand Down
6 changes: 3 additions & 3 deletions packages/server/modules/previews/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
} from '@/modules/previews/repository/previews'
import { publish } from '@/modules/shared/utils/subscriptions'
import {
getObjectCommitsWithStreamIds,
getCommitFactory
getCommitFactory,
getObjectCommitsWithStreamIdsFactory
} from '@/modules/core/repositories/commits'
import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'

Expand Down Expand Up @@ -166,7 +166,7 @@ export const init: SpeckleModule['init'] = (app, isInitial) => {

if (isInitial) {
const listenForPreviewGenerationUpdates = listenForPreviewGenerationUpdatesFactory({
getObjectCommitsWithStreamIds,
getObjectCommitsWithStreamIds: getObjectCommitsWithStreamIdsFactory({ db }),
publish
})
listenForPreviewGenerationUpdates()
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/previews/services/resultListener.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getObjectCommitsWithStreamIds } from '@/modules/core/repositories/commits'
import {
ProjectSubscriptions,
PublishSubscription
} from '@/modules/shared/utils/subscriptions'
import { listenFor, MessageType } from '@/modules/core/utils/dbNotificationListener'
import { GetObjectCommitsWithStreamIds } from '@/modules/core/domain/commits/operations'

const payloadRegexp = /^([\w\d]+):([\w\d]+):([\w\d]+)$/i

type MessageProcessorDeps = {
getObjectCommitsWithStreamIds: typeof getObjectCommitsWithStreamIds
getObjectCommitsWithStreamIds: GetObjectCommitsWithStreamIds
publish: PublishSubscription
}

Expand Down