crudql
v1.0.5
Published
CrudQL: Simplify Your HTTP Server with Simple Function Calls that return [text,status] that can even be used within other functions to Optimize Fetching Later.
Downloads
17
Readme
CrudQL: Simplify Your HTTP Server
Part of the Premium #CrudTruePower https://neil.codehubby.com package (CrudQL will always be free)
CrudQL Tutorial
// tutorial-crudql.js
const creator = {
readSum(user, a, b) {
return [ a+b, 200 ];
}
}
/*
// test
const [text, status] = creator.readSum({}, 1,2);
console.log({text});
//*/
export default creator;
CrudQL Tutorial (index file)
// tutorial-crudql-index.js
import creatorRoutes from './tutorial-crudql.js';
import { createAction, readRoutes } from 'crudql';
export const crudql = {
routes: { creator: creatorRoutes, guest:[] },
getRouteUser(user,requestMode) {
if(user) return 'creator';
else return 'guest';
}
}
const [user, func, args] = [ {user_id:1}, 'readSum', [1,2] ];
const routes = await readRoutes(crudql, user);
console.log( { routes: JSON.stringify(routes) } ); // 1. (optional) Show All User Routes
const result = await createAction(crudql, func, args, user); // ex. inside POST /:funcName
console.log( { result } ); // 2. Execute and Show Result
CrudQL Express Example
// app.js
import express from 'express';
import logger from 'morgan';
import bodyparser from 'body-parser';
import { createAction, readRoutes } from 'crudql';
const app = express();
app.use(logger('dev'));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: true}));
const crudql = {
routes: {
creator: { test(user) { return ['testcreator',200] } },
guest: { test(user) { return ['testguest',200] } },
},
getRouteUser(user,requestmode) {
if(user) return 'creator';
else return 'guest';
}
};
app.options('*', (req, res) => { // all options request respond OK
res.json({ status: 'OK'});
});
app.post('/', async(req, res) => {
try {
let user = null;
const { authorization, requestmode } = req.headers;
// 1. AUTH
if(authorization === 'your authentication check goes here') {
user = {user_id:1}; // your token decode goes here
}
// 2. Read All User Routes
const routes = await readRoutes(crudql,user, requestmode);
res.send(routes);
} catch(err) {
console.log('err',err);
res.status(503).send();
}
});
app.post('/:funcName', async(req, res) => {
let user, requestmode;
try {
const { authorization } = req.headers;
requestmode = req.headers.requestmode;
const { funcName } = req.params;
const args = req.body;
// 1. AUTH
if(authorization === 'your authentication check goes here') {
user = {user_id:1}; // your token decode goes here
}
// 2. EXECUTE
const [text,status] = await createAction(crudql, funcName, args, user, requestmode);
res.status(status).send(text);
} catch(err) {
// Extra: Nice error handling
let text,status;
const crudQLUser = crudql.getRouteUser(user,requestmode);
if(err.toString().includes('function') && err.toString().includes('not found')) {
[text,status] = ['Function not found for ' + crudQLUser + ' user',404];
} else {
console.log('err',err);
[text,status] = ['Unknown error',503];
}
res.status(status).send({error: text});
}
});
const HTTP_PORT=3473;
console.log("[*] Starting http server (CrudQL) on port " + HTTP_PORT);
app.set('port', HTTP_PORT);
app.listen(HTTP_PORT);
Quick Tip to Use Import / ES6 with NodeJS:
Add this to your package.json file
"type":"module",