justinhandley
justinhandley
PPrisma
Created by justinhandley on 12/7/2024 in #help-and-questions
Optimize in a Constructor
That is amazing, thanks @RaphaelEtim - but it still isn't working form me.. Here is my latest.
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { Prisma, PrismaClient } from '@prisma/client'
import { withOptimize } from '@prisma/extension-optimize'

@Injectable()
export class ApiCoreDataAccessService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
public queryCount: number

constructor() {
const config: any = {
datasources: {
db: { url: `${process.env.DATABASE_URL}?connection_limit=30` },
},
log: [
{
emit: 'event',
level: 'query',
},
],
}

super(config)
this.queryCount = 0
}

public async onModuleDestroy(): Promise<void> {
await this.$disconnect()
}

public async onModuleInit(): Promise<void> {
const customPrismaClient = new PrismaClient().$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }))

Object.assign(this, customPrismaClient)

await this.$connect()

if (process.env.LOG_PRISMA_QUERIES === 'true') {
this.$on('query' as never, async (e: Prisma.QueryEvent) => {
console.log(`QUERY: ${e.query} \n\nPARAMS: ${e.params}\n\n\n`)
})
}

if (process.env.COUNT_PRISMA_QUERIES === 'true') {
this.$on('query' as never, async () => {
this.queryCount++
})
}
}

search(conditions: any[], searchQuery: string, searchFields: string[]): any[] {
const query = searchQuery.trim()
const terms: string[] = query?.includes(' ') ? query.split(' ') : [query].filter(Boolean)
const searchFilters = terms.map((term) => ({
OR: searchFields.map((field) => ({
[field]: { contains: term, mode: Prisma.QueryMode.insensitive },
})),
}))

conditions.push(...searchFilters)
return conditions
}
}
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { Prisma, PrismaClient } from '@prisma/client'
import { withOptimize } from '@prisma/extension-optimize'

@Injectable()
export class ApiCoreDataAccessService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
public queryCount: number

constructor() {
const config: any = {
datasources: {
db: { url: `${process.env.DATABASE_URL}?connection_limit=30` },
},
log: [
{
emit: 'event',
level: 'query',
},
],
}

super(config)
this.queryCount = 0
}

public async onModuleDestroy(): Promise<void> {
await this.$disconnect()
}

public async onModuleInit(): Promise<void> {
const customPrismaClient = new PrismaClient().$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }))

Object.assign(this, customPrismaClient)

await this.$connect()

if (process.env.LOG_PRISMA_QUERIES === 'true') {
this.$on('query' as never, async (e: Prisma.QueryEvent) => {
console.log(`QUERY: ${e.query} \n\nPARAMS: ${e.params}\n\n\n`)
})
}

if (process.env.COUNT_PRISMA_QUERIES === 'true') {
this.$on('query' as never, async () => {
this.queryCount++
})
}
}

search(conditions: any[], searchQuery: string, searchFields: string[]): any[] {
const query = searchQuery.trim()
const terms: string[] = query?.includes(' ') ? query.split(' ') : [query].filter(Boolean)
const searchFilters = terms.map((term) => ({
OR: searchFields.map((field) => ({
[field]: { contains: term, mode: Prisma.QueryMode.insensitive },
})),
}))

conditions.push(...searchFilters)
return conditions
}
}
5 replies