Giter Club home page Giter Club logo

mongoose-repl's Introduction

Mongoose Repl

This is a REPL (read-eval-print-loop) for mongoose.

Requirements

Node 7.5

This project relies on async await to provide a good experience, please ensure that you have a compatible version of NodeJs.

Integration

To add to project:

npm install --save-dev @hubba/hubba-repl

To use:

const repl = require('@hubba/hubba-repl');

// Initialize the mongoose connection
mongoose.connect( config.mongo.uri, config.mongo.options );

// initialize the repl
repl({
    dirname: path.join(__dirname, '../server/'),
    mongoose
});

The repl can be initialized with the path of the directory to walk through to find models.

The default model files that will be loaded are: modelFileRegex The regular expression deciding which files are to be loaded.

Once the files are loaded by node, they will be checked if they export a specific interface.

The model files must export mongoose models as their root export. EX:

module.exports = mongoose.model('User', UserSchema);

Once integrated the repl provides some a command:

.models

List all the models that have been loaded.

    node repl.js
    > .models
    [ 
    'UserMetadata',
    'User',
    'SchemaVersion' ]

This will list all of your models that have been loaded and are available in the global scope.

This will also list all the paths of a model if a model is specified.

    node repl.js
    > .models User
    name : String - 
    status : String - draft,published
    updatedAt : Date  
    createdAt : Date  
    deleted : Boolean  
    deletedAt : Date 

The first element in the list is the model property. The second property after the colon is the type. If there is a dash then the following properties are available enum values.

Finding

To find a model -- for the sake of example lets use User -- you may use a variety of commands:

    node repl.js
    > User.find({})
    [{ _id: 597ba6811a651e6be35fe859,
    n: 'Blahblah User'}]
    > User.findById('597ba6811a651e6be35fe859')
    { _id: 597ba6811a651e6be35fe859,
    n: 'Blahblah User'} 
    > User.findOne({_id: ObjectId('597ba6811a651e6be35fe859')})
    { _id: 597ba6811a651e6be35fe859,
    n: 'Blahblah User'} 

ObjectId

ObjectId Method will automatically create a mongoose object id, the idea behind this is for those people who want to use a syntax similiar to the mongodb REPL have the ability.

Creating

To create a model you can use User.create. The model will run all pre and post hooks as well as any validations.

If your model is initialized with the isWritable attribute false, then create will throw an exception when called.

    node repl.js
    > User.create({n: 'Test User'})
    { _id: 597ba6811a651e6be35fe859,
    n: 'Test User'}

Set Where

If you would like to do a multi document update then you can use the User.setWhere(query, values) command.

mongoose-repl's People

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

urcoilbisurco

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.