A simple extensible mongo service with CRUD operations.
var MongoCrudService = require("mongo-crud-service");
var BookService = MongoCrudService(
db, /*node-mongo db instance*/
"books" /*collection name*/
)
MongoCrudService is an ECMA6 class definition; you can extend
its behaviour. See Advanced Usage to see how.
db
: node-mongo db instance. See the Basic Usage example below for where you can find itcollectionName
: name of collection in db
Returns a MongoCrudService
instance
create(props)
returnsPromise
list(filter)
returnsPromise
show(id)
returnsPromise
update(id, props)
returnsPromise
remove(id)
returnsPromise
marshal(it)
reformats and returns given document. See Marshalling section below.
const MongoClient = require("mongodb").MongoClient;
var MongoCrudService = require("mongo-crud-service");
/* establish a mongo connection */
new MongoClient("mongodb://...").connect()
.then(function(mongoConnection) {
return mongoConnection.db;
})
.then(function(db) {
class BookService = new MongoCrudService(db, "books");
BookService.create({
name: "iRobot",
author: "Isaac Asimov"
}).then(function(createdBook) {
console.log("created book with id : " + createdBook._id);
});
BookService.list().then(function(books) {
console.log("there are " + books.length + " books");
//...
});
BookService.show("ID_OF_A_BOOK").then(function(book) {
console.log("found book with id: " + book._id);
});
BookService.update("ID_OF_A_BOOK", {
name: "new name of book"
}).then(function(updatedBook) {
console.log("updated book with id: " + book._id);
});
BookService.remove("ID_OF_A_BOOK").then(function() {
console.log("removed book with id: ID_OF_A_BOOK");
});
});
Because mongo-crud-service uses ECMA6 class definition, it is easily extensible.
class AdvBookService extends MongoCrudService {
create(props) {
props.name = props.name.toUpperCase();
return super.create(props);
}
}
myBookService = new AdvBookService(db, "advbooks");
myBookService.create({
name: "Dream Catcher",
author: "Stephen King",
}).then(function(createdBook) {
console.log(createdBook);
/* this will print:
{
_id: somethingsomethingsomething,
name: "DREAM CATCHER",
author: "Stephen King"
}
*/
});
To marshal/reformat the data you get from CRUD operations you can use
- use the
setMarshaller(fun)
method.fun
is a marshaller function as described belowfunction myMarshaller(it) { it.fullName = it.name + " " + it.surname; it.id = it._id; return it; } var MyService = new MongoCrudService(...).setMarshaller(myMarshaller)
- override the
marshal()
method in your child class:class MyServiceClass extends MongoCrudService { marshal(it) { // do stuff with 'it' return it; } }
MongoCrudService.idMarshaller
is a simple marshalling function that adds an id
field (like mongoose does)
var MyService = new MongoCrudService(...).setMarshaller(MongoCrudService.idMarshaller);
Enable createdAt
and updatedAt
fields by calling enableTimestamps()
on your MongoCrudService
instance
var MyService = new MongoCrudService(...).enableTimestamps()
If you want to use other MongoDB methods, access them through collection
of your MongoCrudService
instance.
This will be an instance of MongoDB Collection.
var BookService = new MongoCrudService(...);
BookService.collection.count({year: {$gt: 2006}})
.then(function(count) {
console.log("There are " + count + " books released after 2006");
});