yarn add gooseberry
OR
npm install gooseberry
{
//...,
"gooseberry": {
"modelDir": "path/to/mongooseModels",
"mongoURI": "mongodb://localhost:27017/{TARGET_DB_NAME}",
"dataDir": "path/to/seedData",
"dropDatabase": true (optional)
}
}
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 ObjectID
s
// seeds/users.json
[{
"_id": ObjectID("5d6e80622037da89a22195f7"),
"firstName": "Jane",
"lastName": "Doe"
}]
// seeds/posts.json
[{
"_id": ObjectID("5d6e80622037da89a22195f8"),
"title": "My new post",
"author": ObjectID("5d6e80622037da89a22195f7")
}]
This package is new and under active development. PRs are welcome.
Check the 1.0 Roadmap for more.
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.
$ 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
...
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
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
The brilliant idea for the smart IDs comes from seedgoose. Gooseberry builds on that idea and adds mongoose validation et al. to that idea.