adapt to modelling

This commit is contained in:
2020-05-02 21:29:25 +02:00
parent fccd69db74
commit 0eea261e56
6 changed files with 4062 additions and 4167 deletions

@@ -1,29 +1,24 @@
import { Request, Response, Router } from 'express';
import { http } from '@scode/consts';
import { DatabaseCrudOptions } from './DatabaseCrudOptions';
import { ControlModel, DatabaseModel } from '@scode/modelling';
import { CrudHandler } from './CrudHandler';
import { HttpHandler } from './HttpHandler';
import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader';
export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
protected table: string;
protected columns: Array<string>;
protected cm: new () => ControlModel;
protected dm: new () => DatabaseModel;
protected options: DatabaseCrudOptionsReader;
public constructor (
public constructor<T extends ControlModel, U extends DatabaseModel> (
table: string,
columns: Array<string>,
options: DatabaseCrudOptions = {}
cm: new () => ControlModel,
dm: new () => DatabaseModel
) {
super ();
this.table = table;
this.columns = columns;
this.cm = cm;
this.dm = dm;
this.options = new DatabaseCrudOptionsReader (options);
if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) {
throw new Error (
'the column id cannot be made available to modification'
);
}
}
protected validate_body (
@@ -45,29 +40,6 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
return null;
}
protected ensure_data (
data: Record<string, unknown>,
res: Response,
fail_on_undef = true
): Promise<Record<string, unknown>> | Record<string, unknown> {
const obj = {};
const keys = Object.keys (data);
for (const col of this.columns) {
if (!keys.includes (col) && fail_on_undef) {
res.status (http.status_bad_request)
.end (`missing field: ${col}`);
return null;
}
obj[col] = data[col];
}
if (typeof this.options.optional_columns !== 'undefined') {
for (const col of this.options.optional_columns)
obj[col] = data[col];
}
return obj;
}
public async create (req: Request, res: Response): Promise<void> {
if (!await this.options.create_authorization (req, res))
return;
@@ -76,16 +48,17 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
if (body_data === null)
return;
const db_data = await this.ensure_data (body_data, res);
if (db_data === null)
return;
const cm = new this.cm;
cm.object = body_data;
cm.update ();
const inserted = await this.knex (this.table)
.returning ('id')
.insert (db_data);
const dm = new this.dm;
for (const key of Object.keys (body_data))
dm.set (key, cm.get (key));
await dm.write ();
res.status (http.status_created)
.end (inserted[0]);
.end (dm.id);
}
public async read (req: Request, res: Response): Promise<void> {
@@ -98,14 +71,14 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
return;
}
const json = await this.knex (this.table)
.where ({ id: req.headers.id })
.select (
'id',
...this.columns
);
res.status (json.length > 0 ? http.status_ok : http.status_not_found)
.json (json[0]);
const dm = new this.dm (req.headers.id);
const found = await dm.read ();
const cm = new this.cm (dm.object);
cm.update ();
res.status (found ? http.status_ok : http.status_not_found)
.json (cm.object);
}
public async update (req: Request, res: Response): Promise<void> {
@@ -116,21 +89,31 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
if (body_data === null)
return;
const db_data = await this.ensure_data (body_data, res, false);
if (db_data === null)
return;
if (typeof req.headers.id === 'undefined') {
res.status (http.status_bad_request)
.end ('id undefined');
return;
}
await this.knex (this.table)
.where ({ id: req.headers.id })
.update (db_data);
const dm = new this.dm (req.headers.id);
const found = await dm.read ();
if (!found) {
res.status (http.status_not_found)
.end ();
return;
}
res.status (http.status_ok)
const cm = new this.cm (dm.object);
cm.update ();
for (const key of Object.keys (body_data))
cm.set (key, body_data[key]);
cm.update ();
dm.object = cm.object;
const written = await dm.write ();
res.status (written ? http.status_ok : http.status_internal_server_error)
.end ();
}
@@ -144,11 +127,17 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
return;
}
await this.knex (this.table)
.where ({ id: req.headers.id })
.delete ();
const dm = new this.dm (req.headers.id);
const found = await dm.read ();
if (!found) {
res.status (http.status_not_found)
.end ();
return;
}
res.status (http.status_ok)
const deleted = await dm.delete ();
res.status (deleted ? http.status_ok : http.status_internal_server_error)
.end ();
}

@@ -1,15 +1,15 @@
import { Request, Response } from 'express';
import { KnexCrudOptions, Authorization } from './DatabaseCrudOptions';
import { DatabaseCrudOptions, Authorization } from './DatabaseCrudOptions';
type AuthRunner = {
(req: Request, res: Response): Promise<boolean>;
}
export class KnexCrudOptionsReader {
private options: KnexCrudOptions;
export class DatabaseCrudOptionsReader {
private _options: DatabaseCrudOptions;
public constructor (options: KnexCrudOptions) {
this.options = options;
public constructor (options: DatabaseCrudOptions) {
this._options = options;
}
private get_auth_runner (
@@ -18,19 +18,23 @@ export class KnexCrudOptionsReader {
if (typeof auth === 'undefined')
return (): Promise<boolean> => new Promise ((r) => r (true));
return (req, res): Promise<boolean> => new Promise ((resolve) => {
const result = auth (req, res, resolve);
if (typeof result !== 'undefined')
resolve (result as boolean);
let resolved = false;
const result = await auth (req, res, (cb) => {
resolved = true;
resolve (typeof cb === 'undefined' || cb === true);
});
if (!resolved)
resolve (result);
});
}
public get optional_columns (): Array<string> | undefined {
return this.options.optional_columns;
return this._options.optional_columns;
}
public get create_authorization (): Authorization {
const general = this.get_auth_runner (this.options.general_authorization);
const specific = this.get_auth_runner (this.options.create_authorization);
const general = this.get_auth_runner (this._options.general_authorization);
const specific = this.get_auth_runner (this._options.create_authorization);
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
@@ -38,8 +42,8 @@ export class KnexCrudOptionsReader {
}
public get read_authorization (): Authorization {
const general = this.get_auth_runner (this.options.general_authorization);
const specific = this.get_auth_runner (this.options.read_authorization);
const general = this.get_auth_runner (this._options.general_authorization);
const specific = this.get_auth_runner (this._options.read_authorization);
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
@@ -47,8 +51,8 @@ export class KnexCrudOptionsReader {
}
public get update_authorization (): Authorization {
const general = this.get_auth_runner (this.options.general_authorization);
const specific = this.get_auth_runner (this.options.update_authorization);
const general = this.get_auth_runner (this._options.general_authorization);
const specific = this.get_auth_runner (this._options.update_authorization);
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
@@ -56,8 +60,8 @@ export class KnexCrudOptionsReader {
}
public get delete_authorization (): Authorization {
const general = this.get_auth_runner (this.options.general_authorization);
const specific = this.get_auth_runner (this.options.delete_authorization);
const general = this.get_auth_runner (this._options.general_authorization);
const specific = this.get_auth_runner (this._options.delete_authorization);
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;