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
moduleexports = User: name: String age: Number pets: Object ;
url.js
moduleexports = 'mongodb://user:password@server:port'
Alternatively, to get the URL you can call Database.createUrl({ username, password, host, port, database})
database.js
const Database = ; const url = ;const schemas = ; const db = url schemas; db; moduleexports = db;
app.js
// Imagine express stuff is up here// Handles connections from the root, don't worry about this if you don't know expressapp;
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.