@@ -61,6 +61,7 @@ import {
6161 TransactionLockHelper ,
6262} from "./TransactionLockHelper" ;
6363import { LogWriter } from "./LogWriter" ;
64+ import { IDBGetAllRecordsOptions } from "./types/extended-idb" ;
6465
6566const IndexPrefix = "nsp_i_" ;
6667
@@ -74,6 +75,14 @@ declare global {
7475 }
7576}
7677
78+ export interface IDBConfigs {
79+ useGetAllRecordsForGetRange : boolean ;
80+ }
81+
82+ const defaultIDBConfigs : IDBConfigs = {
83+ useGetAllRecordsForGetRange : false ,
84+ } ;
85+
7786// The DbProvider implementation for IndexedDB. This one is fairly straightforward since the library's access patterns pretty
7887// closely mirror IndexedDB's. We mostly do a lot of wrapping of the APIs into JQuery promises and have some fancy footwork to
7988// do semi-automatic schema upgrades.
@@ -93,7 +102,8 @@ export class IndexedDbProvider extends DbProvider {
93102 explicitDbFactorySupportsCompoundKeys ?: boolean ,
94103 handleOnClose ?: OnCloseHandler ,
95104 logger ?: IObjectStoreProviderLogger ,
96- upgradeCallback ?: UpgradeCallback
105+ upgradeCallback ?: UpgradeCallback ,
106+ private idbConfigs : IDBConfigs = defaultIDBConfigs
97107 ) {
98108 super ( ) ;
99109
@@ -450,7 +460,8 @@ export class IndexedDbProvider extends DbProvider {
450460 fakeToken ,
451461 schema ,
452462 this . _fakeComplicatedKeys ,
453- this . logWriter
463+ this . logWriter ,
464+ this . idbConfigs
454465 ) ;
455466 const tStore = iTrans . getStore ( storeSchema . name ) ;
456467
@@ -688,7 +699,8 @@ export class IndexedDbProvider extends DbProvider {
688699 transToken ,
689700 this . _schema ! ! ! ,
690701 this . _fakeComplicatedKeys ,
691- this . logWriter
702+ this . logWriter ,
703+ this . idbConfigs
692704 )
693705 ) ;
694706 }
@@ -706,7 +718,8 @@ class IndexedDbTransaction implements DbTransaction {
706718 private _transToken : TransactionToken ,
707719 private _schema : DbSchema ,
708720 private _fakeComplicatedKeys : boolean ,
709- private logWriter : LogWriter
721+ private logWriter : LogWriter ,
722+ private idbConfigs ?: IDBConfigs
710723 ) {
711724 this . _stores = map ( this . _transToken . storeNames , ( storeName ) =>
712725 this . _trans . objectStore ( storeName )
@@ -801,7 +814,8 @@ class IndexedDbTransaction implements DbTransaction {
801814 store ,
802815 indexStores ,
803816 storeSchema ,
804- this . _fakeComplicatedKeys
817+ this . _fakeComplicatedKeys ,
818+ this . idbConfigs
805819 ) ;
806820 }
807821
@@ -839,7 +853,8 @@ class IndexedDbStore implements DbStore {
839853 private _store : IDBObjectStore ,
840854 private _indexStores : IDBObjectStore [ ] ,
841855 private _schema : StoreSchema ,
842- private _fakeComplicatedKeys : boolean
856+ private _fakeComplicatedKeys : boolean ,
857+ private idbConfigs ?: IDBConfigs
843858 ) {
844859 // NOP
845860 }
@@ -1166,7 +1181,8 @@ class IndexedDbStore implements DbStore {
11661181 indexSchema ,
11671182 this . _schema . primaryKeyPath ,
11681183 this . _fakeComplicatedKeys ,
1169- this . _store
1184+ this . _store ,
1185+ this . idbConfigs
11701186 ) ;
11711187 } else {
11721188 const index = this . _store . index ( indexName ) ;
@@ -1177,7 +1193,9 @@ class IndexedDbStore implements DbStore {
11771193 index ,
11781194 indexSchema ,
11791195 this . _schema . primaryKeyPath ,
1180- this . _fakeComplicatedKeys
1196+ this . _fakeComplicatedKeys ,
1197+ undefined ,
1198+ this . idbConfigs
11811199 ) ;
11821200 }
11831201 }
@@ -1187,7 +1205,9 @@ class IndexedDbStore implements DbStore {
11871205 this . _store ,
11881206 undefined ,
11891207 this . _schema . primaryKeyPath ,
1190- this . _fakeComplicatedKeys
1208+ this . _fakeComplicatedKeys ,
1209+ undefined ,
1210+ this . idbConfigs
11911211 ) ;
11921212 }
11931213
@@ -1214,7 +1234,8 @@ class IndexedDbIndex extends DbIndexFTSFromRangeQueries {
12141234 indexSchema : IndexSchema | undefined ,
12151235 primaryKeyPath : KeyPathType ,
12161236 private _fakeComplicatedKeys : boolean ,
1217- private _fakedOriginalStore ?: IDBObjectStore
1237+ private _fakedOriginalStore ?: IDBObjectStore ,
1238+ private idbConfigs ?: IDBConfigs
12181239 ) {
12191240 super ( indexSchema , primaryKeyPath ) ;
12201241 }
@@ -1351,6 +1372,29 @@ class IndexedDbIndex extends DbIndexFTSFromRangeQueries {
13511372 ) {
13521373 return IndexedDbProvider . WrapRequest ( this . _store . getAll ( keyRange , limit ) ) ;
13531374 }
1375+
1376+ const shouldUseGetAllRecords = this . idbConfigs ?. useGetAllRecordsForGetRange ;
1377+ if (
1378+ shouldUseGetAllRecords &&
1379+ ! ! this . _store . getAllRecords &&
1380+ typeof this . _store . getAllRecords === "function" &&
1381+ reverse &&
1382+ ! ! limit &&
1383+ ! offset
1384+ ) {
1385+ // use getAllRecords for reverse order, if available.
1386+ // It does not support offset currently.
1387+ const query_options = {
1388+ direction : "prev" ,
1389+ count : limit ,
1390+ query : keyRange ,
1391+ } as IDBGetAllRecordsOptions ;
1392+
1393+ return IndexedDbProvider . WrapRequest (
1394+ this . _store . getAllRecords ( query_options )
1395+ ) ;
1396+ }
1397+
13541398 const req = this . _store . openCursor ( keyRange , reverse ? "prev" : "next" ) ;
13551399 return this . _resolveCursorResult ( req , limit , offset ) ;
13561400 }
0 commit comments