Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

hennessyevan/gooseberry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oclif Version Downloads/week License

Setup

Install via yarn or npm

yarn add gooseberry
OR
npm install gooseberry

Create a gooseberry config in package.json

{
  //...,
  "gooseberry": {
    "modelDir": "path/to/mongooseModels",
    "mongoURI": "mongodb://localhost:27017/{TARGET_DB_NAME}",
    "dataDir": "path/to/seedData",
    "dropDatabase": true (optional)
  }
}

Create a data file for each collection you want to seed

Data files can be written in json, yaml, ts or js but must return an array of data.

// seeds/users.json
[{
  "_id": "Jane",
  "firstName": "Jane",
  "lastName": "Doe"
}]
// seeds/posts.json
[{
  "title": "My new post",
  "author": "Jane" // use _id from seeded user
}]

_Note: if you don't specify an id or id field, gooseberry will assign it a smartID based on collection name and position in array. (e.g. above example will have post1 as its ID)

Gooseberry will recursively transform these into ObjectIDs

// seeds/users.json
[{
  "_id": ObjectID("5d6e80622037da89a22195f7"),
  "firstName": "Jane",
  "lastName": "Doe"
}]
// seeds/posts.json
[{
  "_id": ObjectID("5d6e80622037da89a22195f8"),
  "title": "My new post",
  "author": ObjectID("5d6e80622037da89a22195f7")
}]

Development

This package is new and under active development. PRs are welcome.

Check the 1.0 Roadmap for more.

Methodology

Gooseberry uses placeholder IDs to reference raw seeding data. It makes 2 passes over the data first to setup the initial data and assign IDs and then to replace the placeholder IDs with the real IDs based on your mongoose schemae.

The transformed data is then fed to mongoose.create as per usual running the validation and methods.

Usage

$ npm install -g gooseberry
$ gooseberry COMMAND
running command...
$ gooseberry (-v|--version|version)
gooseberry/0.1.6 darwin-x64 node-v12.1.0
$ gooseberry --help [COMMAND]
USAGE
  $ gooseberry COMMAND
...

Commands

gooseberry help [COMMAND]

display help for gooseberry

USAGE
  $ gooseberry help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

gooseberry seed

describe the command here

USAGE
  $ gooseberry seed

OPTIONS
  -h, --help     show CLI help
  -v, --verbose

EXAMPLES
  $ gooseberry seed
  $ gooseberry seed [collection]

See code: src/commands/seed.ts

Credits

The brilliant idea for the smart IDs comes from seedgoose. Gooseberry builds on that idea and adds mongoose validation et al. to that idea.