This commit is contained in:
Timo Hocker 2020-05-02 21:48:22 +02:00
parent 0eea261e56
commit 1541018701
7 changed files with 42 additions and 36 deletions

View File

@ -1,8 +1,8 @@
import { Request, Response } from 'express';
export interface CrudHandler {
public create(req: Request, res: Response): Promise<void>;
public read(req: Request, res: Response): Promise<void>;
public update(req: Request, res: Response): Promise<void>;
public delete(req: Request, res: Response): Promise<void>;
create(req: Request, res: Response): Promise<void>;
read(req: Request, res: Response): Promise<void>;
update(req: Request, res: Response): Promise<void>;
delete(req: Request, res: Response): Promise<void>;
}

View File

@ -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<string, string|number|boolean>) => ControlModel;
protected dm: new (id?: number) => DatabaseModel;
protected options: DatabaseCrudOptionsReader;
public constructor<T extends ControlModel, U extends DatabaseModel> (
table: string,
cm: new () => ControlModel,
dm: new () => DatabaseModel
public constructor (
cm: new (object: Record<string, string|number|boolean>) => 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<string, unknown>> | Record<string, unknown> {
): Promise<Record<string, unknown> | null> | Record<string, unknown> | 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<string|string, number|boolean>);
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)

View File

@ -1,5 +1,4 @@
import { Request, Response } from 'express';
import ControlModel from '@scode/modelling';
type Authorization = {
(req: Request, res: Response): Promise<boolean>;
@ -13,7 +12,6 @@ interface DatabaseCrudOptions {
update_authorization?: Authorization;
delete_authorization?: Authorization;
optional_columns?: Array<string>;
control_model?: Type<ControlModel>;
}
export { Authorization, DatabaseCrudOptions };

View File

@ -17,15 +17,19 @@ export class DatabaseCrudOptionsReader {
): AuthRunner {
if (typeof auth === 'undefined')
return (): Promise<boolean> => new Promise ((r) => r (true));
return (req, res): Promise<boolean> => 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<boolean> => new Promise (
(resolve: (value: boolean) => void) => {
(async (): Promise<void> => {
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<string> | undefined {

View File

@ -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 {

View File

@ -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"
}

View File

@ -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"