To find the entity faster, Index.

As shown in the Getting Started section, we can get entities by the following code.

let db =  RootState.Database()

db.bookEntityTable.all()

db.bookEntityTable.find(by: <#T##VergeTypedIdentifier<Book>#>)

db.bookEntityTable.find(in: <#T##Sequence#>)

To do this, we need to manage the Identifier of the entity and additionally, to get an array of entities, we need to manage the order of Identifier.

To do this, VergeORM provides Index feature. Index manages the set of identifiers in several structures.

💡 Meaning of Index might be a bit different than RDB’s Index. At least, Index manages identifiers to find the entity faster than linear search.

Currently, we have the following types,‌

Register Index

Let’s take a look at how to register Index. The whole index is here.

struct Database: DatabaseType {

  struct Schema: EntitySchemaType {
    let book = Book.EntityTableKey()
    let author = Book.EntityTableKey()
  }

  struct Indexes: IndexesType {
    // 👋 Here!
  }

  var _backingStorage: BackingStorage = .init()
}

Indexes struct describes the set of indexes. All of the indexes managed by VergeORM would be here.

For now, we add a simple ordered index just like this.

struct Indexes: IndexesType {
  let allBooks = IndexKey<OrderedIDIndex<Schema, Book>>()
  // or OrderedIDIndex<Schema, Book>.Key()
}

With this, now we have index property on DatabaseType.indexes.

let allBooks = state.db.indexes.allBooks
// allBooks: OrderedIDIndex<Database.Schema, Book>