Closed Firgrep closed 11 months ago
Is there a way for prisma to return the table name/type? The thing about a type
property which is a string
is that it would introduce a runtime vulnerability, as some shoddy line of code, or a database insert, could conceivably introduce a table entry with a typo in that property, so that when it is fetched the front-end has no idea what to do with it.
To try to solve this runtime vulnerability, we would have to introduce a boilerplate check to every instance of that property being set. But this is only suboptimal in this context as it doesn't actually give compile-time safety, since it'd just be string comparisons which can only be evaluated at runtime.
The full compile-time solution is to make type
an Enum property in the model itself, but then we would have a triple repetition of information, in table name, type
, and the enum enumerations of the possible types, which seems like too much redundancy. I think keeping the information content of the DB as low as possible is ideal.
If LessonContent
, LessonTranscript
and CourseDetail
could all be abstracted into an MdxResource
or something, then a type
property in this super-model would make more sense, as it introduces some new information. But, in the case of needing to have three different models (and it makes sense to have three different models as they do serve different roles), I think it's a redundancy that would introduce more complications at the resource creation level.
But this is predicated on prisma providing some type checking capability.
Just to add to the above; in general, I fear database fields above application logic.
An error in the database is unknown until a user that needs that data retrieves it, and it won't be available to a Dev using a development environment especially at later stages of development when there will probably be a distinction between customer data, production data and test data.
An error in the application logic, however, can be spotted by a developer at any time in the development life cycle and can be fixed with a new commit for the dev-branch and new release for the customer.
Code, to me, should be a malleable thing as long as use-cases are fulfilled (and their abuses, known) [I guess this is what TDD is, even though we aren't following that per se], and data as static and non-volatile as possible. And database fields which add redundancy is one of the ways in which maintaining this principle becomes more difficult.
Of course, there is no strict distinction between errors in application logic and erroneous data, the former leads to the latter too. But that statement just seems like a negative reformulation of what coding is in general. In the movement between (logic) -> (data), the movement should be restricted enough to filter out the predictable errors, and one way of working towards that aim is to limit the number of modifiable fields and the possible variance between data sets/rows.
I was thinking through this while writing, so I hope I haven't been too repetitive or trite in my points.
Thanks @ej-choi ! I didn't think of looking if Prisma had a model-checking, but making some quick searches now it seems that, apart from type-checking, this isn't possible. Type-checking also isn't ideal insofar as we might have two models with identical fields: if, for example, LessonContent
and LessonTranscript
, all have the same fields (yet their relation would be different, but that will be controlled on the db side), this may be picked up by TS but I suspect it will be enough to discriminate in runtime(?).
This would work for non-identical fields.
if (typeof material === typeof prisma.lessonContent) {
// do stuff
}
But I am thinking of making the fields of LessonContent
and LessonTranscript
identical as that would bypass the need to check them insofar as retrieval for the MDX compiler is concerned. But to identify the models (for UI for example), I think we should add a new discriminatory field on each model.
Your concern about runtime vulnerabilities and modifiable data is fair, which is why I think this would be controlled closest to the db with an enum
defined in the schema (like what we have done with user Role
), such that whenever a new entry is made of LessonContent
, LessonTranscript
or CourseDetails
, a discriminatory field is defined as well at creation. There would be no need to alter this field afterwards and it would only be for reading purposes, so we don't need to worry about corrupt/unknown/tampered data here, since it should never be changed (same way the ID of a model is never changed after its creation). This should satisfy your concerns on runtime vulnerabilities and modifiable data? Otherwise please correct me if I missed something.
We could conceivably bypass adding new fields by creating additional query to the db, but that seems expensive to me. If the question is adding one more field vs adding one (or more) trips to the db, I'd take adding one more field (provided we're not talking several MBs and more).
So I'm leaning towards adding a new field on those "MDX models" that is an enum defined in the schema and also necessarily defined at entry creation, which could actually just be model <ModelName>
, and changing the content
, transcript
fields into mdx
. What do you think?
(To avoid confusing "type" with TypeScript types, we should avoid using the word "type" for anything but TS related stuff.)
Also, while I agree with you on that code should be malleable--which I think you outlined very well--I don't think we need to be shy about using the db to our needs. When you say "keeping the information content of the DB as low as possible is ideal", I would say that it depends on the type of information more than the volume, namely, persistent information (the db is basically the memory of the app). However, totally agree with limiting the number of modifiable fields and the possible variance between data sets/rows. Less possible worlds, the better.
It's a shame prisma doesn't have model-checking, and I agree that type-checking by itself isn't very useful in this context. The enum method seems like the best solution. And yes, I agree that at this development stage the DB schema itself isn't set in stone so we shouldn't be afraid to change the data fields.
Yes, but one thing Prisma does nicely provide is to allow for default values (I had forgotten about this even though we're already using it for the User model!). Setting default values means we don't need any specific application logic and we can leave the entire thing to the db side. I was typing this out yesterday (not yet pushed to db) and I think we'll run with it:
enum MdxCategory {
CONTENT
TRANSCRIPT
DETAILS
}
model CourseDetails {
id String @id @default(cuid())
course Course @relation(fields: [courseId], references: [id])
courseId String @unique
mdxCategory MdxCategory @default(DETAILS)
mdx Bytes
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model LessonContent {
id String @id @default(cuid())
lesson Lesson @relation(fields: [lessonId], references: [id])
lessonId String @unique
mdxCategory MdxCategory @default(CONTENT)
mdx Bytes
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model LessonTranscript {
id String @id @default(cuid())
lesson Lesson @relation(fields: [lessonId], references: [id])
lessonId String @unique
mdxCategory MdxCategory @default(TRANSCRIPT)
mdx Bytes
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
This is a simple question but I think it reflects a broader issue. When we want to identify certain data, we can create a logic around existing fields or add new fields to existing models. I am wondering what is generally the best long-term approach to this. Let me use an example from our app.
As part of the MDX editor, I had added a header that displays the current type of file being edited - given one could either be editing the lesson content or lesson transcript (and now we also have course details into the mix) - it is very handy for the user to know where one is. So if the user is editing the lesson content, the heading would read "Editing content of
The function expects a data model and basically returns an array of only the mdx and type based on whether the field of the model is "content" or "transcript".
Currently, course details is not supported, which prompted this discussion in my head, and what if we add further models down the road that also need the editor?
So instead of having to tailor the application logic, we create a new field on the model called "type" that is checked in the editor component. No need to additional utility functions.
Further changes
Incidentally, since we now have three "mdx models" which are nearly the same, I'm thinking of changing the name of the field containing the mdx to just
mdx
on all three and have the field "type" added to each to differentiate them.So, just to be clear, here is the current schema:
And I propse:
What do people think? Better to change the db or change the application? I'm leaning towards changing the db here, but I'd appreciate broader reflections on this kind of issue/pattern.