Skip to content

Commit 8b6480c

Browse files
haroldvandy2013weiwang
andauthored
add getAllRecords support (#66)
* update * update setup-node * update setup-node --------- Co-authored-by: weiwang <weiwang@microsoft.com_msteamsmdb>
1 parent 7653670 commit 8b6480c

5 files changed

Lines changed: 100 additions & 13 deletions

File tree

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/checkout@v2
2525

2626
# Sets up Node and an .npmrc file. This is the official Github supported way to set up node.
27-
- uses: actions/setup-node@v2
27+
- uses: actions/setup-node@v4
2828
with:
2929
node-version: "12"
3030
registry-url: "https://registry.npmjs.org"

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
uses: actions/checkout@v2
2424

2525
# Sets up Node and an .npmrc file. This is the official Github supported way to set up node.
26-
- uses: actions/setup-node@v2
26+
- uses: actions/setup-node@v4
2727
with:
2828
node-version: "12"
2929
registry-url: "https://registry.npmjs.org"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/objectstoreprovider",
3-
"version": "0.7.2",
3+
"version": "0.8.0",
44
"description": "A cross-browser object store library",
55
"author": "Mukundan Kavanur Kidambi <mukav@microsoft.com>",
66
"scripts": {

src/IndexedDbProvider.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
TransactionLockHelper,
6262
} from "./TransactionLockHelper";
6363
import { LogWriter } from "./LogWriter";
64+
import { IDBGetAllRecordsOptions } from "./types/extended-idb";
6465

6566
const 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
}

src/types/extended-idb.d.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export {};
2+
3+
export type IDBGetAllRecordsOptions = {
4+
// The maximum number of records to retrieve.
5+
count: number;
6+
7+
// The direction of the cursor when retrieving records.
8+
// "next" for ascending order, "prev" for descending order.
9+
direction: "next" | "prev";
10+
11+
query: IDBKeyRange | null;
12+
};
13+
14+
// Response of new getAllRecords method.
15+
type IDBRecord = {
16+
key: any;
17+
primaryKey: any;
18+
value: any;
19+
};
20+
21+
// Extending interfaces with new methods, which are not in typeScript library yet.
22+
// More details can be found in https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/IndexedDbGetAllEntries/explainer.md
23+
declare global {
24+
interface IDBIndex {
25+
/**
26+
* Retrieves all records in the index.
27+
* This is an experimental new API and may not be available in all environments.
28+
*/
29+
getAllRecords?: (
30+
options?: IDBGetAllRecordsOptions
31+
) => IDBRequest<IDBRecord[]>;
32+
}
33+
34+
interface IDBObjectStore {
35+
/**
36+
* Retrieves all records in the index
37+
* This is an experimental new API and may not be available in all environments.
38+
*/
39+
getAllRecords?: (
40+
options?: IDBGetAllRecordsOptions
41+
) => IDBRequest<IDBRecord[]>;
42+
}
43+
}

0 commit comments

Comments
 (0)