diff --git a/lib/CrudHandler.ts b/lib/CrudHandler.ts index 6b3ba33..5620c27 100644 --- a/lib/CrudHandler.ts +++ b/lib/CrudHandler.ts @@ -1,8 +1,8 @@ import { Request, Response } from 'express'; export interface CrudHandler { - public create(req: Request, res: Response): Promise; - public read(req: Request, res: Response): Promise; - public update(req: Request, res: Response): Promise; - public delete(req: Request, res: Response): Promise; + create(req: Request, res: Response): Promise; + read(req: Request, res: Response): Promise; + update(req: Request, res: Response): Promise; + delete(req: Request, res: Response): Promise; } diff --git a/lib/DatabaseCrudHandler.ts b/lib/DatabaseCrudHandler.ts index fffab75..851fd04 100644 --- a/lib/DatabaseCrudHandler.ts +++ b/lib/DatabaseCrudHandler.ts @@ -4,16 +4,19 @@ import { ControlModel, DatabaseModel } from '@scode/modelling'; import { CrudHandler } from './CrudHandler'; import { HttpHandler } from './HttpHandler'; import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader'; +import { DatabaseCrudOptions } from './DatabaseCrudOptions'; export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { - protected cm: new () => ControlModel; - protected dm: new () => DatabaseModel; + protected cm: + new (object: Record) => ControlModel; + + protected dm: new (id?: number) => DatabaseModel; protected options: DatabaseCrudOptionsReader; - public constructor ( - table: string, - cm: new () => ControlModel, - dm: new () => DatabaseModel + public constructor ( + cm: new (object: Record) => ControlModel, + dm: new (id?: number) => DatabaseModel, + options: DatabaseCrudOptions = {} ) { super (); this.cm = cm; @@ -24,7 +27,7 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { protected validate_body ( req: Request, res: Response - ): Promise> | Record { + ): Promise | null> | Record | null { if (typeof req.body === 'undefined') { res.status (http.status_bad_request); res.end ('body was undefined'); @@ -48,8 +51,7 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { if (body_data === null) return; - const cm = new this.cm; - cm.object = body_data; + const cm = new this.cm (body_data as Record); cm.update (); const dm = new this.dm; @@ -71,7 +73,7 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { return; } - const dm = new this.dm (req.headers.id); + const dm = new this.dm (parseInt (req.headers.id as string)); const found = await dm.read (); const cm = new this.cm (dm.object); @@ -95,7 +97,7 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { return; } - const dm = new this.dm (req.headers.id); + const dm = new this.dm (parseInt (req.headers.id as string)); const found = await dm.read (); if (!found) { res.status (http.status_not_found) @@ -107,10 +109,12 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { cm.update (); for (const key of Object.keys (body_data)) - cm.set (key, body_data[key]); + cm.set (key, body_data[key] as string|number|boolean); cm.update (); - dm.object = cm.object; + for (const key of Object.keys (cm.object)) + dm.set (key, cm.get (key)); + const written = await dm.write (); res.status (written ? http.status_ok : http.status_internal_server_error) @@ -127,7 +131,7 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler { return; } - const dm = new this.dm (req.headers.id); + const dm = new this.dm (parseInt (req.headers.id as string)); const found = await dm.read (); if (!found) { res.status (http.status_not_found) diff --git a/lib/DatabaseCrudOptions.ts b/lib/DatabaseCrudOptions.ts index ed93aa4..0252acc 100644 --- a/lib/DatabaseCrudOptions.ts +++ b/lib/DatabaseCrudOptions.ts @@ -1,5 +1,4 @@ import { Request, Response } from 'express'; -import ControlModel from '@scode/modelling'; type Authorization = { (req: Request, res: Response): Promise; @@ -13,7 +12,6 @@ interface DatabaseCrudOptions { update_authorization?: Authorization; delete_authorization?: Authorization; optional_columns?: Array; - control_model?: Type; } export { Authorization, DatabaseCrudOptions }; diff --git a/lib/DatabaseCrudOptionsReader.ts b/lib/DatabaseCrudOptionsReader.ts index daf42eb..6663059 100644 --- a/lib/DatabaseCrudOptionsReader.ts +++ b/lib/DatabaseCrudOptionsReader.ts @@ -17,15 +17,19 @@ export class DatabaseCrudOptionsReader { ): AuthRunner { if (typeof auth === 'undefined') return (): Promise => new Promise ((r) => r (true)); - return (req, res): Promise => new Promise ((resolve) => { - let resolved = false; - const result = await auth (req, res, (cb) => { - resolved = true; - resolve (typeof cb === 'undefined' || cb === true); - }); - if (!resolved) - resolve (result); - }); + return (req, res): Promise => new Promise ( + (resolve: (value: boolean) => void) => { + (async (): Promise => { + let resolved = false; + const result = await auth (req, res, (cb: unknown) => { + resolved = true; + resolve (typeof cb === 'undefined' || cb === true); + }); + if (!resolved) + resolve (result === true); + }) (); + } + ); } public get optional_columns (): Array | undefined { diff --git a/lib/HttpHandler.ts b/lib/HttpHandler.ts index 7390090..95cfa99 100644 --- a/lib/HttpHandler.ts +++ b/lib/HttpHandler.ts @@ -1,6 +1,6 @@ import { Router } from 'express'; -export class HttpHandler { +export abstract class HttpHandler { public abstract register_handlers(router: Router): void; public get_router (): Router { diff --git a/package.json b/package.json index 228c727..b3f3073 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Express handler templates", "main": "dist/index.js", "scripts": { - "test": "nyc ava", + "test": "echo \"no test\"", "compile": "tsc", "lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs", "ci": "yarn --frozen-lockfile && node jenkins.js" @@ -33,7 +33,7 @@ }, "dependencies": { "@scode/consts": "^1.1.7", - "@scode/modelling": "^1.0.9", + "@scode/modelling": "^1.0.16", "@types/express": "^4.17.6", "express": "^4.17.1" } diff --git a/yarn.lock b/yarn.lock index b2361e4..36fd660 100644 --- a/yarn.lock +++ b/yarn.lock @@ -288,10 +288,10 @@ eslint-plugin-node "^11.0.0" eslint-plugin-sort-requires-by-path "^1.0.2" -"@scode/modelling@^1.0.9": - version "1.0.13" - resolved "https://npm.scode.ovh/@scode%2fmodelling/-/modelling-1.0.13.tgz#0000b821ab973e27a1fea6e06a4b14f9c9beb2cd" - integrity sha512-Ms87Ge3xJNk7CZO0715RHwqa2CMSjTGwfRoOXI9W+iSwjvVwbEvxZTmTK9kX9NCJIuECwX6BCFb5XWMjr9LA0w== +"@scode/modelling@^1.0.16": + version "1.0.16" + resolved "https://npm.scode.ovh/@scode%2fmodelling/-/modelling-1.0.16.tgz#195214d8719c7f115f4860b15e161eb10bdf56ad" + integrity sha512-gAhc1kudd3Oe38jvXih+CHI0aH4tcsiQoedHXyM0ytL6jmWpzyv8VLpwrNZCCYsjwgxV2DAK++ANEQDdEGu+Uw== "@sindresorhus/is@^0.14.0": version "0.14.0"