Hello everyone! I am thinking about creating an au...
# orm-help
o
Hello everyone! I am thinking about creating an audit log. Whenever a customer does a CRUD operation it is logged in the DB with a model called
AuditLogEntry
. Im not sure this is 100% Prisma related but I'm curious if you believe this is viable? Specifically, I wonder if it will be too much data if a few hundred users click around all day and create
AuditLogEntry
everytime they load a page (read from a table), which they will do rather often. Thanks! EDIT: This is how I imagine the model to look like, to give an idea of the data size:
Copy code
model AuditLogEntry {
  id          String   @id @default(cuid())
  createdAt   DateTime @default(now()) @map("created_at")
  authorId    String   @map("author_id")
  author      User     @relation(fields: [authorId], references: [id], onDelete: Cascade)
  patient     Patient? @relation(fields: [patientId], references: [id], onDelete: Cascade)
  patientId   String?  @map("patient_id")
  subject     Subject?
  action      Action?
  title       String
  description String

  @@map("audit_log_entries")
}
1
n
Depends on how many users you’re having and file types introduced? roughly i would say the data size should only be a few MBs like 1-15MB for roughly 1000-+10000 data entries
o
Okay thank you! I have a few hundred users that click around 8 hours a day doing a few CRUDs a minute on average
l
depending on application, there are usually better tools for this like Google Analytics which will deal with a lot of the scaling in something like BigQuery, and be quite flexible in the meta data you pass along with it (i do this for three Flutter apps and two Web apps atm). That said, if you feel like just building it all yourselves (or you don't want any additional dependencies) then you can actually leverage Prisma pretty well with some relationship creation:
Copy code
model Post {
  id   String  @id @default(cuid())
  ...
  logs AuditLogEntry[] 
}

model AuditLogEntry {
  id      String   @id @default(cuid())
  ...
  post    Post?    @relation(fields: [post_id], references: [id])
  post_id String?
  ... (other CRUD Models?)

  @@map("audit_log_entries")
}
Copy code
const logData = (action)=>{
  logs: {
    create: {
      action,
      author: {
        connect: { autherID },
      },
    },
  },
};

const result = await prisma.post.create({
    data:{
      ...PostData,
      ...logData("CREATE"), 
    }
});
being able to create multiple nested relations is super nice with Prisma
n
Also, you could use middleware for AuditLogEntry, middlewares are executed anytime a query is invoked, so it would be a good idea to insert data into AuditLogEntry from middleware.