Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b06afcfb1 | |||
| daea6d3765 |
@@ -5,5 +5,6 @@
|
||||
.swc
|
||||
node_modules
|
||||
ca-cert*
|
||||
.data/*
|
||||
|
||||
dist
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"cSpell.words": ["awilix"]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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"]
|
||||
+34
-16
@@ -1,35 +1,53 @@
|
||||
version: "3.9"
|
||||
version: '3.9'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
main-db:
|
||||
build:
|
||||
context: aiven_postgres
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./.data/main-db:/var/lib/postgresql/data
|
||||
ports:
|
||||
- 5454:5432
|
||||
volumes:
|
||||
- ./.postgres-data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=speckle
|
||||
- POSTGRES_USER=speckle
|
||||
- POSTGRES_DB=speckle_main
|
||||
- POSTGRES_DB=speckle
|
||||
extra_hosts:
|
||||
- host.docker.internal:host-gateway
|
||||
|
||||
region-1:
|
||||
image: postgres:16-alpine
|
||||
region-1-db:
|
||||
build:
|
||||
context: aiven_postgres
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./.data/region-1-db:/var/lib/postgresql/data
|
||||
ports:
|
||||
- 5455:5432
|
||||
volumes:
|
||||
- ./.postgres-region-1:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=speckle
|
||||
- POSTGRES_USER=speckle
|
||||
- POSTGRES_DB=speckle_main
|
||||
- POSTGRES_DB=speckle
|
||||
depends_on:
|
||||
- main-db
|
||||
|
||||
region-2:
|
||||
image: postgres:16-alpine
|
||||
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
|
||||
ports:
|
||||
- 5456:5432
|
||||
volumes:
|
||||
- ./.postgres-region-2:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=speckle
|
||||
- POSTGRES_USER=speckle
|
||||
- POSTGRES_DB=speckle_main
|
||||
- POSTGRES_DB=speckle
|
||||
depends_on:
|
||||
- main-db
|
||||
|
||||
extra_hosts:
|
||||
- host.docker.internal:host-gateway
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@apollo/server": "^4.10.0",
|
||||
"awilix": "^11.0.0",
|
||||
"crypto-random-string": "^3.0.0",
|
||||
"dataloader": "^2.2.2",
|
||||
"dotenv": "^16.4.1",
|
||||
|
||||
Generated
+3202
-2563
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
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')
|
||||
+239
-224
@@ -12,236 +12,251 @@ import {
|
||||
ResourceRegion
|
||||
} from './types'
|
||||
|
||||
export class RegionRepo {
|
||||
db: Knex
|
||||
|
||||
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)
|
||||
export const saveResourceFactory =
|
||||
({ db }: { db: Knex }) =>
|
||||
async (resource: Resource): Promise<void> => {
|
||||
await db<Resource>('resources').insert(resource)
|
||||
}
|
||||
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()) ??
|
||||
export const findResourceFactory =
|
||||
({ db }: { db: Knex }) =>
|
||||
async (resourceId: string): Promise<Resource | null> => {
|
||||
return (
|
||||
(await db<Resource>('resources').where({ id: resourceId }).first()) ??
|
||||
null
|
||||
)
|
||||
}
|
||||
|
||||
async queryUsers (): Promise<UserRecord[]> {
|
||||
return await this.db<UserRecord>('users').select()
|
||||
}
|
||||
|
||||
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)
|
||||
export const saveCommentFactory =
|
||||
({ db }: { db: Knex }) =>
|
||||
async (comment: Comment): Promise<void> => {
|
||||
await db<Comment>('comments').insert(comment)
|
||||
}
|
||||
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')
|
||||
export const countCommentsFactory =
|
||||
({ db }: { db: Knex }) =>
|
||||
async (resourceId: string): Promise<number> => {
|
||||
const [rawCount] = await db<Comment>('comments')
|
||||
.count()
|
||||
.where({ resourceId })
|
||||
.first()) ?? null
|
||||
)
|
||||
}
|
||||
return parseInt(rawCount.count as string)
|
||||
}
|
||||
|
||||
async saveResourceRegion (item: ResourceRegion): Promise<void> {
|
||||
await this.db<ResourceRegion>('resource_region').insert(item)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
+78
-46
@@ -1,6 +1,9 @@
|
||||
import { RegionRepo, MainRepo } from './repositories'
|
||||
import { getComments } from './services/comments'
|
||||
import { createResource, getResources } from './services/resources'
|
||||
import { getCommentsFactory } from './services/comments'
|
||||
import awilix from 'awilix'
|
||||
import {
|
||||
createResourceFactory,
|
||||
getResourcesFactory
|
||||
} from './services/resources'
|
||||
import { GraphQLError } from 'graphql'
|
||||
import {
|
||||
Resource,
|
||||
@@ -16,29 +19,51 @@ import {
|
||||
import {
|
||||
createOrganization,
|
||||
registerRegion,
|
||||
getMainRepo,
|
||||
getRegionRepo,
|
||||
getResourceRepo
|
||||
getResourceDb,
|
||||
getMainDbClient,
|
||||
getRegionDb
|
||||
} from './services/databaseManagement'
|
||||
import { authorizeUserOrgRegion } from './services/authz'
|
||||
import { authorizeUserOrgRegionFactory } 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 getMainRepo().queryUsers()
|
||||
return await queryUsersFactoy({ db })()
|
||||
},
|
||||
async user (_: unknown, args: { id: string }) {
|
||||
return await getMainRepo().findUser(args.id)
|
||||
return await findUserFactory({ db })(args.id)
|
||||
},
|
||||
async resource (
|
||||
_: unknown,
|
||||
args: { id: string, userId: string }
|
||||
): Promise<Resource> {
|
||||
const mainRepo = getMainRepo()
|
||||
const maybeAcl = await mainRepo.getUsersResourceAcl({
|
||||
const maybeAcl = await getUsersResourceAclFactory({ db })({
|
||||
userId: args.userId,
|
||||
resourceId: args.id
|
||||
})
|
||||
@@ -52,8 +77,10 @@ export const resolvers = {
|
||||
}
|
||||
)
|
||||
}
|
||||
const resourceRepo = await getResourceRepo(args.id)
|
||||
const maybeResource = await resourceRepo.findResource(args.id)
|
||||
const resourceDb = await getResourceDb(args.id)
|
||||
const maybeResource = await findResourceFactory({ db: resourceDb })(
|
||||
args.id
|
||||
)
|
||||
if (maybeResource == null) {
|
||||
throw new GraphQLError('Resource not found', {
|
||||
extensions: { code: 'RESOURCE_NOT_FOUND' }
|
||||
@@ -62,18 +89,17 @@ export const resolvers = {
|
||||
return maybeResource
|
||||
},
|
||||
async organizations () {
|
||||
return await getMainRepo().queryOrganizations()
|
||||
return await queryOrganizationsFactory({ db })()
|
||||
},
|
||||
async regions () {
|
||||
return await getMainRepo().queryRegions()
|
||||
return await queryRegionsFactory({ db })()
|
||||
}
|
||||
},
|
||||
User: {
|
||||
async resources (parent: UserRecord, args: PaginationArgs) {
|
||||
const mainRepo = getMainRepo()
|
||||
return await getResources(
|
||||
mainRepo.countUsersResources.bind(mainRepo),
|
||||
mainRepo.queryResources.bind(mainRepo)
|
||||
return await getResourcesFactory(
|
||||
countUsersResourcesFactory({ db }),
|
||||
queryResourcesFactory({ db })
|
||||
)({ userId: parent.id, ...args })
|
||||
}
|
||||
},
|
||||
@@ -82,10 +108,10 @@ export const resolvers = {
|
||||
parent: Resource,
|
||||
{ limit, cursor }: PaginationArgs
|
||||
): Promise<CommentCollection> {
|
||||
const resourceRepo = await getResourceRepo(parent.id)
|
||||
return await getComments(
|
||||
resourceRepo.countComments.bind(resourceRepo),
|
||||
resourceRepo.queryComments.bind(resourceRepo)
|
||||
const resourceDb = await getResourceDb(parent.id)
|
||||
return await getCommentsFactory(
|
||||
countCommentsFactory({ db: resourceDb }),
|
||||
queryCommentsFactory({ db: resourceDb })
|
||||
)({
|
||||
resourceId: parent.id,
|
||||
limit,
|
||||
@@ -99,7 +125,7 @@ export const resolvers = {
|
||||
{ input: { name } }: { input: UserCreateArgs }
|
||||
) {
|
||||
const id = cryptoRandomString({ length: 10 })
|
||||
await getMainRepo().saveUser({ id, name })
|
||||
await saveUserFactory({ db })({ id, name })
|
||||
return id
|
||||
},
|
||||
async registerRegion (
|
||||
@@ -116,40 +142,45 @@ export const resolvers = {
|
||||
return await createOrganization(args.name)
|
||||
},
|
||||
async addRegionToOrganization (_: unknown, args: OrganizationsRegions) {
|
||||
await getMainRepo().saveOrganizationRegion(args)
|
||||
await saveOrganizationRegionFactory({ db })(args)
|
||||
},
|
||||
async addUserToOrganization (
|
||||
_: unknown,
|
||||
{ input: args }: { input: OrganizationAcl }
|
||||
) {
|
||||
await getMainRepo().saveOrganizationAcl(args)
|
||||
await saveOrganizationAclFactory({ db })(args)
|
||||
},
|
||||
async createResource (
|
||||
_: unknown,
|
||||
{ input: args }: { input: ResourceCreateArgs }
|
||||
) {
|
||||
const mainRepo = getMainRepo()
|
||||
await authorizeUserOrgRegion(
|
||||
mainRepo.findOrganizationAcl.bind(mainRepo),
|
||||
mainRepo.findOrganizationRegion.bind(mainRepo)
|
||||
await authorizeUserOrgRegionFactory(
|
||||
findOrganizationAclFactory({ db }),
|
||||
findOrganizationRegionFactory({ db })
|
||||
)(args)
|
||||
|
||||
const repo = args.regionId
|
||||
? await getRegionRepo({ regionId: args.regionId })
|
||||
: mainRepo
|
||||
const resourceDb =
|
||||
args.regionId !== null
|
||||
? await getRegionDb({ regionId: args.regionId })
|
||||
: db
|
||||
|
||||
const resourceId = await createResource(
|
||||
repo.saveResource.bind(repo),
|
||||
mainRepo.saveResourceAcl.bind(mainRepo)
|
||||
)(args)
|
||||
const requestContainer = container.createScope()
|
||||
requestContainer.register({ resourceDb: awilix.asValue(resourceDb) })
|
||||
|
||||
if (args.organizationId) {
|
||||
await mainRepo.saveOrganizationResourceAcl({
|
||||
const saveResource = requestContainer.resolve('saveResource')
|
||||
|
||||
const resourceId = await createResourceFactory({
|
||||
saveResource,
|
||||
saveResourceAcl: saveResourceAclFactory({ db })
|
||||
})(args)
|
||||
|
||||
if (args.organizationId !== null) {
|
||||
await saveOrganizationResourceAclFactory({ db })({
|
||||
organizationId: args.organizationId,
|
||||
resourceId
|
||||
})
|
||||
if (args.regionId) {
|
||||
await mainRepo.saveResourceRegion({
|
||||
if (args.regionId !== null) {
|
||||
await saveResourceRegionFactory({ db })({
|
||||
resourceId,
|
||||
// i know its not null here, the authz function ensures it
|
||||
regionId: args.regionId
|
||||
@@ -162,15 +193,16 @@ export const resolvers = {
|
||||
_: unknown,
|
||||
{ input: args }: { input: CommentCreateArgs }
|
||||
) {
|
||||
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") }
|
||||
const resourceAcl = await getUsersResourceAclFactory({ db })(args)
|
||||
if (resourceAcl == null) {
|
||||
throw new Error("The user doesn't have access to the given resource")
|
||||
}
|
||||
// 2. get resource db client
|
||||
const resourceRepo = await getResourceRepo(args.resourceId)
|
||||
const resourceDb = await getResourceDb(args.resourceId)
|
||||
// 3. save comment to db
|
||||
const id = cryptoRandomString({ length: 10 })
|
||||
const createdAt = new Date()
|
||||
await resourceRepo.saveComment({ id, createdAt, ...args })
|
||||
await saveCommentFactory({ db: resourceDb })({ id, createdAt, ...args })
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
+10
-4
@@ -4,7 +4,7 @@ import {
|
||||
UserOrgRegionArgs
|
||||
} from '../types'
|
||||
|
||||
export const authorizeUserOrgRegion =
|
||||
export const authorizeUserOrgRegionFactory =
|
||||
(
|
||||
orgAclGetter: (params: OrganizationAcl) => Promise<OrganizationAcl | null>,
|
||||
orgRegionGetter: (
|
||||
@@ -12,12 +12,18 @@ export const authorizeUserOrgRegion =
|
||||
) => 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')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ interface GetCommentsArgs extends PaginationArgs {
|
||||
resourceId: string
|
||||
}
|
||||
|
||||
export const getComments =
|
||||
export const getCommentsFactory =
|
||||
(
|
||||
countComments: (resourceId: string) => Promise<number>,
|
||||
queryComments: (params: GetCommentsArgs) => Promise<Comment[]>
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
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 }> = (
|
||||
@@ -21,11 +27,11 @@ const migrateToLatest = async (db: Knex): Promise<void> => {
|
||||
}
|
||||
|
||||
export const migrateAll = async (): Promise<void> => {
|
||||
await migrateToLatest(mainRepo.db)
|
||||
const repos = await getAllRepositories()
|
||||
await migrateToLatest(db)
|
||||
const dbClients = await getAllDbClients()
|
||||
|
||||
await Promise.all([
|
||||
...repos.map(async (repo) => await migrateToLatest(repo.db))
|
||||
...dbClients.map(async (db) => await migrateToLatest(db))
|
||||
])
|
||||
}
|
||||
|
||||
@@ -52,29 +58,33 @@ const createDatabaseConfig = (
|
||||
return config
|
||||
}
|
||||
|
||||
const mainRepo = new MainRepo(knex(createDatabaseConfig(POSTGRES_URL, null)))
|
||||
const db = knex(createDatabaseConfig(POSTGRES_URL, null))
|
||||
|
||||
const _repoStore: Map<string, RegionRepo> = new Map()
|
||||
export const getRegionRepo = async ({
|
||||
const dbClientStore: Map<string, Knex> = new Map()
|
||||
|
||||
const findRegion = findRegionFactory({ db })
|
||||
|
||||
export const getRegionDb = async ({
|
||||
regionId
|
||||
}: {
|
||||
regionId: string | undefined
|
||||
}): Promise<RegionRepo> => {
|
||||
if (!regionId) return mainRepo
|
||||
const maybeRepo = _repoStore.get(regionId)
|
||||
if (maybeRepo != null) return maybeRepo
|
||||
const maybeRegion = await mainRepo.findRegion(regionId)
|
||||
}): Promise<Knex> => {
|
||||
if (!regionId) return db
|
||||
const maybeClient = dbClientStore.get(regionId)
|
||||
if (maybeClient != null) return maybeClient
|
||||
const maybeRegion = await findRegion(regionId)
|
||||
if (maybeRegion == null) throw Error(`region ${regionId} not found`)
|
||||
const repo = new RegionRepo(
|
||||
knex(
|
||||
createDatabaseConfig(maybeRegion.connectionString, maybeRegion.sslCaCert)
|
||||
)
|
||||
const client = knex(
|
||||
createDatabaseConfig(maybeRegion.connectionString, maybeRegion.sslCaCert)
|
||||
)
|
||||
_repoStore.set(regionId, repo)
|
||||
return repo
|
||||
dbClientStore.set(regionId, client)
|
||||
return client
|
||||
}
|
||||
|
||||
export const getMainRepo = (): MainRepo => mainRepo
|
||||
export const getMainDbClient = (): Knex => db
|
||||
|
||||
const queryRegions = queryRegionsFactory({ db })
|
||||
const saveRegion = saveRegionFactory({ db })
|
||||
|
||||
export const registerRegion = async ({
|
||||
name,
|
||||
@@ -85,30 +95,28 @@ export const registerRegion = async ({
|
||||
connectionString: string
|
||||
sslCaCert: string | null
|
||||
}): Promise<string> => {
|
||||
const regions = await mainRepo.queryRegions({ connectionString })
|
||||
const regions = await queryRegions({ connectionString })
|
||||
if (regions.length > 0) throw new Error('This region is already registered')
|
||||
const id = cryptoRandomString({ length: 10 })
|
||||
const repo = new RegionRepo(
|
||||
knex(createDatabaseConfig(connectionString, sslCaCert))
|
||||
)
|
||||
await migrateToLatest(repo.db)
|
||||
_repoStore.set(id, repo)
|
||||
const newDb = knex(createDatabaseConfig(connectionString, sslCaCert))
|
||||
await migrateToLatest(newDb)
|
||||
dbClientStore.set(id, newDb)
|
||||
|
||||
const sslmode = sslCaCert ? 'require' : 'disable'
|
||||
await setUpUserReplication({
|
||||
from: mainRepo.db,
|
||||
to: repo.db,
|
||||
from: db,
|
||||
to: newDb,
|
||||
regionName: name,
|
||||
sslmode
|
||||
})
|
||||
await setUpResourceReplication({
|
||||
from: repo.db,
|
||||
to: mainRepo.db,
|
||||
from: newDb,
|
||||
to: db,
|
||||
regionName: name,
|
||||
sslmode
|
||||
})
|
||||
|
||||
await mainRepo.saveRegion({
|
||||
await saveRegion({
|
||||
id,
|
||||
name,
|
||||
connectionString,
|
||||
@@ -117,9 +125,11 @@ export const registerRegion = async ({
|
||||
return id
|
||||
}
|
||||
|
||||
const saveOrganization = saveOrganizationFactory({ db })
|
||||
|
||||
export const createOrganization = async (name: string): Promise<string> => {
|
||||
const id = cryptoRandomString({ length: 10 })
|
||||
await mainRepo.saveOrganization({ id, name })
|
||||
await saveOrganization({ id, name })
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -196,17 +206,17 @@ const setUpResourceReplication = async ({
|
||||
}
|
||||
}
|
||||
|
||||
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 }))
|
||||
export const getAllDbClients = async (): Promise<Knex[]> => {
|
||||
const regions = await queryRegions({})
|
||||
const regionClients = await Promise.all(
|
||||
regions.map(async (region) => await getRegionDb({ regionId: region.id }))
|
||||
)
|
||||
return [mainRepo, ...regionRepos]
|
||||
return [db, ...regionClients]
|
||||
}
|
||||
|
||||
export const getResourceRepo = async (
|
||||
resourceId: string
|
||||
): Promise<RegionRepo> => {
|
||||
const resourceRegion = await mainRepo.findResourceRegion({ resourceId })
|
||||
return (resourceRegion != null) ? await getRegionRepo(resourceRegion) : getMainRepo()
|
||||
const findResourceRegion = findResourceRegionFactory({ db })
|
||||
|
||||
export const getResourceDb = async (resourceId: string): Promise<Knex> => {
|
||||
const resourceRegion = await findResourceRegion({ resourceId })
|
||||
return resourceRegion != null ? await getRegionDb(resourceRegion) : db
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
interface GetResourcesArgs extends PaginationArgs {
|
||||
userId: string
|
||||
}
|
||||
export const getResources =
|
||||
export const getResourcesFactory =
|
||||
(
|
||||
countResources: (userId: string) => Promise<number>,
|
||||
queryResources: (params: GetResourcesArgs) => Promise<Resource[]>
|
||||
@@ -29,11 +29,14 @@ export const getResources =
|
||||
}
|
||||
}
|
||||
|
||||
export const createResource =
|
||||
(
|
||||
resourceSaver: (resource: Resource) => Promise<void>,
|
||||
resourceAclSaver: (resourceAcl: ResourceAcl) => Promise<void>
|
||||
) =>
|
||||
export const createResourceFactory =
|
||||
({
|
||||
saveResource,
|
||||
saveResourceAcl
|
||||
}: {
|
||||
saveResource: (resource: Resource) => Promise<void>
|
||||
saveResourceAcl: (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
|
||||
@@ -41,7 +44,7 @@ export const createResource =
|
||||
// 4. create resource
|
||||
const id = cryptoRandomString({ length: 10 })
|
||||
const resource = { id, name, createdAt: new Date() }
|
||||
await resourceSaver(resource)
|
||||
await resourceAclSaver({ resourceId: id, userId })
|
||||
await saveResource(resource)
|
||||
await saveResourceAcl({ resourceId: id, userId })
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user