chore(db): add logger to mutations
This commit is contained in:
@@ -1,16 +1,39 @@
|
|||||||
|
import { createLogger } from '@openpanel/logger';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import { readReplicas } from '@prisma/extension-read-replicas';
|
import { readReplicas } from '@prisma/extension-read-replicas';
|
||||||
|
|
||||||
export * from '@prisma/client';
|
export * from '@prisma/client';
|
||||||
|
|
||||||
|
const logger = createLogger({ name: 'db' });
|
||||||
|
|
||||||
const getPrismaClient = () => {
|
const getPrismaClient = () => {
|
||||||
return new PrismaClient({
|
const prisma = new PrismaClient({
|
||||||
log: ['error'],
|
log: ['error'],
|
||||||
}).$extends(
|
})
|
||||||
|
.$extends(
|
||||||
readReplicas({
|
readReplicas({
|
||||||
url: process.env.DATABASE_URL_REPLICA ?? process.env.DATABASE_URL!,
|
url: process.env.DATABASE_URL_REPLICA ?? process.env.DATABASE_URL!,
|
||||||
}),
|
}),
|
||||||
);
|
)
|
||||||
|
.$extends({
|
||||||
|
query: {
|
||||||
|
async $allOperations({ operation, model, args, query }) {
|
||||||
|
if (
|
||||||
|
operation === 'create' ||
|
||||||
|
operation === 'update' ||
|
||||||
|
operation === 'delete'
|
||||||
|
) {
|
||||||
|
logger.info('Prisma operation', {
|
||||||
|
operation,
|
||||||
|
args,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return query(args);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return prisma;
|
||||||
};
|
};
|
||||||
|
|
||||||
const globalForPrisma = globalThis as unknown as {
|
const globalForPrisma = globalThis as unknown as {
|
||||||
|
|||||||
@@ -116,7 +116,27 @@ const enforceAccess = t.middleware(async ({ ctx, next, rawInput }) => {
|
|||||||
|
|
||||||
export const createTRPCRouter = t.router;
|
export const createTRPCRouter = t.router;
|
||||||
|
|
||||||
export const publicProcedure = t.procedure;
|
const loggerMiddleware = t.middleware(
|
||||||
|
async ({ ctx, next, rawInput, path, input, type }) => {
|
||||||
|
// Only log mutations
|
||||||
|
if (type === 'mutation') {
|
||||||
|
ctx.req.log.info('TRPC mutation', {
|
||||||
|
path,
|
||||||
|
rawInput,
|
||||||
|
input,
|
||||||
|
userId: ctx.session?.userId,
|
||||||
|
organizationId: has('organizationId', rawInput)
|
||||||
|
? rawInput.organizationId
|
||||||
|
: undefined,
|
||||||
|
projectId: has('projectId', rawInput) ? rawInput.projectId : undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const publicProcedure = t.procedure.use(loggerMiddleware);
|
||||||
export const protectedProcedure = t.procedure
|
export const protectedProcedure = t.procedure
|
||||||
.use(enforceUserIsAuthed)
|
.use(enforceUserIsAuthed)
|
||||||
.use(enforceAccess);
|
.use(enforceAccess)
|
||||||
|
.use(loggerMiddleware);
|
||||||
|
|||||||
Reference in New Issue
Block a user