1.0.4 • Published 5 years ago

@sfast/mongomanager v1.0.4

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
5 years ago

MongoManager - simple structured way to manage your MongoDB connections and models in a team

Why you need mongoManager when there is mongoose ?

MongoManagar provides the team members to work with the same structure when they have to deal with multiple mongo connections.

  • monitor errors
  • create models based on schemas and handle reconnection
  • provide access to various models in different places of applicaion

If you use just one database connection for mongo server than maybe you don't need this. Lets keep things simple.

But imagine you have a SASS product where you have seperate databases for each customer (i.e with their users, logs etc ...), same server different databases or different servers for different databases.

Practicaly each time when you are implementing multiple connections to mongo(with mongoose or other package), you are implementing connection and then adding another after some time( when you need it) etc ... and at the end you'll build a 'mongomanager' to keep things consistent across the team.

With mongoManager you need to create your schemas and connections and label them. Once connection is initialized the mongoose models based on matched labels (schemas <-> connections) will be created. You can access mongoose model 'A' of connection with id 'C' with i.e let thisIsMongooseModel = mongomanagerInstance.getModel(<C>, <A>)

Type coverage

typewiz coverage tsconfig.json

29 July 2019
801 of 862 types are known.
Your type coverage is: 92.92%

Installation

$ npm i @sfast/mongomanager

How to use?

MongoManager

import { MongoManager } from '@sfast/mongomanager';

// options
let debug = true
let logger = console
let mongooseOptions = {}
// ** this is used to set mongoose options for mongomanager

/* this is the default options if nothing is provided 
let mongooseDefaultOptions = {
  autoIndex: false, // Don't build indexes
  reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  keepAliveInitialDelay: 120,
  promiseLibrary: Promise // can be BlueBird
}
*/

let managerOptions = {
    logger, 
    debug, 
    mongooseOptions,
    // ** in case you want to provide your own mongoose version than provide this
    // mongoose
}

const mongoManager = new MongoManager(managerOptions);

// ** creates a connection with id and label or labels array (we mark connections with labels)
await mongoManager.connect({ id, url, config, label})

// ** registers all schemas that we need for connections with label or labels (we mark schamas with labels)
// ** schema is mongoose schema
// ** - parent will create a descriminator
mongoManager.registerSchema({ name, schema, pk = '_id', parent, options, label });

// ** gets the mongo model to work with
mongoManager.getModel(id, modelName);

MongoConnection

// ** Example of creating mongo connection and registering model via connection

// options
let debug = true
let logger = console

let mongooseOptions = {}
// ** this is used to set mongoose options for mongomanager

let mongoOptions = {
    logger, 
    debug, 
    mongooseOptions,
    // ** in case you want to provide your own mongoose version than provide this
    // mongoose
}

const mongoConnection = new MongoConnection(mongoOptions);

mongoConnection.connect(url, options);

// ** Registers the model based on the schema (parent model if exists should be created already)
mongoConnection.registerModel({ name, schema, options, parent });

// ** gets the name which you gave to register model, and returns registered model 
mongoConnection.getMongooseModel(name)

API

Methods of MongoManager

Methods of MongoConnection

Methods of MongoSchema

For more documentation please generate a Typedoc under docs folder

npm i 
npm run doc

Examples

import { Schema } from "mongoose";
import { MongoManager } from "@sfast/mongomanager";


const mongooseDefaultOptions = {
  autoIndex: false, // Don't build indexes
  reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  promiseLibrary: Promise,
  keepAliveInitialDelay: 100,
};

const mm = new MongoManager({
  mongooseOptions: mongooseDefaultOptions,
  debug: true,
});

const personSchema = new Schema({
    firstname: String,
    lastname: String,
  },
  { collection: "Person", discriminatorKey: "dkey" },
);

const personSchemaMeta = {
  name: "Person",
  schema: personSchema,
  label: "main",
};

const doctorSchema = new Schema({
    hospital: String,
  },
  { collection: "doctors" },
);

const doctorSchemaMeta = {
  name: "Doctor",
  schema: doctorSchema,
  options: { collection: "doctors" },
  label: "main",
  parent: "Person",
};

const run = async () => {
  const managerConfig = {
    id: "main",
    url: "mongodb://localhost:27017/mongotest",
    label: "main",
  };


  mm.registerSchema(personSchemaMeta);

  await mm.connect(managerConfig);

  mm.registerSchema(doctorSchemaMeta);

  const Person = mm.getModel("main", "Person");

  const Doctor = mm.getModel("main", "Doctor");

  if (Person) {
    await Person.create({ firstname: "pfirstName", lastname: "plastName", });
    console.log("PERSON CREATED");
  }

  if (Doctor) {
    await Doctor.create({
      firstname: "dfirstName",
      lastname: "dlastName",
      hospital: "hospitalName",
    });

    console.log("DOCTOR CREATED");
  }
};

run();

Test

The package is well tested and has ~ 93% test coverage

// ** run a mongo server and then run 
npm run test

Contributors

License

MIT

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

0.2.1

5 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago