Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sistematico/45545758b45110f17364edd9551d45aa to your computer and use it in GitHub Desktop.
Save sistematico/45545758b45110f17364edd9551d45aa 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)
// 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