Skip to content

Commit

Permalink
Merge pull request #60 from mmvergara/camelcased-columns
Browse files Browse the repository at this point in the history
Camel Cased Columns
  • Loading branch information
mmvergara authored Aug 21, 2024
2 parents 71ee417 + 824355f commit e6917a9
Show file tree
Hide file tree
Showing 54 changed files with 1,126 additions and 1,136 deletions.
16 changes: 14 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ dart pub add intl

> Unless you are not using any date types, you can skip this step
#### 1.4 Use snake casing for table names and column names (Optional)

this tool will automatically convert snake_case to camelCase for both table (ClassName) and column (FieldName).

```
snake_case => camelCase
user_table => UserTable
```

```
snake_case => camelCase
user_id => userId
```

## 2. Generate Dart Classes

### Using the [Web App](https://supadart.vercel.app/)
Expand Down Expand Up @@ -187,8 +201,6 @@ supadart -u <your-supabase-url> -k <your-supabase-anon-key>
Assuming the following table schema <br/>
> we recommend using **snake_casing** for your table names, as it will be converted to **PamelCasing** in the generated classes.
```sql
create table
public.books (
Expand Down
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.5.3

- Changed model fields to be camel casing

## 1.5.2

- Fix extra " generated on enum values
Expand Down
8 changes: 4 additions & 4 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ separated: true
dart: false
# Optional, used to map table names to class names(case-sensitive)
# mappings:
# books: book
# categories: category
# children: child
# people: person
# books: book
# categories: category
# children: child
# people: person
```

#### Generate Classes
Expand Down
2 changes: 1 addition & 1 deletion cli/bin/supadart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:yaml/yaml.dart';
import 'package:supadart/generator/generator.dart';
import 'package:supadart/generator/swagger.dart';

const String version = 'v1.5.2';
const String version = 'v1.5.3';
const String red = '\x1B[31m'; // Red text
const String green = '\x1B[32m'; // Green text
const String blue = '\x1B[34m'; // Blue text
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/generator/from_json_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ String parseWrapper(String dartType, Column columnDetails, String columnName) {
format = columnDetails.postgresFormat.replaceAll('[]', '');
}

String jsonValue = 'json[\'$columnName\']';
String jsonValue = 'json[\'${columnDetails.dbColName}\']';
String output = '$jsonValue != null ?';

switch (dartType) {
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/generator/insert_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ String generateInsertMethod(

buffer.writeln(isRequired
? "'$columnName': ${toJsonEncodable(dartType, columnDetails.postgresFormat, columnName, columnDetails)},"
: 'if ($columnName != null) \'$columnName\': ${toJsonEncodable(dartType, columnDetails.postgresFormat, columnName, columnDetails)},');
: 'if ($columnName != null) \'${columnDetails.dbColName}\': ${toJsonEncodable(dartType, columnDetails.postgresFormat, columnName, columnDetails)},');
});
buffer.writeln('};');
buffer.writeln('}');
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/generator/static_column_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'swagger.dart';

String generateStaticColumnNames(Map<String, Column> properties) {
String code = "";
properties.forEach((propertyName, _) {
code += "static String get c_$propertyName => '$propertyName';\n";
properties.forEach((propertyName, col) {
code += "static String get c_$propertyName => '${col.dbColName}';\n";
});
return "$code\n";
}
18 changes: 15 additions & 3 deletions cli/lib/generator/swagger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class DatabaseSwagger {
}
}

// snake casing to camel casing
String snakeCaseToCamelCase(String input) {
int i = 0;
return input.split('_').map((String word) {
if (i++ == 0) return word;
return word[0].toUpperCase() + word.substring(1);
}).join('');
}

// Class to represent a database table
class Table {
final String name;
Expand All @@ -58,15 +67,16 @@ class Table {
requiredFields: json['required'] != null
? List<String>.from(json['required'])
: <String>[],
columns:
properties.map((key, value) => MapEntry(key, Column.fromJson(value))),
columns: properties.map((key, value) =>
MapEntry(snakeCaseToCamelCase(key), Column.fromJson(value, key))),
);
}
}

