0.3.1 • Published 6 years ago
@mxxbe/mini-mongoose-client v0.3.1
mini-mongoose-client
a tiny wrapper around mongoose
Installation
npm i --save @mxxbe/mini-mongoose-client
Usage
The client constructor expects at least host
, port
and db
to establish a connection.
Authentication against the specified database is possible by also adding username
and userpwd
.
The following snippet is a minimal working usage example:
// import the module
const MongooseClient = require("@mxxbe/mini-mongoose-client");
// create a client (implicitly connects to the specified db) [auth is optional]
const Account = new MongooseClient("127.0.0.1", "27017", "admin", "root", "secret")
.setModel("account", require("./account.json"));
// the mongoose model is ready to be used
Account.find({ active: false }).then(docs => console.log(docs));
Where account.json
could look something like this:
{
"email": {
"type": "String",
"unique": "true"
},
"pwd": {
"type": "String"
},
"active": {
"type": "Boolean"
}
}
The amount of registered models per client is not limited to one:
const client = new MongooseClient("127.0.0.1", "27017", "admin");
const Order = client.setModel('order', require('order.json'));
const Product = client.setModel('product', require('product.json'));