How to run a Cleanup Script

I saw someone provide a nice script to empty the database, here's a version of it:
async function emptyDBTables(db: PlanetScaleDatabase<typeof schema>) {
console.log("๐Ÿ—‘๏ธ Emptying the entire database");

const tablesSchema = db._.schema;
if (!tablesSchema) throw new Error("Schema not loaded");

const queries = Object.values(tablesSchema).map((table) => {
console.log(`๐Ÿงจ Preparing delete query for table: ${table.dbName}`);
return sql.raw(`DELETE FROM ${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");
}
async function emptyDBTables(db: PlanetScaleDatabase<typeof schema>) {
console.log("๐Ÿ—‘๏ธ Emptying the entire database");

const tablesSchema = db._.schema;
if (!tablesSchema) throw new Error("Schema not loaded");

const queries = Object.values(tablesSchema).map((table) => {
console.log(`๐Ÿงจ Preparing delete query for table: ${table.dbName}`);
return sql.raw(`DELETE FROM ${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");
}
But how do I run the dang thing? I added script to my package.json
scripts {
...
"db:clean": " sst bind ts-node ./src/utils/clean.ts"
},
scripts {
...
"db:clean": " sst bind ts-node ./src/utils/clean.ts"
},
But running pnpm db:clean throws this error:
import { sql } from "drizzle-orm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
import { sql } from "drizzle-orm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
I tried googling some compiler options to set for ts-node in my tsconfig.json file but haven't had success. My goal is to have have a cleanup script and then a seed script, and then add commands to my package.json to be able to run from the terminal. Has anyone done something like that? Are there alternatives? I know this isn't entirely drizzle related and more a javascript thing, but curious what others may have done
1 Reply
ArwanceR
ArwanceRโ€ข17mo ago
How about putting "type": "module" in package.json and replacing ts-node with ts-node-esm in your script
Want results from more Discord servers?
Add your server