Тёмный

Complex Schema Design with Drizzle ORM | Common Patterns 

Syntax
Подписаться 358 тыс.
Просмотров 14 тыс.
50% 1

In this video CJ shows how he used drizzle to implement a complex DB structure to represent a food delivery service called bytedash. He shows how to setup the project, how to create schemas, how to seed the DB with related data and how to query the DB with deeply nested relationships.
View the DB diagram here: dbdocs.io/w3cj/bytedash?schem...
View the code here: github.com/w3cj/bytedash
Read the drizzle docs: orm.drizzle.team/
Listen to Wes and Scott talk about how they use Drizzle in their apps: syntax.fm/show/721/you-should...
00:00 Intro
03:30 Drizzle: A Different Kind of ORM
04:37 Codebase Intro / Setup
07:51 Database Structure High Level Overview
09:20 Drizzle Studio Setup
12:06 Creating the Schema with Drizzle
14:10 Drizzle Foreign Key Constraints
14:45 Drizzle Composite Key Constraints
15:19 Drizzle Index Constraints
16:38 Drizzle Migration Setup
23:18 Seeding the Database with Drizzle
32:38 Querying the Database with Drizzle
34:45 Related Queries with Drizzle
35:39 Creating Table Relations
37:18 Drizzle Generated SQL Queries
38:51 Drizzle Join Query
40:34 Nested Where Queries with Drizzle
44:00 Final Thoughts on Drizzle
------------------------------------------------------------------------------
Hit us up on Socials!
www.syntax.fm/links
Brought to you by Sentry - Use code "tastytreats" to get 2 months free - sentry.io/syntax
#database #typescript #webdevelopment

Наука

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

 

