Repository navigation
feat: Allow generating shared models between server and client - #4737
Conversation
26c35d5 to
cb23c11
Compare
cb23c11 to
5d2f46e
Compare
|
@marcelomendoncasoares Would this allow us to define a For instance if I defined a Let me know if this is off topic and I can create a feature request to move the discussions to. |
@FXschwartz Precisely! Shared packages won't allow the definition of table models, but you'll be able to declare a base model on your shared package and extend it on the server to add a |
This PR introduces the ability to create shared packages that contains only models and the protocol file. This shared package depends exclusively on the
serverpod_serializationpackage and is safe to be imported in both the server and client code.Closes: #3339
Closes: #993
Closes: #3463
The shared package is tied to the project through the
shared_packagesfield in theconfig/generator.yamlfile, but the models and protocol file are generated in the package's own directory. This means that the code on the shared package is generated as part of theserverpod generatecommand called for the project.After merge, we can work on having this shared package as available in the template to close the original issue: #1256.
Related:
Restrictions
Nearly all features are supported on shared models, with the exception of:
tableproperty, since the models are not tied to a database.scopeproperty toserverOnlyon model fields, since models are shared.serverOnlyproperty on the model class, since models are shared.Notes for reviewers
This PR can be reviewed commit by commit for a better comprehension of the changes. The main changes are:
generator.yamlfile.ModelSourceandSerializableModelDefinitionto identify shared models.TypeDefinitionto generate correct references to shared models.DartServerCodeGeneratorandDartClientCodeGenerator.DartSharedCodeGeneratoras a copy of theDartClientCodeGenerator, but targeting the shared models.Regarding tests
Since this feature introduces a new
DartSharedCodeGenerator, the applying tests for theDartServerCodeGeneratorandDartClientCodeGeneratorwere copied and adapted to the new generator. There are only two test files that cover functionality specific to the shared package generator and are worth reviewing:shared_code_generator_test.dartshared_models_extends_test.dartshared_models_restrictions_test.dartPre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Breaking changes
None.