sequelize / cli

The Sequelize CLI
MIT License
2.53k stars 525 forks source link

where can I found the seeding documentation #225

Closed PavelPolyakov closed 7 years ago

PavelPolyakov commented 9 years ago

Hi,

Where I can find the documentation for the methods which I can use during seeding? There is no such here: http://docs.sequelizejs.com/en/latest/api/sequelize/

I found that I can use bulkInsert. So I can do something like:

queryInterface.bulkInsert('users',[{
      name: 'test',
      email: 'test@gmail.com',
      role: 'USER',
      created_at: new Date(),
      updated_at: new Date()
    }])

Which methods I can also use in order to seed the database?

However, assuming that I need to know the user id's to make the further queries. How can I receive the ids? I came up with the solution to use queryInterface.sequelize.query(), however I think something more convenient should be available.

Any thoughts?

Regards,

sdepold commented 8 years ago

There is currently only sequelize help:db:seed. it won't tell you much though :( seeds are basically identical to migrations but use a different folder and meta table

PavelPolyakov commented 8 years ago

@sdepold Are there any methods despite bulkInsert which were designed especially for seeding?

sdepold commented 8 years ago

There is this part of the documentation http://docs.sequelizejs.com/en/latest/docs/migrations/#functions. You can use every function that is specified there. Additionally here is the code for the queryInterface https://github.com/sequelize/sequelize/blob/master/lib/query-interface.js

it seems that there is no documentation about the interface itself in the docs. :( Here is insert for example https://github.com/sequelize/sequelize/blob/master/lib/query-interface.js#L480

gesposito commented 8 years ago

By looking at the code (v2.2.1) I found that we can use seed:create --name seed-name as for Migrations to get a template for the Seed file.

Inside the Seed file you're going to execute some queryInterface functions like: queryInterface.bulkInsert queryInterface.bulkDelete

Then we can run sequelize db:seed to run the Migration. Not much, but a starter.

stuartZhang commented 8 years ago

There is a statement "queryInterface.bulkUpdate" as well. I have discovered it in the source code.

markelog commented 8 years ago

Could you add docs for it please?

sdepold commented 8 years ago

We could actually add some docs to the website :D

markelog commented 8 years ago

@sdepold i would be very appreciative, with conjunction of https://github.com/sequelize/sequelize/issues/5615 had to spend like an hour before i figure out who to work with this module

sgnl commented 8 years ago

FWIW: I dislike how the paragraph intro for the section http://docs.sequelizejs.com/en/latest/docs/migrations/#functions says:

"...you will have access to most of already introduced functions"

Where was it introduced? Definitely not on this page.

If this section is all about documenting the functions available on queryInterface then all the functions should be clearly listed here.

jez9999 commented 7 years ago

Why did this get closed? I just came across bulkInsert and there is still no documentation for it.

jimthedev commented 7 years ago

I wasn't have much luck with seeding so I created https://npm.im/seedquelize to make this a bit easier. It has an async programmatic api. Just pass it some an array of items and a model for them to be created as.

luispcosta commented 7 years ago

You should do just this:

> sequelize init:seeders --seeders-path=...
> sequelize seed:create --name CreateData --seeders-path=...

Note that the flag seeders-path is optional. If you omit it, this will create a folder called seeders inside your root sequelize app.

Now there should be a file called ....-CreateData.js inside your seeders folder with the following structure:

'use strict';
module.exports = {
  up: function (queryInterface, Sequelize) {

  },

  down: function (queryInterface, Sequelize) {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkDelete('Person', null, {});
    */
  }
};

You can now create data by doing:

// in the up function:
// Assuming you have a table called Users
// Create an array of objects where you create fake data
return queryInterface.bulkInsert('Users', usersArray)
eino-makitalo commented 7 years ago

It seems that documentation is still missing in readthedocs

eino-makitalo commented 7 years ago

what are allowed options for bulkInsert. It does not create timestamps like sync etc....

Juraci commented 7 years ago

No documentation yet..

bschlenk commented 7 years ago

What is preventing seeders from having access to the models? It's kind of inconvenient to have to manually create ids and created/updated timestamps when the model would have done that automatically. Could a solution like seedquelize linked above be built in?

pajtai commented 7 years ago

I just require my models in and use those instead, since bulkInsert was erroring out.

Noah-Huppert commented 7 years ago

Still have absolutely no idea what seeders are or where to find any documentation...

Best guess it that seeders are some sort of interface that adds test data into the db.

GuillaumeLeclerc commented 7 years ago

This should definitely not be closed. Having to read the code to figure out what the arguments are is not something i would expect from a 10k+ stars project.

gabeidx commented 7 years ago

+1 for reopening this issue.

I was dumbfounded when I saw a seeders folder on my project after running sequelize init, but couldn't find any reference to it in the docs.

gabeidx commented 7 years ago

What would be a good example of seeder file, so we the community can quickstart a PR for this?

sushantdhiman commented 7 years ago

I will try to add an example in https://github.com/sequelize/express-example soon, reopening this issue as documentation is certainly lacking on this topic

b-camacho commented 7 years ago

Could really use those queryInterface docs

vukanac commented 7 years ago
module.exports = {
  up: function (queryInterface, Sequelize) {

Any suggestions for what we can use Sequilize parameter received in up and down functions? And also any suggestions, is it possible to remove it as a linter annoys me - you have it, you don't use it?

dagumak commented 7 years ago

Did anyone figure this out or does anyone have a link to the line in code?

krishangupta commented 7 years ago

Is there any way to ensure a seed file only gets run once (like migration files)? I noticed they dont get written to the SequelizeMeta table.

vukanac commented 7 years ago

You have to ensure that, either calling seeder from migration or by implementing update script, or manually.

On 2 September 2017 at 02:19, Krishan Gupta notifications@github.com wrote:

Is there any way to ensure a seed file only gets run once (like migration files)? I noticed they dont get written to the SequelizeMeta table.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sequelize/cli/issues/225#issuecomment-326708855, or mute the thread https://github.com/notifications/unsubscribe-auth/ACRhT5cyMTM9t_Cv_qGIYvfoYjLPKjOFks5seJ8OgaJpZM4Gk9pw .

--

Vladimir Vukanac vukanac@gmail.com +381(64)1526688 https://rs.linkedin.com/in/vladimirvukanac

MikeyVelt commented 7 years ago

@Noah-Huppert seeds can be used for test data, however you can also use seeds to insert initial data on a fresh project. For example if for some reason you have a table called countries in your new project and you know for a fact you will have certain countries in there, you can just seed them, it is not like the name of the country is changing anytime soon :) That way after you are deploying your project to production, you seed your database and you have all the data you need.

Been struggling with the way Sequelize seeds aswell. I came from the PHP framework Laravel before and they would "remember" what you have seeded, so that the next time you call the seed command it wouldn't go through all the seeds again, but rather those that are new.

The way I have been doing it with Sequelize is by truncating the table before calling "db:seed:all". That way data is not duplicated.