UNPKG

mongoose-data-seed

Version:

Seed mongodb with data using mongoose models

365 lines (334 loc) 23.7 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="./"> <title data-ice="title">Manual | mongoose-data-seed</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <meta name="description" content="Seed mongodb with data using mongoose models"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="mongoose-data-seed"><meta property="twitter:description" content="Seed mongodb with data using mongoose models"></head> <body class="layout-container manual-root manual-index" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a href="test.html" data-ice="testLink">Test</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> <a style="position:relative; top:3px;" href="https://github.com/sharvit/mongoose-data-seed"><img width="20px" src="./image/github.png"></a></header> <nav class="navigation" data-ice="nav"><div class="manual-toc-root"> <div data-ice="manual"> <ul class="manual-toc"> <li data-ice="manualNav" class="indent-h1" data-link="manual/README.html"><a href="manual/README.html" data-ice="link">mongoose-data-seed</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#install" data-ice="link">Install</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#use" data-ice="link">Use</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#options" data-ice="link">Options</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#seeder-example" data-ice="link">Seeder Example</a></li> <li data-ice="manualNav" class="indent-h3" data-link="manual/README.html"><a href="manual/README.html#md-seed-config-js" data-ice="link">md-seed-config.js</a></li> <li data-ice="manualNav" class="indent-h4" data-link="manual/README.html"><a href="manual/README.html#example" data-ice="link">Example</a></li> <li data-ice="manualNav" class="indent-h3" data-link="manual/README.html"><a href="manual/README.html#configurations" data-ice="link">Configurations</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#examples" data-ice="link">Examples</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/README.html"><a href="manual/README.html#license" data-ice="link">License</a></li> </ul> </div> <div data-ice="manual"> <ul class="manual-toc"> <li data-ice="manualNav" class="indent-h1" data-link="manual/CONTRIBUTING.html"><a href="manual/CONTRIBUTING.html" data-ice="link">Contributing</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CONTRIBUTING.html"><a href="manual/CONTRIBUTING.html#code-of-conduct" data-ice="link">Code of Conduct</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CONTRIBUTING.html"><a href="manual/CONTRIBUTING.html#project-setup" data-ice="link">Project setup</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CONTRIBUTING.html"><a href="manual/CONTRIBUTING.html#committing-and-pushing-changes" data-ice="link">Committing and Pushing changes</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CONTRIBUTING.html"><a href="manual/CONTRIBUTING.html#help-needed" data-ice="link">Help needed</a></li> </ul> </div> <div data-ice="manual"> <ul class="manual-toc"> <li data-ice="manualNav" class="indent-h1" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html" data-ice="link">Contributor Covenant Code of Conduct</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#our-pledge" data-ice="link">Our Pledge</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#our-standards" data-ice="link">Our Standards</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#our-responsibilities" data-ice="link">Our Responsibilities</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#scope" data-ice="link">Scope</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#enforcement" data-ice="link">Enforcement</a></li> <li data-ice="manualNav" class="indent-h2" data-link="manual/CODE_OF_CONDUCT.html"><a href="manual/CODE_OF_CONDUCT.html#attribution" data-ice="link">Attribution</a></li> </ul> </div> </div> </nav> <div class="content" data-ice="content"><div class="github-markdown"> <div class="manual-user-index" data-ice="manualUserIndex"><p><a href="https://www.npmjs.com/package/mongoose-data-seed"><img src="https://img.shields.io/npm/v/mongoose-data-seed.svg?style=flat-square" alt="Package Version"></a> <a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release"></a> <a href="https://npm-stat.com/charts.html?package=mongoose-data-seed&amp;from=2016-04-01"><img src="https://img.shields.io/npm/dm/mongoose-data-seed.svg?style=flat-square" alt="Downloads Status"></a> <a href="https://travis-ci.org/sharvit/mongoose-data-seed"><img src="https://img.shields.io/travis/sharvit/mongoose-data-seed/master.svg?style=flat-square" alt="Build Status: Linux"></a> <a href="https://coveralls.io/github/sharvit/mongoose-data-seed?branch=master"><img src="https://coveralls.io/repos/github/sharvit/mongoose-data-seed/badge.svg?branch=master" alt="Coverage Status"></a> <a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="PRs Welcome"></a> <a href="https://david-dm.org/sharvit/mongoose-data-seed"><img src="https://david-dm.org/sharvit/mongoose-data-seed/status.svg" alt="dependencies Status"></a> <a href="https://david-dm.org/sharvit/mongoose-data-seed?type=dev"><img src="https://david-dm.org/sharvit/mongoose-data-seed/dev-status.svg" alt="devDependencies Status"></a> <a href="https://greenkeeper.io/"><img src="https://badges.greenkeeper.io/sharvit/mongoose-data-seed.svg" alt="Greenkeeper badge"></a> <a href="https://github.com/prettier/prettier"><img src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square" alt="code style: prettier"></a> <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed?ref=badge_shield"><img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed.svg?type=shield" alt="FOSSA Status"></a> <a href="http://opensource.org/licenses/MIT"><img src="https://img.shields.io/npm/l/stack-overflow-copy-paste.svg?style=flat-square" alt="MIT License"></a></p> <h1 id="mongoose-data-seed">mongoose-data-seed</h1><p>Seed mongodb with data using mongoose models</p> <p><img src="https://raw.githubusercontent.com/sharvit/mongoose-data-seed/master/md-seed-run-example.gif" alt="cli example using md-seed run"></p> <h2 id="install">Install</h2><pre><code class="lang-shell"><code class="source-code prettyprint">npm install --save mongoose-data-seed md-seed init</code> </code></pre> <p><code>md-seed init</code> will ask you to choose a folder for your seeders.</p> <p><code>md-seed init</code> will create the <code>seeders</code> folder, generate <code>md-seed-config.js</code> and update your <code>package.json</code>.</p> <h2 id="use">Use</h2><p>Generate seeder file</p> <pre><code class="lang-shell"><code class="source-code prettyprint">md-seed g users</code> </code></pre> <p>Run all seeders</p> <pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run</code> </code></pre> <p>Or run specific seeders</p> <pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run users posts comments</code> </code></pre> <h2 id="options">Options</h2><p>Drop the database before seeding</p> <pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run --dropdb</code> </code></pre> <h2 id="seeder-example">Seeder Example</h2><pre><code class="lang-javascript"><code class="source-code prettyprint">import { Seeder } from &apos;mongoose-data-seed&apos;; import { User } from &apos;../server/models&apos;; const data = [ { email: &apos;user1@gmail.com&apos;, password: &apos;123123&apos;, passwordConfirmation: &apos;123123&apos;, isAdmin: true }, { email: &apos;user2@gmail.com&apos;, password: &apos;123123&apos;, passwordConfirmation: &apos;123123&apos;, isAdmin: false } ]; class UsersSeeder extends Seeder { async shouldRun() { return User.countDocuments() .exec() .then(count =&gt; count === 0); } async run() { return User.create(data); } } export default UsersSeeder; </code> </code></pre> <h3 id="md-seed-config-js">md-seed-config.js</h3><p><code>md-seed</code> expecting to get 3 values from <code>md-seed-config.js</code></p> <ol> <li><code>seedersList</code> - A key/value list of all your seeders, <code>md-seed</code> will run your seeders as they ordered in the list.</li> <li><code>connect</code> - Connect to mongodb implementation (should return promise).</li> <li><code>dropdb</code> - Drop/Clear the database implementation (should return promise).</li> </ol> <h4 id="example">Example</h4><pre><code class="lang-javascript"><code class="source-code prettyprint">import mongoose from &apos;mongoose&apos;; import Users from &apos;./seeders/users.seeder&apos;; import Posts from &apos;./seeders/posts.seeder&apos;; import Comments from &apos;./seeders/comments.seeder&apos;; const mongoURL = process.env.MONGO_URL || &apos;mongodb://localhost:27017/dbname&apos;; /** * Seeders List * order is important * @type {Object} */ export const seedersList = { Users, Posts, Comments, }; /** * Connect to mongodb implementation * @return {Promise} */ export const connect = async () =&gt; await mongoose.connect(mongoURL, { useNewUrlParser: true }); /** * Drop/Clear the database implementation * @return {Promise} */ export const dropdb = async () =&gt; mongoose.connection.db.dropDatabase(); </code> </code></pre> <h3 id="configurations">Configurations</h3><p><code>mongoose-data-seed</code> configurations will get loaded from the <code>mdSeed</code> field in your <code>package.json</code> file.</p> <table> <thead> <tr> <th>Field</th> <th>Default Value</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>seedersFolder</code></td> <td><code>&apos;./seeders&apos;</code></td> <td>Path for your seeders-folder, seeders will be generated into this folder.</td> </tr> <tr> <td><code>customSeederTemplate</code></td> <td><code>undefined</code></td> <td>Path to a custom template file to generate your seeders from.</td> </tr> </tbody> </table> <h2 id="examples">Examples</h2><ol> <li><a href="https://github.com/sharvit/mongoose-data-seed/tree/master/examples/md-seed-example">md-seed-example</a></li> </ol> <h2 id="license">License</h2><p>MIT</p> <p><a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed?ref=badge_shield"><img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed.svg?type=shield" alt="FOSSA Status"></a> <a href="http://opensource.org/licenses/MIT"><img src="https://img.shields.io/npm/l/stack-overflow-copy-paste.svg?style=flat-square" alt="MIT License"></a></p> </div> <div class="manual-cards"> <div class="manual-card-wrap" data-ice="cards"> <div class="manual-card"> <div data-ice="card"><h1>mongoose-data-seed</h1><p>Seed mongodb with data using mongoose models</p><p><img src="https://raw.githubusercontent.com/sharvit/mongoose-data-seed/master/md-seed-run-example.gif" alt="cli example using md-seed run"></p><h2>Install</h2><pre><code class="lang-shell"><code class="source-code prettyprint">npm install --save mongoose-data-seed md-seed init</code> </code></pre><p><code>md-seed init</code> will ask you to choose a folder for your seeders.</p><p><code>md-seed init</code> will create the <code>seeders</code> folder, generate <code>md-seed-config.js</code> and update your <code>package.json</code>.</p><h2>Use</h2><p>Generate seeder file</p><pre><code class="lang-shell"><code class="source-code prettyprint">md-seed g users</code> </code></pre><p>Run all seeders</p><pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run</code> </code></pre><p>Or run specific seeders</p><pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run users posts comments</code> </code></pre><h2>Options</h2><p>Drop the database before seeding</p><pre><code class="lang-shell"><code class="source-code prettyprint">md-seed run --dropdb</code> </code></pre><h2>Seeder Example</h2><pre><code class="lang-javascript"><code class="source-code prettyprint">import { Seeder } from &apos;mongoose-data-seed&apos;; import { User } from &apos;../server/models&apos;; const data = [ { email: &apos;user1@gmail.com&apos;, password: &apos;123123&apos;, passwordConfirmation: &apos;123123&apos;, isAdmin: true }, { email: &apos;user2@gmail.com&apos;, password: &apos;123123&apos;, passwordConfirmation: &apos;123123&apos;, isAdmin: false } ]; class UsersSeeder extends Seeder { async shouldRun() { return User.countDocuments() .exec() .then(count =&gt; count === 0); } async run() { return User.create(data); } } export default UsersSeeder; </code> </code></pre><h3>md-seed-config.js</h3><p><code>md-seed</code> expecting to get 3 values from <code>md-seed-config.js</code></p><ol> <li><code>seedersList</code> - A key/value list of all your seeders, <code>md-seed</code> will run your seeders as they ordered in the list.</li> <li><code>connect</code> - Connect to mongodb implementation (should return promise).</li> <li><code>dropdb</code> - Drop/Clear the database implementation (should return promise).</li> </ol><h4>Example</h4><pre><code class="lang-javascript"><code class="source-code prettyprint">import mongoose from &apos;mongoose&apos;; import Users from &apos;./seeders/users.seeder&apos;; import Posts from &apos;./seeders/posts.seeder&apos;; import Comments from &apos;./seeders/comments.seeder&apos;; const mongoURL = process.env.MONGO_URL || &apos;mongodb://localhost:27017/dbname&apos;; /** * Seeders List * order is important * @type {Object} */ export const seedersList = { Users, Posts, Comments, }; /** * Connect to mongodb implementation * @return {Promise} */ export const connect = async () =&gt; await mongoose.connect(mongoURL, { useNewUrlParser: true }); /** * Drop/Clear the database implementation * @return {Promise} */ export const dropdb = async () =&gt; mongoose.connection.db.dropDatabase(); </code> </code></pre><h3>Configurations</h3><p><code>mongoose-data-seed</code> configurations will get loaded from the <code>mdSeed</code> field in your <code>package.json</code> file.</p><table> <thead> <tr> <th>Field</th> <th>Default Value</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>seedersFolder</code></td> <td><code>&apos;./seeders&apos;</code></td> <td>Path for your seeders-folder, seeders will be generated into this folder.</td> </tr> <tr> <td><code>customSeederTemplate</code></td> <td><code>undefined</code></td> <td>Path to a custom template file to generate your seeders from.</td> </tr> </tbody> </table><h2>Examples</h2><ol> <li><a href="https://github.com/sharvit/mongoose-data-seed/tree/master/examples/md-seed-example">md-seed-example</a></li> </ol><h2>License</h2><p>MIT</p><p><a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed?ref=badge_shield"><img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsharvit%2Fmongoose-data-seed.svg?type=shield" alt="FOSSA Status"></a> <a href="http://opensource.org/licenses/MIT"><img src="https://img.shields.io/npm/l/stack-overflow-copy-paste.svg?style=flat-square" alt="MIT License"></a></p></div> <a data-ice="link" href="manual/README.html"></a> </div> </div> <div class="manual-card-wrap" data-ice="cards"> <div class="manual-card"> <div data-ice="card"><h1>Contributing</h1><p>Contributions are always welcome, no matter how large or small.</p><p><strong>Working on your first Pull Request?</strong> You can learn how from this <em>free</em> series <a href="https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github">How to Contribute to an Open Source Project on GitHub</a></p><h2>Code of Conduct</h2><p>By participating, you are expected to uphold this <a href="./manual/./CODE_OF_CONDUCT.md">Contributor Covenant Code of Conduct</a>. Please report unacceptable behavior to <a href="./manual/mailto:sharvita@gmail.com">sharvita@gmail.com</a>.</p><h2>Project setup</h2><p>First, <a href="https://guides.github.com/activities/forking">fork</a> then clone the repo:</p><pre><code class="lang-sh"><code class="source-code prettyprint">git clone https://github.com/your-username/mongoose-data-seed cd mongoose-data-seed git remote add upstream https://github.com/sharvit/mongoose-data-seed</code> </code></pre><p>Install dependencies:</p><pre><code class="lang-sh"><code class="source-code prettyprint">yarn</code> </code></pre><p>Run test suits to validate the project is working:</p><pre><code class="lang-sh"><code class="source-code prettyprint">yarn test</code> </code></pre><p>Run linter to validate the project code:</p><pre><code class="lang-sh"><code class="source-code prettyprint">yarn lint # to fix linting errors yarn lint --fix</code> </code></pre><p>Run linter to validate your commit message:</p><pre><code class="lang-sh"><code class="source-code prettyprint">yarn lint:commit</code> </code></pre><h2>Committing and Pushing changes</h2><p>Create a branch and start hacking:</p><pre><code class="lang-sh"><code class="source-code prettyprint">git checkout -b my-branch</code> </code></pre><p>Commit and push your changes:</p><p><code>generator-node-mdl</code> uses <a href="https://github.com/commitizen/cz-cli">commitizen</a> to create commit messages so <a href="https://github.com/semantic-release/semantic-release">semantic-release</a> can automatically create releases.</p><pre><code class="lang-sh"><code class="source-code prettyprint">git add . yarn commit # answer the questions git push origin my-branch</code> </code></pre><p>Open this project on <a href="https://github.com/sharvit/generator-node-mdl">GitHub</a>, then click &#x201C;Compare &amp; pull request&#x201D;.</p><h2>Help needed</h2><p>Please watch the repo and respond to questions/bug reports/feature requests, Thanks!</p></div> <a data-ice="link" href="manual/CONTRIBUTING.html"></a> </div> </div> <div class="manual-card-wrap" data-ice="cards"> <div class="manual-card"> <div data-ice="card"><h1>Contributor Covenant Code of Conduct</h1><h2>Our Pledge</h2><p>In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.</p><h2>Our Standards</h2><p>Examples of behavior that contributes to creating a positive environment include:</p><ul> <li>Using welcoming and inclusive language</li> <li>Being respectful of differing viewpoints and experiences</li> <li>Gracefully accepting constructive criticism</li> <li>Focusing on what is best for the community</li> <li>Showing empathy towards other community members</li> </ul><p>Examples of unacceptable behavior by participants include:</p><ul> <li>The use of sexualized language or imagery and unwelcome sexual attention or advances</li> <li>Trolling, insulting/derogatory comments, and personal or political attacks</li> <li>Public or private harassment</li> <li>Publishing others&#x2018; private information, such as a physical or electronic address, without explicit permission</li> <li>Other conduct which could reasonably be considered inappropriate in a professional setting</li> </ul><h2>Our Responsibilities</h2><p>Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.</p><p>Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.</p><h2>Scope</h2><p>This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.</p><h2>Enforcement</h2><p>Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at <a href="./manual/mailto:sharvita@gmail.com">sharvita@gmail.com</a>. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.</p><p>Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project&#x2018;s leadership.</p><h2>Attribution</h2><p>This Code of Conduct is adapted from the <a href="http://contributor-covenant.org">Contributor Covenant</a>, version 1.4, available at <a href="http://contributor-covenant.org/version/1/4/">http://contributor-covenant.org/version/1/4</a></p></div> <a data-ice="link" href="manual/CODE_OF_CONDUCT.html"></a> </div> </div> </div> </div> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>