Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* cleanTables now uses truncate instead of deleteMany #150

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/app/prisma-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './prismaError'
export * from './types'
export { prismaTransaction } from './prismaTransaction'
export type { DbCleaner } from '../test/DbCleaner'
10 changes: 8 additions & 2 deletions packages/app/prisma-utils/src/prismaTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaClient } from '@prisma/client'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vitest } from 'vitest'

import { cleanTables, DB_MODEL } from '../test/DbCleaner'
import { DbCleaner } from '../test/DbCleaner'

import { PRISMA_NOT_FOUND_ERROR, PRISMA_SERIALIZATION_ERROR } from './prismaError'
import { prismaTransaction } from './prismaTransaction'
Expand All @@ -23,8 +23,14 @@ const TEST_ITEM_2: Item2 = {
value: 'two',
}

enum DB_MODEL {
item1 = 'item1',
item2 = 'item2',
}

describe('prismaTransaction', () => {
let prisma: PrismaClient
const dbCleaner: DbCleaner<DB_MODEL> = new DbCleaner('public', DB_MODEL)

beforeAll(() => {
prisma = new PrismaClient({
Expand All @@ -33,7 +39,7 @@ describe('prismaTransaction', () => {
})

beforeEach(async () => {
await cleanTables(prisma, [DB_MODEL.item1, DB_MODEL.item2])
await dbCleaner.cleanTables(prisma)
})

afterAll(async () => {
Expand Down
21 changes: 11 additions & 10 deletions packages/app/prisma-utils/test/DbCleaner.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { PrismaClient } from '@prisma/client'

export enum DB_MODEL {
item1 = 'item1',
item2 = 'item2',
}
export class DbCleaner<DbModel extends object> {
private readonly schema: string
private readonly tableNames: string[]

export async function cleanTables(prisma: PrismaClient, modelNames: readonly DB_MODEL[]) {
const delegates = modelNames.map<{ deleteMany: () => Promise<unknown> }>(
(modelName) => prisma[modelName],
)
constructor(schema: string, dbModel: DbModel) {
this.schema = schema
this.tableNames = Object.values(dbModel)
}

for (const delegate of delegates) {
await delegate.deleteMany()
async cleanTables(prisma: PrismaClient) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it better to only clear the necessary subset of given tables, from perf perspective?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, overoptimized the code

Copy link
Collaborator Author

@Dosexe Dosexe May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it looks to me, that we can simply expose one function without a class:) Unless we want to enclose the schema

for (const table of this.tableNames) {
await prisma.$executeRawUnsafe(`TRUNCATE TABLE ${this.schema}.${table} CASCADE;`)
}
}
}
Loading