remodel to be independant of database
This commit is contained in:
parent
95814c5541
commit
fccd69db74
@ -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'
|
@ -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 };
|
@ -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>;
|
@ -1,3 +1,3 @@
|
||||
export { HttpHandler } from './HttpHandler';
|
||||
export { CrudHandler } from './CrudHandler';
|
||||
export { KnexCrudHandler } from './KnexCrudHandler';
|
||||
export { DatabaseCrudHandler } from './DatabaseCrudHandler';
|
||||
|
@ -29,13 +29,12 @@
|
||||
"ava": "^3.7.1",
|
||||
"eslint": "^6.8.0",
|
||||
"nyc": "^15.0.1",
|
||||
"sqlite3": "^4.1.1",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@scode/consts": "^1.1.5",
|
||||
"@scode/modelling": "^1.0.3",
|
||||
"@types/express": "^4.17.6",
|
||||
"express": "^4.17.1",
|
||||
"knex": "^0.21.0"
|
||||
"express": "^4.17.1"
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import knex from 'knex';
|
||||
import test from 'ava';
|
||||
import express, { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import { KnexCrudHandler } from '../lib/KnexCrudHandler';
|
||||
import { KnexCrudHandler } from '../lib/DatabaseCrudHandler';
|
||||
|
||||
const db = knex ({
|
||||
client: 'sqlite',
|
||||
|
@ -8,7 +8,7 @@
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
// "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. */
|
||||
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
|
Reference in New Issue
Block a user