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

@gigasource/mongoose-sync

v1.0.1

Published

This module is a Mongoose middleware for syncing between 2 databases by replicating actions executed on the primary database

Downloads

1

Readme

This module is a Mongoose middleware for syncing between 2 databases by replicating actions executed on the primary database

1. Features:

  • Support syncing for the following actions:
    • Create
      • Model.create (single document)
      • Model.create (array of documents)
      • document.save
    • Update
      • Model.findOneAndUpdate
      • Model.findByIdAndUpdate (this uses 'findOneAndUpdate' hook)
      • Model.update (with option multi=false - default is false)
      • Model.updateOne
      • Model.updateMany
    • Delete
      • document.remove (Model.remove won't work with post remove middleware)
      • Model.remove (not supported by Mongoose so this module uses a workaround). Note: this use 'model-create' as the hook name
      • Model.findOneAndRemove
      • Model.findByIdAndRemove (this uses 'findOneAndRemove' hook)
  • Job queue: actions are pushed into a job queue for execution.
  • Sync interval: user can specify an interval for syncing. When the job queue has emptied all the tasks, the queue will be paused and resumed on the next interval.
  • Retries: when a task fails, job queue will pause and retry the task on the next interval
  • MongoDB storage for job queue: tasks are stored in a collection in the primary database

2. Tests:

Unit tests for retry logic is currently not implemented on 'master' branch because it requires generating random errors inside queue processing logic but it's inappropriate to put error-generating logic in production code

3. Usage:

The job queue is mandatory for the module to work so user must initialize the queue first using this function: (/src/middleware/index.js):

module.exports.init = async function (syncDbConnectionUrl, syncDbOptions, syncInterval, enableMongoStore) {
...
}

// Example:
const {init} = require('/lib');
await mongoose.connect('mongodb://localhost:27017/', {dbName}); // this must be called first
await init('mongodb://localhost:27017/', {dbName, user, pass}, 15 * 60 * 1000); // enableMongoStore = true by default

It is required that user calls this after mongoose.connect to primary db and before creating Mongoose schemas and models

Now apply the middleware to Mongoose Schema:

const {applySyncMiddleware} = require('/lib');
const carSchema = new Schema({
  model: {
    type: String,
    trim: true,
  },
  year: Number,
  weight: Number,
});

const saveSyncCondition = function (car) {
  return car.year > 2000;
};

const updateSyncCondition = function (updatedCar) {
  return updatedCar.weight > 100;
};

// Add middleware here
// conditions are functions returning true or false
applySyncMiddleware(carSchema, {
  create: saveSyncCondition, // condition for actions related to 'create'
  update: updateSyncCondition, // condition for actions related to 'update'
});

// applySyncMiddleware(carSchema, syncCondition); // if 1 function is passed, it is used for both 'create' and 'update'
// -------------------

const model = mongoose.model(schemaName, carSchema);