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

seedgoose

v2.0.2

Published

Mongoose data seeding with smart id references tracking.

Downloads

334

Readme

Seedgoose

NPM version Build Status Dependency Status DevDependency Status License PR Welcome

Mongoose data seeding with smart id references tracking.

Introduction

Seeding data with id references is always hard. That's why I created this package. Seedgoose recursively goes through your model schemas to setup smart id references tracking for you.

Design Concept

The node.js ecosystem lacks a high quality data seeding tool and fixture loading tool with id references support. I was shocked by this. This kind of tools exist for years in Ruby on Rails world. It's the base functionality for data seeding and unit test fixturing. In our node.js world, every team and developer has their own time consuming and low quality solutions to seed data. A tool is urgently needed to save our precious time and improve data seeding experience.

Without Seedgoose, say you have following data

// authors.json
{
  "name": "Chris Berg",
  "posts": ["84bd43d8a0ffcde34567abcd", "84bd43d8a0ffcde34567abce", "84bd43d8a0ffcde34567abcf"]
}
// posts.json
[
  {
    "_id": "84bd43d8a0ffcde34567abce",
    "title": "Heal the world",
    "content": "Heal the world, make it a better place."
  },
  {
    "_id": "84bd43d8a0ffcde34567abcd",
    "title": "I have a dream",
    "content": "I still have a dream, a dream deeply rooted in the American dream."
  },
]

It's obscure and not descriptive. A lot of patient and time are consumed just to make sure data hooks. Things get even worse when project goes larger and larger and the obscure seed data become larger and larger, hard to read, hard to modify.

This is where Seedgoose comes in. With Seedgoose, we can rewrite these data like this:

// authors.json
{
  "name": "Chris Berg",
  "posts": ["i have a dream", "heal the world", "a didsummer night's dream"]
}
// posts.json
[
  {
    "_id": "heal the world",
    "title": "Heal the world",
    "content": "Heal the world, make it a better place."
  },
  {
    "_id": "i have a dream",
    "title": "I have a dream",
    "content": "I still have a dream, a dream deeply rooted in the American dream."
  },
]

Seedgoose recursively goes through your model schemas, and trying to find out what you are referencing and set the relationships up for you. In this way, you can define the identity of a model in any way you like. It's not restricted to strings, actually number or even boolean value is also fine if it make sense.

According to this data loading nature, seedgoose can load program files as long as it returns a object or array.

const map = require('lodash/map');
const times = require('lodash/times');
const flatten = require('lodash/flatten');
const faker = require('faker');

module.exports = flatten(map(["jack", "queen", "king"], (a) => times(3, (i) => ({
  "_id": `${a} post ${i + 1}`,
  "author": a,
  "title": faker.random.word(),
  "content": faker.lorem.paragraphs()
}))));

Installation

Install Seedgoose with npm.

npm install seedgoose

Usage

To seed data into database, you need to do three things:

  1. Write your data files
  2. Let Seedgoose know where your data files and model files are
  3. Run a seeding command

Write your data files

With Seedgoose, you don't need to write your own programs or scripts to seed data. Seedgoose handles insertions, updations and deletions for you. You just write your data files. Although you don't need to write program or script files, but you can write dynamic data files in programming language that represent dynamic data.

  • Write your data in JSON.
{
  "jack": {
    "name": "Jack Jill",
    "age": 20
  },
  "bill": {
    "name": "Bill John",
    "age": 25
  }
}
  • Write your data in CSON.
jack:
  name: 'Jack Jill'
  age: 20
bill:
  name: 'Bill John'
  age: 25
  • Write your data in YAML.
jack:
  name: Jack Jill
  age: 20
bill:
  name: Bill John
  age: 25

You can also write data files in javaScript and TypeScript by exporting a data array or object.

Configurations

Name your data files by database collection names. For example, if you have a model file named User, then you should name data file users dot whatever the type is. We follow the convention over configuration best practice to save configuration time and suppress arguments.

Create a Seedgoose configuration file like this. You can write the configuration file in any type, too.

// .seedgooserc.js
module.exports = {
  modelBaseDirectory: 'models', // model directory name
  models: '**/*.js', // model matcher
  data: 'data', // data directory name
  db: 'mongodb://localhost:27017/url-to-db' // db connection url
};

If you prefer to include configurations in package.json, you can write them under the seedgoose field.

{
  "seedgoose": {
    "modelBaseDirectory": "models",
    "models": "**/*.js",
    "data": "data",
    "db": "mongodb://localhost:27017/url-to-db"
  }
}

The command line interface

The Seedgoose command receives arguments and options in the following style.

seedgoose [command] [collections...] [options...]

To seed data into database while keep existing untouched, run

seedgoose seed

To seed data into database and updating existing on conflict, run

seedgoose reseed

To remove seeded records, run

seedgoose unseed

Only run seeding on some collections, run

seedgoose seed users posts

To run command and silent output, run

seedgoose seed --silent

Help & Issues

Please open an issue if you encountered troubles and problems.

License

MIT © Zhang Kai Yu