@frappy/js-mongo-store
v1.1.0
Published
MongoDB Store Wrapper
Downloads
1
Readme
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,paging
is optional, defaults topage: 0
andpageSize: 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_id
count(query)
- finds out how many documents match the searchquery
, which is optional (null
returns 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, wherequery
determines which documents to update andupdate
can be a$set
update statement or any other MongoDB operations, such as$push
- returns nothingupdateDocument(update)
- updates a single document with the providedupdate
. Theupdate
needs to be the complete MongoDB document including_id
- returns nothing.