Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Created August 1, 2018 10:12
Show Gist options
  • Select an option

  • Save tlaitinen/b54e5c16462a58f4d90be312331b4c70 to your computer and use it in GitHub Desktop.

Select an option

Save tlaitinen/b54e5c16462a58f4d90be312331b4c70 to your computer and use it in GitHub Desktop.
import {IBaseProtocol} from 'pg-promise';
import cfg from '../app-config';
import * as pgp from 'pg-promise';
import * as types from './admin-api-types';
import {Auth} from './auth';
import {InvalidRequest, AccessDenied} from './errors';
import * as path from 'path';
const requestTypes:string[] = types.RequestT.types.map(rt => rt.props.type.value);
const sql:{[requestType:string]:pgp.QueryFile} = {};
requestTypes.forEach(rt => {
sql[rt] = new pgp.QueryFile(path.join(__dirname, 'sql', 'admin', rt + '.sql'), {
minify: true,
debug: cfg.debug
});
});
export function processRequest(db:IBaseProtocol<any>, auth:Auth, request:types.Request):Promise<types.Response> {
return db.tx(async tx => {
if (!auth.admin) {
throw new AccessDenied('only for admin');
}
const payload = Object.assign({}, request.props, auth);
return tx.oneOrNone(sql[request.type], payload);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment