Skip to content

Commit

Permalink
feat: Add option use_sql_column_name_as_json_key
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal committed Mar 5, 2024
1 parent ca0c70e commit 707eb72
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 9 additions & 1 deletion drift_dev/lib/src/analysis/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ class DriftOptions {
defaultValue: true)
final bool useColumnNameAsJsonKeyWhenDefinedInMoorFile;

/// Uses the sql column name as the json key instead of the name in dart.
///
/// Overrides [useColumnNameAsJsonKeyWhenDefinedInMoorFile] when set to `true`.
@JsonKey(name: 'use_sql_column_name_as_json_key', defaultValue: false)
final bool useSqlColumnNameAsJsonKey;

/// Generate a `connect` constructor in database superclasses.
///
/// This makes drift generate a constructor for database classes that takes a
/// `DatabaseConnection` instead of just a `QueryExecutor` - meaning that
/// stream queries can also be shared across multiple database instances.
/// Starting from drift 2.5, the database connection class implements the
/// `QueryExecutor` interface, making this option unecessary.
/// `QueryExecutor` interface, making this option unnecessary.
@JsonKey(name: 'generate_connect_constructor', defaultValue: false)
final bool generateConnectConstructor;

Expand Down Expand Up @@ -120,6 +126,7 @@ class DriftOptions {
this.skipVerificationCode = false,
this.useDataClassNameForCompanions = false,
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = true,
this.useSqlColumnNameAsJsonKey = false,
this.generateConnectConstructor = false,
this.dataClassToCompanions = true,
this.generateMutableClasses = false,
Expand Down Expand Up @@ -147,6 +154,7 @@ class DriftOptions {
required this.skipVerificationCode,
required this.useDataClassNameForCompanions,
required this.useColumnNameAsJsonKeyWhenDefinedInMoorFile,
required this.useSqlColumnNameAsJsonKey,
required this.generateConnectConstructor,
required this.dataClassToCompanions,
required this.generateMutableClasses,
Expand Down
7 changes: 4 additions & 3 deletions drift_dev/lib/src/analysis/results/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ class DriftColumn implements HasType {
/// The actual json key to use when serializing a data class of this table
/// to json.
///
/// This respectts the [overriddenJsonName], if any, as well as [options].
/// This respects the [overriddenJsonName], if any, as well as [options].
String getJsonKey([DriftOptions options = const DriftOptions.defaults()]) {
if (overriddenJsonName != null) return overriddenJsonName!;

final useColumnName = options.useColumnNameAsJsonKeyWhenDefinedInMoorFile &&
declaredInDriftFile;
final useColumnName = options.useSqlColumnNameAsJsonKey ||
(options.useColumnNameAsJsonKeyWhenDefinedInMoorFile &&
declaredInDriftFile);
return useColumnName ? nameInSql : nameInDart;
}

Expand Down
5 changes: 5 additions & 0 deletions drift_dev/lib/src/generated/analysis/options.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 707eb72

Please sign in to comment.