Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 024e8eabbc |
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
.postgres-*
|
.postgres-data
|
||||||
.tool-versions
|
.tool-versions
|
||||||
.env
|
.env
|
||||||
.envrc
|
.envrc
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
# Multi tenancy sketches
|
|
||||||
|
|
||||||
This project is a synthetic test project for implementing multi tenancy.
|
|
||||||
Its a functional graphql api backend, that is easiest to access from the apollo graphql explorer.
|
|
||||||
The api and the explorer are available by default at `http://localhost:4000` by default,
|
|
||||||
after the app and its dependencies have been started
|
|
||||||
|
|
||||||
## Project setup
|
|
||||||
|
|
||||||
This project is using [`pnpm`](https://pnpm.io/) as its package manager.
|
|
||||||
To start the required databases or other dependencies, run `docker compose up -d`
|
|
||||||
|
|
||||||
## About Postgres setup
|
|
||||||
|
|
||||||
I had to change the `wal_level` on my local postgres instances
|
|
||||||
it is done with running the SQL command below, and restating the database server:
|
|
||||||
|
|
||||||
```sql
|
|
||||||
ALTER SYSTEM SET wal_level = logical;
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: Postgres subscriptions (which we use) in the same db server don't work that easily; easiest way to get things going is to set up multiple db servers locally.
|
|
||||||
|
|
||||||
## Project description
|
|
||||||
|
|
||||||
The app has these basic concepts:
|
|
||||||
|
|
||||||
### User
|
|
||||||
|
|
||||||
A user of the system (obviously). User authn is not implemented, authz is very simplified.
|
|
||||||
|
|
||||||
### Resource
|
|
||||||
|
|
||||||
This is an abstract object representing a project, that multiple users might work on.
|
|
||||||
The notion of work on is currently implemented as the comment create action.
|
|
||||||
A resource might belong to an organization or belong to the default (null) organization.
|
|
||||||
|
|
||||||
### Comment
|
|
||||||
|
|
||||||
A text note, that belongs to a given resource, created by a user.
|
|
||||||
|
|
||||||
### Region
|
|
||||||
|
|
||||||
A geo-located data storage region, currently implemented as a PostgresSQL database server.
|
|
||||||
When providing a connection url to a region, make sure to not include a database name or any trailing `/`-s in the url.
|
|
||||||
|
|
||||||
### Organization
|
|
||||||
|
|
||||||
A collection of users and an owner of resource. Any user may create organizations.
|
|
||||||
Organizations may be granted access to any given region. That action creates a new database in the region DB server. migrates it to the latest DB schema and sets up user and resource publish and subscribe mechanisms.
|
|
||||||
|
|
||||||
## Steps to flex this POC
|
|
||||||
|
|
||||||
Using the exposed graphql explorer, you can go ahead and
|
|
||||||
- create a user
|
|
||||||
- create an organisation
|
|
||||||
- add the user to the organisation
|
|
||||||
- create regions & associate them with an organisation
|
|
||||||
- create a resource in the default organisation, or for a specific organisation & region
|
|
||||||
- etc.
|
|
||||||
|
|
||||||
|
|
||||||
+18
-13
@@ -1,35 +1,40 @@
|
|||||||
version: "3.9"
|
version: "3.9"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
postgres:
|
maindb:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
ports:
|
ports:
|
||||||
- 5454:5432
|
- 5454:5432
|
||||||
volumes:
|
|
||||||
- ./.postgres-data:/var/lib/postgresql/data
|
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_PASSWORD=speckle
|
- POSTGRES_PASSWORD=speckle
|
||||||
- POSTGRES_USER=speckle
|
- POSTGRES_USER=speckle
|
||||||
- POSTGRES_DB=speckle_main
|
- POSTGRES_DB=speckle_main
|
||||||
|
|
||||||
region-1:
|
eu_db:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
ports:
|
ports:
|
||||||
- 5455:5432
|
- 5455:5433
|
||||||
volumes:
|
|
||||||
- ./.postgres-region-1:/var/lib/postgresql/data
|
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_PASSWORD=speckle
|
- POSTGRES_PASSWORD=speckle
|
||||||
- POSTGRES_USER=speckle
|
- POSTGRES_USER=speckle
|
||||||
- POSTGRES_DB=speckle_main
|
- POSTGRES_DB=speckle_eu
|
||||||
|
- PGPORT=5433
|
||||||
|
|
||||||
region-2:
|
us_db:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
ports:
|
ports:
|
||||||
- 5456:5432
|
- 5456:5434
|
||||||
volumes:
|
|
||||||
- ./.postgres-region-2:/var/lib/postgresql/data
|
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_PASSWORD=speckle
|
- POSTGRES_PASSWORD=speckle
|
||||||
- POSTGRES_USER=speckle
|
- POSTGRES_USER=speckle
|
||||||
- POSTGRES_DB=speckle_main
|
- POSTGRES_DB=speckle_us
|
||||||
|
- PGPORT=5434
|
||||||
|
|
||||||
|
start_dependencies:
|
||||||
|
image: tehkapa/docker-wait-for-dependencies
|
||||||
|
depends_on:
|
||||||
|
- maindb
|
||||||
|
- eu_db
|
||||||
|
- us_db
|
||||||
|
container_name: wait-for-dependencies
|
||||||
|
command: maindb:5432 eu_db:5433 us_db:5434
|
||||||
|
|||||||
+39
-2
@@ -1,6 +1,43 @@
|
|||||||
export default {
|
export const mainDBConfig = {
|
||||||
client: 'pg',
|
client: 'pg',
|
||||||
connection: process.env.POSTGRES_URL,
|
connection: {
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 5454,
|
||||||
|
user: 'speckle',
|
||||||
|
database: 'speckle_main',
|
||||||
|
password: 'speckle'
|
||||||
|
},
|
||||||
|
migrations: {
|
||||||
|
directory: 'src/migrations',
|
||||||
|
extension: 'ts'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const euDBConfig = {
|
||||||
|
client: 'pg',
|
||||||
|
connection: {
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 5455,
|
||||||
|
user: 'speckle',
|
||||||
|
database: 'speckle_eu',
|
||||||
|
password: 'speckle'
|
||||||
|
},
|
||||||
|
migrations: {
|
||||||
|
directory: 'src/migrations',
|
||||||
|
extension: 'ts'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const usDBConfig = {
|
||||||
|
client: 'pg',
|
||||||
|
connection: {
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 5456,
|
||||||
|
user: 'speckle',
|
||||||
|
database: 'speckle_us',
|
||||||
|
password: 'speckle'
|
||||||
|
},
|
||||||
migrations: {
|
migrations: {
|
||||||
directory: 'src/migrations',
|
directory: 'src/migrations',
|
||||||
extension: 'ts'
|
extension: 'ts'
|
||||||
|
|||||||
+5
-6
@@ -4,11 +4,11 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/app.ts",
|
"main": "src/app.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "vitest",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"lint": "ts-standard",
|
"lint": "ts-standard",
|
||||||
"tsx": "tsx",
|
"tsx": "docker compose up start_dependencies && tsx watch --clear-screen=false --conditions=typescript ./src/app.ts",
|
||||||
"lint:fix": "ts-standard --fix",
|
"lint:fix": "ts-standard --fix",
|
||||||
"migration:make": "NODE_OPTIONS='--loader ts-node/esm' knex migrate:make",
|
"migration:make": "NODE_OPTIONS='--import tsx' knex migrate:make",
|
||||||
"dev:old": "nodemon --ext ts,graphql --exec node --inspect -r @swc/register src/bin/www.ts",
|
"dev:old": "nodemon --ext ts,graphql --exec node --inspect -r @swc/register src/bin/www.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/app.js",
|
"start": "node dist/app.js",
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@ducktors/tsconfig": "^1.0.0",
|
||||||
"@types/node": "^20.11.13",
|
"@types/node": "^20.11.13",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
||||||
"@typescript-eslint/parser": "^6.20.0",
|
"@typescript-eslint/parser": "^6.20.0",
|
||||||
@@ -26,12 +27,10 @@
|
|||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"ts-standard": "^12.0.2",
|
"ts-standard": "^12.0.2",
|
||||||
"tsx": "^4.7.0",
|
"tsx": "^4.7.0",
|
||||||
"typescript": "^5.3.3",
|
"typescript": "^5.3.3"
|
||||||
"vitest": "^1.2.2"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/server": "^4.10.0",
|
"@apollo/server": "^4.10.0",
|
||||||
"crypto-random-string": "^3.0.0",
|
|
||||||
"dotenv": "^16.4.1",
|
"dotenv": "^16.4.1",
|
||||||
"graphql": "^16.8.1",
|
"graphql": "^16.8.1",
|
||||||
"graphql-scalars": "^1.22.4",
|
"graphql-scalars": "^1.22.4",
|
||||||
|
|||||||
Generated
+2764
-2790
File diff suppressed because it is too large
Load Diff
+37
-16
@@ -1,31 +1,52 @@
|
|||||||
import { ApolloServer } from "@apollo/server";
|
import { ApolloServer } from '@apollo/server'
|
||||||
import { resolvers } from "./resolvers";
|
import { resolvers } from './resolvers'
|
||||||
import { startStandaloneServer } from "@apollo/server/standalone";
|
import { startStandaloneServer } from '@apollo/server/standalone'
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from 'fs'
|
||||||
import { typeDefs as scalarTypeDefs } from "graphql-scalars";
|
import { typeDefs as scalarTypeDefs } from 'graphql-scalars'
|
||||||
import { migrateAll } from "./services/databaseManagement";
|
import { getDB } from './db'
|
||||||
|
import { Regions } from './regions'
|
||||||
|
import { initListeners } from './listeners'
|
||||||
|
|
||||||
const typeDefs = readFileSync("src/schema.graphql", { encoding: "utf-8" });
|
const typeDefs = readFileSync('src/schema.graphql', { encoding: 'utf-8' })
|
||||||
|
|
||||||
// The ApolloServer constructor requires two parameters: your schema
|
// The ApolloServer constructor requires two parameters: your schema
|
||||||
// definition and your set of resolvers.
|
// definition and your set of resolvers.
|
||||||
const server = new ApolloServer({
|
const server = new ApolloServer({
|
||||||
typeDefs: [typeDefs, ...scalarTypeDefs],
|
typeDefs: [typeDefs, ...scalarTypeDefs],
|
||||||
resolvers,
|
resolvers
|
||||||
});
|
})
|
||||||
|
|
||||||
const startServer = async (): Promise<void> => {
|
const startServer = async (): Promise<void> => {
|
||||||
const { url } = await startStandaloneServer(server, {
|
const { url } = await startStandaloneServer(server, {
|
||||||
listen: { port: 4000 },
|
listen: { port: 4000 }
|
||||||
});
|
})
|
||||||
|
|
||||||
await migrateAll();
|
const mainDB = getDB()
|
||||||
|
const regionDBs = Regions.map(region => getDB(region))
|
||||||
|
|
||||||
console.log(`🚀 Server ready at: ${url}`);
|
const databases = [mainDB, ...regionDBs]
|
||||||
};
|
|
||||||
|
for (const db of databases) {
|
||||||
|
const plannedMigrations: Array<{ file: string }> = (
|
||||||
|
await db.migrate.list()
|
||||||
|
)[1]
|
||||||
|
if (plannedMigrations.length > 0) {
|
||||||
|
console.log(
|
||||||
|
`🕰️ planning migrations: ${plannedMigrations
|
||||||
|
.map((m) => m.file)
|
||||||
|
.join(',')}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.migrate.latest()
|
||||||
|
}
|
||||||
|
|
||||||
|
await initListeners()
|
||||||
|
console.log(`🚀 Server ready at: ${url}`)
|
||||||
|
}
|
||||||
|
|
||||||
startServer()
|
startServer()
|
||||||
.then()
|
.then()
|
||||||
.catch((err: Error) =>
|
.catch((err: Error) =>
|
||||||
console.log(`🔥 failed to start server ${err.message}`),
|
console.log(`🔥 failed to start server ${err.message}`)
|
||||||
);
|
)
|
||||||
|
|||||||
@@ -1,4 +1,19 @@
|
|||||||
import Knex from 'knex'
|
import Knex, { Knex as KnexClient } from 'knex'
|
||||||
import config from '../knexfile'
|
import { mainDBConfig, euDBConfig, usDBConfig } from '../knexfile'
|
||||||
|
import { Region } from './regions'
|
||||||
|
|
||||||
export const knex = Knex(config)
|
const mainDB = Knex(mainDBConfig)
|
||||||
|
const regionDBs = new Map<Region, KnexClient>([['eu', Knex(euDBConfig)], ['us', Knex(usDBConfig)]])
|
||||||
|
|
||||||
|
export function getDB(region?: Region) {
|
||||||
|
if (!region) {
|
||||||
|
return mainDB
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = regionDBs.get(region)
|
||||||
|
|
||||||
|
if (!db) {
|
||||||
|
throw new Error('Region not supported')
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { Knex } from "knex";
|
||||||
|
import { getDB } from "./db";
|
||||||
|
import { Regions } from "./regions";
|
||||||
|
import { upsertResourceView } from "./repositories";
|
||||||
|
|
||||||
|
|
||||||
|
export async function initListeners() {
|
||||||
|
for (const region of Regions) {
|
||||||
|
const db = getDB(region)
|
||||||
|
const connection = await (db.client as Knex.Client).acquireRawConnection()
|
||||||
|
connection.query('LISTEN notifications')
|
||||||
|
|
||||||
|
connection.on('notification', async (data: { payload: string }) => {
|
||||||
|
console.log(JSON.parse(data.payload))
|
||||||
|
await upsertResourceView(region, JSON.parse(data.payload))
|
||||||
|
});
|
||||||
|
|
||||||
|
// connection.on('end', (err) => {
|
||||||
|
// reconnectClient(knex);
|
||||||
|
// })
|
||||||
|
connection.on('error', (err) => {
|
||||||
|
console.log(err);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const tableName = "organizations";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.createTable(tableName, (table) => {
|
|
||||||
table.text("id").primary();
|
|
||||||
table.text("name");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.dropTable(tableName);
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const regionsTableName = "regions";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.createTable(regionsTableName, (table) => {
|
|
||||||
table.text("id").primary();
|
|
||||||
table.text("connectionString");
|
|
||||||
});
|
|
||||||
await knex.schema.createTable("organizations_regions", (table) => {
|
|
||||||
table
|
|
||||||
.text("organizationId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("organizations")
|
|
||||||
.notNullable()
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.text("regionId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("regions")
|
|
||||||
.notNullable()
|
|
||||||
.onDelete("cascade");
|
|
||||||
});
|
|
||||||
await knex.schema.createTable("resource_organization_region", (table) => {
|
|
||||||
table
|
|
||||||
.text("resourceId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("resources")
|
|
||||||
.notNullable()
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.text("organizationId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("organizations")
|
|
||||||
.notNullable()
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.text("regionId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("regions")
|
|
||||||
.notNullable()
|
|
||||||
.onDelete("cascade");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.dropTable(regionsTableName);
|
|
||||||
await knex.schema.dropTable("organizations_regions");
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const regionsTableName = "regions";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.alterTable(regionsTableName, (table) => {
|
|
||||||
table.text("name").notNullable().defaultTo("region");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.alterTable(regionsTableName, (table) => {
|
|
||||||
table.dropColumn("name");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const regionsTableName = "regions";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.alterTable(regionsTableName, (table) => {
|
|
||||||
table.text("maintenanceDb").notNullable().defaultTo("region");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
await knex.schema.alterTable(regionsTableName, (table) => {
|
|
||||||
table.dropColumn("maintenanceDb");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const tableName = "organization_acl";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.createTable(tableName, (table) => {
|
|
||||||
table
|
|
||||||
.string("userId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("users")
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.string("organizationId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("organizations")
|
|
||||||
.onDelete("cascade");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.dropTable(tableName);
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const tableName = "organization_resource_acl";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.createTable(tableName, (table) => {
|
|
||||||
table
|
|
||||||
.string("resourceId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("resources")
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.string("organizationId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("organizations")
|
|
||||||
.onDelete("cascade");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.dropTable(tableName);
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import type { Knex } from "knex";
|
|
||||||
|
|
||||||
const tableName = "resource_region_organization";
|
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.createTable(tableName, (table) => {
|
|
||||||
table
|
|
||||||
.string("resourceId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("resources")
|
|
||||||
.onDelete("cascade")
|
|
||||||
.primary();
|
|
||||||
table
|
|
||||||
.string("regionId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("regions")
|
|
||||||
.onDelete("cascade");
|
|
||||||
table
|
|
||||||
.string("organizationId")
|
|
||||||
.references("id")
|
|
||||||
.inTable("organizations")
|
|
||||||
.onDelete("cascade");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function down(knex: Knex): Promise<void> {
|
|
||||||
return await knex.schema.dropTable(tableName);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import type { Knex } from 'knex'
|
||||||
|
|
||||||
|
const tableName = 'resource_views'
|
||||||
|
|
||||||
|
export async function up(knex: Knex): Promise<void> {
|
||||||
|
return await knex.schema.createTable(tableName, (table) => {
|
||||||
|
table.string('resourceId').primary()
|
||||||
|
table.string('region')
|
||||||
|
table.string('resourceName')
|
||||||
|
table.datetime('resourceCreatedAt')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(knex: Knex): Promise<void> {
|
||||||
|
return await knex.schema.dropTable(tableName)
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export type Region = 'eu' | 'us'
|
||||||
|
export const Regions: Region[] = ['eu', 'us']
|
||||||
+63
-171
@@ -1,67 +1,41 @@
|
|||||||
import { Knex } from "knex";
|
import { getDB } from "./db";
|
||||||
import { knex } from "./db";
|
import { Region } from "./regions";
|
||||||
import {
|
import { UserRecord, Resource, ResourceAcl, Comment, ResourceView } from "./types";
|
||||||
UserRecord,
|
|
||||||
Resource,
|
|
||||||
ResourceAcl,
|
|
||||||
Comment,
|
|
||||||
Region,
|
|
||||||
OrganizationsRegions,
|
|
||||||
Organization,
|
|
||||||
OrganizationAcl,
|
|
||||||
OrganizationResourceAcl,
|
|
||||||
ResourceRegion,
|
|
||||||
ResourceRegionOrg,
|
|
||||||
} from "./types";
|
|
||||||
|
|
||||||
const Users = () => knex<UserRecord>("users");
|
const Users = () => getDB()<UserRecord>("users");
|
||||||
const Resources = () => knex<Resource>("resources");
|
const Resources = (region: Region) => getDB(region)<Resource>("resources");
|
||||||
const ResourceAclRepo = () => knex<ResourceAcl>("resource_acl");
|
const ResourceViews = () => getDB()<ResourceView>("resource_views");
|
||||||
|
const ResourceAclRepo = () => getDB()<ResourceAcl>("resource_acl");
|
||||||
|
const Comments = () => getDB()<Comment>("comments");
|
||||||
|
|
||||||
export const queryUser = async (userId: string): Promise<UserRecord | null> => {
|
export const queryUser = async (userId: string): Promise<UserRecord | null> => {
|
||||||
return (await Users().where("id", "=", userId).first()) ?? null;
|
return (await Users().where("id", "=", userId).first()) ?? null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUsersFrom = (db: Knex) => async (): Promise<UserRecord[]> => {
|
export const queryResource = async (
|
||||||
return await db<UserRecord>("users").select();
|
resourceId: string,
|
||||||
|
): Promise<Resource | null> => {
|
||||||
|
const resourceLocation = await ResourceViews().where('resourceId', '=', resourceId).first()
|
||||||
|
if (!resourceLocation?.region) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return await Resources(resourceLocation.region).where("id", "=", resourceId).first() || null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const saveUserTo =
|
export const queryResourceAcl = async ({
|
||||||
(db: Knex) =>
|
resourceId,
|
||||||
async (user: UserRecord): Promise<void> => {
|
userId,
|
||||||
await db<UserRecord>("users").insert(user);
|
}: {
|
||||||
};
|
resourceId: string;
|
||||||
|
userId: string;
|
||||||
export const saveResourceTo =
|
}): Promise<ResourceAcl | null> => {
|
||||||
(db: Knex) =>
|
return (
|
||||||
async (resource: Resource): Promise<void> => {
|
(await ResourceAclRepo()
|
||||||
await db<Resource>("resources").insert(resource);
|
.where("userId", "=", userId)
|
||||||
};
|
.andWhere("resourceId", "=", resourceId)
|
||||||
|
.first()) ?? null
|
||||||
export const queryResourceFrom =
|
);
|
||||||
(db: Knex) =>
|
};
|
||||||
async (resourceId: string): Promise<Resource | null> => {
|
|
||||||
return (
|
|
||||||
(await db<Resource>("resources").where({ id: resourceId }).first()) ??
|
|
||||||
null
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const queryResourceAclFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async ({ resourceId, userId }: ResourceAcl): Promise<ResourceAcl | null> => {
|
|
||||||
return (
|
|
||||||
(await db<ResourceAcl>("resource_acl")
|
|
||||||
.where({ userId, resourceId })
|
|
||||||
.first()) ?? null
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const saveResourceAclTo =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (resourceAcl: ResourceAcl): Promise<void> => {
|
|
||||||
await db<ResourceAcl>("resource_acl").insert(resourceAcl);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const countResources = async (userId: string): Promise<number> => {
|
export const countResources = async (userId: string): Promise<number> => {
|
||||||
const [rawCount] = await ResourceAclRepo().count().where({ userId });
|
const [rawCount] = await ResourceAclRepo().count().where({ userId });
|
||||||
@@ -77,124 +51,42 @@ export const queryResources = async ({
|
|||||||
limit: number;
|
limit: number;
|
||||||
cursor: string | null;
|
cursor: string | null;
|
||||||
}) => {
|
}) => {
|
||||||
const query = Resources()
|
const query = ResourceViews()
|
||||||
.join("resource_acl", "resources.id", "resource_acl.resourceId")
|
.join("resource_acl", "resource_views.resourceId", "resource_acl.resourceId")
|
||||||
.where({ userId });
|
.where({ userId });
|
||||||
|
if (cursor) {
|
||||||
|
query.andWhere("resourceCreatedAt", "<", cursor);
|
||||||
|
}
|
||||||
|
return query.limit(limit);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const countComments = async (resourceId: string): Promise<number> => {
|
||||||
|
const [rawCount] = await Comments().count().where({ resourceId });
|
||||||
|
return parseInt(rawCount.count as string);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const queryComments = async ({
|
||||||
|
resourceId,
|
||||||
|
limit,
|
||||||
|
cursor,
|
||||||
|
}: {
|
||||||
|
resourceId: string;
|
||||||
|
limit: number;
|
||||||
|
cursor: string | null;
|
||||||
|
}): Promise<Comment[]> => {
|
||||||
|
const query = Comments().where({ resourceId });
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
query.andWhere("createdAt", "<", cursor);
|
query.andWhere("createdAt", "<", cursor);
|
||||||
}
|
}
|
||||||
return await query.limit(limit);
|
return query.limit(limit);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const countCommentsIn =
|
export async function upsertResourceView(region: Region, resource: Resource) {
|
||||||
(db: Knex) =>
|
return ResourceViews().insert({
|
||||||
async (resourceId: string): Promise<number> => {
|
resourceId: resource.id,
|
||||||
const [rawCount] = await db<Comment>("comments")
|
resourceName: resource.name,
|
||||||
.count()
|
resourceCreatedAt: resource.createdAt,
|
||||||
.where({ resourceId });
|
region: region,
|
||||||
return parseInt(rawCount.count as string);
|
}).onConflict('resourceId')
|
||||||
};
|
.merge()
|
||||||
|
}
|
||||||
export const queryCommentsFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async ({
|
|
||||||
resourceId,
|
|
||||||
limit,
|
|
||||||
cursor,
|
|
||||||
}: {
|
|
||||||
resourceId: string;
|
|
||||||
limit: number;
|
|
||||||
cursor: string | null;
|
|
||||||
}): Promise<Comment[]> => {
|
|
||||||
const query = db<Comment>("comments").where({ resourceId });
|
|
||||||
if (cursor) {
|
|
||||||
query.andWhere("createdAt", "<", cursor);
|
|
||||||
}
|
|
||||||
return await query.limit(limit);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const saveCommentTo =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (comment: Comment): Promise<void> => {
|
|
||||||
await db<Comment>("comments").insert(comment);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getRegionsFrom = (db: Knex) => async (): Promise<Array<Region>> =>
|
|
||||||
await db<Region>("regions").select();
|
|
||||||
|
|
||||||
export const getRegionFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (id: string): Promise<Region | null> =>
|
|
||||||
(await db<Region>("regions").where({ id }).first()) ?? null;
|
|
||||||
|
|
||||||
export const getOrganizationRegionsFrom =
|
|
||||||
(db: Knex) => async (): Promise<Array<OrganizationsRegions>> =>
|
|
||||||
await db<OrganizationsRegions>("organizations_regions").select();
|
|
||||||
|
|
||||||
export const queryOrganizationRegionsFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async ({
|
|
||||||
regionId,
|
|
||||||
organizationId,
|
|
||||||
}: OrganizationsRegions): Promise<OrganizationsRegions | null> =>
|
|
||||||
(await db<OrganizationsRegions>("organizations_regions")
|
|
||||||
.where({ regionId, organizationId })
|
|
||||||
.first()) ?? null;
|
|
||||||
|
|
||||||
export const saveRegionTo = (db: Knex) => async (region: Region) =>
|
|
||||||
await db<Region>("regions").insert(region);
|
|
||||||
|
|
||||||
export const saveOrganizationTo =
|
|
||||||
(db: Knex) => async (organization: Organization) =>
|
|
||||||
await db<Organization>("organizations").insert(organization);
|
|
||||||
|
|
||||||
export const getOrganizationFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (id: string): Promise<Organization | null> => {
|
|
||||||
return (
|
|
||||||
(await db<Organization>("organizations").where({ id }).first()) ?? null
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getOrganizationsFrom =
|
|
||||||
(db: Knex) => async (): Promise<Organization[]> =>
|
|
||||||
await db<Organization>("organizations").select();
|
|
||||||
|
|
||||||
export const saveOrganizationsRegionsTo =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (or: OrganizationsRegions): Promise<void> =>
|
|
||||||
await db<OrganizationsRegions>("organizations_regions").insert(or);
|
|
||||||
|
|
||||||
export const saveOrganizationAclTo =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (orgAcl: OrganizationAcl): Promise<void> => {
|
|
||||||
await db<OrganizationsRegions>("organization_acl").insert(orgAcl);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const queryOrganizationAclFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async ({
|
|
||||||
userId,
|
|
||||||
organizationId,
|
|
||||||
}: OrganizationAcl): Promise<OrganizationAcl | null> =>
|
|
||||||
(await db<OrganizationAcl>("organization_acl")
|
|
||||||
.where({ userId, organizationId })
|
|
||||||
.first()) ?? null;
|
|
||||||
|
|
||||||
export const saveOrganizationResourceAclTo =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (item: OrganizationResourceAcl): Promise<void> => {
|
|
||||||
await db<OrganizationResourceAcl>("organization_resource_acl").insert(item);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const saveResourceRegionOrganizationTo =
|
|
||||||
(db: Knex) => async (item: ResourceRegionOrg) => {
|
|
||||||
await db<ResourceRegionOrg>("resource_region_organization").insert(item);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const queryResourceRegionOrganizationFrom =
|
|
||||||
(db: Knex) =>
|
|
||||||
async (resourceId: string): Promise<ResourceRegion | null> =>
|
|
||||||
(await db<ResourceRegionOrg>("resource_region_organization")
|
|
||||||
.where({ resourceId })
|
|
||||||
.first()) ?? null;
|
|
||||||
|
|||||||
+7
-141
@@ -1,63 +1,25 @@
|
|||||||
import {
|
import { queryResourceAcl } from "./repositories";
|
||||||
getOrganizationsFrom,
|
import { getUser, getResource, getComments, getResources } from "./services";
|
||||||
getRegionsFrom,
|
|
||||||
queryOrganizationAclFrom,
|
|
||||||
queryOrganizationRegionsFrom,
|
|
||||||
queryResourceAclFrom,
|
|
||||||
saveOrganizationResourceAclTo,
|
|
||||||
saveOrganizationAclTo,
|
|
||||||
saveResourceAclTo,
|
|
||||||
saveResourceTo,
|
|
||||||
saveResourceRegionOrganizationTo,
|
|
||||||
saveCommentTo,
|
|
||||||
queryResourceFrom,
|
|
||||||
queryUser,
|
|
||||||
countCommentsIn,
|
|
||||||
queryCommentsFrom,
|
|
||||||
getUsersFrom,
|
|
||||||
saveUserTo,
|
|
||||||
} from "./repositories";
|
|
||||||
import { getComments } from "./services/comments";
|
|
||||||
import { createResource, getResources } from "./services/resources";
|
|
||||||
import { GraphQLError } from "graphql";
|
import { GraphQLError } from "graphql";
|
||||||
import {
|
import {
|
||||||
Resource,
|
Resource,
|
||||||
UserRecord,
|
UserRecord,
|
||||||
CommentCollection,
|
CommentCollection,
|
||||||
PaginationArgs,
|
PaginationArgs,
|
||||||
ResourceCreateArgs,
|
|
||||||
OrganizationsRegions,
|
|
||||||
OrganizationAcl,
|
|
||||||
CommentCreateArgs,
|
|
||||||
UserCreateArgs,
|
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import {
|
|
||||||
bindRegionToOrganization,
|
|
||||||
createOrganization,
|
|
||||||
getDbClient,
|
|
||||||
getMainDbClient,
|
|
||||||
getResourceDatabaseConnection,
|
|
||||||
registerRegion,
|
|
||||||
} from "./services/databaseManagement";
|
|
||||||
import { authorizeUserOrgRegion } from "./services/authz";
|
|
||||||
import cryptoRandomString from "crypto-random-string";
|
|
||||||
|
|
||||||
// Resolvers define how to fetch the types defined in your schema.
|
// Resolvers define how to fetch the types defined in your schema.
|
||||||
// This resolver retrieves books from the "books" array above.
|
// This resolver retrieves books from the "books" array above.
|
||||||
export const resolvers = {
|
export const resolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
async users() {
|
|
||||||
return await getUsersFrom(getMainDbClient())();
|
|
||||||
},
|
|
||||||
async user(_: unknown, args: { id: string }) {
|
async user(_: unknown, args: { id: string }) {
|
||||||
return await queryUser(args.id);
|
return await getUser(args.id);
|
||||||
},
|
},
|
||||||
async resource(
|
async resource(
|
||||||
_: unknown,
|
_: unknown,
|
||||||
args: { id: string; userId: string },
|
args: { id: string; userId: string },
|
||||||
): Promise<Resource> {
|
): Promise<Resource> {
|
||||||
const mainDb = getMainDbClient();
|
const maybeAcl = await queryResourceAcl({
|
||||||
const maybeAcl = await queryResourceAclFrom(mainDb)({
|
|
||||||
userId: args.userId,
|
userId: args.userId,
|
||||||
resourceId: args.id,
|
resourceId: args.id,
|
||||||
});
|
});
|
||||||
@@ -71,8 +33,7 @@ export const resolvers = {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const db = await getResourceDatabaseConnection(args.id);
|
const maybeResource = await getResource(args.id);
|
||||||
const maybeResource = await queryResourceFrom(db)(args.id);
|
|
||||||
if (maybeResource == null) {
|
if (maybeResource == null) {
|
||||||
throw new GraphQLError("Resource not found", {
|
throw new GraphQLError("Resource not found", {
|
||||||
extensions: { code: "RESOURCE_NOT_FOUND" },
|
extensions: { code: "RESOURCE_NOT_FOUND" },
|
||||||
@@ -80,117 +41,22 @@ export const resolvers = {
|
|||||||
}
|
}
|
||||||
return maybeResource;
|
return maybeResource;
|
||||||
},
|
},
|
||||||
async organizations() {
|
|
||||||
return await getOrganizationsFrom(getMainDbClient())();
|
|
||||||
},
|
|
||||||
async regions() {
|
|
||||||
return await getRegionsFrom(getMainDbClient())();
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
User: {
|
User: {
|
||||||
async resources(parent: UserRecord, args: PaginationArgs) {
|
async resources(parent: UserRecord, args: PaginationArgs) {
|
||||||
return await getResources({ userId: parent.id, ...args });
|
return await getResources({ userId: parent.id, ...args });
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Resource: {
|
ResourceDetail: {
|
||||||
async comments(
|
async comments(
|
||||||
parent: Resource,
|
parent: Resource,
|
||||||
{ limit, cursor }: PaginationArgs,
|
{ limit, cursor }: PaginationArgs,
|
||||||
): Promise<CommentCollection> {
|
): Promise<CommentCollection> {
|
||||||
const db = await getResourceDatabaseConnection(parent.id);
|
return await getComments({
|
||||||
return await getComments(
|
|
||||||
countCommentsIn(db),
|
|
||||||
queryCommentsFrom(db),
|
|
||||||
)({
|
|
||||||
resourceId: parent.id,
|
resourceId: parent.id,
|
||||||
limit,
|
limit,
|
||||||
cursor,
|
cursor,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Mutation: {
|
|
||||||
async createUser(
|
|
||||||
_: unknown,
|
|
||||||
{ input: { name } }: { input: UserCreateArgs },
|
|
||||||
) {
|
|
||||||
const id = cryptoRandomString({ length: 10 });
|
|
||||||
await saveUserTo(getMainDbClient())({ id, name });
|
|
||||||
return id;
|
|
||||||
},
|
|
||||||
async registerRegion(
|
|
||||||
_: unknown,
|
|
||||||
args: {
|
|
||||||
name: string;
|
|
||||||
connectionString: string;
|
|
||||||
maintenanceDb: string;
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
return await registerRegion(args);
|
|
||||||
},
|
|
||||||
async createOrganization(_: unknown, args: { name: string }) {
|
|
||||||
return await createOrganization(args.name);
|
|
||||||
},
|
|
||||||
async addRegionToOrganization(_: unknown, args: OrganizationsRegions) {
|
|
||||||
await bindRegionToOrganization(args);
|
|
||||||
},
|
|
||||||
async addUserToOrganization(
|
|
||||||
_: unknown,
|
|
||||||
{ input: args }: { input: OrganizationAcl },
|
|
||||||
) {
|
|
||||||
await saveOrganizationAclTo(getMainDbClient())(args);
|
|
||||||
},
|
|
||||||
async createResource(
|
|
||||||
_: unknown,
|
|
||||||
{ input: args }: { input: ResourceCreateArgs },
|
|
||||||
) {
|
|
||||||
const mainDb = getMainDbClient();
|
|
||||||
await authorizeUserOrgRegion(
|
|
||||||
queryOrganizationAclFrom(mainDb),
|
|
||||||
queryOrganizationRegionsFrom(mainDb),
|
|
||||||
)(args);
|
|
||||||
|
|
||||||
const db =
|
|
||||||
args.regionId && args.organizationId
|
|
||||||
? await getDbClient({
|
|
||||||
regionId: args.regionId,
|
|
||||||
organizationId: args.organizationId,
|
|
||||||
})
|
|
||||||
: mainDb;
|
|
||||||
|
|
||||||
const resourceId = await createResource(
|
|
||||||
saveResourceTo(db),
|
|
||||||
saveResourceAclTo(mainDb),
|
|
||||||
)(args);
|
|
||||||
|
|
||||||
if (args.organizationId) {
|
|
||||||
await saveOrganizationResourceAclTo(mainDb)({
|
|
||||||
organizationId: args.organizationId,
|
|
||||||
resourceId,
|
|
||||||
});
|
|
||||||
await saveResourceRegionOrganizationTo(mainDb)({
|
|
||||||
resourceId,
|
|
||||||
organizationId: args.organizationId,
|
|
||||||
// i know its not null here, the authz function ensures it
|
|
||||||
regionId: args.regionId!,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return resourceId;
|
|
||||||
},
|
|
||||||
async addComment(
|
|
||||||
_: unknown,
|
|
||||||
{ input: args }: { input: CommentCreateArgs },
|
|
||||||
) {
|
|
||||||
const mainDb = getMainDbClient();
|
|
||||||
const resourceAcl = await queryResourceAclFrom(mainDb)(args);
|
|
||||||
if (!resourceAcl)
|
|
||||||
throw new Error("The user doesn't have access to the given resource");
|
|
||||||
//2. get resource db client
|
|
||||||
const db = await getResourceDatabaseConnection(args.resourceId);
|
|
||||||
//3. save comment to db
|
|
||||||
const id = cryptoRandomString({ length: 10 });
|
|
||||||
const createdAt = new Date();
|
|
||||||
await saveCommentTo(db)({ id, createdAt, ...args });
|
|
||||||
return id;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|||||||
+7
-52
@@ -15,6 +15,12 @@ type Resource {
|
|||||||
id: String!
|
id: String!
|
||||||
name: String!
|
name: String!
|
||||||
createdAt: DateTime!
|
createdAt: DateTime!
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResourceDetail {
|
||||||
|
id: String!
|
||||||
|
name: String!
|
||||||
|
createdAt: DateTime!
|
||||||
comments(limit: Int! = 10, cursor: String = null): CommentCollection!
|
comments(limit: Int! = 10, cursor: String = null): CommentCollection!
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,59 +36,8 @@ type User {
|
|||||||
resources(limit: Int! = 10, cursor: String = null): ResourceCollection!
|
resources(limit: Int! = 10, cursor: String = null): ResourceCollection!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Organization {
|
|
||||||
id: String!
|
|
||||||
name: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
type Region {
|
|
||||||
id: String!
|
|
||||||
name: String!
|
|
||||||
maintenanceDb: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
user(id: String!): User
|
user(id: String!): User
|
||||||
users: [User!]
|
|
||||||
|
|
||||||
resource(id: String!, userId: String!): Resource
|
resource(id: String!): ResourceDetail
|
||||||
|
|
||||||
organizations: [Organization!]
|
|
||||||
regions: [Region!]
|
|
||||||
}
|
|
||||||
|
|
||||||
input ResourceCreateInput {
|
|
||||||
userId: String!
|
|
||||||
name: String!
|
|
||||||
organizationId: String = null
|
|
||||||
regionId: String = null
|
|
||||||
}
|
|
||||||
|
|
||||||
input OrganizationAcl {
|
|
||||||
userId: String!
|
|
||||||
organizationId: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
input CommentInput {
|
|
||||||
userId: String!
|
|
||||||
content: String!
|
|
||||||
resourceId: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
input UserCreateArgs {
|
|
||||||
name: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
type Mutation {
|
|
||||||
createUser(input: UserCreateArgs!): String!
|
|
||||||
registerRegion(
|
|
||||||
name: String!
|
|
||||||
connectionString: String!
|
|
||||||
maintenanceDb: String!
|
|
||||||
): String!
|
|
||||||
createOrganization(name: String!): String!
|
|
||||||
addRegionToOrganization(organizationId: String!, regionId: String!): Boolean
|
|
||||||
addUserToOrganization(input: OrganizationAcl!): Boolean
|
|
||||||
createResource(input: ResourceCreateInput!): String!
|
|
||||||
addComment(input: CommentInput!): String!
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import {
|
||||||
|
queryUser,
|
||||||
|
queryResource,
|
||||||
|
countComments,
|
||||||
|
queryComments,
|
||||||
|
countResources,
|
||||||
|
queryResources,
|
||||||
|
} from "./repositories";
|
||||||
|
import {
|
||||||
|
UserRecord,
|
||||||
|
Resource,
|
||||||
|
CommentCollection,
|
||||||
|
PaginationArgs,
|
||||||
|
ResourceCollection,
|
||||||
|
} from "./types";
|
||||||
|
|
||||||
|
export const getUser = async (id: string): Promise<UserRecord | null> => {
|
||||||
|
return queryUser(id);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getResource = async (id: string): Promise<Resource | null> => {
|
||||||
|
return queryResource(id);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface GetResourcesArgs extends PaginationArgs {
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
export const getResources = async (
|
||||||
|
params: GetResourcesArgs,
|
||||||
|
): Promise<ResourceCollection> => {
|
||||||
|
const totalCount = await countResources(params.userId);
|
||||||
|
const items = await queryResources(params);
|
||||||
|
let cursor = null;
|
||||||
|
if (items.length > 0) {
|
||||||
|
cursor = items.slice(-1)[0].createdAt.toISOString();
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
totalCount,
|
||||||
|
items,
|
||||||
|
cursor,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getComments = async (params: {
|
||||||
|
resourceId: string;
|
||||||
|
limit: number;
|
||||||
|
cursor: string | null;
|
||||||
|
}): Promise<CommentCollection> => {
|
||||||
|
// yes, i should be doing base64 de and encoding with the cursor...
|
||||||
|
const totalCount = await countComments(params.resourceId);
|
||||||
|
const items = await queryComments(params);
|
||||||
|
let cursor = null;
|
||||||
|
if (items.length > 0) {
|
||||||
|
cursor = items.slice(-1)[0].createdAt.toISOString();
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
totalCount,
|
||||||
|
items,
|
||||||
|
cursor,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import {
|
|
||||||
OrganizationAcl,
|
|
||||||
OrganizationsRegions,
|
|
||||||
UserOrgRegionArgs,
|
|
||||||
} from "../types";
|
|
||||||
|
|
||||||
export const authorizeUserOrgRegion =
|
|
||||||
(
|
|
||||||
orgAclGetter: (params: OrganizationAcl) => Promise<OrganizationAcl | null>,
|
|
||||||
orgRegionGetter: (
|
|
||||||
params: OrganizationsRegions,
|
|
||||||
) => Promise<OrganizationsRegions | null>,
|
|
||||||
) =>
|
|
||||||
async ({ userId, regionId, organizationId }: UserOrgRegionArgs) => {
|
|
||||||
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)
|
|
||||||
throw new Error("user doesn't have access to this organization");
|
|
||||||
const orgRegion = await orgRegionGetter({ organizationId, regionId });
|
|
||||||
if (!orgRegion)
|
|
||||||
throw new Error("organization doesnt have access to this region");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import { CommentCollection, PaginationArgs, Comment } from "../types";
|
|
||||||
|
|
||||||
interface GetCommentsArgs extends PaginationArgs {
|
|
||||||
resourceId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getComments =
|
|
||||||
(
|
|
||||||
countComments: (resourceId: string) => Promise<number>,
|
|
||||||
queryComments: (params: GetCommentsArgs) => Promise<Comment[]>,
|
|
||||||
) =>
|
|
||||||
async (params: GetCommentsArgs): Promise<CommentCollection> => {
|
|
||||||
// yes, i should be doing base64 de and encoding with the cursor...
|
|
||||||
const totalCount = await countComments(params.resourceId);
|
|
||||||
const items = await queryComments(params);
|
|
||||||
let cursor = null;
|
|
||||||
if (items.length > 0) {
|
|
||||||
cursor = items.slice(-1)[0].createdAt.toISOString();
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
totalCount,
|
|
||||||
items,
|
|
||||||
cursor,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -1,243 +0,0 @@
|
|||||||
import { POSTGRES_URL } from "../config";
|
|
||||||
import {
|
|
||||||
getOrganizationFrom,
|
|
||||||
getOrganizationRegionsFrom,
|
|
||||||
getRegionFrom,
|
|
||||||
queryResourceRegionOrganizationFrom,
|
|
||||||
saveOrganizationTo,
|
|
||||||
saveOrganizationsRegionsTo,
|
|
||||||
saveRegionTo,
|
|
||||||
} from "../repositories";
|
|
||||||
import { OrganizationsRegions, Region } from "../types";
|
|
||||||
import knex, { Knex } from "knex";
|
|
||||||
import cryptoRandomString from "crypto-random-string";
|
|
||||||
|
|
||||||
const migrateToLatest = async (client: Knex): Promise<void> => {
|
|
||||||
const plannedMigrations: Array<{ file: string }> = (
|
|
||||||
await client.migrate.list()
|
|
||||||
)[1];
|
|
||||||
if (plannedMigrations.length > 0) {
|
|
||||||
console.log(
|
|
||||||
`🕰️ planning migrations: ${plannedMigrations
|
|
||||||
.map((m) => m.file)
|
|
||||||
.join(",")}`,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
console.log("no migrations are planned");
|
|
||||||
}
|
|
||||||
// TODO: make sure if a migration fails, all migrations are rolled back
|
|
||||||
await client.migrate.latest();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const migrateAll = async (): Promise<void> => {
|
|
||||||
await migrateToLatest(mainClient);
|
|
||||||
const databaseSchemas = await getAllDatabaseSchemaConnections();
|
|
||||||
|
|
||||||
await Promise.all([
|
|
||||||
...databaseSchemas.map(async (sc) => await migrateToLatest(sc)),
|
|
||||||
]);
|
|
||||||
// 1. get all regions from main DB
|
|
||||||
// 2. construct region specific knex clients and cache them by
|
|
||||||
// 3. structure the cache so that it accomodates client creation by resource id
|
|
||||||
// 4. get all organization regions from main DB
|
|
||||||
// 5. for in all regions for all organizations, run the migration
|
|
||||||
// 6. do not forget the migration for the main DB
|
|
||||||
//
|
|
||||||
};
|
|
||||||
|
|
||||||
const createDatabaseConfig = (connectionString: string): Knex.Config => {
|
|
||||||
return {
|
|
||||||
client: "pg",
|
|
||||||
connection: {
|
|
||||||
connectionString,
|
|
||||||
},
|
|
||||||
// connection: connectionString,
|
|
||||||
migrations: {
|
|
||||||
directory: "src/migrations",
|
|
||||||
extension: "ts",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const mainClient = knex(createDatabaseConfig(POSTGRES_URL));
|
|
||||||
|
|
||||||
const _connectionStore: Map<string, Knex> = new Map();
|
|
||||||
|
|
||||||
interface RegionWithMaybeOrganization {
|
|
||||||
regionId: string;
|
|
||||||
organizationId?: string | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const _createConnectionKey = ({
|
|
||||||
organizationId,
|
|
||||||
regionId,
|
|
||||||
}: RegionWithMaybeOrganization): string => {
|
|
||||||
return organizationId ? `${organizationId}@${regionId}` : regionId;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getDbClient = async ({
|
|
||||||
regionId,
|
|
||||||
organizationId,
|
|
||||||
}: RegionWithMaybeOrganization): Promise<Knex> => {
|
|
||||||
const connectionKey = _createConnectionKey({ organizationId, regionId });
|
|
||||||
const maybeClient = _connectionStore.get(connectionKey);
|
|
||||||
if (maybeClient) return maybeClient;
|
|
||||||
const maybeRegion = await mainClient<Region>("regions")
|
|
||||||
.select()
|
|
||||||
.where({ id: regionId })
|
|
||||||
.first();
|
|
||||||
if (!maybeRegion) throw Error(`region ${regionId} not found`);
|
|
||||||
const connectionString = organizationId
|
|
||||||
? `${maybeRegion.connectionString}/${organizationId}`
|
|
||||||
: `${maybeRegion.connectionString}/${maybeRegion.maintenanceDb}`;
|
|
||||||
const client = knex(createDatabaseConfig(connectionString));
|
|
||||||
_connectionStore.set(connectionKey, client);
|
|
||||||
return client;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getMainDbClient = (): Knex => mainClient;
|
|
||||||
|
|
||||||
export const registerRegion = async ({
|
|
||||||
name,
|
|
||||||
connectionString,
|
|
||||||
maintenanceDb,
|
|
||||||
}: {
|
|
||||||
name: string;
|
|
||||||
connectionString: string;
|
|
||||||
maintenanceDb: string;
|
|
||||||
}): Promise<string> => {
|
|
||||||
// TODO: validate the connectionString, so that the knex client can connect to it
|
|
||||||
const id = cryptoRandomString({ length: 10 });
|
|
||||||
await saveRegionTo(mainClient)({
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
connectionString,
|
|
||||||
maintenanceDb,
|
|
||||||
});
|
|
||||||
return id;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createOrganization = async (name: string): Promise<string> => {
|
|
||||||
const id = cryptoRandomString({ length: 10 });
|
|
||||||
await saveOrganizationTo(mainClient)({ id, name });
|
|
||||||
return id;
|
|
||||||
};
|
|
||||||
|
|
||||||
const createDb = async (client: Knex, name: string): Promise<void> => {
|
|
||||||
try {
|
|
||||||
await client.raw(`create database "${name}"`);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof Error)) throw err;
|
|
||||||
if (!err.message.includes("already exists")) throw err;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const setUpUserReplication = async ({
|
|
||||||
from,
|
|
||||||
to,
|
|
||||||
}: {
|
|
||||||
from: Knex;
|
|
||||||
to: Knex;
|
|
||||||
}): Promise<void> => {
|
|
||||||
// TODO: ensure its created...
|
|
||||||
const connectionString: string =
|
|
||||||
from.client.config.connection.connectionString;
|
|
||||||
try {
|
|
||||||
await from.raw("CREATE PUBLICATION userspub FOR TABLE users;");
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof Error)) throw err;
|
|
||||||
if (!err.message.includes("already exists")) throw err;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const toUrl = new URL(to.client.config.connection.connectionString);
|
|
||||||
await to.raw(
|
|
||||||
`CREATE SUBSCRIPTION userssub_${toUrl.pathname.replace("/", "")} CONNECTION '${connectionString}' PUBLICATION userspub;`,
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof Error)) throw err;
|
|
||||||
if (!err.message.includes("already exists")) throw err;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const setUpResourceReplication = async ({
|
|
||||||
from,
|
|
||||||
fromRegionName,
|
|
||||||
to,
|
|
||||||
}: {
|
|
||||||
from: Knex;
|
|
||||||
fromRegionName: string;
|
|
||||||
to: Knex;
|
|
||||||
}): Promise<void> => {
|
|
||||||
// TODO: ensure its created...
|
|
||||||
const connectionString: string =
|
|
||||||
from.client.config.connection.connectionString;
|
|
||||||
const connUrl = new URL(connectionString);
|
|
||||||
try {
|
|
||||||
await from.raw("CREATE PUBLICATION resourcepub FOR TABLE resources;");
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof Error)) throw err;
|
|
||||||
if (!err.message.includes("already exists")) throw err;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await to.raw(
|
|
||||||
`CREATE SUBSCRIPTION "resroucesub_${fromRegionName.replace(
|
|
||||||
" ",
|
|
||||||
"",
|
|
||||||
)}_${connUrl.pathname.replace(
|
|
||||||
"/",
|
|
||||||
"",
|
|
||||||
)}" CONNECTION '${connectionString}' PUBLICATION resourcepub;`,
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
if (!(err instanceof Error)) throw err;
|
|
||||||
if (!err.message.includes("already exists")) throw err;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const bindRegionToOrganization = async ({
|
|
||||||
regionId,
|
|
||||||
organizationId,
|
|
||||||
}: OrganizationsRegions): Promise<void> => {
|
|
||||||
const region = await getRegionFrom(mainClient)(regionId);
|
|
||||||
if (!region) throw Error(`region ${regionId} not found`);
|
|
||||||
const organization = await getOrganizationFrom(mainClient)(organizationId);
|
|
||||||
if (!organization) throw Error(`organization ${organizationId} not found`);
|
|
||||||
|
|
||||||
const regionClient = await getDbClient({ regionId });
|
|
||||||
|
|
||||||
await createDb(regionClient, organizationId);
|
|
||||||
|
|
||||||
const client = await getDbClient({ organizationId, regionId });
|
|
||||||
const connectionKey = _createConnectionKey({ organizationId, regionId });
|
|
||||||
|
|
||||||
await migrateToLatest(client);
|
|
||||||
|
|
||||||
await setUpUserReplication({ from: mainClient, to: client });
|
|
||||||
await setUpResourceReplication({
|
|
||||||
from: client,
|
|
||||||
fromRegionName: region.name,
|
|
||||||
to: mainClient,
|
|
||||||
});
|
|
||||||
|
|
||||||
_connectionStore.set(connectionKey, client);
|
|
||||||
await saveOrganizationsRegionsTo(mainClient)({ organizationId, regionId });
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getAllDatabaseSchemaConnections = async (): Promise<Knex[]> => {
|
|
||||||
const organizationRegions = await getOrganizationRegionsFrom(mainClient)();
|
|
||||||
const clients = await Promise.all(
|
|
||||||
organizationRegions.map(async (or) => {
|
|
||||||
const client = await getDbClient(or);
|
|
||||||
return client;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return [mainClient, ...clients];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getResourceDatabaseConnection = async (
|
|
||||||
resourceId: string,
|
|
||||||
): Promise<Knex> => {
|
|
||||||
const resourceRegionOrg =
|
|
||||||
await queryResourceRegionOrganizationFrom(mainClient)(resourceId);
|
|
||||||
return resourceRegionOrg ? await getDbClient(resourceRegionOrg) : mainClient;
|
|
||||||
};
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import cryptoRandomString from "crypto-random-string";
|
|
||||||
import { countResources, queryResources } from "../repositories";
|
|
||||||
import {
|
|
||||||
Resource,
|
|
||||||
PaginationArgs,
|
|
||||||
ResourceCollection,
|
|
||||||
ResourceCreateArgs,
|
|
||||||
ResourceAcl,
|
|
||||||
} from "../types";
|
|
||||||
|
|
||||||
interface GetResourcesArgs extends PaginationArgs {
|
|
||||||
userId: string;
|
|
||||||
}
|
|
||||||
export const getResources = async (
|
|
||||||
params: GetResourcesArgs,
|
|
||||||
): Promise<ResourceCollection> => {
|
|
||||||
const totalCount = await countResources(params.userId);
|
|
||||||
const items = await queryResources(params);
|
|
||||||
let cursor = null;
|
|
||||||
if (items.length > 0) {
|
|
||||||
cursor = items.slice(-1)[0].createdAt.toISOString();
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
totalCount,
|
|
||||||
items,
|
|
||||||
cursor,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
//3. if org and region, validate if org has access to region
|
|
||||||
//4. create resource
|
|
||||||
const id = cryptoRandomString({ length: 10 });
|
|
||||||
const resource = { id, name, createdAt: new Date() };
|
|
||||||
await resourceSaver(resource);
|
|
||||||
await resourceAclSaver({ resourceId: id, userId });
|
|
||||||
return id;
|
|
||||||
};
|
|
||||||
+13
-56
@@ -1,12 +1,11 @@
|
|||||||
export interface CommentCreateArgs {
|
import { Region } from "./regions";
|
||||||
|
|
||||||
|
export interface Comment {
|
||||||
|
id: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
content: string;
|
content: string;
|
||||||
resourceId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Comment extends CommentCreateArgs {
|
|
||||||
id: string;
|
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
resourceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PaginationArgs {
|
export interface PaginationArgs {
|
||||||
@@ -20,17 +19,7 @@ interface Collection<T> {
|
|||||||
items: T[];
|
items: T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CommentCollection extends Collection<Comment> {}
|
export interface CommentCollection extends Collection<Comment> { }
|
||||||
|
|
||||||
export interface UserOrgRegionArgs {
|
|
||||||
userId: string;
|
|
||||||
organizationId: string | null;
|
|
||||||
regionId: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ResourceCreateArgs extends UserOrgRegionArgs {
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Resource {
|
export interface Resource {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -38,14 +27,11 @@ export interface Resource {
|
|||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ResourceCollection extends Collection<Resource> {}
|
export interface ResourceCollection extends Collection<Resource> { }
|
||||||
|
|
||||||
export interface UserCreateArgs {
|
export interface UserRecord {
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UserRecord extends UserCreateArgs {
|
|
||||||
id: string;
|
id: string;
|
||||||
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface User extends UserRecord {
|
export interface User extends UserRecord {
|
||||||
@@ -61,38 +47,9 @@ export interface ResourceAcl {
|
|||||||
resourceId: string;
|
resourceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Region {
|
export interface ResourceView {
|
||||||
id: string;
|
region: Region;
|
||||||
name: string;
|
|
||||||
connectionString: string;
|
|
||||||
maintenanceDb: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Organization {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OrganizationAcl {
|
|
||||||
userId: string;
|
|
||||||
organizationId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OrganizationsRegions {
|
|
||||||
organizationId: string;
|
|
||||||
regionId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OrganizationResourceAcl {
|
|
||||||
organizationId: string;
|
|
||||||
resourceId: string;
|
resourceId: string;
|
||||||
}
|
resourceName: string;
|
||||||
|
resourceCreatedAt: Date;
|
||||||
export interface ResourceRegion {
|
|
||||||
resourceId: string;
|
|
||||||
regionId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ResourceRegionOrg extends ResourceRegion {
|
|
||||||
organizationId: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
+4
-107
@@ -1,109 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"extends": "@ducktors/tsconfig",
|
||||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
"include": [
|
||||||
|
"**/*.ts"
|
||||||
/* Projects */
|
]
|
||||||
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
||||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
||||||
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
||||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
||||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
||||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
||||||
|
|
||||||
/* Language and Environment */
|
|
||||||
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
||||||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
||||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
||||||
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
||||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
||||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
||||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
||||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
||||||
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
||||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
||||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
||||||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
||||||
|
|
||||||
/* Modules */
|
|
||||||
"module": "commonjs" /* Specify what module code is generated. */,
|
|
||||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
||||||
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
||||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
||||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
||||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
||||||
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
||||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
||||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
||||||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
||||||
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
||||||
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
||||||
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
||||||
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
||||||
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
||||||
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
||||||
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
||||||
|
|
||||||
/* JavaScript Support */
|
|
||||||
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
||||||
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
||||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
||||||
|
|
||||||
/* Emit */
|
|
||||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
||||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
||||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
||||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
||||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
||||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
||||||
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
||||||
// "removeComments": true, /* Disable emitting comments. */
|
|
||||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
||||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
||||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
||||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
||||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
||||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
||||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
||||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
||||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
||||||
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
||||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
||||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
||||||
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
||||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
||||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
||||||
|
|
||||||
/* Interop Constraints */
|
|
||||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
||||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
||||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
||||||
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
||||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
||||||
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
||||||
|
|
||||||
/* Type Checking */
|
|
||||||
"strict": true /* Enable all strict type-checking options. */,
|
|
||||||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
||||||
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
||||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
||||||
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
||||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
||||||
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
||||||
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
||||||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
||||||
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
||||||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
||||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
||||||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
||||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
||||||
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
||||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
||||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
||||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
||||||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
||||||
|
|
||||||
/* Completeness */
|
|
||||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
||||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import { defineConfig } from "vitest/config";
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
test: {
|
|
||||||
dir: "tests",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user