Skip to content

Commit

Permalink
Merge pull request #115 from mmvergara/enum-fix
Browse files Browse the repository at this point in the history
Enum fix
  • Loading branch information
mmvergara authored Dec 20, 2024
2 parents c74d881 + 2deccdf commit 8f80f83
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 18 deletions.
43 changes: 33 additions & 10 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ final allBooks = await supabase
- [**Insert Many Data**](#inset-many-data)
- [**Update Data**](#update-data)
- [**Delete Data**](#delete-data)
- [**Enums CRUD**](#enums-crud)
- [**Column Selection Queries**](#column-selection-queries)
- [**Working with Enums**](#enums-crud)
- [**Column Selection Queries**](#column-selection-queries)

### Features 🚀

Expand Down Expand Up @@ -140,8 +140,6 @@ dart pub global run supadart
# Initialize supadart.yaml config file
supadart --init

# Configure `supadart.yaml`

# Generate classes
supadart --url <supabase_url> --key <supabase_anon_key>

Expand All @@ -150,6 +148,9 @@ supadart --url <supabase_url> --key <supabase_anon_key>
supadart
```

If you have enums, you need to specify them in the config file

````yaml
#### CLI Usage

```bash
Expand All @@ -159,7 +160,7 @@ supadart
-u, --url Supabase URL (if not set in yaml)
-k, --key Supabase ANON KEY (if not set in yaml)
-v, --version
```
````
---
Expand Down Expand Up @@ -359,22 +360,40 @@ await supabase.books.update(newData).eq(Books.c_id, 1);
await supabase.books.delete().eq(Books.c_id, 1);
```

#### Enums CRUD
# Working with Enums

**IMPORTANT:**

**IMPORTANT:** You need to specify your enums on supadart.yaml config file
- You need to specify your enums on supadart.yaml config file
- Enum `names` are converted to `UPPERCASE` to follow dart enum naming conventions
- Enum `values` are converted to `LOWERCASE` to follow dart enum naming conventions

Assuming the following schema

```sql
-- assuming the following schema
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral', 'excited', 'angry');
CREATE TABLE enum_types (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
mood mood NOT NULL
);
```

In the supadart.yaml you need to specify the enums

```yaml
enums:
mood: [happy, sad, neutral, excited, angry]
```

### Generated Enum Class

```dart
// enum names are converted to .toUpperCase()
enum MOOD { happy, sad, neutral, excited, angry }
```

### Create / Read / Update with Enums

```dart
MOOD firstEnumVal = MOOD.angry;
MOOD newEnumVal = MOOD.excited;
Expand All @@ -393,7 +412,11 @@ await supabase.enum_types
await supabase.enum_types.select().withConverter(EnumTypes.converter);
```

#### Column Selection Queries
# Column Selection Queries

**IMPORTANT:**

- When you select columns, the class will fill the missing columns with the default values

```dart
final book = await supabase
Expand Down
6 changes: 6 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.6.9

- Enum `names` are converted to `UPPERCASE` to follow dart enum naming conventions
- Enum `values` are converted to `LOWERCASE` to follow dart enum naming conventions
- Update supadart.yaml documentation for enums

## 1.6.8

- Add "New" method to generated classes
Expand Down
6 changes: 5 additions & 1 deletion cli/DEV_SETUP.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ dart pub get
### Compile and Run

```bash
dart compile exe bin/supadart.dart ; ./bin/supadart.exe
# Compile and run > Initialize config file supadart.yaml
dart compile exe bin/supadart.dart ; ./bin/supadart.exe --init

# Compile and run > Generate dart classes
dart compile exe bin/supadart.dart ; ./bin/supadart.exe --url <supabase_url> --key <supabase_anon_key>
```

### Testing
Expand Down
2 changes: 1 addition & 1 deletion cli/bin/supadart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:supadart/generators/storage/fetch_storage.dart';
import 'package:supadart/generators/utils/fetch_swagger.dart';
import 'package:yaml/yaml.dart';

const String version = 'v1.6.8';
const String version = 'v1.6.9';
const String red = '\x1B[31m';
const String green = '\x1B[32m';
const String blue = '\x1B[34m';
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/config_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ SUPABASE_URL:
SUPABASE_ANON_KEY:
# Do you have enums in your database? map them here
# Enums in your database? map them here
# Please take a look at the documentation to see how to work with enums
enums:
# mood: [happy, sad, neutral, excited, angry]
# Optional, where to place the generated classes files default: ./lib/models/
output: lib/models/
# Set to true, if you want to generate separated files for each classes
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/generators/standalone/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ String generateEnums(Map<String, List<String>> mapOfEnums) {
final code = StringBuffer();
mapOfEnums.forEach((enumName, enumValues) {
code.write(
"enum ${enumName.split(".").last.toUpperCase().replaceAll('"', "")} { ${enumValues.join(", ")} }\n");
"enum ${enumName.split(".").last.toUpperCase().replaceAll('"', "")} { ${enumValues.map((e) => e.toLowerCase()).join(", ")} }\n");
});
return code.toString();
}
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.6.8
version: 1.6.9

repository: https://github.com/mmvergara/supadart
homepage: https://github.com/mmvergara/supadart
Expand Down
2 changes: 1 addition & 1 deletion cli/test/models/generated_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension SupadartStorageClient on SupabaseStorageClient {}
// Enums
enum MOOD { happy, sad, neutral, excited, angry }

enum USERGROUP { USERS, ADMIN, MODERATOR }
enum USERGROUP { users, admin, moderator }

// Utils
extension DurationFromString on Duration {
Expand Down
3 changes: 2 additions & 1 deletion cli/test/supadart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void main() async {
final isSeperated = false;
final mappings = null;
final mapOfEnums = {
'mood': ["happy", "sad", "neutral", "excited", "angry"]
'mood': ["happy", "sad", "neutral", "excited", "angry"],
'usergroup': ["USERS", "ADMIN", "MODERATOR"]
};

if (url == null || anonKey == null) {
Expand Down

0 comments on commit 8f80f83

Please sign in to comment.