Skip to content

Commit

Permalink
Merge pull request #46 from mmvergara/generate-views-models
Browse files Browse the repository at this point in the history
Generate views models
  • Loading branch information
mmvergara authored Aug 2, 2024
2 parents dd639bb + 47d4cb6 commit 0d24d63
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ supadart -u <your-supabase-url> -k <your-supabase-anon-key>
-o, --output Output folder path, add ./ prefix -- (default: "./lib/models/")
-d, --dart Enable if you are not using Flutter, just normal Dart project
-s, --seperated Generate Seperate files for each classes
-v, --version v1.3.7
-v, --version
```
---
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.3.8

- Fix Crash when using the tool with views table

## 1.3.7

- Fix Json Array and DateTime parsing
Expand Down
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ supadart -u <your-supabase-url> -k <your-supabase-anon-key>
-o, --output Output folder path, add ./ prefix -- (default: "./lib/models/")
-d, --dart Enable if you are not using Flutter, just normal Dart project
-s, --seperated Generate Seperate files for each classes
-v, --version v1.3.7
-v, --version
```
2 changes: 1 addition & 1 deletion cli/bin/supadart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:dotenv/dotenv.dart';
import 'package:supadart/generator/generator.dart';
import 'package:supadart/generator/swagger.dart';

const String version = 'v1.3.7';
const String version = 'v1.3.8';
const String red = '\x1B[31m'; // Red text
const String green = '\x1B[32m'; // Green text
const String blue = '\x1B[34m'; // Blue text
Expand Down
4 changes: 3 additions & 1 deletion cli/lib/generator/swagger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Table {
final properties = json['properties'] as Map<String, dynamic>;
return Table(
name: name,
requiredFields: List<String>.from(json['required']),
requiredFields: json['required'] != null
? List<String>.from(json['required'])
: <String>[],
columns:
properties.map((key, value) => MapEntry(key, Column.fromJson(value))),
);
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.3.7
version: 1.3.8

repository: https://github.com/mmvergara/supadart
homepage: https://github.com/mmvergara/supadart
Expand Down
86 changes: 85 additions & 1 deletion cli/test/models/generated_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import 'package:intl/intl.dart';

// INTL is an official package from Dart and is used for parsing dates
// flutter pub add intl or dart pub add intl

// THIS FILE IS AUTO GENERATED. MODIFY WITH CAUTION
extension SupadartClient on SupabaseClient {
SupabaseQueryBuilder get string_types => from('string_types');
SupabaseQueryBuilder get boolean_bit_types => from('boolean_bit_types');
SupabaseQueryBuilder get combined_types_view => from('combined_types_view');
SupabaseQueryBuilder get misc_types => from('misc_types');
SupabaseQueryBuilder get books => from('books');
SupabaseQueryBuilder get geometric_types => from('geometric_types');
Expand Down Expand Up @@ -317,6 +317,90 @@ class BooleanBitTypes implements SupadartClass<BooleanBitTypes> {
}
}

class CombinedTypesView implements SupadartClass<CombinedTypesView> {
final String? numeric_id;
final int? col_integer;
final double? col_double;
final String? string_id;
final String? col_text;
final String? col_uuid;

const CombinedTypesView({
this.numeric_id,
this.col_integer,
this.col_double,
this.string_id,
this.col_text,
this.col_uuid,
});

static String get table_name => 'combined_types_view';
static String get c_numeric_id => 'numeric_id';
static String get c_col_integer => 'col_integer';
static String get c_col_double => 'col_double';
static String get c_string_id => 'string_id';
static String get c_col_text => 'col_text';
static String get c_col_uuid => 'col_uuid';

static List<CombinedTypesView> converter(List<Map<String, dynamic>> data) {
return data.map(CombinedTypesView.fromJson).toList();
}

static CombinedTypesView converterSingle(Map<String, dynamic> data) {
return CombinedTypesView.fromJson(data);
}

static Map<String, dynamic> insert({
String? numeric_id,
int? col_integer,
double? col_double,
String? string_id,
String? col_text,
String? col_uuid,
}) {
return {
if (numeric_id != null) 'numeric_id': numeric_id.toString(),
if (col_integer != null) 'col_integer': col_integer.toString(),
if (col_double != null) 'col_double': col_double.toString(),
if (string_id != null) 'string_id': string_id.toString(),
if (col_text != null) 'col_text': col_text.toString(),
if (col_uuid != null) 'col_uuid': col_uuid.toString(),
};
}

static Map<String, dynamic> update({
String? numeric_id,
int? col_integer,
double? col_double,
String? string_id,
String? col_text,
String? col_uuid,
}) {
return {
if (numeric_id != null) 'numeric_id': numeric_id.toString(),
if (col_integer != null) 'col_integer': col_integer.toString(),
if (col_double != null) 'col_double': col_double.toString(),
if (string_id != null) 'string_id': string_id.toString(),
if (col_text != null) 'col_text': col_text.toString(),
if (col_uuid != null) 'col_uuid': col_uuid.toString(),
};
}

factory CombinedTypesView.fromJson(Map<String, dynamic> json) {
return CombinedTypesView(
numeric_id:
json['numeric_id'] != null ? json['numeric_id'].toString() : '',
col_integer: json['col_integer'] != null ? json['col_integer'] as int : 0,
col_double: json['col_double'] != null
? double.tryParse(json['col_double'].toString())
: 0.0,
string_id: json['string_id'] != null ? json['string_id'].toString() : '',
col_text: json['col_text'] != null ? json['col_text'].toString() : '',
col_uuid: json['col_uuid'] != null ? json['col_uuid'].toString() : '',
);
}
}

class MiscTypes implements SupadartClass<MiscTypes> {
final String id;
final String? col_money;
Expand Down
22 changes: 22 additions & 0 deletions test/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,25 @@ CREATE TABLE public.misc_types (
col_txid_snapshot TXID_SNAPSHOT NULL,
col_txid_snapshot_array TXID_SNAPSHOT[] NULL
);



-- Create a simple view
CREATE OR REPLACE VIEW public.combined_types_view AS
SELECT
nt.id AS numeric_id,
nt.col_integer,
nt.col_double,
st.id AS string_id,
st.col_text,
st.col_uuid
FROM
public.numeric_types nt
JOIN
public.string_types st ON nt.id = st.id;

-- add comment to view
-- COMMENT ON VIEW public.combined_types_view IS '[supadart:view]';

-- Remove view
-- DROP VIEW IF EXISTS public.combined_types_view;

0 comments on commit 0d24d63

Please sign in to comment.