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

tramway-connection-mongodb

v0.3.0

Published

A Provider that permits a Tramway application to connect to an MongoDB database

Downloads

5

Readme

Tramway MongoProvider is a simple Provider add-on to simplify the process of implementing calls to mongodb databases and provides an async/await wrapper on the mongodb module.

Installation:

  1. npm install tramway-connection-mongodb --save

Example project

https://gitlab.com/tramwayjs/tramway-connection-example

Documentation

Recommended Folder Structure in addition to Tramway

  • config
  • providers
  • repositories
  • entities

MongoProvider

The MongoProvider is a derived Provider which follows the same interface.

Configuration parameters

| Parameter | Type | Required | Default | Usage | | --- | --- | --- | --- | --- | | host | string | no | localhost | The host of your mongodb database | | database | string | yes | | The name of the default database to connect to | | port | number | no | 27017 | The port for the database | | username | string | no | | The connection username for the database | | password | string | no | | The connection password for the database | | hosts | array: [{host: string, port: number}] | no | | The connection password for the database | | options | Object | no | | Options object that would be passed to Mongo at connection time | | ssl | bool | no | false | Determines if the connection protocol should enforce ssl via mongodb+srv:// |

Getting started

It is recommended to make a config file with the core mongodb configuration and make an extension class to handle it before injecting that extension class into the Repository to work with the rest of Tramway's built-in features. The alternative to the extension class is calling the config parameters with the Repository every time the repository is used instead of just importing the provider to the repository.

In your config folder add a 'mongodb' file.

mongodb.js:

export default {
    "host": "127.0.0.1",
    "port": 27017,
    "database": "base",
};

Note, the same can be achieved using your environment variables and passing the declaration from your instantiated .env to the placeholders in the above example.

To get the most of it, pass the provider to your Repository class.

import MongoProvider, {repositories} from 'tramway-connection-mongodb';
import options from '../config/mongodb.js';

const {MongoRepository} = repositories;

new MongoRepository(new MongoProvider(options), new Factory());

The following can also be easily achieved with dependency injection using tramway-core-dependency-injector.

Exposed Methods with this Library

Provider

Note, the extended MongoProvider expects an additional parameter collectionName on most methods. Using the MongoRepository handles this for you. In addition, bulk inserts are possible.

| Function | Availability | | ----- | ----- | | getOne(id: any, collectionName: string) | Available | | getMany(ids: any[], collectionName: string) | Unavailable | | get(collectionName: string) | Available | | find(conditions: string/Object, collectionName: string) | Available | | has(id: any, collectionName: string) | Available | | hasThese(ids : any[], collectionName: string) | Unavailable | | count(conditions: any, collectionName: string) | Available | | create(item: Entity/Object, collectionName: string) | Available | | createMany(item: Entity/Object[], collectionName: string) | Additional, creates a transaction for bulk inserts | | update(id: any, item: Entity/Object, collectionName: string) | Available | | delete(id: any, collectionName: string) | Available | | deleteMany(ids : any[], collectionName: string) | Unavailable | | query(query: string/Object, values: Object) | Available |

Repository

| Function | Usability | | --- | --- | | exists(id: any) | Usable | | getOne(id: any) | Usable | | get() | Usable | | create(entity: Entity) | Usable | | createMany(entities: Entity[]) | Additional | | update(entity: Entity) | Usable | | delete(id: any) | Usable | | find(condtions: string/Object) | Usable | | getMany(ids: any[]) | Unusable | | count(conditions) | Usable |

Advanced Connection Options

For most basic connections, a mongo.js file like this will be sufficient:

export default {
    "host": "127.0.0.1",
    "port": 27017,
    "database": "base",
};

If the basic connection had authentication:

export default {
    "host": "127.0.0.1",
    "port": 27017,
    "database": "base",
    "username": "user",
    "password": "password",
};

However, Mongo can also be set up as a Replica Set or Sharded Cluster.

To connect to a replica set:

export default {
    "database": "base",
    "hosts": [
        {"host": "db0.example.com", "port": 27017},
        {"host": "db1.example.com", "port": 27017},
        {"host": "db2.example.com", "port": 27017},
    ],
    "options": {
        "replicaSet": "replicaSetName"
    }
};

...with authentication:

export default {
    "database": "base",
    "hosts": [
        {"host": "db0.example.com", "port": 27017},
        {"host": "db1.example.com", "port": 27017},
        {"host": "db2.example.com", "port": 27017},
    ],
    "options": {
        "replicaSet": "replicaSetName"
    },
    "username": "user",
    "password": "password",
};

To connect to a sharded cluster:

export default {
    "database": "base",
    "hosts": [
        {"host": "dbs0.example.com", "port": 27017},
        {"host": "dbs1.example.com", "port": 27017},
        {"host": "dbs2.example.com", "port": 27017},
    ],
};

...with authentication:

export default {
    "database": "base",
    "hosts": [
        {"host": "dbs0.example.com", "port": 27017},
        {"host": "dbs1.example.com", "port": 27017},
        {"host": "dbs2.example.com", "port": 27017},
    ],
    "username": "user",
    "password": "password",
};