-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mobile): sqlite #16668
base: main
Are you sure you want to change the base?
feat(mobile): sqlite #16668
Conversation
@override | ||
Future<bool> updateAll(List<User> users) async { | ||
await transaction( | ||
() async => await _db.batch((batch) { | ||
final rows = users.map((u) => u.toCompanion()); | ||
for (final row in rows) { | ||
batch.insert( | ||
_db.userEntity, | ||
row, | ||
onConflict: DoUpdate((_) => row), | ||
); | ||
} | ||
}), | ||
); | ||
|
||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm just not used to Drift, but this syntax is not very readable or "SQL-like" to me.
} | ||
|
||
extension on User { | ||
UserEntityCompanion toCompanion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drift requires a class that implements a specific interface for inserts / updates. So we need to convert our domain object to a companion provided by drift that inserts the data. This is particularly useful to do partial updates where you can create a companion object with only the updated fields.
Why no postgres on mobile :( |
4576e17
to
8fb2da5
Compare
|
||
@TableIndex(name: 'user_uid', columns: {#uid}) | ||
@UseRowClass(model.User) | ||
class UserEntity extends Table { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will want to mirror the columns with the PostgreSQL database
IntColumn get avatarColor => intEnum<model.AvatarColor>()(); | ||
|
||
@override | ||
Set<Column> get primaryKey => {id}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have to explicitly use this getter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the way to define a custom column as a primary key with drift.
} | ||
|
||
extension on User { | ||
UserEntityCompanion toCompanion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this live under the user.entity.dart
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because we are converting the user domain object to the format required by drift. We can probably move this to UserConverter class though
f3ee008
to
8cbeee8
Compare
Description