node-mongo-collections-loader
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Node JS Mongodb collections loader

This library loads a Node.JS instance for each of your Mongodb schemas including an access object for each collection.

Then you can keep in memory those "Singleton" objects and use each one to access to each collection.

It uses Node.JS Mongodb native driver.

Install

npm install node-mongo-collections-loader

Usage

Import library

const {MongoCollectionsLoader} = require('node-mongo-collections-loader');

Define your schemas and your collections.

const instances = [
  {
    id: 'userSchema',
    collections: [
      'Users',
      'Access'
    ],
  },
  {
    id: 'myBusinessSchema',
    collections: [
      'Quotes',
      'Orders',
    ],
  },
];

Configure your Mongodb Auth object (Or retrieve it from secret)

{
  userSchema: {
    url: mongodb://myuserserver:27017?userSchema,
    options: {
      readPreference: 'secondaryPreferred'
    },
  },
  myBusinessSchema: {
    url: mongodb://mybusinessserver:27017?myBusinessSchema,
    options: {
      readPreference: 'secondaryPreferred'
    },
  },
}

Then, load collections (add an async function if you are in a sync function) ... and you can use collections singletons with Mongodb native driver's Collection functions

(async () => {
  const dao = await dao(app);
  dao.userschema.Users.find(...)
  dao.userSchema.Access.update(...)
  dao.myBusinessSchema.Quotes.find(...)
})();

License

MIT

Package Sidebar

Install

npm i node-mongo-collections-loader

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

12.5 kB

Total Files

11

Last publish

Collaborators

  • pilerou