Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/objectstoreprovider",
"version": "0.8.0",
"version": "0.8.1",
"description": "A cross-browser object store library",
"author": "Mukundan Kavanur Kidambi <[email protected]>",
"scripts": {
Expand Down
33 changes: 28 additions & 5 deletions src/InMemoryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,20 +918,43 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
const keyLow = serializeKeyToString(keyLowRange, this._keyPath);
const keyHigh = serializeKeyToString(keyHighRange, this._keyPath);
const iterator = this._indexTree.entries();
const keys = [];
const keys: string[] = [];

for (const entry of iterator) {
const key = entry.key;
let key = entry.key;
if (key === undefined) {
continue;
}

if (
const isMatch =
(key > keyLow || (key === keyLow && !lowRangeExclusive)) &&
(key < keyHigh || (key === keyHigh && !highRangeExclusive))
) {
(key < keyHigh || (key === keyHigh && !highRangeExclusive));
if (!isMatch) {
continue;
}

// If the current index is not the primary one. We need to find primary key for each value and return that instead.
if (entry.value && this._keyPath !== this._primaryKeyPath) {
for (const value of entry.value) {
key = getSerializedKeyForKeypath(value, this._primaryKeyPath) ?? key;

if (key === entry.key) {
this.logger.warn(
`getSerializedKeyForKeypath returned undefined key in InMemoryIndex for table: ${this.tableName}, with index: ${this._indexSchema?.name}`
);
}

if (!keys.includes(key)) {
keys.push(key);
}
}
}
// Otherwise, we can just use the key as is.
else {
keys.push(key);
}
}

return keys;
}

Expand Down
134 changes: 134 additions & 0 deletions src/tests/ObjectStoreProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,140 @@ describe("ObjectStoreProvider", function () {
);
});

it("Remove range (all removed with index)", (done) => {
// Not working with index, need to be fix in the future.
if (provName === "indexeddbfakekeys") {
done();
return;
}

openProvider(
provName,
{
version: 1,
stores: [
{
name: "test",
primaryKeyPath: "id",
indexes: [
{
name: "index",
keyPath: "a",
},
],
},
],
},
true
)
.then((prov) => {
return prov
.put(
"test",
[1, 2, 3, 4, 5].map((i) => {
return { id: "a" + i, a: "index_value_a_" + i };
})
)
.then(() => {
return prov.getAll("test", undefined).then((rets) => {
assert(!!rets);
assert.equal(rets.length, 5);
return prov
.removeRange(
"test",
"index",
"index_value_a_1",
"index_value_a_5"
)
.then(() => {
return prov
.getAll("test", undefined)
.then((retVals) => {
const rets = retVals as TestObj[];
assert(!!rets);
assert.equal(rets.length, 0);
});
});
});
})
.then(() => prov.close())
.catch((e) => prov.close().then(() => Promise.reject(e)));
})
.then(
() => done(),
(err) => done(err)
);
});

it("Remove range (all removed with index not unique)", (done) => {
// Not working with index, need to be fix in the future.
if (provName === "indexeddbfakekeys") {
done();
return;
}

openProvider(
provName,
{
version: 1,
stores: [
{
name: "test",
primaryKeyPath: "id",
indexes: [
{
name: "index",
keyPath: "a",
unique: false,
},
],
},
],
},
true
)
.then((prov) => {
return prov
.put(
"test",
[1, 1, 2, 3, 4, 5].map((i, index) => {
return {
id: index,
a: "index_value_a_" + i,
};
})
)
.then(() => {
return prov.getAll("test", undefined).then((rets) => {
assert(!!rets);
assert.equal(rets.length, 6);
return prov
.removeRange(
"test",
"index",
"index_value_a_1",
"index_value_a_5"
)
.then(() => {
return prov
.getAll("test", undefined)
.then((retVals) => {
const rets = retVals as TestObj[];
assert(!!rets);
assert.equal(rets.length, 0);
});
});
});
})
.then(() => prov.close())
.catch((e) => prov.close().then(() => Promise.reject(e)));
})
.then(
() => done(),
(err) => done(err)
);
});

it("Invalid Key Type", (done) => {
openProvider(
provName,
Expand Down
Loading