How are you all seeding your database?
I see a question from back in March (5 months ago -- https://discord.com/channels/1043890932593987624/1088404663344316476 -- ) answered that it 's planned for in-code custom migrations. IMO seeding from within migrations isn't great, and is actually warned against in some other stacks (eg: Rails).
I've fiddled around a bit this morning, trying to get a basic "hello world" ts-node script to run and load my ''src/lib/db" to get connection and schema, but had no luck. It seems like this should be an easy thing, to simply have a stand-alone script that could load up the drizzle-orm connection and schema, and use them to insert some data.
Can I get a pointer in the right direction? Thanks!!
10 Replies
Oh hey I found this on youtube which might get me like 90% of the way there: https://youtu.be/a1DPO7siG4s?t=1404
M27Lab
YouTube
Drizzle ORM Tutorial - Project Setup | Alternative to Prisma, TypeO...
Drizzle ORM - Project Setup
1. database connection 0:00
2. schema design 7:52
3. migration generate with drizzle kit 13:29
4. create migrator to apply migration into db. 17:22
5. write simple query with drizzle orm. 25:55
--------
Source Code Link: https://github.com/milon27/drizzle-tutorial/tree/part1
My github: https://github.com/milon27
My We...
And I found this which clears up so many questions: https://discord.com/channels/1043890932593987624/1141321911155036232
I just have a api endpoint that deletes all the content from my databases, and another one where I create a few maps that iterate and add the iteration number to the end of the fields.
If I change the schemas too much, I just delete all the tables on my database manager, delete my migrations and ensure the array in the meta/_journal.json file is removed too. Then I just do my generate and deploy again.
It's just the best way i've found for fast iteration and testing, but i'll change to using the migrations system fully when nearing production, or working with a team environment.
Write some jest scripts that use drizzle to add data to the database
I found this follow up video (https://www.youtube.com/watch?v=n9rtLhMN3cc). I have a very similar approach, but am struggling with actually getting the script to run and i"m curious if others have solved this.
Okay, so I was able to solve this by using
tsx
instead of ts-node
. Not sure why.tsx ftw
Anas Rin
Seeding Database With Drizzle ORM - Anas Rin
Database This post assume using PostgreSQL for database, check orm.drizzle.team/docs/overview for more information how to connnect to your database. Setup D...
For some reason if your use esbuild-register and had an error, you can try use tsx instead:š
yeah i had posted that as a comment when i was trying to get it work as i got the error. the author included it in the post later i guess. in a few days, we can all use bun instead of tsx, dotenv, etc...