UNPKG

sequelize-admin-panel

Version:
159 lines (136 loc) 4.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: cli/index.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: cli/index.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** @module cli */ /* eslint no-case-declarations: 0, no-console: 0 */ const path = require('path') const { defaultLocale, defaultTranslation } = require('../l10n') const exec = path.basename(process.argv[1]), command = process.argv[2], options = process.argv.slice(3) function help() { console.error(`Usage: ${process.argv0} ${exec} command [options] Commands: dumptranslation [--empty] [--hints] - show translation skeleton createsuperuser user password - create superuser init [--all] - (re)create user table or all tables, drop all data`) } function dumpTranslations(sequelizeInstance, empty = false, hints = false) { if (empty) Object.keys(defaultTranslation[defaultLocale].ui).map( key => (defaultTranslation[defaultLocale].ui[key] = '') ) Object.keys(sequelizeInstance.models).map(modelName => { const model = sequelizeInstance.models[modelName] const fields = Object.keys(model.attributes) .filter(fName => !model.attributes[fName].references) .concat(Object.keys(model.associations)) defaultTranslation[defaultLocale].models = Object.assign( defaultTranslation[defaultLocale].models || {}, { [model.name]: { label: empty ? '' : model.options.name.singular, plural: empty ? '' : model.options.name.plural, [hints ? 'hint' : undefined]: '', fields: fields.reduce( (acc, curr) => Object.assign(acc, { [curr]: empty ? '' : curr, [hints ? curr + '_hint' : undefined]: '' }), {} ) } } ) }) defaultTranslation[defaultLocale].actions = { delete: 'Delete' } return JSON.stringify(defaultTranslation, null, 2) } /** * node cli -h * @method cli * @param {Sequelize} sequelizeInstance db connection */ module.exports = function cli(sequelizeInstance) { const User = require('../userSession').userModelBuilder(sequelizeInstance) switch (command) { case 'dumptranslation': console.log( dumpTranslations( sequelizeInstance, options.includes('--empty'), options.includes('--hints') ) ) break case 'createsuperuser': const [username, password] = options if (username &amp;&amp; password) { User.create( { username, password, superuser: true, enabled: true }, { logging: false } ) .then(() => console.log(`User ${username} created`)) .catch(e => console.error(e.name, e.message)) .then(() => sequelizeInstance.close()) } else help() break case 'init': const object = options[0] === '--all' ? sequelizeInstance : User, message = options[0] === '--all' ? 'All tables are initialized' : 'User table is initialized' object .sync({ force: true, logging: false }) .then(() => console.log(message)) .catch(e => console.error(e.name, e.message)) .then(() => sequelizeInstance.close()) break default: help() } } </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-cli.html">cli</a></li><li><a href="module-sequelize-admin-panel.html">sequelize-admin-panel</a></li></ul><h3>Classes</h3><ul><li><a href="ModelAdmin.html">ModelAdmin</a></li><li><a href="ModelAdminManager.html">ModelAdminManager</a></li></ul><h3>Namespaces</h3><ul><li><a href="req.html">req</a></li><li><a href="req.SA.html">SA</a></li><li><a href="req.SA.utils.html">utils</a></li><li><a href="res.html">res</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed May 30 2018 09:23:34 GMT+0700 (+07) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>