DEV Community

Cover image for Supadart Typesafe Supabase Flutter Queries
mmvergara
mmvergara

Posted on

Supadart Typesafe Supabase Flutter Queries

Supabase has type generators for other languages... but not for Dart / Flutter

Supadart automatically generates Dart classes based on your Supabase schema, providing you with a typesafe way to interact with your database.

You can generate via CLI or in the Official Website

Assuming you have the following Supabase Schema

create table
  public.books (
    id bigint generated by default as identity,
    name character varying not null,
    description text null,
    price integer not null,
    created_at timestamp with time zone not null default now(),
    constraint books_pkey primary key (id)
  ) tablespace pg_default;
Enter fullscreen mode Exit fullscreen mode

Then it will generate these classes

class Books implements SupadartClass<Books> {
  final BigInt id;
  final String name;
  final String? description;
  final int price;
  final DateTime? createdAt;

  const Books({
    required this.id,
    required this.name,
    this.description,
    required this.price,
    this.createdAt,
  });

  static String get table_name => 'books';
  static String get c_id => 'id';
  static String get c_name => 'name';
  static String get c_description => 'description';
  static String get c_price => 'price';
  static String get c_createdAt => 'created_at';

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

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

  static Map<String, dynamic> _generateMap({
    BigInt? id,
    String? name,
    String? description,
    int? price,
    DateTime? createdAt,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (name != null) 'name': name,
      if (description != null) 'description': description,
      if (price != null) 'price': price,
      if (createdAt != null) 'created_at': createdAt.toUtc().toIso8601String(),
    };
  }

  static Map<String, dynamic> insert({
    BigInt? id,
    required String name,
    String? description,
    required int price,
    DateTime? createdAt,
  }) {
    return _generateMap(
      id: id,
      name: name,
      description: description,
      price: price,
      createdAt: createdAt,
    );
  }

  static Map<String, dynamic> update({
    BigInt? id,
    String? name,
    String? description,
    int? price,
    DateTime? createdAt,
  }) {
    return _generateMap(
      id: id,
      name: name,
      description: description,
      price: price,
      createdAt: createdAt,
    );
  }

  factory Books.fromJson(Map<String, dynamic> jsonn) {
    return Books(
      id: jsonn['id'] != null
          ? BigInt.parse(jsonn['id'].toString())
          : BigInt.from(0),
      name: jsonn['name'] != null ? jsonn['name'].toString() : '',
      description:
          jsonn['description'] != null ? jsonn['description'].toString() : '',
      price: jsonn['price'] != null ? int.parse(jsonn['price'].toString()) : 0,
      createdAt: jsonn['created_at'] != null
          ? DateTime.parse(jsonn['created_at'].toString())
          : DateTime.fromMillisecondsSinceEpoch(0),
    );
  }

  Map<String, dynamic> toJson() {
    return _generateMap(
      id: id,
      name: name,
      description: description,
      price: price,
      createdAt: createdAt,
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

This makes it easy to interact with your database, and you can use the generated classes in your code.

GitHub logo mmvergara / supadart

Typesafe queries in Supabase Flutter! Generate Flutter / Dart 🎯 classes from your Supabase schema.

Pub Version Pub Points GitHub Stars Runtime Test GitHub License

Supadart 🎯

Typesafe Supabase Flutter Queries
Generate Flutter / Dart 🎯 classes from your Supabase schema.

// allBooks is a typeof List<Books>
final allBooks = await supabase
      .books
      .select("*")
      .withConverter(Books.converter);
Enter fullscreen mode Exit fullscreen mode

Table of Contents 📚

Features 🚀

  • 🌐 Cli and Web App
  • 🛠️ Typesafe Queries (Create, Read, Equality)
  • 🧱 Immutable Generated Classes
  • 📊 Supports Column Selection Queries
  • 🔢 Supports all Supabase Major datatypes
  • 🗂️ Supports Defined as array types
  • 🗂️ Supports Enums

Conversion Table 📊

Supabase Identifier PostgreSQL Format JSON Type Dart Type Runtime Tested
# int2 smallint integer int type ✅ type[]✅
# int4 integer integer int type ✅ type[]✅
# int8 bigint integer BigInt type

Top comments (0)