1 Commits

Author SHA1 Message Date
Gergő Jedlicska 06e97372f0 WIP: add different implementation sketches 2024-05-10 17:25:53 +02:00
23 changed files with 3073 additions and 3676 deletions
-1
View File
@@ -5,6 +5,5 @@
.swc
node_modules
ca-cert*
.data/*
dist
-3
View File
@@ -1,3 +0,0 @@
{
"cSpell.words": ["awilix"]
}
-25
View File
@@ -1,25 +0,0 @@
FROM postgres:14.5-alpine as builder
RUN apk add --no-cache 'git=~2.36' \
'build-base=~0.5' \
'clang=~13.0' \
'llvm13=~13.0'
WORKDIR /
RUN git clone --branch 1.1.9 https://github.com/aiven/aiven-extras.git aiven-extras
WORKDIR /aiven-extras
RUN git checkout 36598ab \
&& git clean -df \
&& make \
&& make install
FROM postgres:14.5-alpine
COPY --from=builder /aiven-extras/aiven_extras.control /usr/local/share/postgresql/extension/aiven_extras.control
COPY --from=builder /aiven-extras/sql/aiven_extras.sql /usr/local/share/postgresql/extension/aiven_extras--1.1.9.sql
COPY --from=builder /aiven-extras/aiven_extras.so /usr/local/lib/postgresql/aiven_extras.so
EXPOSE 5432
CMD ["postgres"]
+16 -34
View File
@@ -1,53 +1,35 @@
version: '3.9'
version: "3.9"
services:
main-db:
build:
context: aiven_postgres
dockerfile: Dockerfile
volumes:
- ./.data/main-db:/var/lib/postgresql/data
postgres:
image: postgres:16-alpine
ports:
- 5454:5432
volumes:
- ./.postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=speckle
- POSTGRES_USER=speckle
- POSTGRES_DB=speckle
extra_hosts:
- host.docker.internal:host-gateway
- POSTGRES_DB=speckle_main
region-1-db:
build:
context: aiven_postgres
dockerfile: Dockerfile
volumes:
- ./.data/region-1-db:/var/lib/postgresql/data
region-1:
image: postgres:16-alpine
ports:
- 5455:5432
volumes:
- ./.postgres-region-1:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=speckle
- POSTGRES_USER=speckle
- POSTGRES_DB=speckle
depends_on:
- main-db
- POSTGRES_DB=speckle_main
extra_hosts:
- host.docker.internal:host-gateway
region-2-db:
build:
context: aiven_postgres
dockerfile: Dockerfile
volumes:
- ./.data/region-2-db:/var/lib/postgresql/data
region-2:
image: postgres:16-alpine
ports:
- 5456:5432
volumes:
- ./.postgres-region-2:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=speckle
- POSTGRES_USER=speckle
- POSTGRES_DB=speckle
depends_on:
- main-db
extra_hosts:
- host.docker.internal:host-gateway
- POSTGRES_DB=speckle_main
-1
View File
@@ -31,7 +31,6 @@
},
"dependencies": {
"@apollo/server": "^4.10.0",
"awilix": "^11.0.0",
"crypto-random-string": "^3.0.0",
"dataloader": "^2.2.2",
"dotenv": "^16.4.1",
+2561 -3200
View File
File diff suppressed because it is too large Load Diff
-19
View File
@@ -1,19 +0,0 @@
import awilix from 'awilix'
import { saveResourceAclFactory, saveUserFactory } from './repositories'
import { Knex } from 'knex'
import { getMainDbClient } from './services/databaseManagement'
export const container = awilix.createContainer({
strict: true,
injectionMode: awilix.InjectionMode.PROXY
})
container.register({
db: awilix.asFunction(getMainDbClient).singleton(),
saveResource: awilix
.asFunction((regionDb: Knex) => saveUserFactory({ db: regionDb }))
.scoped(),
saveResourceAcl: awilix.asFunction(saveResourceAclFactory).scoped()
})
container.resolve('saveResource')
+227 -242
View File
@@ -12,251 +12,236 @@ import {
ResourceRegion
} from './types'
export const saveResourceFactory =
({ db }: { db: Knex }) =>
async (resource: Resource): Promise<void> => {
await db<Resource>('resources').insert(resource)
}
export class RegionRepo {
db: Knex
export const findResourceFactory =
({ db }: { db: Knex }) =>
async (resourceId: string): Promise<Resource | null> => {
return (
(await db<Resource>('resources').where({ id: resourceId }).first()) ??
constructor (db: Knex) {
this.db = db
}
async saveResource (resource: Resource): Promise<void> {
await this.db<Resource>('resources').insert(resource)
}
async findResource (resourceId: string): Promise<Resource | null> {
return (
(await this.db<Resource>('resources')
.where({ id: resourceId })
.first()) ?? null
)
}
async saveComment (comment: Comment): Promise<void> {
await this.db<Comment>('comments').insert(comment)
}
async countComments (resourceId: string): Promise<number> {
const [rawCount] = await this.db<Comment>('comments')
.count()
.where({ resourceId })
return parseInt(rawCount.count as string)
}
async queryComments ({
resourceId,
limit,
cursor
}: {
resourceId: string
limit: number
cursor: string | null
}): Promise<Comment[]> {
const query = this.db<Comment>('comments').where({ resourceId })
if (cursor) {
query.andWhere('createdAt', '<', cursor)
}
return await query.limit(limit)
}
}
export class MainRepo extends RegionRepo {
async findUser (userId: string): Promise<UserRecord | null> {
return (
(await this.db<UserRecord>('users').where('id', '=', userId).first()) ??
null
)
}
)
}
export const saveCommentFactory =
({ db }: { db: Knex }) =>
async (comment: Comment): Promise<void> => {
await db<Comment>('comments').insert(comment)
}
async queryUsers (): Promise<UserRecord[]> {
return await this.db<UserRecord>('users').select()
}
export const countCommentsFactory =
({ db }: { db: Knex }) =>
async (resourceId: string): Promise<number> => {
const [rawCount] = await db<Comment>('comments')
.count()
async saveUser (user: UserRecord): Promise<void> {
await this.db<UserRecord>('users').insert(user)
}
async getUsersResourceAcl ({
resourceId,
userId
}: ResourceAcl): Promise<ResourceAcl | null> {
return (
(await this.db<ResourceAcl>('resource_acl')
.where({ userId, resourceId })
.first()) ?? null
)
}
async saveResourceAcl (resourceAcl: ResourceAcl): Promise<void> {
await this.db<ResourceAcl>('resource_acl').insert(resourceAcl)
}
async countUsersResources (userId: string): Promise<number> {
const [rawCount] = await this.db<ResourceAcl>('resource_acl')
.count()
.where({ userId })
return parseInt(rawCount.count as string)
}
async findUsersResource ({
resourceId,
userId
}: ResourceAcl): Promise<ResourceAcl | null> {
return (
(await this.db<ResourceAcl>('resource_acl')
.where({ userId, resourceId })
.first()) ?? null
)
}
async queryResources ({
userId,
limit,
cursor
}: {
userId: string
limit: number
cursor: string | null
}): Promise<Resource[]> {
let query = this.db<Resource & ResourceAcl>('resources')
.join('resource_acl', 'resources.id', 'resource_acl.resourceId')
.where({ userId })
if (cursor) {
query = query.andWhere('createdAt', '<', cursor)
}
const items = await query.orderBy('createdAt', 'desc').limit(limit)
return items
}
async countResourceComments (resourceId: string): Promise<number> {
const [rawCount] = await this.db<Comment>('comments')
.count()
.where({ resourceId })
return parseInt(rawCount.count as string)
}
async queryComments ({
resourceId,
limit,
cursor
}: {
resourceId: string
limit: number
cursor: string | null
}): Promise<Comment[]> {
let query = this.db<Comment>('comments').where({ resourceId })
if (cursor) {
query = query.andWhere('createdAt', '<', cursor)
}
return await query.orderBy('createdAt', 'desc').limit(limit)
}
async queryRegions (
params:
| {
connectionString?: string | undefined
}
| undefined = undefined
): Promise<Region[]> {
const query = this.db<Region>('regions')
if ((params != null) && params.connectionString) query.where(params)
return await query.select()
}
async findRegion (id: string): Promise<Region | null> {
return (await this.db<Region>('regions').where({ id }).first()) ?? null
}
async queryOrganizationsRegions (): Promise<OrganizationsRegions[]> {
return await this.db<OrganizationsRegions>('organizations_regions').select()
}
async findOrganizationRegion ({
regionId,
organizationId
}: OrganizationsRegions): Promise<OrganizationsRegions | null> {
return (
(await this.db<OrganizationsRegions>('organizations_regions')
.where({ regionId, organizationId })
.first()) ?? null
)
}
async saveRegion (region: Region): Promise<void> {
await this.db<Region>('regions').insert(region)
}
async saveOrganization (organization: Organization) {
await this.db<Organization>('organizations').insert(organization)
}
async findOrganization (id: string): Promise<Organization | null> {
return (
(await this.db<Organization>('organizations').where({ id }).first()) ??
null
)
}
async queryOrganizations (): Promise<Organization[]> {
return await this.db<Organization>('organizations').select()
}
async saveOrganizationRegion (or: OrganizationsRegions): Promise<void> {
return await this.db<OrganizationsRegions>('organizations_regions').insert(
or
)
}
async saveOrganizationAcl (orgAcl: OrganizationAcl): Promise<void> {
await this.db<OrganizationsRegions>('organization_acl').insert(orgAcl)
}
async findOrganizationAcl ({
userId,
organizationId
}: OrganizationAcl): Promise<OrganizationAcl | null> {
return (
(await this.db<OrganizationAcl>('organization_acl')
.where({ userId, organizationId })
.first()) ?? null
)
}
async saveOrganizationResourceAcl (
item: OrganizationResourceAcl
): Promise<void> {
await this.db<OrganizationResourceAcl>('organization_resource_acl').insert(
item
)
}
async findResourceRegion ({
resourceId
}: {
resourceId: string
}): Promise<ResourceRegion | null> {
return (
(await this.db<ResourceRegion>('resource_region')
.where({ resourceId })
return parseInt(rawCount.count as string)
}
.first()) ?? null
)
}
export const findUserFactory =
({ db }: { db: Knex }) =>
async (userId: string): Promise<UserRecord | null> => {
return (
(await db<UserRecord>('users').where('id', '=', userId).first()) ?? null
)
}
export const queryUsersFactoy =
({ db }: { db: Knex }) =>
async (): Promise<UserRecord[]> => {
return await db<UserRecord>('users').select()
}
export const saveUserFactory =
({ db }: { db: Knex }) =>
async (user: UserRecord): Promise<void> => {
await db<UserRecord>('users').insert(user)
}
export const getUsersResourceAclFactory =
({ db }: { db: Knex }) =>
async ({ resourceId, userId }: ResourceAcl): Promise<ResourceAcl | null> => {
return (
(await db<ResourceAcl>('resource_acl')
.where({ userId, resourceId })
.first()) ?? null
)
}
export const saveResourceAclFactory =
({ db }: { db: Knex }) =>
async (resourceAcl: ResourceAcl): Promise<void> => {
await db<ResourceAcl>('resource_acl').insert(resourceAcl)
}
export const countUsersResourcesFactory =
({ db }: { db: Knex }) =>
async (userId: string): Promise<number> => {
const [rawCount] = await db<ResourceAcl>('resource_acl')
.count()
.where({ userId })
return parseInt(rawCount.count as string)
}
export const findUsersResourceFactory =
({ db }: { db: Knex }) =>
async ({ resourceId, userId }: ResourceAcl): Promise<ResourceAcl | null> => {
return (
(await db<ResourceAcl>('resource_acl')
.where({ userId, resourceId })
.first()) ?? null
)
}
export const queryResourcesFactory =
({ db }: { db: Knex }) =>
async ({
userId,
limit,
cursor
}: {
userId: string
limit: number
cursor: string | null
}): Promise<Resource[]> => {
let query = db<Resource & ResourceAcl>('resources')
.join('resource_acl', 'resources.id', 'resource_acl.resourceId')
.where({ userId })
if (cursor !== null) {
query = query.andWhere('createdAt', '<', cursor)
}
const items = await query.orderBy('createdAt', 'desc').limit(limit)
return items
}
export const countResourceCommentsFactory =
({ db }: { db: Knex }) =>
async (resourceId: string): Promise<number> => {
const [rawCount] = await db<Comment>('comments')
.count()
.where({ resourceId })
return parseInt(rawCount.count as string)
}
export const queryCommentsFactory =
({ db }: { db: Knex }) =>
async ({
resourceId,
limit,
cursor
}: {
resourceId: string
limit: number
cursor: string | null
}): Promise<Comment[]> => {
let query = db<Comment>('comments').where({ resourceId })
if (cursor !== null) {
query = query.andWhere('createdAt', '<', cursor)
}
return await query.orderBy('createdAt', 'desc').limit(limit)
}
export const queryRegionsFactory =
({ db }: { db: Knex }) =>
async (
params:
| {
connectionString?: string | undefined
}
| undefined = undefined
): Promise<Region[]> => {
let query = db<Region>('regions')
if (params?.connectionString !== undefined) query = query.where(params)
return await query.select()
}
export const findRegionFactory =
({ db }: { db: Knex }) =>
async (id: string): Promise<Region | null> => {
return (await db<Region>('regions').where({ id }).first()) ?? null
}
export const queryOrganizationsRegionsFactory =
({ db }: { db: Knex }) =>
async (): Promise<OrganizationsRegions[]> => {
return await db<OrganizationsRegions>('organizations_regions').select()
}
export const findOrganizationRegionFactory =
({ db }: { db: Knex }) =>
async ({
regionId,
organizationId
}: OrganizationsRegions): Promise<OrganizationsRegions | null> => {
return (
(await db<OrganizationsRegions>('organizations_regions')
.where({ regionId, organizationId })
.first()) ?? null
)
}
export const saveRegionFactory =
({ db }: { db: Knex }) =>
async (region: Region): Promise<void> => {
await db<Region>('regions').insert(region)
}
export const saveOrganizationFactory =
({ db }: { db: Knex }) =>
async (organization: Organization): Promise<void> => {
await db<Organization>('organizations').insert(organization)
}
export const findOrganizationFactory =
({ db }: { db: Knex }) =>
async (id: string): Promise<Organization | null> => {
return (
(await db<Organization>('organizations').where({ id }).first()) ?? null
)
}
export const queryOrganizationsFactory =
({ db }: { db: Knex }) =>
async (): Promise<Organization[]> => {
return await db<Organization>('organizations').select()
}
export const saveOrganizationRegionFactory =
({ db }: { db: Knex }) =>
async (or: OrganizationsRegions): Promise<void> => {
return await db<OrganizationsRegions>('organizations_regions').insert(or)
}
export const saveOrganizationAclFactory =
({ db }: { db: Knex }) =>
async (orgAcl: OrganizationAcl): Promise<void> => {
await db<OrganizationsRegions>('organization_acl').insert(orgAcl)
}
export const findOrganizationAclFactory =
({ db }: { db: Knex }) =>
async ({
userId,
organizationId
}: OrganizationAcl): Promise<OrganizationAcl | null> => {
return (
(await db<OrganizationAcl>('organization_acl')
.where({ userId, organizationId })
.first()) ?? null
)
}
export const saveOrganizationResourceAclFactory =
({ db }: { db: Knex }) =>
async (item: OrganizationResourceAcl): Promise<void> => {
await db<OrganizationResourceAcl>('organization_resource_acl').insert(item)
}
export const findResourceRegionFactory =
({ db }: { db: Knex }) =>
async ({
resourceId
}: {
resourceId: string
}): Promise<ResourceRegion | null> => {
return (
(await db<ResourceRegion>('resource_region')
.where({ resourceId })
.first()) ?? null
)
}
export const saveResourceRegionFactory =
({ db }: { db: Knex }) =>
async (item: ResourceRegion): Promise<void> => {
await db<ResourceRegion>('resource_region').insert(item)
}
async saveResourceRegion (item: ResourceRegion): Promise<void> {
await this.db<ResourceRegion>('resource_region').insert(item)
}
}
+46 -78
View File
@@ -1,9 +1,6 @@
import { getCommentsFactory } from './services/comments'
import awilix from 'awilix'
import {
createResourceFactory,
getResourcesFactory
} from './services/resources'
import { RegionRepo, MainRepo } from './repositories'
import { getComments } from './services/comments'
import { createResource, getResources } from './services/resources'
import { GraphQLError } from 'graphql'
import {
Resource,
@@ -19,51 +16,29 @@ import {
import {
createOrganization,
registerRegion,
getResourceDb,
getMainDbClient,
getRegionDb
getMainRepo,
getRegionRepo,
getResourceRepo
} from './services/databaseManagement'
import { authorizeUserOrgRegionFactory } from './services/authz'
import { authorizeUserOrgRegion } from './services/authz'
import cryptoRandomString from 'crypto-random-string'
import {
countCommentsFactory,
countUsersResourcesFactory,
findOrganizationAclFactory,
findOrganizationRegionFactory,
findResourceFactory,
findUserFactory,
getUsersResourceAclFactory,
queryCommentsFactory,
queryOrganizationsFactory,
queryRegionsFactory,
queryResourcesFactory,
queryUsersFactoy,
saveCommentFactory,
saveOrganizationAclFactory,
saveOrganizationRegionFactory,
saveOrganizationResourceAclFactory,
saveResourceAclFactory,
saveResourceRegionFactory,
saveUserFactory
} from './repositories'
import { container } from './iocContainer'
const db = getMainDbClient()
// Resolvers define how to fetch the types defined in your schema.
// This resolver retrieves books from the "books" array above.
export const resolvers = {
Query: {
async users () {
return await queryUsersFactoy({ db })()
return await getMainRepo().queryUsers()
},
async user (_: unknown, args: { id: string }) {
return await findUserFactory({ db })(args.id)
return await getMainRepo().findUser(args.id)
},
async resource (
_: unknown,
args: { id: string, userId: string }
): Promise<Resource> {
const maybeAcl = await getUsersResourceAclFactory({ db })({
const mainRepo = getMainRepo()
const maybeAcl = await mainRepo.getUsersResourceAcl({
userId: args.userId,
resourceId: args.id
})
@@ -77,10 +52,8 @@ export const resolvers = {
}
)
}
const resourceDb = await getResourceDb(args.id)
const maybeResource = await findResourceFactory({ db: resourceDb })(
args.id
)
const resourceRepo = await getResourceRepo(args.id)
const maybeResource = await resourceRepo.findResource(args.id)
if (maybeResource == null) {
throw new GraphQLError('Resource not found', {
extensions: { code: 'RESOURCE_NOT_FOUND' }
@@ -89,17 +62,18 @@ export const resolvers = {
return maybeResource
},
async organizations () {
return await queryOrganizationsFactory({ db })()
return await getMainRepo().queryOrganizations()
},
async regions () {
return await queryRegionsFactory({ db })()
return await getMainRepo().queryRegions()
}
},
User: {
async resources (parent: UserRecord, args: PaginationArgs) {
return await getResourcesFactory(
countUsersResourcesFactory({ db }),
queryResourcesFactory({ db })
const mainRepo = getMainRepo()
return await getResources(
mainRepo.countUsersResources.bind(mainRepo),
mainRepo.queryResources.bind(mainRepo)
)({ userId: parent.id, ...args })
}
},
@@ -108,10 +82,10 @@ export const resolvers = {
parent: Resource,
{ limit, cursor }: PaginationArgs
): Promise<CommentCollection> {
const resourceDb = await getResourceDb(parent.id)
return await getCommentsFactory(
countCommentsFactory({ db: resourceDb }),
queryCommentsFactory({ db: resourceDb })
const resourceRepo = await getResourceRepo(parent.id)
return await getComments(
resourceRepo.countComments.bind(resourceRepo),
resourceRepo.queryComments.bind(resourceRepo)
)({
resourceId: parent.id,
limit,
@@ -125,7 +99,7 @@ export const resolvers = {
{ input: { name } }: { input: UserCreateArgs }
) {
const id = cryptoRandomString({ length: 10 })
await saveUserFactory({ db })({ id, name })
await getMainRepo().saveUser({ id, name })
return id
},
async registerRegion (
@@ -142,45 +116,40 @@ export const resolvers = {
return await createOrganization(args.name)
},
async addRegionToOrganization (_: unknown, args: OrganizationsRegions) {
await saveOrganizationRegionFactory({ db })(args)
await getMainRepo().saveOrganizationRegion(args)
},
async addUserToOrganization (
_: unknown,
{ input: args }: { input: OrganizationAcl }
) {
await saveOrganizationAclFactory({ db })(args)
await getMainRepo().saveOrganizationAcl(args)
},
async createResource (
_: unknown,
{ input: args }: { input: ResourceCreateArgs }
) {
await authorizeUserOrgRegionFactory(
findOrganizationAclFactory({ db }),
findOrganizationRegionFactory({ db })
const mainRepo = getMainRepo()
await authorizeUserOrgRegion(
mainRepo.findOrganizationAcl.bind(mainRepo),
mainRepo.findOrganizationRegion.bind(mainRepo)
)(args)
const resourceDb =
args.regionId !== null
? await getRegionDb({ regionId: args.regionId })
: db
const repo = args.regionId
? await getRegionRepo({ regionId: args.regionId })
: mainRepo
const requestContainer = container.createScope()
requestContainer.register({ resourceDb: awilix.asValue(resourceDb) })
const resourceId = await createResource(
repo.saveResource.bind(repo),
mainRepo.saveResourceAcl.bind(mainRepo)
)(args)
const saveResource = requestContainer.resolve('saveResource')
const resourceId = await createResourceFactory({
saveResource,
saveResourceAcl: saveResourceAclFactory({ db })
})(args)
if (args.organizationId !== null) {
await saveOrganizationResourceAclFactory({ db })({
if (args.organizationId) {
await mainRepo.saveOrganizationResourceAcl({
organizationId: args.organizationId,
resourceId
})
if (args.regionId !== null) {
await saveResourceRegionFactory({ db })({
if (args.regionId) {
await mainRepo.saveResourceRegion({
resourceId,
// i know its not null here, the authz function ensures it
regionId: args.regionId
@@ -193,16 +162,15 @@ export const resolvers = {
_: unknown,
{ input: args }: { input: CommentCreateArgs }
) {
const resourceAcl = await getUsersResourceAclFactory({ db })(args)
if (resourceAcl == null) {
throw new Error("The user doesn't have access to the given resource")
}
const mainRepo = getMainRepo()
const resourceAcl = await mainRepo.getUsersResourceAcl(args)
if (resourceAcl == null) { throw new Error("The user doesn't have access to the given resource") }
// 2. get resource db client
const resourceDb = await getResourceDb(args.resourceId)
const resourceRepo = await getResourceRepo(args.resourceId)
// 3. save comment to db
const id = cryptoRandomString({ length: 10 })
const createdAt = new Date()
await saveCommentFactory({ db: resourceDb })({ id, createdAt, ...args })
await resourceRepo.saveComment({ id, createdAt, ...args })
return id
}
}
+4 -10
View File
@@ -4,7 +4,7 @@ import {
UserOrgRegionArgs
} from '../types'
export const authorizeUserOrgRegionFactory =
export const authorizeUserOrgRegion =
(
orgAclGetter: (params: OrganizationAcl) => Promise<OrganizationAcl | null>,
orgRegionGetter: (
@@ -12,18 +12,12 @@ export const authorizeUserOrgRegionFactory =
) => Promise<OrganizationsRegions | null>
) =>
async ({ userId, regionId, organizationId }: UserOrgRegionArgs) => {
if (!organizationId && regionId) {
throw new Error("public org doesn't support regions")
}
if (!organizationId && regionId) { throw new Error("public org doesn't support regions") }
if (organizationId) {
if (!regionId) throw new Error('organizations can only write to regions')
const orgAcl = await orgAclGetter({ organizationId, userId })
if (orgAcl == null) {
throw new Error("user doesn't have access to this organization")
}
if (orgAcl == null) { throw new Error("user doesn't have access to this organization") }
const orgRegion = await orgRegionGetter({ organizationId, regionId })
if (orgRegion == null) {
throw new Error('organization doesnt have access to this region')
}
if (orgRegion == null) { throw new Error('organization doesnt have access to this region') }
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ interface GetCommentsArgs extends PaginationArgs {
resourceId: string
}
export const getCommentsFactory =
export const getComments =
(
countComments: (resourceId: string) => Promise<number>,
queryComments: (params: GetCommentsArgs) => Promise<Comment[]>
+41 -51
View File
@@ -1,13 +1,7 @@
import { POSTGRES_URL } from '../config'
import { RegionRepo, MainRepo } from '../repositories'
import knex, { Knex } from 'knex'
import cryptoRandomString from 'crypto-random-string'
import {
findRegionFactory,
findResourceRegionFactory,
queryRegionsFactory,
saveOrganizationFactory,
saveRegionFactory
} from '../repositories'
const migrateToLatest = async (db: Knex): Promise<void> => {
const plannedMigrations: Array<{ file: string }> = (
@@ -27,11 +21,11 @@ const migrateToLatest = async (db: Knex): Promise<void> => {
}
export const migrateAll = async (): Promise<void> => {
await migrateToLatest(db)
const dbClients = await getAllDbClients()
await migrateToLatest(mainRepo.db)
const repos = await getAllRepositories()
await Promise.all([
...dbClients.map(async (db) => await migrateToLatest(db))
...repos.map(async (repo) => await migrateToLatest(repo.db))
])
}
@@ -58,33 +52,29 @@ const createDatabaseConfig = (
return config
}
const db = knex(createDatabaseConfig(POSTGRES_URL, null))
const mainRepo = new MainRepo(knex(createDatabaseConfig(POSTGRES_URL, null)))
const dbClientStore: Map<string, Knex> = new Map()
const findRegion = findRegionFactory({ db })
export const getRegionDb = async ({
const _repoStore: Map<string, RegionRepo> = new Map()
export const getRegionRepo = async ({
regionId
}: {
regionId: string | undefined
}): Promise<Knex> => {
if (!regionId) return db
const maybeClient = dbClientStore.get(regionId)
if (maybeClient != null) return maybeClient
const maybeRegion = await findRegion(regionId)
}): Promise<RegionRepo> => {
if (!regionId) return mainRepo
const maybeRepo = _repoStore.get(regionId)
if (maybeRepo != null) return maybeRepo
const maybeRegion = await mainRepo.findRegion(regionId)
if (maybeRegion == null) throw Error(`region ${regionId} not found`)
const client = knex(
createDatabaseConfig(maybeRegion.connectionString, maybeRegion.sslCaCert)
const repo = new RegionRepo(
knex(
createDatabaseConfig(maybeRegion.connectionString, maybeRegion.sslCaCert)
)
)
dbClientStore.set(regionId, client)
return client
_repoStore.set(regionId, repo)
return repo
}
export const getMainDbClient = (): Knex => db
const queryRegions = queryRegionsFactory({ db })
const saveRegion = saveRegionFactory({ db })
export const getMainRepo = (): MainRepo => mainRepo
export const registerRegion = async ({
name,
@@ -95,28 +85,30 @@ export const registerRegion = async ({
connectionString: string
sslCaCert: string | null
}): Promise<string> => {
const regions = await queryRegions({ connectionString })
const regions = await mainRepo.queryRegions({ connectionString })
if (regions.length > 0) throw new Error('This region is already registered')
const id = cryptoRandomString({ length: 10 })
const newDb = knex(createDatabaseConfig(connectionString, sslCaCert))
await migrateToLatest(newDb)
dbClientStore.set(id, newDb)
const repo = new RegionRepo(
knex(createDatabaseConfig(connectionString, sslCaCert))
)
await migrateToLatest(repo.db)
_repoStore.set(id, repo)
const sslmode = sslCaCert ? 'require' : 'disable'
await setUpUserReplication({
from: db,
to: newDb,
from: mainRepo.db,
to: repo.db,
regionName: name,
sslmode
})
await setUpResourceReplication({
from: newDb,
to: db,
from: repo.db,
to: mainRepo.db,
regionName: name,
sslmode
})
await saveRegion({
await mainRepo.saveRegion({
id,
name,
connectionString,
@@ -125,11 +117,9 @@ export const registerRegion = async ({
return id
}
const saveOrganization = saveOrganizationFactory({ db })
export const createOrganization = async (name: string): Promise<string> => {
const id = cryptoRandomString({ length: 10 })
await saveOrganization({ id, name })
await mainRepo.saveOrganization({ id, name })
return id
}
@@ -206,17 +196,17 @@ const setUpResourceReplication = async ({
}
}
export const getAllDbClients = async (): Promise<Knex[]> => {
const regions = await queryRegions({})
const regionClients = await Promise.all(
regions.map(async (region) => await getRegionDb({ regionId: region.id }))
export const getAllRepositories = async (): Promise<RegionRepo[]> => {
const regions = await mainRepo.queryRegions({})
const regionRepos = await Promise.all(
regions.map(async (region) => await getRegionRepo({ regionId: region.id }))
)
return [db, ...regionClients]
return [mainRepo, ...regionRepos]
}
const findResourceRegion = findResourceRegionFactory({ db })
export const getResourceDb = async (resourceId: string): Promise<Knex> => {
const resourceRegion = await findResourceRegion({ resourceId })
return resourceRegion != null ? await getRegionDb(resourceRegion) : db
export const getResourceRepo = async (
resourceId: string
): Promise<RegionRepo> => {
const resourceRegion = await mainRepo.findResourceRegion({ resourceId })
return (resourceRegion != null) ? await getRegionRepo(resourceRegion) : getMainRepo()
}
+8 -11
View File
@@ -10,7 +10,7 @@ import {
interface GetResourcesArgs extends PaginationArgs {
userId: string
}
export const getResourcesFactory =
export const getResources =
(
countResources: (userId: string) => Promise<number>,
queryResources: (params: GetResourcesArgs) => Promise<Resource[]>
@@ -29,14 +29,11 @@ export const getResourcesFactory =
}
}
export const createResourceFactory =
({
saveResource,
saveResourceAcl
}: {
saveResource: (resource: Resource) => Promise<void>
saveResourceAcl: (resourceAcl: ResourceAcl) => Promise<void>
}) =>
export const createResource =
(
resourceSaver: (resource: Resource) => Promise<void>,
resourceAclSaver: (resourceAcl: ResourceAcl) => Promise<void>
) =>
async ({ userId, name }: ResourceCreateArgs): Promise<string> => {
// 1. if no org, create project in main region, validate that, regionId is null
// 2. if org, validate if user has access to the org
@@ -44,7 +41,7 @@ export const createResourceFactory =
// 4. create resource
const id = cryptoRandomString({ length: 10 })
const resource = { id, name, createdAt: new Date() }
await saveResource(resource)
await saveResourceAcl({ resourceId: id, userId })
await resourceSaver(resource)
await resourceAclSaver({ resourceId: id, userId })
return id
}
+33
View File
@@ -0,0 +1,33 @@
import { knex } from "../../db";
import { Knex } from "knex";
type Thing = {
id: string;
name: string;
};
// talk to the DB
const repo =
(db: Knex<Thing>) =>
async (id: string): Promise<Thing | null> => {
return (await db.where({ id }).first()) ?? null;
};
// business / domain logic
const service =
(thingGetter: (id: string) => Promise<Thing | null>) =>
async (id: string): Promise<Thing | null> => {
return thingGetter(id);
};
const getThingClient = (id: string | undefined): Knex => {
if (!id) return knex;
return knex;
};
// graphql entry
export const resolver = async (args: { id: string }): Promise<Thing> => {
const thing = await service(repo(getThingClient(args.id)))(args.id);
if (!thing) throw new Error("not found");
return thing;
};
+33
View File
@@ -0,0 +1,33 @@
import { Knex } from "knex";
import { knex } from "../../db";
type Thing = {
id: string;
name: string;
};
type ServedThing = {
foo: number;
} & Thing;
// talk to the DB
const repo =
({ db }: { db: Knex<Thing> }) =>
async (id: string): Promise<Thing | null> => {
return (await db().where({ id }).first()) ?? null;
};
// business / domain logic
const service =
({ thingGetter }: { thingGetter: (id: string) => Promise<Thing | null> }) =>
async (id: string): Promise<ServedThing | null> => {
const thing = await thingGetter(id);
const foo = 123;
return thing ? { ...thing, foo } : null;
};
// graphql entry
export const resolver = async (id: string): Promise<ServedThing> => {
const thing = await service({ thingGetter: repo({ db: knex }) })(id);
if (!thing) throw new Error("not found");
return thing;
};
+8
View File
@@ -0,0 +1,8 @@
export type Thing = {
id: string;
};
export type ThingRepo = {
findThing: (id: string) => Promise<Thing | null>;
queryThing: () => Promise<Thing[]>;
};
+12
View File
@@ -0,0 +1,12 @@
import { Knex } from "knex";
import { Thing } from "./domain";
const findThing =
({ db }: { db: Knex }) =>
async (id: string): Promise<Thing | null> => {
return null;
};
export const thingRepo = ({ db }: { db: Knex }) => ({
findThing: findThing({ db }),
});
+11
View File
@@ -0,0 +1,11 @@
import { ServedThing, service } from "./services/service";
import { thingRepo } from "./repo";
import { knex } from "../../db";
export const resolver = async (id: string): Promise<ServedThing> => {
const thing = await service({
thingRepo: thingRepo({ db: knex }),
})(id);
if (!thing) throw new Error("not found");
return thing;
};
+19
View File
@@ -0,0 +1,19 @@
import { Thing, type ThingRepo } from "../domain";
export type ServedThing = {
foo: number;
} & Thing;
export const service =
({ thingRepo }: { thingRepo: Pick<ThingRepo, "findThing"> }) =>
async (id: string): Promise<ServedThing | null> => {
const thing = await thingRepo.findThing(id);
const foo = 123;
return thing ? { ...thing, foo } : null;
};
export const service2 = ({
thingRepo,
}: {
thingRepo: Pick<ThingRepo, "queryThing">;
}) => {};
View File
+24
View File
@@ -0,0 +1,24 @@
import { knex } from "../../db";
type Thing = {
id: string;
name: string;
};
const Things = () => knex<Thing>("things");
// talk to the DB
const repo = async (id: string): Promise<Thing | null> => {
return (await Things().where({ id }).first()) ?? null;
};
// business / domain logic
const service = async (id: string): Promise<Thing | null> => {
return repo(id);
};
// graphql entry
export const resolver = async (id: string): Promise<Thing> => {
const thing = await service(id);
if (!thing) throw new Error("not found");
return thing;
};
View File
+29
View File
@@ -0,0 +1,29 @@
import { expect, beforeAll, describe, it } from 'vitest'
import {
getOrganizationRegionsFrom,
getRegionsFrom
} from '../../src/repositories'
import {
getMainDbClient,
migrateAll
} from '../../src/services/databaseManagement'
import { Knex } from 'knex'
describe('regions', () => {
let dbClient: Knex
beforeAll(async () => {
dbClient = await getMainDbClient()
})
it('gets all regions', async () => {
const regions = await getRegionsFrom(dbClient)()
expect(regions.length).toBeGreaterThan(0)
})
it('gets organizations regions', async () => {
const orgRegions = await getOrganizationRegionsFrom(dbClient)()
expect(orgRegions.length).toBeGreaterThan(0)
})
it('migrates all', async () => {
await migrateAll()
})
})