Created
November 6, 2023 09:59
-
-
Save ThimoDEV/b071dc83308d6b0a5e165efb6efa4902 to your computer and use it in GitHub Desktop.
A simple script to clear all the data of your planetscale DB (force-reset from PrismaORM)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// db.ts | |
import * as schema from "./schema" | |
export const db = drizzle(connection, { schema }) | |
//reset.ts | |
async function reset() { | |
const tableSchema = db._.schema | |
if (!tableSchema) { | |
throw new Error("No table schema found") | |
} | |
console.log("🗑️ Emptying the entire database") | |
const queries = Object.values(tableSchema).map((table) => { | |
console.log(`🧨 Preparing delete query for table: ${table.dbName}`) | |
return sql.raw(`TRUNCATE TABLE ${table.dbName};`) | |
}) | |
console.log("📨 Sending delete queries...") | |
await db.transaction(async (tx) => { | |
await Promise.all( | |
queries.map(async (query) => { | |
if (query) await tx.execute(query) | |
}) | |
) | |
}) | |
console.log("✅ Database emptied") | |
} | |
reset().catch((e) => { | |
console.error(e) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment