🏠 Homepage
- Introduction
- Prerequisites
- Installation
- Usage
- CLI Options
- Contributing
- FAQ
- Documentation
- Credits
- License
This package, Mongoosejs-cli, is a package for nodejs to help generate and run migrations and seeders for mongoosejs with ease.
Note that this is an unofficial CLI package for mongoosejs.
- node >=11.0.0
- yarn >= 1.16.0 || npm >= 6.0.0
- mongoosejs >= 5.5.12
There are two ways to of installing and using this package:
yarn global add mongoosejs-cli
- Usage
- mongoose
yarn add mongoosejs-cli
- Usage
- npx mongoosejs-cli
It is recommended to install the package in your project(local). We'll be using the local approach in the following examples.
Make sure you have mongoose already installed in your project before proceeding.
npx mongoosejs-cli
You'll see the following on your terminal:
Mongoosee CLI [Node: 11.10.0, CLI: 1.0.5, ODM: 5.5.12]
mongoose [command]
Commands:
mongoose db:migrate Run pending migrations
mongoose db:migrate:status List the status of all migrations
mongoose db:migrate:undo Reverts a migration
mongoose db:migrate:undo:all Revert all migrations ran
mongoose db:seed Run specified seeder
mongoose db:seed:undo Deletes data from the database
mongoose db:seed:all Run every seeder
mongoose db:seed:undo:all Deletes data from the database
mongoose init Initializes project
mongoose init:config Initializes configuration
mongoose init:migrations Initializes migrations
mongoose init:models Initializes models
mongoose init:seeders Initializes seeders
mongoose migration:generate Generates a new migration file [aliases: migration:create]
mongoose model:generate Generates a model and its migration [aliases: model:create]
mongoose seed:generate Generates a new seed file [aliases: seed:create]
Options:
--help Show help [boolean]
--version Show version number [boolean]
We recommend that after viewing the list of commands, first thing to do, is to generate the required files and directories needed to get started. It can achieved by entering the following command.
npx mongoosejs-cli init
This will generate the following:
config/
config.json
models/
index.js
migrations/
seeders/
- config/ => the directory containing all your configuration files
- config.json => the default configuration files that contains the database connection strings based on the environment(NODE_ENV). You can add extra environments as well.
- models/ => the directory that contains all your mongoose models you generated through the package
- index.js => this file does the database connection and imports all your models
- migrations/ => directory containing all your migration files
- seeders/ => directory containing all your seed files
By default, mongoosejs-cli generates the migrations, seeders and models directories and files in the root directory of your project. To change this behaviour, create a new file called .mongooserc
manually or use the the following command:
touch .mongooserc
and paste the following code inside the new created file
const path = require('path');
module.exports = {
'config': path.resolve('config', 'database.json'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
}
Now the CLI will look for its
- configuration settings inside ./config/database.json
- models files inside ./db/models/
- migration files inside ./db/migrations/
- seed files inside ./db/seeders/
By default the CLI will try to use the file config/config.js
. You can modify that path either via the --config
flag or via the option mentioned earlier. Here is how a configuration file might look like (this is the one that npx mongoosejs-cli init
generates):
{
"development": {
"database": {
"url": "mongodb://localhost/mongoose_dev",
"options": {
"useNewUrlParser": true
}
}
},
"test": {
"database": {
"url": "mongodb://localhost/mongoose_test",
"options": {
"useNewUrlParser": true
}
}
},
"production": {
"database": {
"protocol": "mongodb",
"username": "root",
"password": "password",
"name": "database_production",
"host": "localhost",
"port": "",
"options": {
"useNewUrlParser": true
}
}
}
}
In case you are using MongoDB Atlas or any other services that supports srv, kindly remove database name known as "name"
from the database main object and assign its value to a new string called "dbName"
in the options object, such as the following.
{
"production": {
"database": {
"protocol": "mongodb+srv",
"username": "root",
"password": "password",
"host": "subdomain.mongodb.com",
"port": "",
"options": {
"useNewUrlParser": true,
"dbName": "database_production",
}
}
}
}
In case you want to use a url string instead, do the following
{
"production": {
"database": {
"url": "mongodb+srv://root:[email protected]/database_production",
"options": {
"useNewUrlParser": true,
"dbName": "database_production",
}
}
}
}
More coming soon...
If you love this package, there are different ways in which you can contribute.
👍 Show you Support
Give a ⭐️ if this project helped you!
General Issues or Feature Requests
Please make sure to read the full guidelines. Your issue may be closed without warning if you do not.
Before reporting a new issue, kindly check issues page to see if similar issues haven't been solved yet. Else, go ahead and create a new issue.
Github issues should follow specified template. When you start creating a new issue, an empty template will be made available to you.
Please make sure issue you are reporting is strictly related to Mongoosejs CLI.
If you want to propose new features to Mongoosejs CLI, you may ignore issue template. You still need to clearly state new feature. Feature request should give various examples, API suggestions and references to support idea behind it.
- Preparing your environment
Start by cloning Mongoosejs CLI repo
$ git clone [email protected]:waptik/mongoose-cli.git
$ git clone https://github.com/waptik/mongoose-cli.git # Using HTTPS
Make sure you have all required dependencies, you will need
- Node v10 or above
- Yarn v1.16 or above
Now go to cloned repository folder
$ cd /path/to/cloned/repository
Install required modules
$ yarn
$ yarn test
Test can take about 7 to 10 minutes to finish, subjected to hardware configuration.
Improving Documentation
If you want to improve or expand our documentation you can start with this readme file.
The Mongoosejs Command Line Interface (CLI) Frequently Asked Question
$ npx mongoosejs-cli init
Specify model name with --name
argument. List of table fields can be passed with --attributes
option. Note that the datatypes are using Mongoose SchemaTypes when defining them using the CLI.
$ npx mongoosejs-cli model:create --name User --attributes name:String,state:Boolean,birth:Date,card:Number
You can create your own custom datatypes as plugins after the model file has been generated for you. Refer to Mongoose Plugins, on how to do so.
Specify migration name with --name
argument
$ npx mongoosejs-cli migration:create --name <migration_name>
You can call/use a model(eg: Player) by doing the following:
const models = require('path_to_models_folder');
const players = await models.Player.find({});
console.log(players) // will print players collection
$ npx mongoosejs-cli db:migrate
$ npx mongoosejs-cli db:migrate:undo:all
Specify seeder name with --name
argument
$ npx mongoosejs-cli seed:create --name <seeder_name>
$ npx mongoosejs-cli db:seed:all
$ npx mongoosejs-cli db:seed:undo:all
Yes. Please check the examples folder in this project. Screenshots can also be found here
This package would not have been made possible if not for the following:
- Mongoosejs Contributors and maintainers for the awesome ORM specially for mongodb on nodejs
- Sequelize for their CLI, which Mongoosejs-cli structure was heavily based on.
- Kefranabg, for his readme-md-generator package which generated the skeleton for this readme file.
- Nodejs, Yarnpkg, NPM etc...
Copyright © 2019 Stephane Mensah.
This project is MIT licensed.
👤 Stephane Mensah
This README was generated with ❤️ by readme-md-generator