Skip to content

Commit

Permalink
Merge pull request #12 from digicatapult/feature/l3-43
Browse files Browse the repository at this point in the history
Feature/l3 43
  • Loading branch information
n3op2 authored Mar 13, 2023
2 parents d5502ff + 0363d94 commit 167b5e5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
32 changes: 32 additions & 0 deletions src/controllers/attachments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Controller, Get, Route } from 'tsoa'
import { Logger } from 'pino'

import { logger } from '../../lib/logger'
import Database from '../../lib/db'
import type { Attachments } from '../../models'

@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; attachments: Attachments[] }> {
this.log.debug({
msg: 'this is a test route to validate tsoa/swagger',
})

return {
status: 200,
attachments: await this.db.attachments(),
}
}
}
23 changes: 10 additions & 13 deletions src/lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@ 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 = (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
}
}
}
6 changes: 6 additions & 0 deletions src/models/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Attachments {
id: string
filename: string
binary_blob: string
datetime: Date
}
1 change: 1 addition & 0 deletions src/models/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type * from './health'
export type * from './attachments'
3 changes: 1 addition & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Express> => {
const app: Express = express()
new Database().init() /* not assigned due to linting */

app.use(json())
app.use(cors())
Expand Down

0 comments on commit 167b5e5

Please sign in to comment.