7 июл 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 64   
@syntaxfm
@syntaxfm 2 месяца назад
03:30 Drizzle: A Different Kind of ORM 04:37 Codebase Intro / Setup 07:51 Database Structure High Level Overview 09:20 Drizzle Studio Setup 12:06 Creating the Schema with Drizzle 14:10 Drizzle Foreign Key Constraints 14:45 Drizzle Composite Key Constraints 15:19 Drizzle Index Constraints 16:38 Drizzle Migration Setup 23:18 Seeding the Database with Drizzle 32:38 Querying the Database with Drizzle 34:45 Related Queries with Drizzle 35:39 Creating Table Relations 37:18 Drizzle Generated SQL Queries 38:51 Drizzle Join Query 40:34 Nested Where Queries with Drizzle 44:00 Final Thoughts on Drizzle Listen to Wes and Scott talk about how they use Drizzle in their apps: syntax.fm/show/721/you-should-learn-drizzle-the-typescript-sql-orm
@Plaswin
@Plaswin 2 месяца назад
Couldn't follow him before in streams because I get distracted by that format. But this edited presentation are absolutely awesome
@ofeenee
@ofeenee 2 месяца назад
I could not agree more!
@Anbaraen
@Anbaraen 2 месяца назад
Stream vids felt second-best (this is pretty much always the way with stream vods unless you use Theo or CJs old approach of heavily editing them down). This is perfect.
@ddrweb_
@ddrweb_ 2 месяца назад
I started learning SQL, feels like this is exactly what I will need once fully understand what I am doing. Nice, clean patterns btw. Thanks for this you did an amazing job! This new role at syntax really suits you, everything you do has so much value to it, keep being amazing.
@czhablog9559
@czhablog9559 25 дней назад
Bro this video is pure gold! Thanks for sharing your amazing technical knowledge to the world.
@NicholasMaietta
@NicholasMaietta 2 месяца назад
I'm totally stealing the use of the Zod for getting the env vars checked before doing anything else. This actually would have been nice a few days ago when I couldn't figure out why my DB connections were not working in production. I wasn't getting any errors in logs! This is such a great idea.
@iukeay
@iukeay 29 дней назад
Legit, I never have never seen that done until this video! Clever. Clever
@fennecbesixdouze1794
@fennecbesixdouze1794 12 дней назад
Yes validating your .env is good, but totally overcomplicated to use Zod for this use case just write simple js.
@NicholasMaietta
@NicholasMaietta 12 дней назад
@fennecbesixdouze1794 I've implemented a caching mechanism that skips the check after the first successful pass. Each fresh deployment gets this extra step on first http request.
@IsakFilms
@IsakFilms 2 месяца назад
Great video! I've been learning Drizzle/SQL the past months and this was a great refresher. It really helped me understand relationships better
@chill-hot-stream
@chill-hot-stream 2 месяца назад
cj never stop releasing videos .Your coolify video from last week helped me manage my server effectively.Was using normal terminal but now everything is simple and easy to setup effectively
@jbphilippi
@jbphilippi 2 месяца назад
I agree and hope he will do more coolify videos!
@Stoney_Eagle
@Stoney_Eagle 2 месяца назад
Nice job! I have watched many videos to make sense of the chaos in de docs and all of them combined missed a lot of information that I needed and is shown here. I wish I had this video when I started using it.
@joelkuijper
@joelkuijper 2 месяца назад
This is awesome, thanks for the hard work your putting into these videos CJ!
@MrAbuYT
@MrAbuYT 2 месяца назад
Awesome! Thank you for this very helpful explanation. Good job CJ.
@kuldar
@kuldar 2 месяца назад
Absolutely love this new content from CJ. Thank you for making Syntax even better!
@paulclarke4099
@paulclarke4099 2 месяца назад
Your videos are fantastic and always so well explained, thanks so much CJ 💯😃👍
@talensjr
@talensjr 2 месяца назад
Yet another one of this amazing tutorials!! Keep this going, super helpful from a fullstack developer point of view. Thanks! 🙏
@VincentFulco
@VincentFulco Месяц назад
This was an outstanding tutorial. Will watch a few times. Thanks!
@vitalikda
@vitalikda 2 месяца назад
Omg! It's pure gold! Thank you
@prashlovessamosa
@prashlovessamosa 2 месяца назад
Great explanation CJ thanks Please keep uploading good stuff.
@truthzp
@truthzp Месяц назад
Thanks for the brilliant content! It's exactly what I need now.
@yantakushevich1014
@yantakushevich1014 10 дней назад
Thanks for the video! Would be great if you also show some examples of inserting data.
@fennecbesixdouze1794
@fennecbesixdouze1794 12 дней назад
@39:40 You could use postgres array_agg and add a group_by clause if you want to aggregate up the menu items in the sql syntax.
@zhanezar
@zhanezar 2 месяца назад
another great video thanks i really liked the idea of using Zod for Environment variables instead of adding another libray , thanks CJ
@Michael291288
@Michael291288 Месяц назад
Amazing! Would love to see the same implementation done with Prisma 🤩
@adamcollier1907
@adamcollier1907 2 месяца назад
Great video as always CJ! I know you touched on migrations but I’d love to know how to handle migrations for prod, e.g you already have an app running and need to run a migration without breaking anything. Also worth mentioning Drizzles proxy driver, was great for my move away from Planetscale and still allowed me to use Vercel edge functions
@petermckeever2360
@petermckeever2360 2 месяца назад
Well this was incredibly helpful
@pavank4466
@pavank4466 2 месяца назад
Great video, Thank You 👏
@bashiryousufy1
@bashiryousufy1 Месяц назад
CJ is the best, been watching him on Coding Garden
@ArionKosturi
@ArionKosturi 2 месяца назад
Thank you CJ
@joaquimley
@joaquimley Месяц назад
Cj you are amazing ☘️
@brennenrocks
@brennenrocks Месяц назад
How do you run migrations on your Production DB? When in a production environment typically you won't have your devDependencies which means drizzle-kit won't be there which then means the migration script will not run because the defineConfig is coming from drizzle-kit
@yiannis_p
@yiannis_p 2 месяца назад
Amazing video as usual! One thing I would also add here is that drizzle offers a db:push functionality so you don’t have to deal with any migrations! Also the foreign keys are optional, as you can still use their orm style query builder without foreign keys just using the relationships you defined in the schema! Works really well in an environment like planetscale which only has online ddl and has no foreign keys!
@mehmethanifierenler3612
@mehmethanifierenler3612 7 дней назад
thanks to you very very
@paoloricciuti
@paoloricciuti Месяц назад
I think i actually did the same error when i used Drizzle with sqlite but from what I've researched closing the db connection is not handled by drizzle so with PG you can use the Pool to use a connection pool but you have to find ways to do the same with other drivers otherwise you will either clog up or all your connections will pile up. This is obviously less of a problem with serverless but i think is worth mentioning.
@haziqali6024
@haziqali6024 18 дней назад
great video! I have just started to look into drizzle to integrate with expo-sqlite. I had a question: Is there any way to generate schema.ts file automatically from an existing sqlitedb which already has sql schema defined?
@ayushgogna9732
@ayushgogna9732 Месяц назад
18:07 actually in the version @0.21.0 there is a way to handle migrations in drizzle without a migration file with "drizzle-kit migrate" No more push:pg, generate:mysql. Just use push, generate and specify dialect in config once!
@syntaxfm
@syntaxfm Месяц назад
I have updated the repo with a note for anyone that comes across this: github.com/w3cj/bytedash/#drizzle-kit-updates
@w3mw
@w3mw 2 месяца назад
Great video as always CJ! I’m building a project right now with nest js and prisma as backend and I thinking of next js with drizzle in front end is that possible tough or should I use the same ORM in both ways? Never tried drizzle before so I’m not sure if it works good with nest js but I guess it does 😄 what do you recommend?
@syntaxfm
@syntaxfm 2 месяца назад
Drizzle is just typescript, so it will work just fine with nest js. It is possible to use 2 different DB libraries to connect to the DB, but if you can consolidate to a single one, that would be ideal. If you are working in a mono repo, you could also have a library specifically for the db / queries and share this between projects. -CJ
@officialdanieldsouza
@officialdanieldsouza 2 месяца назад
Hey awsome video, this video really clarified organization of my files in drizzle. I was wondering what is the best field type to use for the primary key, I know in your example you have used serial but I was just wondering in terms of security, performance, running out of integers. Would love to hear your thoughts on this dude!
@syntaxfm
@syntaxfm 2 месяца назад
There's always a trade off in performance / usability. string ids (guids) are good for large distributed systems where you might be inserting many many rows all at once across several nodes. guids can also be generated outside of the DB, so you could generate them before insert. auto incrementing integer keys might be vulnerable to enumeration attacks if your application code does not protect against it. e.g. someone might try requesting /users/1, /users/2, /users/3 etc. but this is only vulnerable if you have not locked down the API. postgres serial / int maxes out at 2,147,483,647 but if you are worried about running out, you could use bigint / bigserial which maxes out at 9,223,372,036,854,775,807 I have a lot of apps still in production that used int / serial keys, but those apps only have a few thousand users. Worrying about running out of primary keys / insertion performance is likely only needed if your going to be working on things at a much bigger scale. -CJ
@cpakken
@cpakken Месяц назад
What is the vscode theme you're using?
@russpalermo
@russpalermo 2 месяца назад
cool CJ. On seeding the db have you thought about or tried running the seeding as a single transaction in postgres? I think I did this in a prior project using knex where I set the table fk contraints to 'deferred' the ran a promise.all on the seed files where the commit did not happen until all data was stuffed in the tables.
@syntaxfm
@syntaxfm 2 месяца назад
Yes this should be easy enough with drizzle, just wrap the insert code into a db.transaction call: orm.drizzle.team/docs/transactions
@brennenrocks
@brennenrocks Месяц назад
What are your thoughts on using UUIDs for primary keys instead of `serial` in Postgres?
@danfascia
@danfascia 22 дня назад
Just add another column for uuid as well as id(int) because indexing uuids is expensive
@brennenrocks
@brennenrocks 22 дня назад
@@danfascia what's the point of having the uuid if you aren't going to do lookups on it or index it?
@danfascia
@danfascia 22 дня назад
@@brennenrocks not what I said. Use integer as your primary key index and use that to carry out internal relationships as indexing integers in this context across relationships faster. No issue having a uuid index for direct item lookups
@premdasv8
@premdasv8 Месяц назад
What's the font you are using in your vscode?
@syntaxfm
@syntaxfm Месяц назад
It's called anonymous pro - www.marksimonson.com/fonts/view/anonymous-pro
@sasquatch_devs
@sasquatch_devs 2 месяца назад
Would love your thoughts on Lucide ORM by the creators of AdonisJS!
@syntaxfm
@syntaxfm 2 месяца назад
I'm not sure how I feel about the lack of type safety 🤔 The author talks about it here: github.com/thetutlage/meta/discussions/8 I think I would rather have a single source of truth with drizzle for the table schema / inferred insert / select types, inferred validators etc. than worry about a query not having a typescript error when it should. I haven't run into the type issues during querying the author brings up in that post. With lucid you need to define your models separately from creating the tables. Migrating needs to be done manually for every column / change and then you need to update the model to match. This is very similar to my experience with sequelize. With drizzle, you update the schema type and then the migrations are auto generated from the schema change and the inferred types continue to flow through. Seems like a trade of better type safety during querying vs better type safety / single source of truth from model definitions. In my experience with other libraries, keeping the models in the code base up to date with the table schema is one of the biggest pain points, not querying. -CJ
@dummy_code
@dummy_code 2 месяца назад
Does it support raw sql queries?
@syntaxfm
@syntaxfm 2 месяца назад
Yes and it parameterizes them by default: orm.drizzle.team/docs/goodies#raw-sql-queries-execution If you don't want parameterization you can use sql.raw
@fennecbesixdouze1794
@fennecbesixdouze1794 12 дней назад
"Run as a single statement" doesn't really mean much in terms of performance, if you're doing nested selects you should take the produced sql statement and put it through a PLAN analyzer to make sure it's performant and getting all the joins etc it needs without full table scans.
@polioann
@polioann 2 месяца назад
How about recursive schemas? (like the comment section)
@syntaxfm
@syntaxfm 2 месяца назад
Self referencing keys is possible and set up in the same way: orm.drizzle.team/docs/rqb#one-to-one
@polioann
@polioann 2 месяца назад
@@syntaxfm Nice! Thx
@user-tb4ig7qh9b
@user-tb4ig7qh9b 2 месяца назад
For me drizzle sucks and prisma sucks sorry for that i prefer kysley
@syntaxfm
@syntaxfm 2 месяца назад
What do you like more about Kysley? Also what makes you say Drizzle and Prisma sucks, in my experience they are both great options
@user-tb4ig7qh9b
@user-tb4ig7qh9b 2 месяца назад
@@syntaxfm i worked with prisma and drizzle before but for very easy jobs but sometimes you pull data from alot of table and do aggregation and window function and lots of stuff okay sometimes we make a view on database but when i write new feature i need a tool to provide me most of things and drizzle already lack alot of sql features and prisma have the same problems i work on my job with ecto it is lib written in elixir and man working with so much easier. I used alot of orm like prisma awesome until you start making joins and take data from alot of tables and make aggregation you just lost the way for prisma. Drizzle just low level implementation of sql if we have versions for sql i will give drizzle like 0.1 I sometimes even work with laravel and for meduim things it is okay but after that you will write sql in your hand. so i think the only true orm i used until ecto and linq give you all the power it make the write mapping for objects and alot of stuff. Why kysley i was have a project it is scrapper and i need to feed this data to db so i tried prisma and drizzle does not fit but when i tried kysley and make inspect db i just get the completion type safety and do not need to deal with writing my geolocation types i think that a joke you have orm that does not support goelocation 😦
Далее
Pagination in MySQL - offset vs. cursor
13:20
Просмотров 55 тыс.
I tried 8 different Postgres ORMs
9:46
Просмотров 390 тыс.
Looks realistic #tiktok
00:22
Просмотров 30 млн
Drizzle vs Prisma: Which ORM is right for YOU?
5:59
Просмотров 2,2 тыс.
Learn Drizzle ORM in 13 mins (crash course)
14:00
Просмотров 22 тыс.
How was this not in the browser before???
16:30
Просмотров 101 тыс.
Hands On Docker Examples for Developers
49:35
Просмотров 10 тыс.
I've been using Redis wrong this whole time...
20:53
Просмотров 334 тыс.
JS Promises Fundamentals  - Part 1
22:01
Просмотров 3 тыс.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Microservices with Databases can be challenging...
20:52
Writing My Own Database From Scratch
42:00
Просмотров 162 тыс.
Сравнили apple и xiaomi!
0:21
Просмотров 28 тыс.
Собери ПК и Получи 10,000₽
1:00
Просмотров 2,2 млн