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