sequelize-admin-panel
Version:
Admin panel for sequelize ORM
125 lines (100 loc) • 3.84 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: ModelAdminManager/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: ModelAdminManager/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const ModelAdmin = require('../ModelAdmin')
const { userModelBuilder, UserModelAdmin } = require('../userSession')
/**
* @class Manage ModelAdmin instances
*/
class ModelAdminManager {
/**
* @constructs ModelAdminManager
* @param {Sequelize.Instance}
* @param {ModelAdminManager~modelAdmins}
*/
constructor(sequelizeInstance, modelAdmins = []) {
this._modelList = []
this._modelMap = {}
this._modelAdminList = []
const User = userModelBuilder(sequelizeInstance)
modelAdmins.map(arr => {
const [model, modelAdminClass = ModelAdmin] = arr
this._modelList.push(model.name)
this._modelMap[model.name] = { model, modelAdminClass }
})
Object.keys(sequelizeInstance.models)
.map(modelName => sequelizeInstance.models[modelName])
.filter(model => model.name !== User.name && !this._modelMap[model.name])
.map(model => {
this._modelMap[model.name] = { model, modelAdminClass: ModelAdmin }
if (!modelAdmins.length) this._modelList.push(model.name)
})
this._modelList.push(User.name)
this._modelMap[User.name] = { model: User, modelAdminClass: UserModelAdmin }
Object.keys(this._modelMap).map(modelName => {
const { modelAdminClass, model } = this._modelMap[modelName]
this._modelMap[modelName].modelAdmin = new modelAdminClass(
model
).postInit()
})
this._modelList.map(modelName =>
this._modelAdminList.push(this._modelMap[modelName].modelAdmin)
)
}
/**
* Get ModelAdmin instance by model name
* @param {string} modelName model name
* @returns instance of {@link ModelAdmin}
*/
getModelAdminByModelName(modelName) {
return (this._modelMap[modelName] || {}).modelAdmin
}
/**
* property getter for {@link ModelAdmin} instances, same order it was passed to constructor
* @type {array<ModelAdmin>}
* @readonly
*/
get modelAdminList() {
return this._modelAdminList
}
}
module.exports = ModelAdminManager
/**
* Array of two-elements arrays with Sequelize.Model and ModelAdmin members, e.g.
* <pre>[
* [ItemModel, ItemAdminModel],
* ...
* ]</pre>
* @typedef ModelAdminManager~modelAdmins
* @type {Array<Array<Sequelize.Model, ModelAdmin>>}
*/
</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>