Skip to content

Instantly share code, notes, and snippets.

@keizie
Created February 12, 2025 12:03
Show Gist options
  • Save keizie/d727154f17c0c31b43212261d3608dfd to your computer and use it in GitHub Desktop.
Save keizie/d727154f17c0c31b43212261d3608dfd to your computer and use it in GitHub Desktop.
nest.js service from prisma $extends (type 2)
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