Skip to content

Instantly share code, notes, and snippets.

@ThimoDEV
Created November 6, 2023 09:59
Show Gist options
  • Save ThimoDEV/b071dc83308d6b0a5e165efb6efa4902 to your computer and use it in GitHub Desktop.
Save ThimoDEV/b071dc83308d6b0a5e165efb6efa4902 to your computer and use it in GitHub Desktop.

Revisions

  1. ThimoDEV created this gist Nov 6, 2023.
    35 changes: 35 additions & 0 deletions clear-planetscale-db-drizzle.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    // 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)
    })