In this documentation we will describe the files found in the Helpers folder of _core/
. These files contain the functions used within the application and are therefore broken down into files according to their context of use.
We will then see how it is possible to use _core/
helpers in your development and also how to add your cutom helper in your app/
.
Here is a general description of the purpose of each file contained in the _core/helpers/folder
.
You will find more details (description of the functions and their parameters are in the JSDoc of the application)
Access management within the application.
Functions used by the address component.
Functions used for generating the API documentation for the application.
Functions used for datatables (datatable.js and NodeaTable on the client side).
Functions used for document template component.
Functions used by entities routes in general.
File and storage management.
Language and translation in the application.
Functions used by models and Sequelize.
Functions used by the program system in the application.
Functions used for status component.
All core helpers are accessible in your app/
folder:
const helpers = require('@core/helpers');
// helpers[filename][function], example:
helpers.access.isLoggedIn();
helpers.file.read(filepath);
If you want to add your own helpers function you can do it however you want but here is the Nodea way:
helpers/
folder into your app/
directoryindex.js
fileindex.js
file:const fs = require('fs-extra');
const path = require('path');
const basename = path.basename(module.filename);
const helpers = {};
fs.readdirSync(__corePath + '/helpers')
.filter(file => file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js')
.forEach(file => {
// eslint-disable-next-line global-require
helpers[file.replace('.js', '')] = require('@core/helpers/' + file);
});
fs.readdirSync(__appPath + '/helpers')
.filter(file => file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js')
.forEach(file => {
// eslint-disable-next-line global-require
helpers[file.replace('.js', '')] = require('@app/helpers/' + file);
});
module.exports = (_ => {
return {...helpers};
})();
4. Write one or multiple custom helpers files like this app/helpers/my_helpers.js
for example:
exports.prefixLog = function(text) {
console.log('Log => ', text);
}
5. Use your custom helpers along with core helper like this:
const helpers = require('@app/helpers'); // Require app helpers instead of core helpers
helpers.access.isLoggedIn(); // Core helper
helpers.file.read(filepath); // Core helper
helpers.my_helpers.prefixLog('Hello there'); // App helper
Be aware that in this case you cannot write app helpers file with a filename that is the same as an already existing core helpers file.
As a workaround if necessary you can prefix core helpers in the app/helpers/index.js
file:
const helpers = {core:{}};
// ... File content ...
helpers.core[file.replace('.js', '')] = require('@core/helpers/' + file);
// Use example: helpers.core.access.isLoggedIn