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 4 - getStreamBranch(es)ByNameFactory #3135

Merged
merged 1 commit into from
Sep 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@/modules/core/graph/generated/graphql'
import {
getBranchLatestCommits,
getStreamBranchesByName
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import {
getAllBranchCommits,
Expand Down Expand Up @@ -67,7 +67,7 @@ export async function addCommentCreatedActivity(params: {
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getStreamBranchesByName,
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
})
Expand Down
8 changes: 4 additions & 4 deletions packages/server/modules/cli/commands/download/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { cliLogger } from '@/logging/logging'
import { getStream, getStreamCollaborators } from '@/modules/core/repositories/streams'
import {
getBranchLatestCommits,
getStreamBranchByName,
getStreamBranchesByName
getStreamBranchByNameFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import { getUser } from '@/modules/core/repositories/users'
import { createCommitByBranchId } from '@/modules/core/services/commit/management'
Expand Down Expand Up @@ -88,7 +88,7 @@ const command: CommandModule<
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getStreamBranchesByName,
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
})
Expand All @@ -115,7 +115,7 @@ const command: CommandModule<

const downloadCommit = downloadCommitFactory({
getStream,
getStreamBranchByName,
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
getStreamCollaborators,
getUser,
createCommitByBranchId,
Expand Down
7 changes: 4 additions & 3 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { downloadCommitFactory } from '@/modules/cross-server-sync/services/comm
import { getStream, getStreamCollaborators } from '@/modules/core/repositories/streams'
import {
getBranchLatestCommits,
getStreamBranchByName,
getStreamBranchesByName
getStreamBranchByNameFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import { getUser } from '@/modules/core/repositories/users'
import { createCommitByBranchId } from '@/modules/core/services/commit/management'
Expand Down Expand Up @@ -84,7 +84,7 @@ const command: CommandModule<
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getStreamBranchesByName,
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
})
Expand All @@ -108,6 +108,7 @@ const command: CommandModule<
addReplyAddedActivity
})

