Replies: 2 comments 10 replies
-
Hello @abournier, You shouldn't catch the insertion error. I don't understand why so many developers have such a urge to hide errors by catching and printing them, so that nobody can never look at it and handle it once the error happens in production. It's important to be able to look at errors straight in the eyes, because this is the real way to make applications robust. Your sample code should instead read as below, with all the useless error handling removed: extension Wine {
func insert(_ db: Database) throws {
generateUuidStringAsId()
try performInsert(db)
}
} But now I don't find that it's that much heavier than: extension Wine {
func beforeCreate() {
generateUuidStringAsId()
}
} And I don't understand how callbacks would make your code significantly simpler. I understand you may be used to ActiveRecord callbacks, but I'm not sure it's significantly more difficult to get used to the GRDB technique. |
Beta Was this translation helpful? Give feedback.
-
@abournier, GRDB 6 has callbacks :-) |
Beta Was this translation helpful? Give feedback.
-
Hello,
I just start learning GRDB. It looks like amazing.
However, is there a feature like Callbacks in Rails ? (beforeSave, afterSave, beforeCreate...)
I have found "didInsert" after an insert is performed. But is there an equivalent to "beforeSave" ? It could be useful, for example, to share code between multiple models.
As an example, I used this to generate at the right place an Id as UUID string:
But I have 10 models using an UUID string as Id. If I could have something like:
without having to implement
insert()
for all my models, it could be very helpful.Beta Was this translation helpful? Give feedback.
All reactions