Skip to content

Commit

Permalink
fix!: Remove CreateIndexDefinition.keyPrefix (#667)
Browse files Browse the repository at this point in the history
It was deprecated 4 months ago

BREAKING CHANGE
  • Loading branch information
arv committed Nov 4, 2021
1 parent 2d23402 commit 883c864
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 139 deletions.
258 changes: 127 additions & 131 deletions src/replicache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,145 +418,141 @@ testWithBothStores('subscribe', async () => {
expect(queryCallCount).to.equal(4);
});

for (const prefixPropertyName of ['prefix', 'keyPrefix']) {
testWithBothStores(
`subscribe with index {prefixPropertyName: ${prefixPropertyName}}`,
async () => {
const log: [[string, string], ReadonlyJSONValue][] = [];

const rep = await replicacheForTesting('subscribe-with-index', {
mutators: {
addData,
},
});

await rep.createIndex({
name: 'i1',
jsonPointer: '/id',
[prefixPropertyName]: 'a',
});

let queryCallCount = 0;
let onDataCallCount = 0;
const cancel = rep.subscribe(
async (tx: ReadTransaction) => {
queryCallCount++;
return await tx.scan({indexName: 'i1'}).entries().toArray();
},
{
onData: (values: Iterable<[[string, string], ReadonlyJSONValue]>) => {
onDataCallCount++;
for (const entry of values) {
log.push(entry);
}
},
},
);
// for (const 'prefix' of ['prefix', 'keyPrefix']) {
testWithBothStores(`subscribe with index`, async () => {
const log: [[string, string], ReadonlyJSONValue][] = [];

expect(log).to.have.length(0);
expect(queryCallCount).to.equal(0);
expect(onDataCallCount).to.equal(0);
const rep = await replicacheForTesting('subscribe-with-index', {
mutators: {
addData,
},
});

await rep.mutate.addData({
a1: {id: 'a-1', x: 1},
a2: {id: 'a-2', x: 2},
b: {id: 'bx'},
});
await rep.createIndex({
name: 'i1',
jsonPointer: '/id',
prefix: 'a',
});

expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
]);
expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add.
expect(onDataCallCount).to.equal(2);
let queryCallCount = 0;
let onDataCallCount = 0;
const cancel = rep.subscribe(
async (tx: ReadTransaction) => {
queryCallCount++;
return await tx.scan({indexName: 'i1'}).entries().toArray();
},
{
onData: (values: Iterable<[[string, string], ReadonlyJSONValue]>) => {
onDataCallCount++;
for (const entry of values) {
log.push(entry);
}
},
},
);

log.length = 0;
await rep.mutate.addData({a3: {id: 'a-3', x: 3}});
expect(log).to.have.length(0);
expect(queryCallCount).to.equal(0);
expect(onDataCallCount).to.equal(0);

expect(queryCallCount).to.equal(3);
expect(onDataCallCount).to.equal(3);
expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
[
['a-3', 'a3'],
{
id: 'a-3',
x: 3,
},
],
]);
await rep.mutate.addData({
a1: {id: 'a-1', x: 1},
a2: {id: 'a-2', x: 2},
b: {id: 'bx'},
});

await rep.dropIndex('i1');
expect(queryCallCount).to.equal(4);
expect(onDataCallCount).to.equal(3); // scan({indexName: 'i1'}) fails since we do not have that index any more.
expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
]);
expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add.
expect(onDataCallCount).to.equal(2);

log.length = 0;
await rep.createIndex({
name: 'i1',
jsonPointer: '/id',
});
log.length = 0;
await rep.mutate.addData({a3: {id: 'a-3', x: 3}});

expect(queryCallCount).to.equal(5);
expect(onDataCallCount).to.equal(4);
expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
[
['a-3', 'a3'],
{
id: 'a-3',
x: 3,
},
],
[
['bx', 'b'],
{
id: 'bx',
},
],
]);
expect(queryCallCount).to.equal(3);
expect(onDataCallCount).to.equal(3);
expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
[
['a-3', 'a3'],
{
id: 'a-3',
x: 3,
},
],
]);

cancel();
},
);
}
await rep.dropIndex('i1');
expect(queryCallCount).to.equal(4);
expect(onDataCallCount).to.equal(3); // scan({indexName: 'i1'}) fails since we do not have that index any more.

log.length = 0;
await rep.createIndex({
name: 'i1',
jsonPointer: '/id',
});

expect(queryCallCount).to.equal(5);
expect(onDataCallCount).to.equal(4);
expect(log).to.deep.equal([
[
['a-1', 'a1'],
{
id: 'a-1',
x: 1,
},
],
[
['a-2', 'a2'],
{
id: 'a-2',
x: 2,
},
],
[
['a-3', 'a3'],
{
id: 'a-3',
x: 3,
},
],
[
['bx', 'b'],
{
id: 'bx',
},
],
]);

cancel();
});

testWithBothStores('subscribe with index and start', async () => {
const log: [[string, string], ReadonlyJSONValue][] = [];
Expand Down Expand Up @@ -1792,7 +1788,7 @@ testWithBothStores('index', async () => {
.instanceOf(Error)
.with.property('message', 'Unknown index name: aIndex');

await rep.createIndex({name: 'bc', keyPrefix: 'c/', jsonPointer: '/bc'});
await rep.createIndex({name: 'bc', prefix: 'c/', jsonPointer: '/bc'});
await testScanResult(rep, {indexName: 'bc'}, [[['8', 'c/0'], {bc: '8'}]]);
await add({
'c/1': {bc: '88'},
Expand Down
9 changes: 1 addition & 8 deletions src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,6 @@ export interface CreateIndexDefinition {
/** The name of the index. This is used when you [[ReadTransaction.scan|scan]] over an index. */
name: string;

/**
* The prefix, if any, to limit the index over. If not provided the values of
* all keys are indexed.
* @deprecated Use [[prefix]] instead.
*/
keyPrefix?: string;

/**
* The prefix, if any, to limit the index over. If not provided the values of
* all keys are indexed.
Expand Down Expand Up @@ -296,7 +289,7 @@ export class IndexTransactionImpl
await this._dbtx.createIndex(
this._lc,
options.name,
options.prefix ?? options.keyPrefix ?? '',
options.prefix ?? '',
options.jsonPointer,
);
}
Expand Down

0 comments on commit 883c864

Please sign in to comment.