remodel to be independant of database

This commit is contained in:
Timo Hocker 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 { Request, Response, Router } from 'express';
import { http } from '@scode/consts'; import { http } from '@scode/consts';
import Knex from 'knex'; import { DatabaseCrudOptions } from './DatabaseCrudOptions';
import { KnexCrudOptions } from './KnexCrudOptions';
import { CrudHandler } from './CrudHandler'; import { CrudHandler } from './CrudHandler';
import { HttpHandler } from './HttpHandler'; 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 table: string;
protected columns: Array<string>; protected columns: Array<string>;
protected options: KnexCrudOptionsReader; protected options: DatabaseCrudOptionsReader;
protected knex: Knex;
public constructor ( public constructor (
knex: Knex,
table: string, table: string,
columns: Array<string>, columns: Array<string>,
options: KnexCrudOptions = {} options: DatabaseCrudOptions = {}
) { ) {
super (); super ();
this.knex = knex;
this.table = table; this.table = table;
this.columns = columns; this.columns = columns;
this.options = new KnexCrudOptionsReader (options); this.options = new DatabaseCrudOptionsReader (options);
if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) { if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) {
throw new Error ( throw new Error (
'the column id cannot be made available to modification' 'the column id cannot be made available to modification'

View File

@ -1,17 +1,19 @@
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>;
(req: Request, res: Response, next: Function): unknown; (req: Request, res: Response, next: Function): unknown;
} }
interface KnexCrudOptions { interface DatabaseCrudOptions {
general_authorization?: Authorization; general_authorization?: Authorization;
create_authorization?: Authorization; create_authorization?: Authorization;
read_authorization?: Authorization; read_authorization?: Authorization;
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, KnexCrudOptions }; export { Authorization, DatabaseCrudOptions };

View File

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

View File

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

View File

@ -29,13 +29,12 @@
"ava": "^3.7.1", "ava": "^3.7.1",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"nyc": "^15.0.1", "nyc": "^15.0.1",
"sqlite3": "^4.1.1",
"typescript": "^3.8.3" "typescript": "^3.8.3"
}, },
"dependencies": { "dependencies": {
"@scode/consts": "^1.1.5", "@scode/consts": "^1.1.5",
"@scode/modelling": "^1.0.3",
"@types/express": "^4.17.6", "@types/express": "^4.17.6",
"express": "^4.17.1", "express": "^4.17.1"
"knex": "^0.21.0"
} }
} }

View File

@ -2,7 +2,7 @@ import knex from 'knex';
import test from 'ava'; import test from 'ava';
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import { http } from '@scode/consts'; import { http } from '@scode/consts';
import { KnexCrudHandler } from '../lib/KnexCrudHandler'; import { KnexCrudHandler } from '../lib/DatabaseCrudHandler';
const db = knex ({ const db = knex ({
client: 'sqlite', client: 'sqlite',

View File

@ -8,7 +8,7 @@
// "allowJs": true, /* Allow javascript files to be compiled. */ // "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */ // "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */ "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */ // "outFile": "./", /* Concatenate and emit output to single file. */

1558
yarn.lock

File diff suppressed because it is too large Load Diff