leoric icon indicating copy to clipboard operation
leoric copied to clipboard

[rfc] static indexes

Open cyjake opened this issue 3 years ago • 0 comments

Model needs someway to declare table indexes in class, here's a proposal:

class User extends Bone {
  static indexes = [
    { fields: [ 'organization_id', 'type' ] },
    { fields: [ 'email' ], unique: true },
  ]
}

which can then be consumed when calling Model.sync():

const database = new Database();
await database.sync();
// compares indexes of tables in database and Model.indexes,  
// then calls driver.addIndex or driver.removeIndex correspondingly

In the ActiveRecord like use case, we can store the indexes fetched when loading schema info from database:

const database = new Database();
await database.connect();
// if Model.indexes exists, compares the fetched and the declared, set Model.synchronized if everything match
// if Model.indexes doesn't exist, set the fetched indexes to Model.indexes

cyjake avatar Jul 28 '21 02:07 cyjake