Giter Club home page Giter Club logo

mongoose-cli's Introduction

Welcome to mongoosejs-cli 👋

npm version Documentation Maintenance License: MIT Build Status Greenkeeper badge

npm

mongoosejs-cli logo

Table of Contents

Introduction

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.

Prerequisites🔨

  • node >=11.0.0
  • yarn >= 1.16.0 || npm >= 6.0.0
  • mongoosejs >= 5.5.12

Installation🛠

Globally

There are two ways to of installing and using this package:

yarn global add mongoosejs-cli
  • Usage
    • mongoose

Locally

yarn add mongoosejs-cli
  • Usage
    • npx mongoosejs-cli

Note 📓

It is recommended to install the package in your project(local). We'll be using the local approach in the following examples.

Usage

Make sure you have mongoose already installed in your project before proceeding.

To display list of options that the command has, do the following

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]

To initialize the project

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

Options

Changing path

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/

Configuration file

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
      }
    }
  }
}

Configuration for connecting over SRV

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...

Contributing🤝

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

Reporting issues or bugs🐛

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.

Proposing new features

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.

Fixing Bugs or Implementing Features

  1. 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

Running tests

$ 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.

FAQ

The Mongoosejs Command Line Interface (CLI) Frequently Asked Question

Initialize mongoose to create necessary files in the project

$ npx mongoosejs-cli init

How can I generate a model?

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.

How can I create a migration?

Specify migration name with --name argument

$ npx mongoosejs-cli migration:create --name <migration_name>

How do call/use a model defined?

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

What is the command to execute all migrations?

$ npx mongoosejs-cli db:migrate

How can I make a migrations rollback?

$ npx mongoosejs-cli db:migrate:undo:all

How can I create a seeder?

Specify seeder name with --name argument

$ npx mongoosejs-cli seed:create --name <seeder_name>

How can I run the seeders?

$ npx mongoosejs-cli db:seed:all

How can I make the seeders rollback?

$ npx mongoosejs-cli db:seed:undo:all

Do you have an example of how the structure of the project look like?

Yes. Please check the examples folder in this project. Screenshots can also be found here

Documentation📓

Credits👌

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...

License📝

Copyright © 2019 Stephane Mensah.
This project is MIT licensed.

Author

👤 Stephane Mensah


This README was generated with ❤️ by readme-md-generator

mongoose-cli's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar justinkworkman avatar waptik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongoose-cli's Issues

Seeders path in config file ".mongooserc"

What you are doing?

Initialize the mongoose-cli using mongoose init

using .mongooserc from readme

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')
}

What do you expect to happen?

Setup the folders for migrations, seeders, etc

What is actually happening?

Error of:
Unknown arguments: seeders-path, seedersPath

I noticed looking at the code in the core/yargs code that it actually doesn't exist. If this is intentional please remove it from the readme thank you

Mongoosejs CLI version: Latest
Mongoose version: Latest

An in-range update of mongoose is breaking the build 🚨

The devDependency mongoose was updated from 5.5.12 to 5.5.13.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mongoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 19 commits.

  • dab9d1d chore: release 5.5.13
  • 1a641fd Merge pull request #7868 from dfdeagle47/docs-typo
  • 851f2df docs(model): fix typo projetion to projection
  • 32ab77c fix(model): support getting discriminator by value when creating a new model
  • 4315e83 test(model): repro #7851
  • 23dcf48 docs(transactions): add section about the withTransaction() helper
  • 6a8607b docs(model+query): link to findOneAndUpdate docs
  • 99abcb8 Merge branch 'master' of github.com:Automattic/mongoose
  • d8e46e1 fix(update): run setters on array elements when doing $addToSet, $push, etc
  • d9fa219 test(update): repro #4185
  • 0676db4 Merge pull request #7860 from Fonger/delete-one-options
  • f7432b1 docs(schema): correct schema options lists
  • e667670 feat(model): support options in deleteOne
  • 9245a03 test(model): add deleteOne with options test
  • 6581ef2 test(transactions): repro #7857

There are 19 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of mongoose is breaking the build 🚨

The devDependency mongoose was updated from 5.5.14 to 5.5.15.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mongoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 14 commits.

  • 473b126 chore: release 5.5.15
  • cb62f08 Merge pull request #7891 from jyrkive/master
  • ea283b7 fix(array): copy atomics from source array (#7889)
  • 276b8e2 test(document): repro #7889
  • d7ae2e3 fix(connection): reject initial connect promise even if there is an on('error') listener
  • d14f96d style: fix lint
  • fdfe3bc fix(update): use discriminator schema to cast update if discriminator key specified in filter
  • 51cc5a5 test(update): repro #7843
  • eb5078e fix(map): make of automatically convert POJOs to schemas unless typeKey is set
  • d03129c test(map): repro #7859
  • 08c348f Merge pull request #7887 from Mickael-van-der-Beek/fix-schema-add-this
  • 339504a fix(schema): return this when Schema.prototype.add is called with Schema instance as argument
  • bb219a9 fix(model): add numAffected and result to DocumentNotFoundError re: #7844
  • 63f1736 chore: now working on 5.5.15

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.