test structure
This commit is contained in:
@ -2,4 +2,10 @@ import { Router } from 'express';
|
||||
|
||||
export class HttpHandler {
|
||||
public abstract register_handlers(router: Router): void;
|
||||
|
||||
public get_router (): Router {
|
||||
const r = Router ();
|
||||
this.register_handlers (r);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
import { Request, Response, Router } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import Knex from 'knex';
|
||||
import { KnexCrudOptions } from './KnexCrudOptions';
|
||||
import { CrudHandler } from './CrudHandler';
|
||||
import { Knex } from './KnexInterface';
|
||||
import { HttpHandler } from './HttpHandler';
|
||||
import { KnexCrudOptionsReader } from './KnexCrudOptionsReader';
|
||||
|
||||
export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
protected table: string;
|
||||
protected columns: Array<string>;
|
||||
protected options: KnexCrudOptions;
|
||||
protected options: KnexCrudOptionsReader;
|
||||
protected knex: Knex;
|
||||
|
||||
public constructor (
|
||||
@ -21,7 +22,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
this.knex = knex;
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.options = options;
|
||||
this.options = new KnexCrudOptionsReader (options);
|
||||
if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) {
|
||||
throw new Error (
|
||||
'the column id cannot be made available to modification'
|
||||
@ -32,7 +33,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
protected validate_body (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<object> | object {
|
||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
||||
if (typeof req.body === 'undefined') {
|
||||
res.status (http.status_bad_request);
|
||||
res.end ('body was undefined');
|
||||
@ -49,10 +50,10 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
}
|
||||
|
||||
protected ensure_data (
|
||||
data: object,
|
||||
data: Record<string, unknown>,
|
||||
res: Response,
|
||||
fail_on_undef = true
|
||||
): Promise<object>|object {
|
||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
||||
const obj = {};
|
||||
const keys = Object.keys (data);
|
||||
for (const col of this.columns) {
|
||||
@ -63,8 +64,10 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
}
|
||||
obj[col] = data[col];
|
||||
}
|
||||
for (const col of this.options.optional_columns)
|
||||
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;
|
||||
}
|
||||
|
@ -5,84 +5,13 @@ type Authorization = {
|
||||
(req: Request, res: Response, next: Function): unknown;
|
||||
}
|
||||
|
||||
type AuthRunner = {
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
interface KnexCrudOptions {
|
||||
general_authorization?: Authorization;
|
||||
create_authorization?: Authorization;
|
||||
read_authorization?: Authorization;
|
||||
update_authorization?: Authorization;
|
||||
delete_authorization?: Authorization;
|
||||
optional_columns?: Array<string>;
|
||||
}
|
||||
|
||||
export class KnexCrudOptions {
|
||||
private _general_authorization?: Authorization;
|
||||
private _create_authorization?: Authorization;
|
||||
private _read_authorization?: Authorization;
|
||||
private _update_authorization?: Authorization;
|
||||
private _delete_authorization?: Authorization;
|
||||
|
||||
public optional_columns?: Array<string>;
|
||||
|
||||
private get_auth_runner (
|
||||
auth?: Authorization
|
||||
): AuthRunner {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
public set general_authorization (value: Authorization) {
|
||||
this._general_authorization = value;
|
||||
}
|
||||
|
||||
public get create_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._create_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set create_authorization (value: Authorization) {
|
||||
this._create_authorization = value;
|
||||
}
|
||||
|
||||
public get read_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._read_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set read_authorization (value: Authorization) {
|
||||
this._read_authorization = value;
|
||||
}
|
||||
|
||||
public get update_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._update_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set update_authorization (value: Authorization) {
|
||||
this._update_authorization = value;
|
||||
}
|
||||
|
||||
public get delete_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._delete_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set delete_authorization (value: Authorization) {
|
||||
this._delete_authorization = value;
|
||||
}
|
||||
}
|
||||
export { Authorization, KnexCrudOptions };
|
||||
|
66
lib/KnexCrudOptionsReader.ts
Normal file
66
lib/KnexCrudOptionsReader.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { KnexCrudOptions, Authorization } from './KnexCrudOptions';
|
||||
|
||||
type AuthRunner = {
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
}
|
||||
|
||||
export class KnexCrudOptionsReader {
|
||||
private options: KnexCrudOptions;
|
||||
|
||||
public constructor (options: KnexCrudOptions) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
private get_auth_runner (
|
||||
auth?: Authorization
|
||||
): AuthRunner {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
public get optional_columns (): Array<string> | undefined {
|
||||
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);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
|
||||
interface KnexQuery {
|
||||
insert(data: object): Promise<Array<unknown>>;
|
||||
update(data: object): Promise<unknown>;
|
||||
select(...columns: Array<string>): Promise<Array<object>>;
|
||||
delete(): Promise<unknown>;
|
||||
where(filter: object): KnexQuery;
|
||||
whereNot(filter: object): KnexQuery;
|
||||
whereIn(filter: object): KnexQuery;
|
||||
whereNotIn(filter: object): KnexQuery;
|
||||
whereNull(filter: object): KnexQuery;
|
||||
whereNotNull(filter: object): KnexQuery;
|
||||
whereExists(filter: object): KnexQuery;
|
||||
whereNotExists(filter: object): KnexQuery;
|
||||
whereBetween(filter: object): KnexQuery;
|
||||
whereNotBetween(filter: object): KnexQuery;
|
||||
join(table: string, col: string, on: string): KnexQuery;
|
||||
leftJoin(table: string, col: string, on: string): KnexQuery;
|
||||
rightJoin(table: string, col: string, on: string): KnexQuery;
|
||||
leftOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
rightOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
fullOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
returning(column: string | Array<string>): KnexQuery;
|
||||
}
|
||||
|
||||
type Knex = {
|
||||
(table: string): KnexQuery;
|
||||
}
|
||||
|
||||
export { Knex, KnexQuery };
|
Reference in New Issue
Block a user