nseed
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

nSeed

Logo

The Node.Js Database Seeder


Overview

nSeed is an Open-Source CLI Tool for NodeJS used to generate and seed your database with fake data for testing purposes, powered by @faker-js/faker.

Stop wasting time writing seeders with hard-coded data each time you want to test your new application; just make a template and we'll handle the rest!

Installation

You can install it locally, globally or as a dev dependency with both npm and yarn:

  • local:

    npm install nseed or yarn add nseed

  • dev:

    npm install --save-dev nseed or yarn add nseed --dev

  • global:

    npm install -g nseed or yarn global add nseed

Basic Usage

We want nSeed to be an easy and intuitive tool, so having tons of functionalities was not intended apart from what it promises:

" Giving a connection string, a template and the amount of documents to be created, this tool will generate fake data and seed it into your database. "

The most basic usage is as it follows:

nseed [connection string] --db [database name] --tmpl [path to template] --amount [amount]

If you installed it as a dev dependency you can use it with npx:

npx nseed [connection string] --db [database name] --tmpl [path to template] --amount [amount]

Note

Currently we only support MongoDB databases, but we plan to expand it to other kind of databases such as PostgreSQL, MySQL, CouchDB, Redis, etc.

CLI

Options Alias Description
--db -d Flag used to specify the name of the database to be seeded.
--collection -c A string denoting the name of the collection to be generated.
--tmpl -t A Flag to specify the path for the templates to be used.
--amount -a The number of documents to be generated.
--del N/A Indicates that the database must be dropped before seeding.
--version -v Displays the current version.
--help -h Displays help.

Note

The CLI will ask you for any non-provided required argument.

The path for the template MUST be a relative path. (i.e: ./foo/bar.js)

Example:

nseed mongodb://localhost:27017/ -d Test -c Users -t './template.js' -a 2000 --del

Templates

nSeed templates are js/ts files which exports an unnamed function which returns the template object. The structure of the templates should be as it follows:

module.exports = () => {
	return {
		"foo": "bar1",
		"foo": "bar",
		"foo": ["bar"],
		"foo": {
			"foo": "baz",
			"foo": "baz",
			"foo": "baz"
		}
	}
}

The values on the template can be any kind of valid JSON Data Type; but in order to use autogenerated data with the @faker-js/faker methods you must import and instantiate the Types class as follows:

const { Types } = require('nSeed')
const types = new Types()

module.exports = () => {
	return {
		{
			"firstName": types.faker.name.firstName(),
			"lastName": types.faker.name.lastName(),
			"job": types.faker.name.jobTitle(),
			"address" :{
				"line1": "529 14th St NW",
				"line2": "Apt. 742",
				"state": "Florida",
				"zip": 33054,
			},
			"interests": ["Sports", "Videogames", "Technology"],
			"activeUser": true
		}
	}
}

The Types class has an instance of the @faker-js/faker package and some other utility functions you may find useful. You can use any faker method here so you may want to check their documentation to see what you can do!

Here is a list of all functionalities so far:

  • id( value: string | undefined ): It generates a MongoDB ObjectId, you can give it one as a string on the parameters or it will create one randomly.
  • oneOf( any[] ): Randomly picks one of the values of a given array (the array on the parameters is required).
  • newDate( string | undefined ): It generates a date based on the given date-like string. If no value is passed as parameter it will return the current date.

Example:

const { Types } = require('nSeed')
const types = new Types()

module.exports = () => {
	return {
		status: types.oneOf(['active', 'inactive', 'banned', 'bamboozed']),
		firstName: types.faker.name.firstName(),
		lastName: types.faker.name.lastName(),
		gender: types.faker.name.gender(),
		job: types.faker.name.jobTitle()
	}
}

Note

You must have nSeed installed as a dependency on the project in order to import it into your template.

Configuration

You can configure nSeed with a nseed.config.json file; this gives you the capacity to generate specific configuration per each project you and your team are working on, making it suitable for container environments such as Docker.

All the options available on the CLI can be configured through this file:

{
	"url": "mongodb://<user>:<password>@localgost:27017/",
	"collections": [
		{
			"name": "users",
			"template": "./path/to/template",
			"amount": 70
		},
		{
			"name": "activities",
			"template": "./path/to/template"
		},
		{
			"name": "locations",
			"template": "./path/to/template"
		}
	],
	"amount": 125
}

License

Copyright (c) 2022 Facundo Carbonel / nSeed

This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.

Package Sidebar

Install

npm i nseed

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

29.9 kB

Total Files

7

Last publish

Collaborators

  • f2bear