Тёмный

Creating PostgreSQL Database Models with Sequelize 

Ben Awad
Подписаться 499 тыс.
Просмотров 45 тыс.
50% 1

Learn how to set up a PostgreSQL database with Sequelize. We will be adding this onto our GraphQL server that's built with Node.js and Express.js
Code: github.com/ben...
Links from video:
docs.sequelizej...
github.com/seq...
----
Video Suggestions:
Trello: trello.com/b/m...
Join the Trello board: trello.com/inv...
----
Follow Me Online Here:
GitHub: github.com/ben...
Instagram: / benawad97
Patreon: / benawad
----
Join the Discord: / discord
Twitter: / benawad
#benawad
TikTok: / benawad ​

----
Follow me online: voidpet.com/be...
#benawad

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 109   
@JOJO_THE_PROGRAMMER
@JOJO_THE_PROGRAMMER 2 года назад
Seriously I Miss this type of tutorials
@76Freeman
@76Freeman 6 лет назад
Hi Ben, Thank you very much for taking the time to create this series. It's what I've been looking for. I've been looking for a nice series on Graphql with React so I'm looking forward to watching the other videos. Do you have a Patreon or any other place where I could pay you a beer? :) Thank you very much again for the great work.
@bawad
@bawad 6 лет назад
Glad my videos have been helpful! I haven't set up a patreon yet, but I will be soon. In the time being you can tip here if you like: paypal.me/freevultrmonth
@76Freeman
@76Freeman 6 лет назад
Hi Ben, I've done a small contribution. I'll gladly do more contributions along the series. Thank you again for taking the time.
@bawad
@bawad 6 лет назад
Thanks, I appreciate that!
@siddharthlakhara287
@siddharthlakhara287 6 лет назад
Hi Ben, You can also install sequelize-cli, then run the following command to have basic model setup "node_modules/.bin/sequelize init" This command will setup the config and models folder. After having config files edited to your project, you can run the following command for creating db: "node_modules/.bin/sequelize db:create" For creating models: "node_modules/.bin/sequelize model:generate --name model_name --attributes attribute1:type,attrib2:type"
@bawad
@bawad 6 лет назад
Thanks
@chriswilson8584
@chriswilson8584 5 лет назад
For most people that is true. There are the edge cases where people code in TypeScript which makes the CLI problematic. Again, you are right almost all the time but TS users should be warned.
@FunkyToe369
@FunkyToe369 7 лет назад
What tool did you use to mock out your tables and relationships? Looks really useful. I always try to write it all out and I think the visualization would help me immensely.
@bawad
@bawad 7 лет назад
+Evan Fleet I used the guest user for dbdesigner.net
@FunkyToe369
@FunkyToe369 7 лет назад
Thanks! Looks great
@zemielasma8986
@zemielasma8986 7 лет назад
Hi Ben! Nice series, Thank you for sharing! These are the things that I am looking for. I am not a ninja on Javascript world, but I am currently working on a VueJs project & Laravel as backend. My question is, why did you choose Sequelize over KnexJs?
@bawad
@bawad 7 лет назад
+Zemiel Asma I chose sequelize because I know it better and it's more popular. I want to try out knexjs, it looks really nice
@khalidkhan5308
@khalidkhan5308 3 года назад
And I believe you are the best in the whole world :p - You have used the believe word many time in this video in case u forgot why I am commenting like this haha - Thank you Ben for such outstanding content
@willcalltickets
@willcalltickets 7 лет назад
Great video! In a previous video you had alluded to wanting to making all of your column names lowercase aka postgres friendly. Did you disregard this because the column names you camel-cased are relations/foreignKeys? Just wondering because of your previous comments why you may have done it this way.
@bawad
@bawad 7 лет назад
+Rob Kurtz I go back and forth on this. I like to use Snake_case for Postgresql, but GraphQL schemas use camelCase. I like having the names match, so I end up using camel case for Postgresql.
@willcalltickets
@willcalltickets 7 лет назад
understood! I just always hate with having to quote anything for postgres. What if you kept the scheme you are using, but changed the models/index.js ``` const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { dialect: 'postgres', define: { underscored: true, }, }); ``` Would this mess up how GraphQL talks to postgres? Perhaps there is a similar config option in GraphQL?
@bawad
@bawad 7 лет назад
+Rob Kurtz that looks perfect, I didn't know that was an option. I am definitely going to try that out. I think it will work nicely.
@willcalltickets
@willcalltickets 7 лет назад
It works for the sequelize=>postgres naming, trying to figure out how it works for the graphql=>postgres :)
@bawad
@bawad 7 лет назад
+Rob Kurtz graphql goes through sequelize so it should work out. GraphQL=>sequelize=>postgres
@bantinggamer4962
@bantinggamer4962 5 лет назад
Thanks so much for this tutoail. its the best on i can find teaching postgres and node. your a rock star!
@gregg2696
@gregg2696 4 года назад
Great series!!!! Great video!!!!! What is the purpose of this in index.js? Couldn't find it anywhere in the documentation. models.sequelize = sequelize models.Sequelize = Sequelize
@balavigneshs3778
@balavigneshs3778 6 лет назад
Hi Ben! Video is great to learn. I am facing an issue to create a model for existing table's in a pg database. I should restrict them recreating the table's because i have huge data stored in it.Share your idea please
@bawad
@bawad 6 лет назад
I don't understand, could you elaborate
@maksimmarkelov1971
@maksimmarkelov1971 7 лет назад
Thank you, Ben! Awesome part! But I just have some troubles, so it doesn't work until i install 'pg' in project. It is a question for me, why i should did it, but you got it all fine without this package. And after all i get warning from sequelize: sequelize deprecated String based operators are now deprecated. So in index.js i add operatorsAliases: false in options. So i want to ask you about this warning. What is it about? And what i should do? Thank a lot!
@bawad
@bawad 7 лет назад
+Максим Маркелов I didn't realize it, but I already had pg installed. I think I may have a older version too because I don't get the depreciation warning you get. I will try upgrading pg and let you know if I get that warnings
@stevereid636
@stevereid636 7 лет назад
I couldn't see pg the package.json of the code repo you've added a link to?
@bawad
@bawad 7 лет назад
I think I have pg installed globally.
@GregorEpet
@GregorEpet 7 лет назад
Hey, great video series. How do you navigate in the editor so quickly?
@bawad
@bawad 7 лет назад
+Hackfleisch Hackender Zerhacker I'm using the vim keybindings for visual studio code. So I'm using vim shortcuts
@GregorEpet
@GregorEpet 7 лет назад
OK Thanks! I'm gonna look into it.
@matiasfigueroasaint-jour3621
@matiasfigueroasaint-jour3621 6 лет назад
Nice video. I've been looking for this
@warrenwong4518
@warrenwong4518 3 года назад
can you do a video on sequelize indexing with postgres
@mr.random8447
@mr.random8447 3 года назад
What did you use to create database design?
@sai.santhosh
@sai.santhosh 6 лет назад
Both Sync and Migrations are used to create tables in database right? Why must we use sync in order to fetch it back to the database, any ideas on that? cause I'm confused with using them?
@bawad
@bawad 6 лет назад
Sync makes sure your database matches your models
@connorc44
@connorc44 4 года назад
Does anyone know what is used to create the visualization of the data flow for your tables at the very beginning of the video?
@Mihail1985
@Mihail1985 6 лет назад
Great video!
@hrmny_
@hrmny_ 7 лет назад
you have babel for import (in node8 with .mjs, but not done yet) and destructuring (which is already in node) so why don't you use `.associate = ({ User }) => {...}`?
@bawad
@bawad 7 лет назад
+harmony yeah I could destructure there. I just didn't think of it at the time
@shabbar0333
@shabbar0333 7 лет назад
really helpfull. how to add associations to existing tables?
@bawad
@bawad 7 лет назад
+SHAMS Azam you will have to do a migration if the table already exists
@billpliske
@billpliske 6 лет назад
Question for you, Ben. In your models, from the very beginning, you went without an ID on your user, instead going with username, email and password. Yet you still reference userId in authorization, as well as with foreign keys in other tables. If you aren't creating a user_id in your models, where is it coming from to use elsewhere? Could you possibly explain a little bit of that? Thanks!
@bawad
@bawad 6 лет назад
Sequelize automatically creates an auto-incrementing field called id on the user. Then when I did belongsTo and set the foreignKey to 'userId', sequelize behind the scenes creates a field on that model/table called 'userId' which is a foreign key that references the id field on the user.
@billpliske
@billpliske 6 лет назад
@@bawad so does Sequelize auto-create IDs for ALL of my models, or just users?
@bawad
@bawad 6 лет назад
All of them
@pojntfxlegacy611
@pojntfxlegacy611 6 лет назад
Awesome!
@devilsey3
@devilsey3 6 лет назад
For those who's code didn't create table you can use this, If you haven't any babel or resolver then you can write const models = { User: require('./user')(sequelize,Sequelize), Channel: require('./channel')(sequelize,Sequelize), Message: require('./message')(sequelize,Sequelize), Team: require('./team')(sequelize,Sequelize), }
@MaxJacobson1
@MaxJacobson1 6 лет назад
Thank-you for this great tutorial. I am running into an issue that I can't figure out. I initially created a User model with username and password for the columns. When I save and restart my server, I see the console drops the table if it exists and creates everything and it reflects correctly on the database. However, I needed to add firstName and lastName as fields on the table. When I save, it looks like it updates the table, but my database doesn't reflect the new information. The only way I can get it to update is if I put in force: true as you did in the sync statement. That would be fine, but then it drops all the existing data from the table. How would I add a column on an existing table without having to drop it completely?
@bawad
@bawad 6 лет назад
+Max Jacobson you could do a migration: docs.sequelizejs.com/manual/tutorial/migrations.html
@MaxJacobson1
@MaxJacobson1 6 лет назад
Thank-you! I will take a look at it!
@ibrahimkus2542
@ibrahimkus2542 6 лет назад
for channel and messages you wrote 1:M, but isn't that a channel belons to one team, one message belongs to one channel, and one user? so 1:1? or am I missing something?
@bawad
@bawad 6 лет назад
One channel can have many messages
@rainoftearvnbibi6525
@rainoftearvnbibi6525 6 лет назад
hi Ben, can I use Mongodb instead of PostgreSQL ?
@bawad
@bawad 6 лет назад
Yeah
@omarshiha3965
@omarshiha3965 5 лет назад
Hey its working ! ... i just don't get it how sequelize connects without giving it the url of the database when connecting ?? how does it know where the database is running while the connection function has only username and password ?
@bawad
@bawad 5 лет назад
it defaults to a local postgres database
@nwease
@nwease 6 лет назад
Hey Ben I am getting this crazy error. Do you know why? /Users/user/Desktop/slack-clone-server/node_modules/sequelize/lib/dialects/postgres/connection-m anager.js:29 throw new Error('Please install \'' + (sequelize.config.dialectModulePath || 'pg') + '\' module manually');
@bawad
@bawad 6 лет назад
+nick wease yes, run: npm install pg
@nwease
@nwease 6 лет назад
Thanks Ben one last thing. I installed Postgres in the beginning and created the DB but get this error code now despite the fact that I downloaded it. Unhandled rejection SequelizeConnectionError: role "postgres" does not exist
@bawad
@bawad 6 лет назад
+nick wease you'll want to create a user in your postgres database. Make username and password match what you have in sequelize. If your following the same as me the username and password will be postgres stackoverflow.com/questions/10861260/how-to-create-user-for-a-db-in-postgresql
@omarshiha3965
@omarshiha3965 5 лет назад
is there a way to create a database if it does not exist ?
@bawad
@bawad 5 лет назад
probably but I'm not sure how
@enkos1990
@enkos1990 5 лет назад
you need to use the mysql npm package for make raw queries, after that you can make an SQL instruction for create the database
@camerongiles1295
@camerongiles1295 4 года назад
IMPORTANT: use 'pg' version '^6.4.1' or earlier. version 7+ will not work with this code
@estudodeidiomas
@estudodeidiomas 4 года назад
Can you show How Can I make this in 2020? Class number one also.
@blasttrash
@blasttrash 5 лет назад
why did u not use nosql db here? how do u decide to use rdbms?
@bawad
@bawad 5 лет назад
I generally default to SQL, I like it better
@davy__gravy
@davy__gravy 6 лет назад
What extensions are you using
@bawad
@bawad 6 лет назад
For vscode?
@VGplayingHD
@VGplayingHD 6 лет назад
I would like to know the VSCode extensions too. Thanks
@bawad
@bawad 6 лет назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-659O4HME7Mg.html
@HassanSani
@HassanSani 7 лет назад
I am having serious problem setting up PostgreSQL
@bawad
@bawad 7 лет назад
+Hassan Sani are you on Windows? www.postgresqltutorial.com/install-postgresql/
@HassanSani
@HassanSani 7 лет назад
No I am on Mac my problem is it always return psql: FATAL: password authentication failed for user
@bawad
@bawad 7 лет назад
+Hassan Sani have you tried any of these things stackoverflow.com/questions/2942485/psql-fatal-ident-authentication-failed-for-user-postgres
@pojntfxlegacy611
@pojntfxlegacy611 6 лет назад
sudo -u postgres psql # Did it for me (Linux tho)
@ranielgarcia8685
@ranielgarcia8685 5 лет назад
Why Member table don't have id or primary key?
@bawad
@bawad 5 лет назад
join tables don't need an id
@ranielgarcia8685
@ranielgarcia8685 5 лет назад
@@bawad But is it possible? can I use member id as foreign key to other table?
@bawad
@bawad 5 лет назад
sure
@mubashshiralibaig6104
@mubashshiralibaig6104 5 лет назад
getting error "please install pg manually" what should i do?
@bawad
@bawad 5 лет назад
npm i pg
@mubashshiralibaig6104
@mubashshiralibaig6104 5 лет назад
@@bawad thanks man you're awesome
@studio_3f
@studio_3f 4 года назад
Hey man I don't know why my code is not gonna work even if I wrote totally same with you. just keep occurring this god damn error like 'sequelize.import is not a function' I'm doing this on window OS and I did everything you did at your previous videos
@abhishekshah7368
@abhishekshah7368 4 года назад
See my reply. It might help
@hadiwall
@hadiwall 5 лет назад
F A C E . J P G
@bawad
@bawad 5 лет назад
😉
@blasttrash
@blasttrash 4 года назад
This is one the reasons why I like typed languages like Java. Mapping associations is dead easy in Java with JPA annotations. Sequelize is quite ugly.
@pieter-venter
@pieter-venter 6 лет назад
Hi Ben, thanks for the great fully fledged series. I stumbled across it while looking for styled components examples. When I saw the example is using GraphQL on top of it I figured I could just base my next project off this. What would be great is if you could switch out the whole postgreSQL part with Prisma, which under the hood also uses postgreSQL. Check it out: blog.graph.cool/introducing-prisma-1ff423fd629e?gi=47a16554dd6f
@bawad
@bawad 6 лет назад
+Pieter Venter I think Prisma only works with MySQL, but either way I'm exploring Prisma right now
@pieter-venter
@pieter-venter 6 лет назад
Ben Awad prisma has their own db in the cloud. So no need for local one.
@bawad
@bawad 6 лет назад
+Pieter Venter yeah I saw that it looks like they will manage the db for you which is interesting
@bawad
@bawad 6 лет назад
+Pieter Venter yeah I saw that it looks like they will manage the db for you which is interesting
@BladeAndrade
@BladeAndrade 5 лет назад
where is part 2?
@bawad
@bawad 5 лет назад
Slack Clone Using GraphQL and React: ru-vid.com/group/PLN3n1USn4xlkdRlq3VZ1sT6SGW0-yajjL
@colekake
@colekake 6 лет назад
Hi Ben, I keep getting sequelize deprecated error: sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at docs.sequelizejs.com/manual/tutorial/querying.html#operators checked the guide but kind of not resolving the issue, any comments?
@bawad
@bawad 6 лет назад
I've had the same problem, I'm not sure the best way to silence it
@colekake
@colekake 6 лет назад
OK, looks liek it's known issue but no resolution, i guess: thanx!
@mikkim7275
@mikkim7275 7 лет назад
I'm getting: "Unhandled rejection SequelizeConnectionError: role "postgres" does not exist" and my slack database is still empty when i type \d. Do you know what I'm missing? i've used postgres on my computer before so im not sure what the error means
@bawad
@bawad 7 лет назад
+Mikki M it means you don't have a user called postgres. You can create that user or a different one. If you create a different one, you will have to change the username/password we supply to sequelize
@stevereid636
@stevereid636 7 лет назад
How come you've got a user called postgres and we haven't?
@bawad
@bawad 7 лет назад
+Steve Reid I thought it came default with Postgresql. I'm not sure why you guys have a different user. But it's easy to create a new user
Далее
PostgreSQL Naming Conventions and GraphQL Schemas
16:59
Creating GraphQL Schemas and Resolvers using Sequelize
31:01
Which part do you like?😂😂😂New Meme Remix
00:28
Sequelize Migration
27:19
Просмотров 33 тыс.
Register Form in React Using Semantic UI and GraphQL
20:09
Postgres & Sequelize - Associations
48:58
Просмотров 43 тыс.
Sequelize ORM Tutorial (all in one video)
49:59
Просмотров 163 тыс.
I tried 8 different Postgres ORMs
9:46
Просмотров 418 тыс.
Getting Started with Sequelize and PostgreSQL
18:39
Просмотров 17 тыс.
How to build a REST API with Node js & Express
58:40
Просмотров 1,7 млн
PERN Stack Course - Postgres, Express, React, and Node
1:22:45
Sequelize: An Introduction (1/TBD)
8:54
Просмотров 103 тыс.
Which part do you like?😂😂😂New Meme Remix
00:28