-
Notifications
You must be signed in to change notification settings - Fork 2
Adding plugins
To handle server-side logic and custom behaviours, you can add plugins that we provide, or create your own if you like.
Only one plugin is currently available. More are coming!
Right now, the only plugin that we are providing is a plugin to handle login to the server through a JWT token or a couple email/password. Of course, this is just a beginning.
The login plugin takes an object parameter containing 4 properties:
- userTable : The name of the table beeing used to store the users
- login : The column being used to store the logins
- password : The column being used to store the passwords
- salt (optional) : The column being used to store the salts encrypting the password
Example
const { login : { loginPlugin } } = require('simple-ql');
const plugins = [
loginPlugin({
login: 'email',
password: 'password',
salt: 'salt',
userTable: 'User',
}),
];
To log into a table, just send the following request:
const request = {
User : {
email : 'myLogin',
password : 'myPassword',
}
}
You will receive a SimpleQL response of this type
{
User : {
email : 'myLogin',
jwt : 'jwt token',
}
}
As a matter of facts, you can also provide express middleware and an error handler directly when creating the server. They will be merged with the plugins middlewares.
const middleware = (req, res, next) => next();
const middlewares = [middleware];
const errorHandler = (err, req, res, next) => next(err);
createServer({port : 80, tables, database, rules, plugins, middlewares, errorHandler});
['middleware', 'onRequest', 'onCreation', 'onDeletion', 'onResult', 'preRequisite', 'errorHandler']; A plugin consist in an object containing the following optional properties:
- preRequisite : A function that will make sure that the plugin is correctly configured.
- middleware : A middleware that might intercept the whole request.
- onRequest : An object containing functions being call before any request in a specific table.
- onCreation : An object containing functions being call each time an element is created into a specific table.
- onDeletion : An object containing functions being call each time an element is deleted from a specific table.
- onResult : An object containing functions being call after a request was resolved in a specific table.
- errorHandler : A middleware able to handle errors generated by this plugin.
This should be a function taking the tables as a parameter. It should then return a Promise that would reject if the plugin configuration is wrong given the provided tables. For instance, if the userTable
that have been provided upon creation is actually not present in the tables
object.
function preRequisite(tables) { return Promise.resolve(); }
This is exactly an express middleware.
function middleware(req, res, next) { return Promise.reject().catch(next).then(() => next()); }
This is an object describing all the functions that should be called when a request starts on any table. It will receive the onEvent parameters as second parameter.
const onRequest = {
User : (request, {parent, query, update, isAdmin}) => Promise.resolve()
}
This is an object describing all the functions that should be called when an object is created inside any table. It will receive the onEvent parameters as second parameter.
const onCreation = {
User : (createdObject, {parent, query, update, isAdmin}) => Promise.resolve()
}
This is an object describing all the functions that should be called when an object is deleted from any table. It will receive the onEvent parameters as second parameter.
const onDeletion = {
User : (deletedObjectsArray, {parent, query, update, isAdmin}) => Promise.resolve()
}
This is an object describing all the functions that should be called when a request has been fully executed in any table. It will receive the onEvent parameters as second parameter.
const onResult = {
User : (results, {parent, query, update, isAdmin}) => Promise.resolve()
}
This is an express errorHandler.
function middleware(err, req, res, next) { return next(err); }
The onEvent parameter consist of an object containing the following properties
- parent : The parent of the request currently being executed.
- isAdmin : A boolean indicating if the current request is being executed as an administrator.
- query : A function that you can use to make a SimpleQL query to the database.
- update : A function that makes you able to edit one of the parameters of the request execution
This is the parent of the current request part.
Consider the following request:
{
Feed : {
participants : [
{ email : '[email protected]' },
{ email : '[email protected]' },
],
comments : {
add : {
create : true,
content : 'test',
title : 'Test',
author: {
email : '[email protected]',
}
}
}
}
}
This request will create a comment inside the Comment
table, and add this comment to the Feed table. The parent
of the Comment
request is the Feed
request. This will be the value of parent
:
{
participants : [
{ email : '[email protected]' },
{ email : '[email protected]' },
],
comments : {
add : {
create : true,
content : 'test',
title : 'Test',
author: {
email : '[email protected]',
}
}
}
}
This is a boolean indicating if the current request is being executed with admin rights. You can use this to avoid looping into your event handler. Especially for onRequest
and onResult
The query function provided can be used to make SimpleQL requests to the database. This function takes 2 parameters: the SimpleQL request, and an optional object having the following properties:
- admin (boolean : default false) : indicate that the request should be made with admin credentials. Be careful, this will ignore all access rules.
- readOnly (boolean : default false) : indicate that the request should only read data and is not allowed to make any change to the database.
This function makes you able to change some parameters of the request execution. Right now, the only parameter that you can change is the authId
.
For instance:
update('authId', privateKey)
This would make the complete request being executed as admin
. (this should be avoided at all cost!)