import { Request, Response } from 'express'; import { http } from '@scode/consts'; import { try_parse_json } from '@scode/utilities'; import { KnexCrudOptions } from './KnexCrudOptions'; import { CrudHandler } from './CrudHandler'; export class KnexCrudHandler implements CrudHandler { protected table: string; protected columns: Array; protected options: KnexCrudOptions; public constructor ( table: string, columns: Array, options: KnexCrudOptions = {} ) { this.table = table; this.columns = columns; this.options = options; } private call_auth ( auth?: Function, req: Request, res: Response ): Promise { if (typeof auth === 'undefined') return true; const promise = new Promise ((resolve) => { const result = auth (req, res, resolve); if (typeof result !== 'undefined') resolve (result); }); return promise; } protected validate_body ( req: Request, res: Response ): Promise | object { if (typeof req.body === 'undefined') { res.status (http.status_bad_request); res.end (); return null; } return try_parse_json (req.body); } public async create (req: Request, res: Response): Promise { if (!await this.call_auth (this.options.create_authentication, req, res)) return; if (!await this.call_auth (this.options.create_authorization, req, res)) return; const body_data = await this.validate_body (req, res); if (body_data === null) return; } public async read (req: Request, res: Response): Promise { } public async update (req: Request, res: Response): Promise { } public async delete (req: Request, res: Response): Promise { } }