const getStreamBranchByName = getStreamBranchByNameFactory({ db })
const downloadProject = downloadProjectFactory({
downloadCommit: downloadCommitFactory({
getStream,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/comments/graph/resolvers/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import { getStreamObjects } from '@/modules/core/repositories/objects'
import { adminOverrideEnabled } from '@/modules/shared/helpers/envHelper'
import {
getBranchLatestCommits,
getStreamBranchesByName
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'

const streamResourceCheck = streamResourceCheckFactory({
Expand Down Expand Up @@ -177,7 +177,7 @@ const getViewerResourceItemsUngrouped = getViewerResourceItemsUngroupedFactory({
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getStreamBranchesByName,
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
})
Expand Down
15 changes: 14 additions & 1 deletion packages/server/modules/core/domain/branches/operations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Branch } from '@/modules/core/domain/branches/types'
import { Optional } from '@speckle/shared'
import { Nullable, Optional } from '@speckle/shared'

export type GenerateBranchId = () => string

Expand All @@ -16,3 +16,16 @@ export type GetBranchById = (
streamId: string
}>
) => Promise<Optional<Branch>>

export type GetStreamBranchesByName = (
streamId: string,
names: string[],
options?: Partial<{
startsWithName: boolean
}>
) => Promise<Branch[]>

export type GetStreamBranchByName = (
streamId: string,
name: string
) => Promise<Nullable<Branch>>
12 changes: 6 additions & 6 deletions packages/server/modules/core/graph/resolvers/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
} = require('@/modules/shared/utils/subscriptions')
const { authorizeResolver } = require('@/modules/shared')

const { getBranchByNameAndStreamId } = require('@/modules/core/services/branches')
const {
createBranchAndNotify,
updateBranchAndNotify,
Expand All @@ -20,7 +19,10 @@ const {

const { getUserById } = require('../../services/users')
const { Roles } = require('@speckle/shared')
const { getBranchByIdFactory } = require('@/modules/core/repositories/branches')
const {
getBranchByIdFactory,
getStreamBranchByNameFactory
} = require('@/modules/core/repositories/branches')
const { db } = require('@/db/knex')

// subscription events
Expand All @@ -29,6 +31,7 @@ const BRANCH_UPDATED = BranchPubsubEvents.BranchUpdated
const BRANCH_DELETED = BranchPubsubEvents.BranchDeleted

const getBranchById = getBranchByIdFactory({ db })
const getStreamBranchByName = getStreamBranchByNameFactory({ db })

/** @type {import('@/modules/core/graph/generated/graphql').Resolvers} */
module.exports = {
Expand All @@ -45,10 +48,7 @@ module.exports = {
// When getting a branch by name, if not found, we try to do a 'hail mary' attempt
// and get it by id as well (this would be coming from a FE2 url).

const branchByName = await getBranchByNameAndStreamId({
streamId: parent.id,
name: args.name
})
const branchByName = await getStreamBranchByName(parent.id, args.name)
if (branchByName) return branchByName

const branchByIdRes = await getBranchById(args.name)
Expand Down
5 changes: 3 additions & 2 deletions packages/server/modules/core/graph/resolvers/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {
getBranchLatestCommits,
getModelTreeItems,
getStreamBranchesByName
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import { BranchNotFoundError } from '@/modules/core/errors/branch'
import { CommitNotFoundError } from '@/modules/core/errors/commit'
Expand All @@ -34,11 +34,12 @@ import {
getAllBranchCommits,
getSpecificBranchCommits
} from '@/modules/core/repositories/commits'
import { db } from '@/db/knex'

const getViewerResourceGroups = getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getStreamBranchesByName,
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
})
Expand Down
3 changes: 2 additions & 1 deletion packages/server/modules/core/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
getBranchesByIdsFactory,
getBranchLatestCommits,
getStreamBranchCounts,
getStreamBranchesByName
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import { CommentRecord } from '@/modules/comments/helpers/types'
import { metaHelpers } from '@/modules/core/helpers/meta'
Expand Down Expand Up @@ -106,6 +106,7 @@ const getCommentReplyCounts = getCommentReplyCountsFactory({ db })
const getCommentReplyAuthorIds = getCommentReplyAuthorIdsFactory({ db })
const getCommentParents = getCommentParentsFactory({ db })
const getBranchesByIds = getBranchesByIdsFactory({ db })
const getStreamBranchesByName = getStreamBranchesByNameFactory({ db })

/**
* TODO: Lazy load DataLoaders to reduce memory usage
Expand Down
79 changes: 43 additions & 36 deletions packages/server/modules/core/repositories/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { getMaximumProjectModelsPerPage } from '@/modules/shared/helpers/envHelp
import {
GenerateBranchId,
GetBranchById,
GetBranchesByIds
GetBranchesByIds,
GetStreamBranchByName,
GetStreamBranchesByName
} from '@/modules/core/domain/branches/operations'

const tables = {
Expand Down Expand Up @@ -48,45 +50,50 @@ export const getBranchByIdFactory =
return branch as Optional<BranchRecord>
}

export async function getStreamBranchesByName(
streamId: string,
names: string[],
options?: Partial<{
/**
* Set to true if you want to find branches that start with specified names as prefixes
*/
startsWithName: boolean
}>
): Promise<BranchRecord[]> {
if (!streamId || !names?.length) return []
const { startsWithName } = options || {}

const q = Branches.knex<BranchRecord[]>()
.where(Branches.col.streamId, streamId)
.andWhere((w1) => {
w1.where(
knex.raw('LOWER(??) ilike ANY(?)', [
Branches.col.name,
names.map((n) => n.toLowerCase() + (startsWithName ? '%' : ''))
])
)
export const getStreamBranchesByNameFactory =
(deps: { db: Knex }): GetStreamBranchesByName =>
async (
streamId: string,
names: string[],
options?: Partial<{
/**
* Set to true if you want to find branches that start with specified names as prefixes
*/
startsWithName: boolean
}>
): Promise<BranchRecord[]> => {
if (!streamId || !names?.length) return []
const { startsWithName } = options || {}

const q = tables
.branches(deps.db)
.where(Branches.col.streamId, streamId)
.andWhere((w1) => {
w1.where(
knex.raw('LOWER(??) ilike ANY(?)', [
Branches.col.name,
names.map((n) => n.toLowerCase() + (startsWithName ? '%' : ''))
])
)

if (!options?.startsWithName) {
// There are some edge cases with branches that have backwards slashes in their name that break the query,
// hence the extra condition
w1.orWhereIn(Branches.col.name, names)
}
})
if (!options?.startsWithName) {
// There are some edge cases with branches that have backwards slashes in their name that break the query,
// hence the extra condition
w1.orWhereIn(Branches.col.name, names)
}
})

return await q
}
return await q
}

export async function getStreamBranchByName(streamId: string, name: string) {
if (!streamId || !name) return null
export const getStreamBranchByNameFactory =
(deps: { db: Knex }): GetStreamBranchByName =>
async (streamId: string, name: string) => {
if (!streamId || !name) return null

const [first] = await getStreamBranchesByName(streamId, [name])
return first || null
}
const [first] = await getStreamBranchesByNameFactory(deps)(streamId, [name])
return first || null
}

export function getBatchedStreamBranches(
streamId: string,
Expand Down
7 changes: 5 additions & 2 deletions packages/server/modules/core/services/branch/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
createBranch,
deleteBranchById,
getBranchByIdFactory,
getStreamBranchByName,
getStreamBranchByNameFactory,
updateBranch
} from '@/modules/core/repositories/branches'
import { getStream, markBranchStreamUpdated } from '@/modules/core/repositories/streams'
Expand All @@ -40,7 +40,10 @@ export async function createBranchAndNotify(
creatorId: string
) {
const streamId = isBranchCreateInput(input) ? input.streamId : input.projectId
const existingBranch = await getStreamBranchByName(streamId, input.name)
const existingBranch = await getStreamBranchByNameFactory({ db })(
streamId,
input.name
)
if (existingBranch) {
throw new BranchCreateError('A branch with this name already exists')
}
Expand Down
5 changes: 0 additions & 5 deletions packages/server/modules/core/services/branches.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
const knex = require('@/db/knex')
const {
getStreamBranchByName,
getStreamBranchCount,
createBranch: createBranchInDb
} = require('@/modules/core/repositories/branches')
Expand Down Expand Up @@ -59,10 +58,6 @@ module.exports = {
return await getStreamBranchCount(streamId)
},

async getBranchByNameAndStreamId({ streamId, name }) {
return await getStreamBranchByName(streamId, name)
},

/**
* @deprecated Use 'deleteBranchAndNotify'
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { db } from '@/db/knex'
import {
addCommitDeletedActivity,
addCommitMovedActivity
Expand All @@ -15,7 +16,7 @@ import {
import { Roles } from '@/modules/core/helpers/mainConstants'
import {
createBranch,
getStreamBranchByName
getStreamBranchByNameFactory
} from '@/modules/core/repositories/branches'
import {
deleteCommits,
Expand Down Expand Up @@ -104,7 +105,7 @@ async function validateCommitsMove(
}

const stream = streams[0]
const branch = await getStreamBranchByName(stream.id, targetBranch)
const branch = await getStreamBranchByNameFactory({ db })(stream.id, targetBranch)

if (
!branch &&
Expand Down
6 changes: 3 additions & 3 deletions packages/server/modules/core/services/commit/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { CommitRecord } from '@/modules/core/helpers/types'
import {
getBranchByIdFactory,
getStreamBranchByName,
getStreamBranchByNameFactory,
markCommitBranchUpdated
} from '@/modules/core/repositories/branches'
import {
Expand Down Expand Up @@ -188,7 +188,7 @@ export async function createCommitByBranchName(
const { notify = true } = options || {}

const branchName = params.branchName.toLowerCase()
let myBranch = await getStreamBranchByName(streamId, branchName)
let myBranch = await getStreamBranchByNameFactory({ db })(streamId, branchName)
if (!myBranch) {
myBranch = (await getBranchByIdFactory({ db })(branchName)) || null
}
Expand Down Expand Up @@ -272,7 +272,7 @@ export async function updateCommitAndNotify(
if (newBranchName) {
try {
const [newBranch, oldBranch] = await Promise.all([
getStreamBranchByName(streamId, newBranchName),
getStreamBranchByNameFactory({ db })(streamId, newBranchName),
getCommitBranch(commitId)
])

Expand Down
Loading