Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate companion data from json #2929

Open
ChizobaAmadi opened this issue Mar 21, 2024 · 0 comments
Open

Generate companion data from json #2929

ChizobaAmadi opened this issue Mar 21, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ChizobaAmadi
Copy link

Is your feature request related to a problem? Please describe.
Hello,
I am currently working on implementing data synchronization. Since the server will receive partial data from clients, it will be nice to have a seamless way to map this data to drift.

Describe the solution you'd like
I propose adding an option in the build.yaml to create a fromJson method for the companion data class.

I have implemented a workaround following the guide in this previous issue, however, the solution is no longer as straightforward since I make use of DialectAwareSqlType type. For example:

/// columns: Derived from table.columnsByName.
/// data: The json to convert
Map<String, Expression> convertToExpressionMap(
    Map<String, GeneratedColumn> columns, Map<String, dynamic> data) {
  return data.map((key, value) {
    final column = columns[key];
    final variable = switch (column?.type) {
      genericDate => Variable(fallBackDate.read(value), genericDate),
      genericTimestampTz =>
        Variable(fallbackTimestampTz.read(value), genericTimestampTz),
      genericTimestamp =>
        Variable(fallbackTimestamp.read(value), genericTimestamp),
      _ => Variable(value)
    };

    return MapEntry(key, variable);
  });
}

I also ried simplifying the solution by using the typeMapping.read() but this expects to parse a dart type and not JSON.

It would be much better (developer-wise) and safer if the companion could support parsing JSON. Thanks !!

@ChizobaAmadi ChizobaAmadi added the enhancement New feature or request label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant