json2src is a minimalistic code generator. It produces software code by running your data through templates. Your data is just any json and the templates are written in handlebars. That's it. The tool was designed to get out of your way. It is up to you what your data and your templates are.
- table2json - database structure reader which can get table definitions from postgres and sqlserver.
- postgres stored procedures template - template generating CRUD ( and some more ) stored procedures for your tables.
This program needs to be added to your package.json. To install run:
npm install --save https://github.com/jakubiszon/json2src.git
Example usage:
constjson2src=require('json2src');(asyncfunction(){// preparing an engine, this will compile the templates, partials and assign helpersconstengine=awaitjson2src({// path to templates directorytemplateRoot : 'path/to/templates',// OPTIONAL - path to partials directorypartialsRoot : 'path/to/partials',// OPTIONAL - an object storing helper functions, the keys of the object will be used as helper nameshelpers : {/* ... */}});// now we have an engine, we can run it any number of timesawaitengine({// data passed to each templatedata : { ... },// path to output directoryoutputRoot : 'path/to/output',// filename prefix or a function naming the files, explained belowfilePrefix : 'filename prefix for this run',// OPTIONAL - a function taking full template name as param and returning boolean// to decide if the given template should be included in this runisTemplateIncludedCallback: function(templateKey){ ... }});})();The above example used async/await. Callbacks are not supported.
At the moment all input file names are expected to end with .hbs.
The structure of the output files matches the input structure. If we call:
constengine=awaitjson2src({templateRoot : 'template_root'});awaitengine({data:{/*...*/},outputRoot : 'output_root',filePrefix: 'person'});And the template_root contains the following stucture:
/template_root/
├─ /data/
│ ├─ .js.hbs
├─ /modules/
│ ├─ controller.js.hbs
│ ├─ .js.hbs
The output_root will be stuffed with following files:
/output_root/
├─ /data/
│ ├─ person.js
├─ /modules/
│ ├─ person_controller.js
│ ├─ person.js
We can continue running the same engine to produce more files:
awaitengine({data:{/*...*/},outputRoot : 'output_root',filePrefix: 'order'})awaitengine({data:{/*...*/},outputRoot : 'output_root',filePrefix: 'order_item'})We will end up with:
/output_root/
├─ /data/
│ ├─ order.js
│ ├─ order_item.js
│ ├─ person.js
├─ /modules/
│ ├─ order_controller.js
│ ├─ order.js
│ ├─ order_item_controller.js
│ ├─ order_item.js
│ ├─ person_controller.js
│ ├─ person.js
Output file names are controlled by the filePrefix passed as part of RunParameters object.
- for input files formed as
name.ext.hbsthefilePrefixis added with an underscore and results withfilePrefix_name.ext - for input files without a name e.g.
.ext.hbsthe output isfilePrefix.ext - when the prefix is assigned a falsy value - the output file names are just the template names without the
.hbsextensions filePrefixcould also be assigned as a function:
/** * @param {string} filename - name of the template, without the path and .hbs extension * @param {string} templateKey - relative path to the file and name without extension, helps to distinguish files with same name in different directories */function(filename,templateKey){// the filename param is the // the returned string will be used as the filename// no tests were done for return values containing pathsreturn'some-other-file-name.js';}Partial names reflect the structure of the partialsRoot directory.
Example partial structure:
/partials_root/
├─ /controller/
│ ├─ post.hbs
│ ├─ put.hbs
├─ database.hbs
Example usage of the above:
The helpers property of EngineParameters object passed to json2src is expected to store functions. The keys of the helpers object will be used as names when registering helpers to handlebars.
Example
lethelpers={"toLowerCase" : function(input){returninput.toString().toLowerCase();},"toUpperCase" : function(input){returninput.toString().toUpperCase();},}The above helpers could be referenced as:
Run parameters can include a callback function isTemplateIncludedCallback which can tell the engine to skip selected files.
The following run will not process templates which are found inside modules subdirectory of the templateRoot. Additionally output directories are only created when an output file needs them. In the case below, modules directory would not be created inside outputRoot.
awaitengine({data:{/*...*/},outputRoot : 'output_root',filePrefix: 'test',isTemplateIncludedCallback: function(templateKey){return!templateKey.startsWith('modules/');}});If you need the list of template keys before running the engine, the list is assigned as templateKeys property of the engine function.
constengine=awaitjson2src({templateRoot : 'template_root'});// print all template keys registered in the engineconsole.log(engine.templateKeys);