Add support for seeding a database
See original GitHub issueI am opening this ticket as a place for discussion around possible ways to implement command line seed support (ala rails db:seed) This need is coming up in a few tickets #58 #48.
So to start, in my situation I was porting an older small app of mine that was backed by mongo. I have a command line node tool to pump in seed data when I stood up new instances. During my port to nodal, which I was doing as a public example of a nodal app, I really wanted a single db cli command (see #50) to prepare, migrate and seed the sample data so that someone could fork the repo and use the built in deploy to heroku button and have a running app with real data
So my first thought is do we have a large json doc that can be used to seed models (like schema.json) or do we actually generate a real module that can be required and run to allow for model validations, model relations etc (ala rails)
so for a quick and dirty comparison of the thoughts (excuse errors, thinking as a I go here)
a seed.json (which has to have proper ordering)
{
"user": {
"id": 1,
"username": "mark",
"email": "mlussier@gmail.com"
},
"tweet": {
"id": 1,
"body": "Hey There",
"user_id": 1
}
}
or in js (remember, just a rough idea)
'use strict';
const Nodal = require('nodal');
const User = Nodal.require('app/models/user.js');
const Tweet = Nodal.require('app/models/user.js');
let markUser;
User.create({ username: "mark", email: "mlussier@gmail.com"}, (err, model) => {
markUser = model;
});
Tweet.create({ body: "Hey There", user_id: markUser.id}, (err, model) => {
});
or even
'use strict';
const Nodal = require('nodal');
const User = Nodal.require('app/models/user.js');
const Tweet = Nodal.require('app/models/user.js');
User.create({ username: "mark", email: "mlussier@gmail.com"}, (err, model) => {
Tweet.create({ body: "Hey There", user_id: model.id}, (err, model) => {
});
});
Okay seed (no pun intended) laid, thoughts?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Was thinking in the shower it would be good to either have the seed.json support env’s (like db.json)
or if we want to be crazy, make the seed a js file so that we can do nutty stuff like generating fake test data
obviously that js example could be way better, but it illustrates…
Added in:
https://github.com/keithwhor/nodal/commit/b4eb007de6d9d71185d2477953a0167fa66bfcbe
😃