How to Drizzle Migrate?
How to do migration in drizzle?
for prod we should create migrate.ts file instead of drizzle-kit but how then how should we run the .ts file?
dockerfile
start.sh
error
9 Replies
You cannot run ts files without compiling them first, technically. But there are many tools that allow you to do it with a bit of magic in the background. My favourite is to use
tsx
for that. I personally setup my programmatic migrations with mjs to avoid this exact compilation problemAlso it seems that in your case
drizzle-kit: not found
it is not installed - I would look into whether npm ci
installs dev-dependencies or not. I'm assuming that you have drizzle-kit as a dev-depi'd appreciate the response, thanks! could you please explain a bit more about mjs and how can i run the migrate.ts file?
With tsx - you install tsx and just run
tsx migrate.ts
, as simple as that
With mjs, it's just a regular js file that utilizes ESM modules, so you can use import { ... } from "..."
as usual
the .mjs is just a convention to tell that a given JS file uses ESM-import syntax, instead of node's require
Besides that, MJS is just a regular JS file - and since it is, you can run it without compilation step that ts files requireMDN Web Docs
JavaScript modules - JavaScript | MDN
This guide gives you all you need to get started with JavaScript module syntax.
thank you! i finally made it to work with drizzle-kit
but i'm copying the node_modules into the docker which is not ideal
because drizzle-kit or tsx are in the node_modules
i was wondering if it's possible to just do it in a better (efficient) way
Move it from dev-dep to normal deps then
Ah, I think I misunderstood. Instead of copying full node_modules, you can install these two packages on the stages you need them, I can't really think about any other solution
Or, another idea - you can create a one-shot container, that would contain just drizzle-kit, run the migrations from it
docker-compose has
depends
or something similar, so you could utilize that
Also, since you're using multi-stage build - you might want to use slimmer base image instead of just node
thank you mate, i tried to do it with the depends but made it a lot complex so i left it to do full copy node modules for now
hopefully i find a better solution
If you ever do - please, let us know - you might help someone who will have similar setup. Good luck!