How do you run a one-off prisma script?

Hello 👋 I'm trying to run a little script to change some stuff in my database. Kind of like a prisma seed or some sort of scripted migrate . I need to be in a JS environment (not just SQL) so I can run some logic, and I also need to import some functions from the rest of my codebase. The issue is the classic "node is bad at importing web stuff" and "typescript needs to be compiled". So I'm trying ts-node but it doesn't solve the importing issues. I've tried many variations of this script in my package.json
"foo": "ts-node --project tsconfig.json --compiler-options {\\\"module\\\":\\\"CommonJS\\\"} src/scripts/test.ts"
"foo": "ts-node --project tsconfig.json --compiler-options {\\\"module\\\":\\\"CommonJS\\\"} src/scripts/test.ts"
which all resulted in many variations of
Error: Cannot find module 'server/db/client'
Error: Cannot find module 'server/db/client'
As you can tell I'm using aliased imports everywhere in my project. But I need to be able to import functions. If not for like the above (that's just the prisma client, which I could instantiate directly within my script), at least so that I don't have to rewrite some of the logic I need that already exists somewhere in the project. I've had this problem many times before and never quite found a good solution for it. It's always involved copy/pasting stuff into .mjs files and stripping the types and that's really ugly. I'm hoping the T3 community has some bright idea.
4 Replies
JulieCezar
JulieCezar•2y ago
If you are writing here then I will assume you know how to use Next. If it's something you just want to do once, just create an API route with all your logic and call it once 🙂
bostonsheraff
bostonsheraff•2y ago
Thank you for taking the time to answer! Yeah I've written pretty big Next projects in the past and creating a temporary route is usually what I do but somehow that never felt really good. Plus it means that while I'm distributing the script to all of the servers, this route keeps existing, so it's either behind credentials and I can't call the route simply from the command line, or it's in open access and it seems like a poor man's "security by obfuscation"
JulieCezar
JulieCezar•2y ago
That's true... But then again, you don't need to deploy this anywhere, just run it on your own dev machine once and you are done. If we are talking about executing it only once when you want to. Assuming that you can reach the servers or w/e you need from your dev machine directly
JulieCezar
JulieCezar•2y ago
Otherwise... Maybe try this: https://khalilstemmler.com/blogs/typescript/node-starter-project/ whenever I need to start a new TS project this is the setup I use, and I didn't have problems yet
How to Setup a TypeScript + Node.js Project | Khalil Stemmler
In this guide, we walk through the process of creating a TypeScript project from scratch with cold-reloading, and scripts for building, development, and production environments.
Want results from more Discord servers?
Add your server