fast-mongoose
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

Fast Mongoose!

Mongoose, but easier to use and faster to set up!

I made this for my own projects, to simplify model/schema creation.

Usage:

schemas.js

module.exports = {
    User: {
        name: String,
        age: Number,
        pets: [Object]
    }
};

url.js

module.exports = 'mongodb://user:password@server:port'

Alternatively, to get the URL you can call Database.createUrl({ username, password, host, port, database})

database.js

const Database = require('fast-mongoose');
 
const url = require('./url');
const schemas = require('./schemas');
 
const db = new Database(url, schemas);
 
db.connect();
 
module.exports = db;

app.js

// Imagine express stuff is up here
// Handles connections from the root, don't worry about this if you don't know express
app.get('/', (req, res, next) => {
   const username = req.body.username;
   const age = req.body.age;
   const pets = req.body.pets;
   
   // do some validation here
   
   const user = new db.User({username: user, age: age, pets: pets});
   
   user.save(function(err) {
        if (err) {
            //error handling
            return;
        }
        
        // Yay, it's now saved to the DB!
   });
});

This module consists of a single file, which has a class called Database.

Database's constructor is (url, schemas), where url is a string, an example of which is shown above, and schemas is an object, an example of which is also shown above.

Each schema (or rather, the model which was created from your schema) will be accessible from the database instance based on the key. For instance, if your schema is Dog: {stuff} then db.Dog holds the Dog model.

You must call db.connect() for the database to actually connect.

Database also has an isReady() method, which takes no arguments and returns true if the database is ready for reading/writing, and false if not. This is particularly useful for webserver stuff.

You can access mongoose using the mongoose property, e.g. db.mongoose.

Finally, Database is an EventEmitter that only emits the events ready and open... which are the same thing.

Readme

Keywords

none

Package Sidebar

Install

npm i fast-mongoose

Weekly Downloads

8

Version

1.3.1

License

none

Unpacked Size

7.17 kB

Total Files

10

Last publish

Collaborators

  • arcticzeroo