Group Group Group Group Group Group Group Group Group

TILModels Swift Package for Client & Server Models?

@0xtim Can a single model (e.g. Acronym) be shared between client and server via a TILModels swift package? Obviously the attributes used to model the Fluent relationships wouldn’t be utilized by the client, but can’t they just be ignored or excluded via a compiler directive? It seems like having a universal model package would be a significant advantage in an app with a nontrivial data model.

Something like this is alluded to at the bottom of “Getting started” in chapter 12, but it isn’t fully explained. Given it’s Swift on both ends it would be nice to avoid having models and DTOs.

So if you want to share the full model, property wrappers an all then you need to add Fluent as a dependency to your iOS app (which is certainly possible). You could wrap all the property wrappers in compiler checks but you’d likely hit encoding/decoding errors with relationships.

DTOs are by far the best way to control this and in my experience, for any app with non-trivial data models it’s the only way forward. For instance, having a User model with a password and date of birth, you don’t want to return the password at all in any API call and only want to return the DoB when it’s that user requesting their own data.

We have an article on sharing code here

Hope that helps!

1 Like

I appreciate the detailed response. This is helpful.

Thank you for the great book. I’ll check out the Sharing Swift Code tutorial.

1 Like