brick_offline_first_with_supabase_build 2.1.0
brick_offline_first_with_supabase_build: ^2.1.0 copied to clipboard
Code generation library for the Brick Offline First Domain using Supabase
Brick Offline First with Supabase Build #
Code generator that provides (de)serializing functions for Brick adapters using SupabaseProvider and SqliteProvider within the OfflineFirstWithSupabase domain. Classes annotated with ConnectOfflineFirstWithSupabase and extending the model OfflineFirstWithSupabase will be discovered.
Setup #
dart:mirrors will conflict with Flutter, so this package should be imported as a dev dependency and executed before an app's run time.
dev_dependencies:
brick_offline_first_with_supabase_build:
Build your code:
cd my_app; (flutter) pub run build_runner build
How does this work? #

-
A class is discovered with the
@ConnectOfflineFirstWithSupabaseannotation.@ConnectOfflineFirstWithSupabase( sqliteConfig: SqliteSerializable( nullable: false, ), supabaseConfig: SupabaseSerializable( tableName: 'users', ) ) class MyClass extends OfflineFirstWithSupabaseModel -
OfflineFirstGeneratorexpands respective sub configuration from the@ConnectOfflineFirstWithSupabaseconfiguration. -
Instances of
SupabaseFieldsandSqliteFieldsare created and passed to their respective generators. This will expand all fields of the class into consumable code. Namely, the#sortedmethod ensures there are no duplicates and the fields are passed in the order they're declared in the class. -
SupabaseSerialize,SupabaseDeserialize,SqliteSerialize, andSqliteDeserializegenerators are created from the previous configurations and the aforementioned fields. Since these generators inherit from the same base class, this documentation will continue withSupabaseSerializeas the primary example. -
The fields are iterated through
SupabaseSerialize#coderForFieldto generate the transforming code. This function produces output by checking the field's type. For example,final List<Future<int>> futureNumbersmay produce'future_numbers': await Future.wait<int>(futureNumbers). -
The output is gathered via
SupabaseSerialize#generateand wrapped in a function such asMODELToSupabase(). All such functions from all generators are included in the output of the adapter generator. As some down-stream providers or repositories may require extra information in the adapter (such assupabaseEndpointortableName), this data is also passed through#generate. -
Now with the complete adapter code, the AdapterBuilder saves
adapters/MODELNAME.g.dart. -
Now with all annotated classes having adapter counterparts, a model dictionary is generated and saved to
brick.g.dartwith the ModelDictionaryBuilder. -
Concurrently, the super generator may produce a new schema that reflects the new data structure.
SqliteSchemaGeneratorgenerates a new schema. UsingSchemaDifference, a new migration is created (this will be saved todb/migrations/VERSION_migration.dart). The new migration is logged and prepended to the generated code. This will be saved todb/schema.g.dartwith the SqliteSchemaBuilder. A new migration will be saved todb/<INCREMENT_VERSION>.g.dartwith the NewMigrationBuilder.
FAQ #
Why doesn't this library use JsonSerializable? #
While JsonSerializable is an incredibly robust library, it is, in short, opinionated. Just like this library is opinionated. This prevents incorporation in a number of ways:
@JsonSerializabledetects serializable models via a class method check. Since@ConnectOfflineFirstWithSupabaseuses an abstracted builder, checking the source class is not effective.@JsonSerializableonly supports enums as strings, not as indexes. While this is admittedly more resilient, it can’t be retrofitted to enums passed as integers from an API.- Lastly, dynamically applying a configuration is an uphill battle with
ConstantReader(the annotation would have to be converted into a digestable format). While ultimately this could be possible, the library is still unusable because of the aforementioned points.
JsonSerializable is an incredibly robust library and should be used for all other scenarios.