// Class to represent a database column
class Column {
final String postgresFormat;
final String dbColName;
final List<String> enumValues;
final dynamic defaultValue;
final String? description;
Expand All @@ -76,6 +86,7 @@ class Column {

Column({
required this.postgresFormat,
required this.dbColName,
this.enumValues = const [],
this.defaultValue,
this.description,
Expand All @@ -93,9 +104,10 @@ class Column {

bool get hasDefaultValue => defaultValue != null;

factory Column.fromJson(Map<String, dynamic> json) {
factory Column.fromJson(Map<String, dynamic> json, String colName) {
return Column(
postgresFormat: json['format'],
dbColName: colName,
enumValues:
json['enum'] != null ? List<String>.from(json['enum']) : <String>[],
defaultValue: json['default'],
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/generator/update_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ String generateUpdateMethod(

columns.forEach((columnName, columnDetails) {
code +=
"if ($columnName != null) '$columnName': ${toJsonEncodable(columnDetails.dartType, columnDetails.postgresFormat, columnName, columnDetails)},\n";
"if ($columnName != null) '${columnDetails.dbColName}': ${toJsonEncodable(columnDetails.dartType, columnDetails.postgresFormat, columnName, columnDetails)},\n";
});

code += '};\n';
Expand Down
2 changes: 1 addition & 1 deletion cli/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: supadart
description: Generate Dart classes from your Supabase schema.
version: 1.5.2
version: 1.5.3

repository: https://github.com/mmvergara/supadart
homepage: https://github.com/mmvergara/supadart
Expand Down
10 changes: 5 additions & 5 deletions cli/test/datatypes/boolean_bit/bool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ Future<void> performBooleanTest(SupabaseClient supabase) async {
var readResult = await readBoolean(supabase);
assert(readResult is List<BooleanBitTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_boolean, isA<bool>());
expect(readResult[0].col_boolean, updatedBoolean);
expect(readResult[0].colBoolean, isA<bool>());
expect(readResult[0].colBoolean, updatedBoolean);
});
}

Future<Object?> createBoolean(SupabaseClient supabase, bool insertVal) async {
try {
await supabase.boolean_bit_types.insert(BooleanBitTypes.insert(
col_boolean: insertVal,
colBoolean: insertVal,
));
return null;
} catch (error) {
Expand All @@ -45,9 +45,9 @@ Future<Object?> updateBoolean(
try {
await supabase.boolean_bit_types
.update(BooleanBitTypes.update(
col_boolean: value,
colBoolean: value,
))
.eq(BooleanBitTypes.c_col_boolean, oldValue);
.eq(BooleanBitTypes.c_colBoolean, oldValue);
return null;
} catch (error) {
print("updateBoolean error");
Expand Down
8 changes: 4 additions & 4 deletions cli/test/datatypes/boolean_bit/bool_arr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Future<void> performBooleanArrayTest(SupabaseClient supabase) async {
var readResult = await readBooleanArray(supabase);
assert(readResult is List<BooleanBitTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_boolean_array, isA<List<bool>>());
expect(readResult[0].col_boolean_array![0], updatedBooleanArray[0]);
expect(readResult[0].colBooleanArray, isA<List<bool>>());
expect(readResult[0].colBooleanArray![0], updatedBooleanArray[0]);
});
}

Expand All @@ -33,7 +33,7 @@ Future<Object?> createBooleanArray(
try {
await supabase.boolean_bit_types.insert(BooleanBitTypes.insert(
id: uuidx,
col_boolean_array: insertVal,
colBooleanArray: insertVal,
));
return null;
} catch (error) {
Expand All @@ -46,7 +46,7 @@ Future<Object?> updateBooleanArray(
try {
await supabase.boolean_bit_types
.update(BooleanBitTypes.update(
col_boolean_array: value,
colBooleanArray: value,
))
.eq(BooleanBitTypes.c_id, uuidx);
return null;
Expand Down
14 changes: 7 additions & 7 deletions cli/test/datatypes/datetime/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ Future<void> performDateTest(SupabaseClient supabase) async {
var readResult = await readDate(supabase);
assert(readResult is List<DatetimeTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_date?.year, updatedDate.year);
expect(readResult[0].col_date?.month, updatedDate.month);
expect(readResult[0].col_date?.day, updatedDate.day);
expect(readResult[0].col_date, isA<DateTime>());
expect(readResult[0].colDate?.year, updatedDate.year);
expect(readResult[0].colDate?.month, updatedDate.month);
expect(readResult[0].colDate?.day, updatedDate.day);
expect(readResult[0].colDate, isA<DateTime>());
});
}

Future<Object?> createDate(SupabaseClient supabase, DateTime insertVal) async {
try {
await supabase.datetime_types.insert(DatetimeTypes.insert(
col_date: insertVal,
colDate: insertVal,
));
return null;
} catch (error) {
Expand All @@ -61,8 +61,8 @@ Future<Object?> updateDate(
SupabaseClient supabase, DateTime oldValue, DateTime value) async {
try {
await supabase.datetime_types
.update(DatetimeTypes.update(col_date: value))
.eq(DatetimeTypes.c_col_date, oldValue);
.update(DatetimeTypes.update(colDate: value))
.eq(DatetimeTypes.c_colDate, oldValue);
return null;
} catch (error) {
print("updateDate error");
Expand Down
12 changes: 6 additions & 6 deletions cli/test/datatypes/datetime/date_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Future<void> performDateArrayTest(SupabaseClient supabase) async {
var readResult = await readDateArray(supabase);
assert(readResult is List<DatetimeTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_date_array, isA<List<DateTime>>());
expect(readResult[0].colDateArray, isA<List<DateTime>>());
for (int i = 0; i < insertDate.length; i++) {
expect(readResult[0].col_date_array![i].year, updatedDate[i].year);
expect(readResult[0].col_date_array![i].month, updatedDate[i].month);
expect(readResult[0].col_date_array![i].day, updatedDate[i].day);
expect(readResult[0].colDateArray![i].year, updatedDate[i].year);
expect(readResult[0].colDateArray![i].month, updatedDate[i].month);
expect(readResult[0].colDateArray![i].day, updatedDate[i].day);
}
});
}
Expand All @@ -39,7 +39,7 @@ Future<Object?> createDateArray(
try {
await supabase.datetime_types.insert(DatetimeTypes.insert(
id: uuidx,
col_date_array: insertVal,
colDateArray: insertVal,
));
return null;
} catch (error) {
Expand All @@ -65,7 +65,7 @@ Future<Object?> updateDateArray(SupabaseClient supabase,
List<DateTime> oldValue, List<DateTime> value) async {
try {
await supabase.datetime_types
.update(DatetimeTypes.update(col_date_array: value))
.update(DatetimeTypes.update(colDateArray: value))
.eq(DatetimeTypes.c_id, uuidx);
return null;
} catch (error) {
Expand Down
14 changes: 7 additions & 7 deletions cli/test/datatypes/datetime/time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ Future<void> performTimeTest(SupabaseClient supabase) async {
var readResult = await readTime(supabase);
assert(readResult is List<DatetimeTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_time?.hour, updatedTime.hour);
expect(readResult[0].col_time?.minute, updatedTime.minute);
expect(readResult[0].col_time?.second, updatedTime.second);
expect(readResult[0].col_time, isA<DateTime>());
expect(readResult[0].colTime?.hour, updatedTime.hour);
expect(readResult[0].colTime?.minute, updatedTime.minute);
expect(readResult[0].colTime?.second, updatedTime.second);
expect(readResult[0].colTime, isA<DateTime>());
});
}

Future<Object?> createTime(SupabaseClient supabase, DateTime insertVal) async {
try {
await supabase.datetime_types.insert(DatetimeTypes.insert(
col_time: insertVal,
colTime: insertVal,
));
return null;
} catch (error) {
Expand All @@ -61,8 +61,8 @@ Future<Object?> updateTime(
SupabaseClient supabase, DateTime oldValue, DateTime value) async {
try {
await supabase.datetime_types
.update(DatetimeTypes.update(col_time: value))
.eq(DatetimeTypes.c_col_time, oldValue);
.update(DatetimeTypes.update(colTime: value))
.eq(DatetimeTypes.c_colTime, oldValue);
return null;
} catch (error) {
print("updateTime error");
Expand Down
12 changes: 6 additions & 6 deletions cli/test/datatypes/datetime/time_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Future<void> performTimeArrayTest(SupabaseClient supabase) async {
var readResult = await readTimeArray(supabase);
assert(readResult is List<DatetimeTypes>);
expect(readResult!.length, 1);
expect(readResult[0].col_time_array, isA<List<DateTime>>());
expect(readResult[0].colTimeArray, isA<List<DateTime>>());
for (int i = 0; i < insertTime.length; i++) {
expect(readResult[0].col_time_array![i].hour, updatedTime[i].hour);
expect(readResult[0].col_time_array![i].minute, updatedTime[i].minute);
expect(readResult[0].col_time_array![i].second, updatedTime[i].second);
expect(readResult[0].colTimeArray![i].hour, updatedTime[i].hour);
expect(readResult[0].colTimeArray![i].minute, updatedTime[i].minute);
expect(readResult[0].colTimeArray![i].second, updatedTime[i].second);
}
});
}
Expand All @@ -39,7 +39,7 @@ Future<Object?> createTimeArray(
SupabaseClient supabase, List<DateTime> insertVal) async {
try {
await supabase.datetime_types
.insert(DatetimeTypes.insert(col_time_array: insertVal, id: uuidx));
.insert(DatetimeTypes.insert(colTimeArray: insertVal, id: uuidx));
return null;
} catch (error) {
return error;
Expand All @@ -63,7 +63,7 @@ Future<Object?> updateTimeArray(SupabaseClient supabase,
List<DateTime> oldValue, List<DateTime> value) async {
try {
await supabase.datetime_types
.update(DatetimeTypes.update(col_time_array: value))
.update(DatetimeTypes.update(colTimeArray: value))
.eq(DatetimeTypes.c_id, uuidx);
return null;
} catch (error) {
Expand Down
Loading

0 comments on commit e6917a9

Please sign in to comment.