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-seed-faker

v1.0.3

Published

A tool with FAKER.js power to quickly populate your mongo db from a .json file.

Downloads

184

Readme

MONGO-SEED-FAKER

A tool to easily populate mongodb database with power of faker.js

Installation

npm i mongo-seed-faker

Usage

const mongoSeedFaker =  require("mongo-seed-faker");

const mongoUri = 'mongodb://localhost:27017/test';
const dbName = 'test';
const seedData = [{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	// if want to populate same structure of data multiple times
	template: { name: '@faker.name.findName()', email: 'Some static value' },
	// how many times to populate data specified by 'template' 
	howMany: 10,
	
	// We can also pass mulitple documents and all document structure can be differs
	data:[{name: '@faker.name.findName()', email: 'Some static value' },
	{name: '@faker.name.findName()', email: 'random different email' },]
}];

// or if you want to use json file replace seedData parameter with seedDataFromFile in mongoSeedFaker function below
const seedDataFromFile = JSON.parse(require('./seedData.json'));

mongoSeedFaker(seedData, dbUri, dbName);

Function mongoSeedFaker params

| Param |Data type |Description | |----------------|-------------------------------|-----------------------------| |seedData|Array<object> | Seed data | |dbUri |String |Mongo connection uri | dbName | String| Database name | databaseOption| Object| mongodb connect function options (availble options)

Seed Data Object Keys

| Key |Data type |Description | |----------------|-------------------------------|-----------------------------| |collectionName|STRING e.g. 'users' | Name of mongodb's collection | |seedOnlyIfEmpty |BOOLEAN e.g. true |Should populate if collection have some data already? | |template |OBJECT e.g. { name: 'Test'}|Structure to follow when populating database| |howMany|NUMBER e.g. 10 | How many documents to make with template stucture | data |ARRAY OF OBJECTS e.g. [{name: 'Test'}]| Array of documents that needs to be populated

How to use faker.js inside seedData

We can use faker.js in both 'template' and 'data' in seedData object like this:- name: '@faker.name.firstName()'

Just add '@' on front of faker.js function and make it string.
e.g. faker.name.firstName() should be '@faker.name.firstName()' so that it parses properly.

click here to see availble faker.js methods

How to populate mongodb objectId and ISODate

We can also populate mongodb objectId and ISODate through seedData object. Just pass them as string. e.g. _id: "ObjectId('5c86135dff97166fd7e28b46')", createdAt: "ISODate('2019-03-11T07:49:37.640Z')"

Some seed data examples:

// multiple users data with some field from fakerjs
seedData = [{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	data:[{name: 'John', email: '[email protected]' },
	      {name: '@faker.name.findName()', email: '[email protected]' },]
}];
	
// Populate 10 users with a static role and dynamic fakerjs's names
const seedData = [{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	template: { name: '@faker.name.findName()', email: '[email protected]', role:'user' },
	howMany: 10,
}];

// Both template and data at same time
seedData = [{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	data:[{name: 'John', email: '[email protected]' },
		  {name: 'Rock', email: '[email protected]' },],
	template: { name: '@faker.name.findName()', email: '[email protected]', role:'user' },
	howMany: 10,
}];


// Seeding for multiple collections
seedData = [{
	collectionName: 'authors',
	seedOnlyIfEmpty: true,
	data:[{name: 'Albert', email: '[email protected]' },
		  {name: 'Ram', email: '[email protected]' },],
	template: { name: '@faker.name.findName()', email: '[email protected]', role:'user' },
	howMany: 10,
},{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	data:[{name: 'John', email: '[email protected]' },
		  {name: 'Rock', email: '[email protected]' },],
	template: { name: '@faker.name.findName()', email: '[email protected]', role:'user' },
	howMany: 10,
}];


// Passing  ObjectId and ISODate
seedData = [{
	collectionName: 'users',
	seedOnlyIfEmpty: true,
	data:[{_id: "ObjectId('5c86135dff97166fd7e28b46')", name: 'John', createdAt: "ISODate('2019-03-11T07:49:37.640Z')"},
	      {name: '@faker.name.findName()', email: '[email protected]' },]
}];

License

mongo-faker-seed is distributed under MIT license.

That's it

Note: Developement is in progress and there are still more features to come :).