Comments (4)
Thanks a lot, marking as closed then :)
Have a great year in advance and thanks a lot for your library!
from drift.
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.
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.
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)
- Wrong link in the docs which lead to 404 HOT 1
- Add coverage ignore for generated files HOT 2
- My lord, serious lock problem when try accessing data while updating it. HOT 6
- How do I SUM column in join? HOT 2
- When using PgTypes cannot generate a dialect for Sqllite. HOT 2
- SQLite file is modified with select statements HOT 5
- Correct declination for tables ending on "y" / "ies" HOT 3
- Bug on built flutter web HOT 2
- About nested chat messages query efficiency HOT 8
- Cascade delete is not working HOT 1
- Support storing arbitrary dart types as JSONB BLOBs with configuration support within .drift files HOT 3
- drift file view "Expression has an unknown type, the generated code can be inaccurate." HOT 2
- Wrong generated code when using import alias for Dart tables HOT 2
- How to couple the table created by the drift file 🤔️? HOT 1
- DataClass toJson/fromJson with column names HOT 2
- Does the uuidvalue from the drift_progres package not have a default converter? HOT 2
- Unable to scroll table sideways in drift viewer in Flutter DevTools HOT 2
- Wrong message when a non nullable converter is used on a nullable column:
- Storing multiple entries in a row using JSON encoding - Many-to-Many example HOT 1
- Encryption documentation (for `encrypted_drift`) (`file is not a database`) HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drift.