Skip to content

Instantly share code, notes, and snippets.

@dickermoshe
Last active April 30, 2025 20:06
Show Gist options
  • Save dickermoshe/251e6002bbfcb95908639ec1593efb38 to your computer and use it in GitHub Desktop.
Save dickermoshe/251e6002bbfcb95908639ec1593efb38 to your computer and use it in GitHub Desktop.

In order to support all of Drifts features, we would nedd a TON of decorators. I thinkg typed_sql get's away with this becuase they have a much smaller feature set

class Group extends Row {

  @AutoIncrementPrimaryKey()  
  int get id;
  
}

@Indexes({{#age,#name},{#age,#groupId}})
class User extends Row{
  @AutoIncrement()
  @PrimaryKey()  
  int get id;  // Maybe have an @AutoIncrementPrimaryKey() too?

  // This 
  @Reference(Group,#id,name:'group',as:"users")
  int get groupId
  // Group get group; << Generated 

  // Or This
  @Reference(#id,as:"users")
  Group get group
  // int get groupId << Generated (fieldName + "Id" = "groupId")
  
  
  @DefaultValue(0) // THis is part of the schema, client default should be moved to a dataclass section
  int get age;
  
  @Checks(LengthCheck(min:1), ContainsPlus())
  String get name;
  
  @Generated(upperCase)
  String get uppercaseName;
  
  
  @Unique() // Globaly Unique
  String get email;
  
  @Unique(group: 1) // Unique with lastName
  String get firstName;
  
  @Unique(group: 1) // Unique with firstName
  String get lastName;

} 

class ContainsPlus extends CutomCheck<String> {
 const ContainsPlus();
  
  Expression<bool> check(Expression<String> expr) {
    // Some code to check that the string contains a '+'
    ...
  }
}

Expression<String> upperCase(Expression<String> name){
  // Some code to uppercase a string
}

class Uint8List {}

class DriftAny {}

class CustomType {}

class _Sentinal { const _Sentinal(); static const defaultValue = _Sentinal(); }

class Relation {}

class Column { final dynamic defaultValue; final bool primaryKey; final bool autoIncrement; final Relation? relation; static const pk = Column(primaryKey: true, autoIncrement: true);

const Column({ this.defaultValue = _Sentinal.defaultValue, this.primaryKey = false, this.autoIncrement = false, this.relation, }) : assert( defaultValue is T || defaultValue == _Sentinal.defaultValue, "Default must be a subtype of $T", ), assert( T == int || T == double || T == BigInt || T == String || T == bool || T == Uint8List || T == DriftAny || T == DateTime || T == Enum || T == CustomType, "Drift only supports int, double, BigInt, String, bool, Uint8List, DriftAny, DateTime, enums or custom types", ); }

class Row {}

class Table { final Map<String, Column> columns; const Table(this.columns); }

const groupTable = Table({ "id": Column.pk, });

class Schema {}

void main() { final user = Column(); // true print(identical(const Formatter(pretty: true), new Formatter(pretty: true))); // true print( !identical(const Formatter(pretty: true), const Formatter(pretty: false)), ); // Pretty! print(switch (const Formatter(pretty: true)) { // Is exhaustive. PrettyFormatter _ => "Pretty!", DenseFormatter _ => "Dense!", }); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment