mongoose-cachedbulkinsert

0.1.1 • Public • Published

mongoose-cachedBulkInsert

Insert docs into your collection every n documents and/or every n seconds. (n is up to you!)

Build Status

Coverage Status

NPM

Usage

test.js

#!/usr/bin/env node
'use strict';
 
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const cachedBulkInsert = require('mongoose-cachedbulkinsert');
const DB = 'test';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { useNewUrlParser: true, useUnifiedTopology: true };
 
const schema = new Schema({
  name: String
});
 
let array = [];
 
const pluginOptions = {
  array,
  wait: 5000,
  cycles: 3,
  intervalRes: (res) => {
    if (res && res.insertedCount) {
      console.log(`intervalBasedInsert: ${res.insertedCount}`);
    }
  }
};
 
schema.plugin(cachedBulkInsert, pluginOptions);
 
const Test = mongoose.model('test', schema);
 
async function run() {
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
 
  for (let i = 0; i < 100; i++) {
    await Test.cachedInsert({ name: `test${i}` })
      .then(res => {
        if (res && res.insertedCount) {
          console.log(`lengthBasedInsert: ${res.insertedCount}`);
        }
      });
  }
 
  for (let i = 0; i < 19; i++) {
    await Test.cachedInsert({ name: `test2${i}` });
  }
 
  await forMS(6000);
 
  await connection.close();
}
 
run();
 
function forMS(n) {
  return new Promise(res => {
    setTimeout(res, n);
  });
}

Output

$ ./test.js
lengthBasedInsert: 20
lengthBasedInsert: 20
lengthBasedInsert: 20
lengthBasedInsert: 20
lengthBasedInsert: 20
intervalBasedInsert: 19
$

Package Sidebar

Install

npm i mongoose-cachedbulkinsert

Weekly Downloads

0

Version

0.1.1

License

Apache 2.0

Unpacked Size

51.5 kB

Total Files

13

Last publish

Collaborators

  • lineus