Skip to content

Commit

Permalink
Implement deleteDataFromAllTables function (#710)
Browse files Browse the repository at this point in the history
Co-authored-by: Niall Maher <[email protected]>
  • Loading branch information
John-Paul-Larkin and NiallJoeMaher authored Feb 4, 2024
1 parent c54cff1 commit 86f2711
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from "@prisma/client";
import { Prisma, PrismaClient } from "@prisma/client";
import { nanoid } from "nanoid";
import { Chance } from "chance";

Expand Down Expand Up @@ -106,7 +106,7 @@ ${chance.paragraph()} If you want to try a link click this [test link](https://w
${"```"}
function test() {
console.log("notice the blank line before this function?");
console.log("notice the blank line before this function?");
}
${"```"}
Expand Down Expand Up @@ -149,7 +149,7 @@ const generateUserData = (count = 100) => {
const userData = generateUserData();
const communityData = generateCommunityData(10);

async function main() {
async function addSeedDataToDb() {
console.log(`Start seeding, please wait...`);
userData.forEach(async (user) => {
await prisma.user.upsert({
Expand Down Expand Up @@ -207,7 +207,28 @@ async function main() {
console.log(`Seeding finished.`);
}

main()
async function deleteDataFromAllTables() {
const models = Object.keys(prisma) as (keyof typeof prisma)[];

for (const model of models) {
const modelInstance = prisma[model] as {
deleteMany?: (
options?: Prisma.UserDeleteManyArgs,
) => Prisma.PrismaPromise<Prisma.BatchPayload>;
};

if (modelInstance?.deleteMany) {
await modelInstance.deleteMany({});
}
}
await prisma.$disconnect();
}

deleteDataFromAllTables()
.then(() => {
console.log("Data deleted");
addSeedDataToDb();
})
.then(async () => {
await prisma.$disconnect();
})
Expand Down

0 comments on commit 86f2711

Please sign in to comment.