Indexes are a feature of IndexedDB that allow for faster queries if used properly.
Example
// 1st use case, multi-field ordering: // An index can be created with multiple fields, when using that index the results will be ordered by the fields in the index.
@Table({ indexes: { nameAge: { fields: ['name', 'age'], }, }, }) classTestextendsModel { id!: number name!: string age!: number }
// The results will be ordered by name and then by age awaitTest.withIndex('nameAge').all()
// 2nd use case, unique fields: // An index can be created with the `unique` option, this will enforce that the field, or combination of fields is unique. @Table({ indexes: { uniqueName: { fields: ['firstName', 'lastName'], unique:true }, } }) classTest2extendsModel { id!: number firstName!: string lastName!: string }
awaitTest2.create({ firstName:'John', lastName:'Doe', id:1 }) // This will fail awaitTest2.create({ firstName:'John', lastName:'Doe', id:2 })
// 3rd use case, multi-field filtering: // An index with multiple fields can also be used to speed up filtering. @Table({ indexes: { nameAge: { fields: ['name', 'age'], }, }, }) classTest3extendsModel { id!: number name!: string age!: number } // Looking for people named John that are 30 years old awaitTest3.withIndex('nameAge', Between(['John', 30], ['John', 30])).all() // The alternative will only use the name index and check the age for each result awaitTest3.filter({ name:'John', age:30 }).all() // "Between" can also be used for ranges, eg. looking for people named John that are between 30 and 40 years old awaitTest3.withIndex('nameAge', Between(['John', 30], ['John', 40])).all()
// 4th use case, filter + ordering: // You can combine all this information and use the first field(s) for filtering and leaving the rest for ordering.
// Looking for people named John, ordered by age awaitTest3.withIndex('nameAge', Between(['John', BetweenFilter.minKey], ['John', BetweenFilter.maxKey])).all()
See
W-ORM.Between for more information about the Between filter
Indexes are a feature of IndexedDB that allow for faster queries if used properly.
Example
See
Betweenfilter