Created
February 12, 2025 12:03
-
-
Save keizie/d727154f17c0c31b43212261d3608dfd to your computer and use it in GitHub Desktop.
nest.js service from prisma $extends (type 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common' | |
import { PrismaClient } from '@prisma/client' | |
import { faker } from '@faker-js/faker' | |
@Injectable() | |
export class DatabaseService | |
extends PrismaClient | |
implements OnModuleInit, OnModuleDestroy | |
{ | |
constructor() { | |
super() | |
const self = this | |
Object.assign( | |
this, | |
new PrismaClient().$extends({ | |
name: 'webinarAfterCreate', | |
query: { | |
webinar: { | |
async create({ args, query }) { | |
console.log('webinarAfterCreate', args) | |
const result = await query(args) | |
console.log(result) | |
// afterCreate logic | |
console.log( | |
await self.topBanner.create({ | |
data: { | |
title: faker.commerce.product(), | |
url: | |
'https://papago.naver.com/' + | |
result.title, | |
}, | |
}), | |
) | |
return result | |
}, | |
}, | |
}, | |
}), | |
) | |
} | |
async onModuleInit() { | |
await this.$connect() | |
} | |
async onModuleDestroy() { | |
await this.$disconnect() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment