cyjake / leoric

👑 JavaScript ORM for MySQL, PostgreSQL, and SQLite.

Home Page:https://leoric.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[rfc] static indexes

cyjake opened this issue · 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