diff --git a/src/replicache.test.ts b/src/replicache.test.ts index 68c88da0..cffff2d3 100644 --- a/src/replicache.test.ts +++ b/src/replicache.test.ts @@ -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][] = []; @@ -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'}, diff --git a/src/transactions.ts b/src/transactions.ts index 04c1de55..2018a222 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -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. @@ -296,7 +289,7 @@ export class IndexTransactionImpl await this._dbtx.createIndex( this._lc, options.name, - options.prefix ?? options.keyPrefix ?? '', + options.prefix ?? '', options.jsonPointer, ); }