0.0.17 • Published 5 years ago
make-it-easy v0.0.17
Make It Easy
Express based with prebuilt nedb as database, fomantic/semantic ui as user interface and use DataTables to display the data. Come with concept Controller, Form and View(not view like ejs, twig and others, its view to display the data).
Just some experiment, never tested in production. Support node.js v10.
Installation
npm install make-it-easyAfter installation you will see prebuilt source in folder app
app
├── controllers # Controller files
├── forms # Form files
├── models # Model files
├── templates # Template files(View engine using twigjs)
└── views # View filesUsage
const MIE = require('make-it-easy');
let app = new MIE();
app.start();API
Maybe not all api described here, doc need to be updated.
Model
Curently avaiable only for nedb model
const Model = require("make-it-easy").model;
class User extends Model{
// V12 or higher
/*
static properties = {
username: {unique: true, required: true},
name: {required: true},
password: {required: true, protected: true},
roles: {options: ["Admin"]}
}
*/
async getRoles(){
return this.roles;
}
};
// V10 or higher
User.properties = {
username: {unique: true, required: true},
name: {required: true},
password: {required: true, protected: true},
roles: {options: ["Admin"]}
}
module.exports = User;Options
- tableName: (string) name for table. one model save in one db (optional, default to class name)
- timestamp: (boolean) options to create attribute
_createdand_updated(optional, default to true) - properties: (object) list of model properties. some of options not implemented yet like
uniqueand some other can be loaded for form options- required: (boolean) requires options, if its set to
truebut the attribute not set, when save the model it will fail.
- required: (boolean) requires options, if its set to
- beforeSave: (function) event before save data to database, has parameter
args - save: (method) save data to database
- afterSave: (function) event after save data to database, has parameter
args - staticfind: (method) function for find document. return array objct. params:
- conditions: (object) condition/option to find doc using nedb (optional)
- limit: (number) condition/option to find doc using nedb (optional, default to 0/unlimited)
- sort: (object) sorting documents (optional)
- staticfindById(id): (method) find one document using id.
- staticfindOne(conditions): (method) find one document using conditions.
- staticremoveById(id): (method) remove document using id.
Controller
Put files in folder app/controllers, then it will be loaded automatically
const Controller = require('make-it-easy').controller;
module.exports = [
new Controller({
name: 'index',
path: '/',
method: 'GET',
acl: '*',
fn: (req, res, next) => {
return res.json({success: true});
}
})
];Options
- name: (string) unique. used in template to get route using
route(route_name)(required) - path: (string) route path (required)
- method: (string|array) request method. If multiple, use array
method: ['GET', 'POST'](required) - acl: (string|array) access controll list. If multiple use array
acl: ['@', 'Admin']. Avaiable acl options: (optional, default to*)*for all users (guest|authenticated)@for authenticated only andtextfor roles
- fn: (function) controller function, has
req,resandnextparams (required) - csrf: (boolean) if you want to use csurf for get and post (optional, default to
false)
Form
Put files in folder app/forms, then it will be loaded automatically
const Form = require("make-it-easy").form;
const User = require("../models/user");
const bcrypt = require('bcryptjs');
module.exports = new Form(User, {
name: 'form_user',
path: '/user',
acl: '*',
items: {
username: { unique: true, required: true },
name: { required: true },
password: {type: 'password', required: true},
roles: { type: 'select', multiple: true }
},
beforeSave: async (args) => {
if (args.doc.password) {
const hash = await bcrypt.hashSync(args.doc.password, 10);
args.doc.password = hash;
} else {
args.doc.password = args.prevDoc.password;
}
},
beforeOpen: async (args) => {
args.items.password.hide = !args.editMode;
args.items.password.required = args.editMode && !args.data._id ? true : false;
args.data.password = "";
}
});Options
- model: (model) reference model for the form
- option: (object) form options
- name: (string) unique. used in template to get route using
route(route_name)(required) - path: (string) route path (required)
- acl: (string|array) access controll list. If multiple use array
acl: ['@', 'Admin']. Avaiable acl options: (optional, default to*)*for all users (guest|authenticated)@for authenticated only andtextfor roles
- items: Form items
- required: (function|boolean) items required option, validation using bacis html form validation
- type: (string) items type. Currently only support:
textnumbertextareadatetimeselectradiocheckboxtogglecomputeditems(like level 2 of form items, must have attribute form items)
- readers: (function|boolean) function to determine can access the form or not
- authors: (function|boolean) function to determine can save/edit/remove the form or not
- javascripts: (string|array) add js files to the form (relative path)
- styles: (string|array) add css files to the form (relative path)
- variables: (object) add variables to the form with name
vars. (can be string, object, array, function or async function) - beforeOpen: (function) event before open the document
- beforeSave: (function) event before save the document
- afterSave: (function) event after save the document
- name: (string) unique. used in template to get route using
View
Put files in folder app/views, then it will be loaded automatically
const View = require("make-it-easy").view;
const User = require("../models/user");
const FormUser = require("../forms/user");
module.exports = new View(User, {
name: 'view_users',
path: '/users',
form: FormUser,
acl: '*',
column: [
{data: 'name'},
{data: 'username'},
{data: 'roles'}
],
});Options
- model: (model) reference model for the form
- option: (object) view options
- name: (string) unique. used in template to get route using
route(route_name)(required) - path: (string) route path (required)
- form: (Form) form reference to open the form
- acl: (string|array) access controll list. If multiple use array
acl: ['@', 'Admin']. Avaiable acl options: (optional, default to*)*for all users (guest|authenticated)@for authenticated only andtextfor roles
- column: Column list you want to display in view.
- data: (string) attributes from model
- type: (string) currently only support
date,datetime - title: (string) header title
- render: (function) render function, has parameter
colandrows
- readers: (function|boolean) function to determine can access the form or not
- conditions: (object|nedb condition) condition to filter view
- name: (string) unique. used in template to get route using
Contributing
- Fork it
- Create your feature branch (
git checkout -b feature/fooBar) - Commit your changes (
git commit -am 'Add some fooBar') - Push to the branch (
git push origin feature/fooBar) - Create a new Pull Request
Lisence
MIT