1.1.0 • Published 5 years ago
@frappy/js-mongo-store v1.1.0
MongoDB Store for NodeJS
General MongoDB Store Wrapper for NodeJS, wrapping individual collections.
Usage
Generic Store without Custom Functionality
import mongodb from "mongodb"
import MongoDbStore from "@frappy/js-mongo-store"
const MONGO_URL = process.env.MONGO_URL || "mongodb://localhost:27017"
// create mongoDB connection
mongodb.MongoClient.connect(MONGO_URL, {
useNewUrlParser: true,
}).then(client => {
// initialise store
const myStore = new MongoDbStore(client, "myDatabaseName", "myCollectionName")
// run some operations on the store
myStore.create({ foo: "bar" }).then(newDocId => myStore.get(newDocId)).then(newDoc => {
console.log("Created a new document", newDoc)
myStore.delete(newDoc._id)
})
})Custom Store Class
import mongodb from "mongodb"
import MongoDbStore from "@frappy/js-mongo-store"
const MONGO_URL = process.env.MONGO_URL || "mongodb://localhost:27017"
class MyCustomStore extends MongoDbStore {
findCustomValues(attributeValue) {
return this.find({ customAttribute: attributeValue }).then(matchingDocuments => {
// map the list of documents to one of their attributes (or return it directly)
return matchingDocuments.map(doc => doc.someOtherAttribute)
})
}
}
// create mongoDB connection
mongodb.MongoClient.connect(MONGO_URL, {
useNewUrlParser: true,
}).then(client => {
const myStore = new MyCustomStore(client, "myDatabaseName", "myCollectionName")
myStore.findCustomValues("foobar").then(result => {
// this will print out a list of `someOtherAttribute` returned by the store method
console.log("someAttributes:", result)
})
}) Methods
All methods return a Promise, which returns either a document (get), a string (create), a number (count) or a
list of documents (find).
getAll(paging)- retrieves all documents from the collection and returns them as list,pagingis optional, defaults topage: 0andpageSize: 25- aliaslist()insert(doc)- creates a new document and returns the new document_id- alias:create(doc)find(query, projection, sort, paging)- runs a query and returns an array of matches. All parameters are optional and default tonull, except thepaging, which behaves as described ingetAll(paging). All options get proxied to MongoDB.findOne(query)- tries to find a single document, will return the first matching document or null, if no document matches the queryget(docId)- returns a document with the given_idcount(query)- finds out how many documents match the searchquery, which is optional (nullreturns the total number of documents in the collection)delete(docId)- deletes a document with the given_id, returns nothing - aliasremove(docId)update(query, update)- performs a given update on potentially multiple documents, wherequerydetermines which documents to update andupdatecan be a$setupdate statement or any other MongoDB operations, such as$push- returns nothingupdateDocument(update)- updates a single document with the providedupdate. Theupdateneeds to be the complete MongoDB document including_id- returns nothing.
1.1.0
5 years ago