justinhandley
justinhandley
PPrisma
Created by justinhandley on 12/7/2024 in #help-and-questions
Optimize in a Constructor
I use prisma in a nest.js api and instantiate a globally used version in a constructor - I've been trying to get optimize to work, and while the console log says it is set up and good to go, no data ever flows through. It looks like this - any idea what I'm doing wrong, or how to do this correctly?
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',
},
],
}

console.log('process.env.DATABASE_URL', process.env.DATABASE_URL)

super(config)
this.queryCount = 0

console.log('Initializing Prisma Optimize...')

try {
this.$extends(
withOptimize({
apiKey: process.env.OPTIMIZE_API_KEY,
}),
)
console.log('Prisma Optimize extension initialized successfully')
} catch (error) {
console.error('Failed to initialize Prisma Optimize:', error)
}
}

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

public async onModuleInit(): Promise<void> {
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++
})
}
}
}
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',
},
],
}

console.log('process.env.DATABASE_URL', process.env.DATABASE_URL)

super(config)
this.queryCount = 0

console.log('Initializing Prisma Optimize...')

try {
this.$extends(
withOptimize({
apiKey: process.env.OPTIMIZE_API_KEY,
}),
)
console.log('Prisma Optimize extension initialized successfully')
} catch (error) {
console.error('Failed to initialize Prisma Optimize:', error)
}
}

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

public async onModuleInit(): Promise<void> {
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++
})
}
}
}
5 replies
PPrisma
Created by justinhandley on 8/15/2024 in #help-and-questions
Understanding Prisma Pulse
I'm trying to figure out if there is any use case for me for Prisma Pulse - when I hear real time subscriptions, I think firebase - where I'm building a front-end app that uses prisma directly to query / load data from the database. I have an app with an api and several front ends - the API is a nest server, and we use apollo for subscriptions right now. In this setup, there would be no real benefit to pulse as we already have real time data subscriptions - am I wrong?
4 replies