crud/CrudUnit.js

/**
 * @module flitter-crud/CrudUnit
 */

const Unit = require('libflitter/Unit')
const path = require('path')
const StopError = require('libflitter/errors/StopError')

/**
 * Unit to register functionality provided by the flitter-crud package.
 * @extends module:libflitter/Unit~Unit
 */
class CrudUnit extends Unit {
    /**
     * Get the name of the service provided by this unit: 'crud'
     * @returns {string} - 'crud'
     */
    static get name() {
        return 'crud'
    }

    /**
     * Bootstrap the unit's functionality.
     * @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
     * @param {module:libflitter/Context~Context} context
     * @returns {Promise<void>}
     */
    async go(app) {

        // Make sure we have the required services
        const di = app.di()
        const required_services = ['forms', 'routers', 'models', 'controllers']
        for ( const service_name of required_services ) {
            if ( !di.has(service_name) ) {
                throw (new StopError()).unit(this.name()).required(required_services)
            }
        }
        
    }

    /**
     * Get the templates provided by this unit.
     * Currently, {@link module:flitter-crud/templates/crud} assigned as "crud".
     * @returns {{crud: {template: ((function(*, *=): string)|*), extension: string, directory: void | Promise<void> | Promise<any>}}}
     */
    templates(){
        return {
            crud: {
                template: require('./templates/crud'),
                directory: path.resolve(this.app.directories.models, 'crud'),
                extension: '.model.js'
            },
            'crud:controller': {
                template: require('./templates/controller'),
                directory: path.resolve(this.app.directories.controllers, 'crud'),
                extension: '.controller.js'
            },
            'crud:router': {
                template: require('./templates/router'),
                directory: path.resolve(this.app.directories.routers, 'crud'),
                extension: '.routes.js'
            }
        }
    }

}

module.exports = exports = CrudUnit