remodel to be independant of database

This commit is contained in:
2020-04-23 18:58:06 +02:00
parent 95814c5541
commit fccd69db74
8 changed files with 85 additions and 1508 deletions

View File

@ -1,28 +1,24 @@
import { Request, Response, Router } from 'express';
import { http } from '@scode/consts';
import Knex from 'knex';
import { KnexCrudOptions } from './KnexCrudOptions';
import { DatabaseCrudOptions } from './DatabaseCrudOptions';
import { CrudHandler } from './CrudHandler';
import { HttpHandler } from './HttpHandler';
import { KnexCrudOptionsReader } from './KnexCrudOptionsReader';
import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader';
export class KnexCrudHandler extends HttpHandler implements CrudHandler {
export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
protected table: string;
protected columns: Array<string>;
protected options: KnexCrudOptionsReader;
protected knex: Knex;
protected options: DatabaseCrudOptionsReader;
public constructor (
knex: Knex,
table: string,
columns: Array<string>,
options: KnexCrudOptions = {}
options: DatabaseCrudOptions = {}
) {
super ();
this.knex = knex;
this.table = table;
this.columns = columns;
this.options = new KnexCrudOptionsReader (options);
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'

View File

@ -1,17 +1,19 @@
import { Request, Response } from 'express';
import ControlModel from '@scode/modelling';
type Authorization = {
(req: Request, res: Response): Promise<boolean>;
(req: Request, res: Response, next: Function): unknown;
}
interface KnexCrudOptions {
interface DatabaseCrudOptions {
general_authorization?: Authorization;
create_authorization?: Authorization;
read_authorization?: Authorization;
update_authorization?: Authorization;
delete_authorization?: Authorization;
optional_columns?: Array<string>;
control_model?: Type<ControlModel>;
}
export { Authorization, KnexCrudOptions };
export { Authorization, DatabaseCrudOptions };

View File

@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { KnexCrudOptions, Authorization } from './KnexCrudOptions';
import { KnexCrudOptions, Authorization } from './DatabaseCrudOptions';
type AuthRunner = {
(req: Request, res: Response): Promise<boolean>;

View File

@ -1,3 +1,3 @@
export { HttpHandler } from './HttpHandler';
export { CrudHandler } from './CrudHandler';
export { KnexCrudHandler } from './KnexCrudHandler';
export { DatabaseCrudHandler } from './DatabaseCrudHandler';