Skip to content
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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

feat(mobile): sqlite #16668

wants to merge 4 commits into from

Conversation

shenlong-tanwen
Copy link
Member

Description

  • Added Drift for Sqlite
  • Added UserEntity for Drift and Corresponding repository

Comment on lines 155 to 171
@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;
}
Copy link
Contributor

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() {
Copy link
Contributor

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?

Copy link
Member Author

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.

@mmomjian
Copy link
Contributor

mmomjian commented Mar 6, 2025

Why no postgres on mobile :(


@TableIndex(name: 'user_uid', columns: {#uid})
@UseRowClass(model.User)
class UserEntity extends Table {
Copy link
Contributor

@alextran1502 alextran1502 Mar 7, 2025

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};
Copy link
Contributor

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?

Copy link
Member Author

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() {
Copy link
Contributor

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?

Copy link
Member Author

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

@shenlong-tanwen shenlong-tanwen force-pushed the refactor/mobile-user-entity branch from f3ee008 to 8cbeee8 Compare March 10, 2025 17:34
Base automatically changed from refactor/mobile-user-entity to main March 12, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants