GithubHelp home page GithubHelp logo

Comments (4)

elkSal avatar elkSal commented on June 12, 2024 1

Thanks a lot, marking as closed then :)
Have a great year in advance and thanks a lot for your library!

from drift.

simolus3 avatar simolus3 commented on June 12, 2024

If the phone numbers are stored in the users table, for instance by encoding them as a JSON array of strings, you can combine custom row classes with type converters to get what you're looking for:

import 'package:drift/drift.dart';

@UseRowClass(User)
class Users extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get email => text()();
  TextColumn get phoneNumbers =>
      text().map(TypeConverter.json(fromJson: (data) {
        return <PhoneNumber>[
          for (final value in data) PhoneNumber.parse(value)
        ];
      }))();
}

class User {
  int id;
  String email;
  List<PhoneNumber> phoneNumbers;

  User({required this.id, required this.email, required this.phoneNumbers});
}

class PhoneNumber {
  Country country;
  String phoneNumber;

  PhoneNumber({required this.country, required this.phoneNumber});

  static PhoneNumber parse(String text) {
    throw UnimplementedError();
  }
}

class Country {
  String name;
  String localeName;
  String phonePrefix;

  Country(
      {required this.name,
      required this.localeName,
      required this.phonePrefix});
}

from drift.

elkSal avatar elkSal commented on June 12, 2024

thanks a lot as usual for the quick feedback.

What I had in mind was if whether it was possible to link nested row classes. For instance, in the above example, there would be also a RowClass for PhoneNumber and one for Country as they have some additional relationships with other tables that don't allow to use a type converter.
Given that each user can have multiple phone numbers and each number has one country, it was an example to see how would you implement nested row classes with one to many and one to one relationships.

PS:
It would be interesting to see also how would you implement inheritance in row classes.
Let's say we are collecting animal data, we have an class Pet and the various extensions like Dog, Snake, Bird.
Each User (User has a Row Class) can own pets. How would you implement it?

class User {
String email;
Pet pet;
User({required this.email. required this.pet)};
}

class Pet {
int id;
String name;
Pet({required this.id, required this.name});
}

class Dog {
int id;
String race;
Sex sex;
....
}

class Snake{
int id;
bool isVenoumous;
...
}

So far I did it in the usual SQL way, which would be to add a parentId Column in the children Table for one to one or one to many relationship and add a link table with the parentId and the childrenId for many to many relationships.
I was curious if with row classes it would be possible to preserve these relationships in another way.

Thanks a lot and have a great weekend.

from drift.

simolus3 avatar simolus3 commented on June 12, 2024

Given that each user can have multiple phone numbers and each number has one country, it was an example to see how would you implement nested row classes with one to many and one to one relationships.
I was curious if with row classes it would be possible to preserve these relationships in another way.

This is not possible with cuatom row classes because drift, by design, doesn't hide the relational structure of the database away from you. You can denormalize your schema and store values as arrays where that is appropriate, but one row class will always describe a single row, not the result of a join.

Each User (User has a Row Class) can own pets. How would you implement it?

If pets are stored in a different table, this shouldn't be part of the row class for users. Instead, you write an additiona class storing the user and their pets as separate fields and write a query with joins to resolve them.

from drift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.