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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to couple the table created by the drift file 馃锔忥紵 #2846

Closed
greenking19 opened this issue Jan 19, 2024 · 1 comment
Closed
Labels
enhancement New feature or request

Comments

@greenking19
Copy link

This is a table created in the Dart file, which allows for coupling.
What should I do in the Drift file?
It seems that the Existing row classes in the document needs to be created first. My table is a bit complicated, so it would be a bit troublesome to manually create it.
If there are other ways, that would be even better.

Example

enum StatusEnum { open, close }

mixin WithStatus on Table {
  IntColumn get status => intEnum<StatusEnum>()();
}

class TodoItems extends Table with WithStatus {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
  TextColumn get userId => text()();
}

class TaskItems extends Table with WithStatus {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
  TextColumn get userId => text()();
}

void doSomething(dynamic data) {
  if (data is WithStatus) {
    // do something
  }
}
@greenking19 greenking19 added the enhancement New feature or request label Jan 19, 2024
@simolus3
Copy link
Owner

There's no way to do declare an inheritance structure for tables in drift files, they can't share a a common mixin for columns.

But while there is no way to do this with type safety, you can still access columns of abstract tables. For instance, if you had

CREATE TABLE todo_items (
  status INTEGER ...,
  ...
);

CREATE TABLE task_items (
  status INTEGER ...,
  ...
);

Then you can do

void doSomething(TableInfo data) {
  if (data.columnsByName.containsKey('status')) {
    // do something
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants