Moor: create indices without .moor file

Created on 13 Oct 2020  路  4Comments  路  Source: simolus3/moor

Is there a plan for creating indices like:

@override
Set<Column> get index => {group, user};
enhancement

Most helpful comment

There's nothing preventing us from declaring indices in Dart. However, we'd probably use a designated class for that, something like

class MyIndex extends Index<Users> { // Where Users is a Dart table
  @override
  Set<Column> get columns => {group, user};
}

That would allow users to override the name of the index or define a custom where clause.

All 4 comments

There's nothing preventing us from declaring indices in Dart. However, we'd probably use a designated class for that, something like

class MyIndex extends Index<Users> { // Where Users is a Dart table
  @override
  Set<Column> get columns => {group, user};
}

That would allow users to override the name of the index or define a custom where clause.

@simolus3 could you please explain creating index in code in more detail.

Because I could not find such Index class as in your example. I could find only this.

@ycherniavskyi That was just an api suggestion, it's not implemented yet.

If you want to define an index without moor files, you could use something like this today:

class YourDatabase extends _$YourDatabase {
  // ...

  @override
  Iterable<SchemaEntity> get allSchemaEntities {
    return [
      ...super.allSchemaEntities,
      Index('my_index', 'CREATE INDEX my_index ON  ...'),
    ];
  }
}

Aha, I see.

@simolus3 and thank you for your suggestion, I like it more than which I come with 馃槉:

class YourDatabase extends _$YourDatabase {
  // ...

  @override
  MigrationStrategy get migration => MigrationStrategy(
    onCreate: (m) async {
      await m.createAll();
      await m.createIndex(Index('my_index', 'CREATE INDEX my_index ON ...'));
    },
    // ...
  );
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

VadimOsovsky picture VadimOsovsky  路  3Comments

apoleo88 picture apoleo88  路  3Comments

simolus3 picture simolus3  路  4Comments

felixjunghans picture felixjunghans  路  4Comments

easazade picture easazade  路  3Comments