From d10371c9d4ff1af03df0d461223950818550e938 Mon Sep 17 00:00:00 2001 From: Paulius Date: Mon, 13 Mar 2023 14:41:42 +0000 Subject: [PATCH 1/5] L3-43: new db and tsoa models. --- .eslintrc | 1 + src/controllers/attachments/index.ts | 30 ++++++++++++++++++++++++++++ src/lib/db/index.ts | 24 ++++++++++------------ src/models/attachments.ts | 6 ++++++ src/models/index.d.ts | 1 + src/server.ts | 3 +-- 6 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 src/controllers/attachments/index.ts create mode 100644 src/models/attachments.ts diff --git a/.eslintrc b/.eslintrc index 31e44d5f..9c3f62a0 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,6 +13,7 @@ }, "rules": { "prettier/prettier": "error", + "@typescript-eslint/no-explicit-any": "off", "no-console": 2 }, "overrides": [ diff --git a/src/controllers/attachments/index.ts b/src/controllers/attachments/index.ts new file mode 100644 index 00000000..9d1e66da --- /dev/null +++ b/src/controllers/attachments/index.ts @@ -0,0 +1,30 @@ +import { Controller, Get, Route } from 'tsoa' +import { Logger } from 'pino' + +import { logger } from '../../lib/logger' +import Database from '../../lib/db' + +@Route('attachments') +export class attachments extends Controller { + log: Logger + // TMP update once we have more defined schema + dbClient: any = new Database() + db: any + + constructor() { + super() + this.log = logger.child({ controller: '/attachments' }) + this.db = this.dbClient.db() + } + + @Get('/') + public async get(): Promise<{ status: number }> { + this.log.debug({ + msg: 'this is a test route to validate tsoa/swagger', + }) + + return Promise.resolve({ + status: 200, + }) + } +} diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 04658f76..8a7e3de1 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -15,22 +15,20 @@ const MODELS_DIRECTORY = path.join(__dirname, '../../models') export default class Database { public client: Knex private log: Logger - public db: { [k: string]: unknown } + public db: any constructor() { this.log = logger this.client = knex(pgConfig) - this.db = {} - } - - init(): void { - this.log.debug('initializing db models') - - fs.readdirSync(MODELS_DIRECTORY).forEach((file: string) => { - const { name } = path.parse(file) - - // TODO check if table exists -> append to the db object - if (name != 'index.d') this.db[name] = () => this.client(name) - }) + this.db = () => { + const models: any = {} + fs.readdirSync(MODELS_DIRECTORY).forEach((file: string): any => { + this.log.debug('initializing db models') + const { name } = path.parse(file) + // TODO check if table exists -> append to the db object + if (name != 'index.d') models[name] = () => this.client(name) + }) + return models + } } } diff --git a/src/models/attachments.ts b/src/models/attachments.ts new file mode 100644 index 00000000..2b47678d --- /dev/null +++ b/src/models/attachments.ts @@ -0,0 +1,6 @@ +export interface Attachments { + id: string + filename: string + binary_blob: string + datetime: Date +} diff --git a/src/models/index.d.ts b/src/models/index.d.ts index d1e998bf..46625ee7 100644 --- a/src/models/index.d.ts +++ b/src/models/index.d.ts @@ -1 +1,2 @@ export type * from './health' +export type * from './attachments' diff --git a/src/server.ts b/src/server.ts index df335f56..843642cb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7,11 +7,10 @@ import { errorHandler } from './lib/error-handler' import { RegisterRoutes } from './routes' import * as swaggerJson from './swagger.json' -import Database from './lib/db' +// TODO review this any export default async (): Promise => { const app: Express = express() - new Database().init() /* not assigned due to linting */ app.use(json()) app.use(cors()) From 5473529f7520e43be4f2b14a7a4b09270e99e857 Mon Sep 17 00:00:00 2001 From: Paulius Date: Mon, 13 Mar 2023 14:42:07 +0000 Subject: [PATCH 2/5] L3-43: version bump. --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0676fa46..0b562e98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@digicatapult/dscp-matchmaker-api", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@digicatapult/dscp-matchmaker-api", - "version": "0.0.2", + "version": "0.0.3", "license": "Apache-2.0", "dependencies": { "body-parser": "^1.20.0", diff --git a/package.json b/package.json index cb901749..00647e5a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@digicatapult/dscp-matchmaker-api", - "version": "0.0.2", + "version": "0.0.3", "description": "An open api typescript nodejs/express api service template", "main": "src/index.ts", "scripts": { From 692ff6ca80d184e5f6ab3ea528c7ae62ead1895c Mon Sep 17 00:00:00 2001 From: Paulius Date: Mon, 13 Mar 2023 14:54:33 +0000 Subject: [PATCH 3/5] L3-29: return all attachments, same doing anyway. --- src/controllers/attachments/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/controllers/attachments/index.ts b/src/controllers/attachments/index.ts index 9d1e66da..81e58bae 100644 --- a/src/controllers/attachments/index.ts +++ b/src/controllers/attachments/index.ts @@ -3,6 +3,7 @@ import { Logger } from 'pino' import { logger } from '../../lib/logger' import Database from '../../lib/db' +import { Attachments } from 'src/models' @Route('attachments') export class attachments extends Controller { @@ -18,13 +19,14 @@ export class attachments extends Controller { } @Get('/') - public async get(): Promise<{ status: number }> { + public async get(): Promise<{ status: number; attachments: Attachments }> { this.log.debug({ msg: 'this is a test route to validate tsoa/swagger', }) - return Promise.resolve({ + return { status: 200, - }) + attachments: await this.db.attachments(), + } } } From 4cb221fe1055bed1ccac09a60f6c7c945a757b06 Mon Sep 17 00:00:00 2001 From: Paulius Date: Mon, 13 Mar 2023 14:56:42 +0000 Subject: [PATCH 4/5] L3-43: return all attachments. --- src/controllers/attachments/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/attachments/index.ts b/src/controllers/attachments/index.ts index 81e58bae..4fbf160c 100644 --- a/src/controllers/attachments/index.ts +++ b/src/controllers/attachments/index.ts @@ -3,7 +3,7 @@ import { Logger } from 'pino' import { logger } from '../../lib/logger' import Database from '../../lib/db' -import { Attachments } from 'src/models' +import type { Attachments } from '../../models' @Route('attachments') export class attachments extends Controller { @@ -19,7 +19,7 @@ export class attachments extends Controller { } @Get('/') - public async get(): Promise<{ status: number; attachments: Attachments }> { + public async get(): Promise<{ status: number; attachments: Attachments[] }> { this.log.debug({ msg: 'this is a test route to validate tsoa/swagger', }) From 0363d9405d77ae5f78cf710f9c3dde5b65511bca Mon Sep 17 00:00:00 2001 From: Paulius Date: Mon, 13 Mar 2023 15:00:51 +0000 Subject: [PATCH 5/5] L3-43: a little nicer way to initiate database. --- .eslintrc | 1 - src/lib/db/index.ts | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index 9c3f62a0..31e44d5f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,7 +13,6 @@ }, "rules": { "prettier/prettier": "error", - "@typescript-eslint/no-explicit-any": "off", "no-console": 2 }, "overrides": [ diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 8a7e3de1..68cdbd55 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -20,8 +20,7 @@ export default class Database { constructor() { this.log = logger this.client = knex(pgConfig) - this.db = () => { - const models: any = {} + this.db = (models: any = {}) => { fs.readdirSync(MODELS_DIRECTORY).forEach((file: string): any => { this.log.debug('initializing db models') const { name } = path.parse(file)