Run Seeders with Node JS and Sequelize - Rest API with NodeJS and MySQL (2020)

preview_player
Показать описание
Today we gonna Run Seeders with Node JS and Sequelize to insert some bulk set of data to the database in a single operation.

Normally, we use seeds to insert some default set of data to the database. For a example when a project is deployed we may wanna insert some default users, categories and things like that. For that we use seeders.

So, to demonstrate this, we gonna use a sequelize seeder example which is seeding some default categories to our category table.

0:00 Intro
2:31 Install and Configure Sequelize
3:01 Create a Seeder with Sequelize
7:37 Run seeder file
8:29 Undo a seed
9:40 Validate category ID when creating a post
13:47 Fix user ID with actual user ID decoded from the access token
17:18 Outro

To use seeders with nodeJS, we need an ORM (Object Relational Mapping). Which is why we use Sequelize. Seeding a MySQL database with Sequelize is quiet easy.

We can create a Seeder with the following command.
sequelize seed:generate --name category-seeder

This will create a Sequelize seeder file. Which we need to edit and enter our records we want to enter and save it.

Now, we can run that seeder with the following command.
sequelize db:seed --seed category-seeder

That's it! And if you think you made a mistake, you can always undo a seed. To undo Sequelize seed run the following command.

sequelize db:seed:undo --seed category-seeder

You can always refer the Sequelize seeder documentation to learn more about Sequelize seeders.

So, that's pretty much it in this Nodejs Sequelize seeder example.

In the previous video we Implemented Image Uploader in NodeJS API

This is the part 11 of the tutorial series, How to Create REST API with NodeJS and MySQL using Express and Sequelize.

Watch the Series from the beginning:

Connect to Database with Sequelize:

Implement CRUD with NodeJS and Sequelize

Implement Authentication with NodeJS and JsonWebToken

Hit Subscribe and smash the Like button.

Рекомендации по теме
Комментарии
Автор

😂😂I finally understand
You are the best teacher I've seen!
And I'm not exaggerating

hhajajahqh
Автор

real good one.waiting for next.your explanations are realy good

shehanprasanna
Автор

Keep it up. Congratulations on having 1st 1000 subs 😇😇😇

loraxtoothless
Автор

Muy interesante los 11 tutoriales... estaré atento al siguiente... MUCHAS GRACIAS!!!

marioguevara
Автор

Hello Sir. I have and am still enjoying and learning so much while watching all of your video tutorials on NodeJS and MySQL. If you find time, please integrate Flutter with NodeJS and MySQL. Again, thanks so much for all of your help. I will register as I look forward to more.

gilmcglory
Автор

Hi, Thank you for sharing your knowledge.
It's well explained and we learn another way of doing things. Good luck :-)

chambane
Автор

really helpful thank you soo much.
waiting for next :-)

pind
Автор

Hey, where is your next Videos regarding this series? Your whole series is very good and also containt full. Please tell about next vidoes like create Associaon and find queries etc.

kamaalkakaushal
Автор

what if u try to update without the categoryId in your patch request

neboeleyazer
Автор

thank sir ! good tutorial . i hope soon tutorial make api-ecommerce with node form your.

moonnight
Автор

Amazing tutorial thank you for such a good content 🫶🎉

motivationalquotes
Автор

Dear Sensei
i tried bulkInsert but heres is the error
ERROR: Field 'createdAt' doesn't have a default value
i fixed it but i want to know why this didnt happend in your code ?
i follow your tutorial but tried to code by myself with some paraphrase like (instead of promise i used async await)
i hope this comment reach you soon
thank you Sensei

MinhNguyen-fwkr
Автор

Hi my querries are taking too long to return data approximately 1.4 minutes, What could be the probem?

danielwaiguru
Автор

i have a problem and i really can't fix it, this error appears in my console while i am running the seeder.
this is how i run the seeder: sequelize db:seed --seed

Loaded configuration file "config\config.json".
Using environment "development".
== migrating
Options:
--version Show version number [boolean]
--help Show help [boolean]
--env The environment to run the command in [string] [default: "development"]
--config The path to the config file [string]
--options-path The path to a JSON file with additional options [string]
--migrations-path The path to the migrations folder [string] [default: "migrations"]
--seeders-path The path to the seeders folder [string] [default: "seeders"]
--models-path The path to the models folder [string] [default: "models"]
--url The database connection string to use. Alternative to using --config files [string]
--debug When available show various debug information [boolean] [default: false]
--seed List of seed files [array]

DatabaseError [SequelizeDatabaseError]: Field 'createdAt' doesn't have a default value
at Query.formatError
at Query.run
at processTicksAndRejections {
parent: Error: Field 'createdAt' doesn't have a default value
at Packet.asError
at Query.execute
at Connection.handlePacket
at PacketParser.onPacket
at PacketParser.executeStart
at Socket.<anonymous>
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead {
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlState: 'HY000',
sqlMessage: "Field 'createdAt' doesn't have a default value",
sql: "INSERT INTO `categories` (`name`) VALUES ('HTML'), ('CSS'), ('JavaScript'), ('Node.js'), ('REact.js'), ('MySql');",
parameters: undefined
},
original: Error: Field 'createdAt' doesn't have a default value
at Packet.asError
at Query.execute
at Connection.handlePacket
at PacketParser.onPacket
at PacketParser.executeStart
at Socket.<anonymous>
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead {
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlState: 'HY000',
sqlMessage: "Field 'createdAt' doesn't have a default value",
sql: "INSERT INTO `categories` (`name`) VALUES ('HTML'), ('CSS'), ('JavaScript'), ('Node.js'), ('REact.js'), ('MySql');",
parameters: undefined
},
sql: "INSERT INTO `categories` (`name`) VALUES ('HTML'), ('CSS'), ('JavaScript'), ('Node.js'), ('REact.js'), ('MySql');",
parameters: undefined
}

OmarMohamed-jlob
Автор

En que momento hiciste la tabla comentarios? y la pusiste en funcionamiento? necesito que cada comentario pertenece a un usuario.

dilanoviedo
Автор

Hey bother, doing a great job. I followed your video step by step to make user authentication. Can you help with a video where you show how we can navigate thru different pages based on user authentication from UI

danishmumtaz
Автор

Hello sir, I need urgent help
I followed this series right from start to now. I coded everything myself and now I'm finally done....but I'm having problems deploying it. It is very very frustrating. I have some questions to ask you sir

Note:-I used free heroku to try to deploy it and changed my database to production

1)I tried to use addon on heroku but they asked me for credit card details which I don't have. But I still tried to deploy it to heroku, it didn't work

So I want I ask
If I try deploying it to heroku without a database will there be any problems?
And what do you think the error is?

Im self taught full stack developer and I'm just 16 so please I need your help

whatyoutubeviewerswant
Автор

In Whcih you learn comment controller tutorial I did not found it

sp
Автор

when i deploy code on server cpanel it shows 503 error

che-
Автор

nice tutorial how can someone use a legacy table in this

oluwaseunolukayode
visit shbcf.ru