npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-datasource

v1.0.1

Published

Simple API providing CRUD operations on a MongoDB collection.

Downloads

5

Readme

MongoDatasource

MongoDatasource is a simple CRUD interface for a MongoDB collection that presents the datasource interface expected by the ExpressMicroserviceController.

The class will have the path to the MongoDB server, the database name, and the collection name injected through the constructor.

The methods of this class rely on the Promise technology used in the MongoDB Node.js driver version 3. Async/await from ES7 is used to handle the MongoDB Promises in this class, and the class methods all return Promises using async.

Getting Started

Prerequisites

This JavaScript class is dependent on JavaScript 2017 and async/await. Node 7.6.0 has the V8 5.5 engine, which is the earliest version to support async/await.

Installing

$ npm install mongo-datasource --save

Using

Require the class and create an instance. Dependencies are injected through the constructor: the path to the MongoDB server, the database name, and the collection name. This class provides the following interface:

  • async query(constraints) - returns a list of records, the constraints may be passed directly from express.urlencoded parsing the query string and leaving it in req.params.query.
  • async retrieve(id).
  • async insert(record) - returns the record with the the primary key updated.
  • async update(id, oldRecord, newRecord) - the oldrecord and newRecord must have a primary key that matches id (the key cannot be updated), and will use optimistic concurrency to update the record (only if the oldRecord is an exact match for all fields).
  • async delete(id).

All of these methods are asynchronous (or return Promises). If there is any error, it will be thrown (or the Promise rejected).

The reference project example using this creates an instance of the datasource for a particular database and collection, and then passes it to an instance of ExpressMicroserviceController that handles network requests:

let MongoDatasource = require('./MongoDatasource');
let ExpressMicroserviceController = require('./ExpressMicroserviceController');

class Server {

    constructor() {

        let collection = process.env.MDBCOLLECTION || 'customers'
        let database = process.env.MDBDATABASE || 'customers'
        let mongodb = process.env.MONGODB || 'mongodb://localhost:27017'
        let port = parseInt(process.env.PORT) || 8080
        
        let datasource = new MongoDatasource(mongodb, database, collection)
        let microservice = new ExpressMicroserviceController(port, datasource)

        microservice.launch()
        console.log(`Service is listening on port ${microservice.port} and connecting to ${mongodb}/${database}/${collection}`)
    }
}

exports = module.exports = new Server()

Extending

See the code for details. The class may be easily extended. The constructor and CRUD methods may be overriden to provide additional business logic as necessary. For example the constructor and methods could be extended to make sure that a sales order record has a corresponding customer record in another collection by adding the name of the second collection to the constructor, and using the second collection to verify the record in the insert method.

Built With

  • ES7

Contributing

Please read contributing.md for details on the code of conduct, and the process for submitting pull requests.

Versioning

SemVer is used for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the License.md file for details