f210d9b749
* feat(projects): add project regions, default to null * feat(multiregion): add projectRegion Db client lookup logic * feat(multiregion): add project region repositories and caching * feat(multiRegion): db initialization and get project db client * feat(docker-compose): add second db for regions testing * feat(multiRegion): initialize region with pubs and subs working * fix(multiRegion): get region client even if it was registered in another pod * feat(workspaces): create workspace resolver split * feat: update server region metadata * feat(projects): rewrite project creation * feat(multiRegion): getRegionDb * fix(workspaces): get projects now can retur null * feat(multiRegion): make local multi region DB-s work * feat: set d efault workspace region * CR changes * tests * feat(multiRegion): bind region properly * fe update * test fixes * feat(multiRegion): automatically create aiven extras plugin * ci(postgres): use published postgres with aiven extras * fix(multiRegion): roll back the aiven extras migration, there is a better way * tests fix * fix(billing): we do not need to add a seat, if the workspace is on a plan, but has no sub --------- Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { StreamNotFoundError } from '@/modules/core/errors/stream'
|
|
import {
|
|
AsyncRegionKeyStore,
|
|
CachedRegionKeyLookup,
|
|
RegionKeyLookupResult,
|
|
StorageRegionKeyLookup,
|
|
SyncRegionKeyLookup,
|
|
SyncRegionKeyStore
|
|
} from '@/modules/multiregion/domain/operations'
|
|
import { Knex } from 'knex'
|
|
|
|
export type GetProjectRegionKey = (args: {
|
|
projectId: string
|
|
}) => Promise<RegionKeyLookupResult>
|
|
|
|
export const getProjectRegionKeyFactory =
|
|
({
|
|
getRegionKeyFromMemory,
|
|
writeRegionToMemory,
|
|
getRegionKeyFromCache,
|
|
writeRegionKeyToCache,
|
|
getRegionKeyFromStorage
|
|
}: {
|
|
getRegionKeyFromMemory: SyncRegionKeyLookup
|
|
writeRegionToMemory: SyncRegionKeyStore
|
|
getRegionKeyFromCache: CachedRegionKeyLookup
|
|
writeRegionKeyToCache: AsyncRegionKeyStore
|
|
getRegionKeyFromStorage: StorageRegionKeyLookup
|
|
}): GetProjectRegionKey =>
|
|
async ({ projectId }) => {
|
|
let regionKey = getRegionKeyFromMemory({ projectId })
|
|
// if undefined, cache missed
|
|
if (regionKey !== undefined) return regionKey
|
|
|
|
regionKey = await getRegionKeyFromCache({ projectId })
|
|
// if undefined, cache missed
|
|
if (regionKey !== undefined) {
|
|
writeRegionToMemory({ projectId, regionKey })
|
|
return regionKey
|
|
}
|
|
|
|
// if this returns null, means we're in the default region
|
|
regionKey = await getRegionKeyFromStorage({ projectId })
|
|
if (regionKey === undefined) throw new StreamNotFoundError()
|
|
writeRegionToMemory({ projectId, regionKey })
|
|
await writeRegionKeyToCache({ projectId, regionKey })
|
|
return regionKey
|
|
}
|
|
|
|
export type GetRegionDb = (args: { regionKey: string }) => Promise<Knex>
|
|
type GetDefaultDb = () => Knex
|
|
|
|
export type GetProjectDb = (args: { projectId: string }) => Promise<Knex>
|
|
export const getProjectDbClientFactory =
|
|
({
|
|
getProjectRegionKey,
|
|
getDefaultDb,
|
|
getRegionDb
|
|
}: {
|
|
getProjectRegionKey: GetProjectRegionKey
|
|
getDefaultDb: GetDefaultDb
|
|
getRegionDb: GetRegionDb
|
|
}): GetProjectDb =>
|
|
async ({ projectId }) => {
|
|
const regionKey = await getProjectRegionKey({ projectId })
|
|
if (!regionKey) return getDefaultDb()
|
|
return getRegionDb({ regionKey })
|
|
}
|