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

mongo-fixeeds

v1.1.2

Published

Simple fixtures and seeds loading tool for MongoDB inspired by pow-mongodb-fixtures

Downloads

4

Readme

mongo-fixeeds

Simple fixtures and seeds loading tool for MongoDB inspired by pow-mongodb-fixtures

Installation

$ npm install mongo-fixeeds

How to use

Fixtures or seeds can be placed into separate files or one in different hierarchy of directories, there are 2 modes using this tool, normal mode and custom mode, you can specify the operation for each fixtures and seeds in custom mode.

The data files must export objects with the same key name as the MongoDB collection name, and the value must be an array or object contains the documents.

suppose we have some data to load as follows:

// user-seeds.js
const users = [
  {
    _id: new ObjectId('5a714a8e7a50fd6a8ca792c0'),
    name: 'Alice',
    sex: 0,
    createdAt: new Date(),
    updatedAt: new Date()
  }
];

const accounts = [
  {
    _id: new ObjectId('5a79f976cd1f373fd5a75980'),
    user: new Objectid('5a714a8e7a50fd6a8ca792c0'),
    name: '[email protected]',
    createdAt: new Date(),
    updatedAt: new Date()
  }
];

Normal mode

All documents will be loaded by default in MongoDB using upsert behaviour, _id must be specified for each document.

exports.users = users;
exports.accounts = accounts;

Custom mode

You can specify operation or behaviour for each document in custom mode, exports data key must be an object, the documents must be set to data property as its value.

Insert operation

// users will be inserted to MongoDB except that
// those can be found by _id in MongoDB.
// if _id not specified for user document, next time running will load a new user to MongoDB
exports.users = { 
  data: users, 
  operation: 'insert',
  except: e => ({_id: e._id }) // can be omitted here, because it's the default behaviour
};

// accounts will be inserted to MongoDB except that
// those can be found by name in MongoDB.
exports.accounts = {
  data: accounts,
  operation: 'insert',
  except: e => ({ name: e.name })
};

Update operation

// accounts will be upserted to MongoDB except that
// those can be found by name in MongoDB
// excludes properties will not be modified at next running
// excludes will only work on operation 'update'
exports.accounts = {
  data: accounts,
  operation: 'update',
  excludes: ['createdAt'],
  except: e => ({ name: e.name })
};

Ignore documents

Sometimes data will only be exported to other documents to use, you can use ignore to specify the documents will not be loaded to MongoDB.

module.exports = {
  data: accountInfos,
  ignore: true
};

APIs

Loader(mongoUrl, options)

Return a new Loader instance connect to a specified MongoDB and load strategy.

  • mongoUrl, complete url with specified database name
  • options
    • excludes, properties will be ignored when data running repeatedly, only valid on operation update, all proerties will be updated every load if not set
    • operations, only insert and update supported, default value is update if not set
    • files, the files will be loaded to MongoDB, all files on that directory will be loaded if not set
const mongoFixeeds = require('mongo-fixeeds');
const options = {
  excludes: ['createdAt'],
  operation: 'update',
  files: ['seeds.js']
};
const mongoUrl = 'mongodb://localhost:27017/mongo-test';
const seedsLoder = new mongoFixeeds.Loader(mongoUrl, options);

load(dataFilesPath)

Load all specified data to MongoDB under the specified directory recursively.

const mongoFixeeds = require('mongo-fixeeds');
const seedsPath = path.join(__dirname, '../data/seeds');
const options = {
  excludes: ['createdAt'],
  operation: 'update',
  files: ['seeds.js']
};
const mongoUrl = 'mongodb://localhost:27017/mongo-test';
const seedsLoder = new mongoFixeeds.Loader(mongoUrl, options);
seedsLoder.load(seedsPath)
  .then(({ mc, uc, ic }) => {
    console.log('modifiedCount:', mc);
    console.log('upsertedCount:', uc);
    console.log('insertedCount:', ic);
  });

clearAll()

Clear all collections data in MongoDB.

seedsLoder.clearAll();

clearAllAndLoad(dataFilesPath)

Clear all collections data and then load all data into MongoDB.

seedsLoder.clearAllAndLoad(seedsPath)
  .then(({ mc, uc, ic }) => {
    console.log('modifiedCount:', mc);
    console.log('upsertedCount:', uc);
    console.log('insertedCount:', ic);
  });

License

MIT License

Copyright (c) 2018 Luky Wong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.