keyv-secondary
v0.7.0
Published
Keyv with secondary indexes
Readme
Keyv-secondary
Keyv with secondary indexes
Requires Node.js >= 22.19 (Keyv 6 and p-queue 10).
Features
- Field-based indexing: Index by any field in your values
- Custom mapping: Use
mapfunction for computed or transformed indexes - Filtered indexes: Only index values that match your filter criteria
- Concurrency safety: Built-in locking mechanism prevents race conditions
- TypeScript support: Full type safety with generics
Basic Example
import { KeyvSecondary } from 'keyv-secondary'
type Id = number & { __brand: 'id' }
type Person = { age: number; firstName: string; lastName: string }
type Indexes = 'byAge' | 'byLastName'
const kv = new KeyvSecondary<Id, Person, Indexes>(
{}, // Keyv options
{
indexes: [
{
field: 'age',
filter: () => true,
name: 'byAge',
},
{
field: 'lastName',
filter: () => true,
name: 'byLastName',
},
],
}
)
await kv.set(1 as Id, { age: 30, firstName: 'Galina', lastName: 'Ivanova' })
await kv.set(2 as Id, { age: 59, firstName: 'Zinaida', lastName: 'Petrovna' })
// Get all people with last name "Petrovna"
const petrovnas = await kv.getByIndex('byLastName', 'Petrovna')Advanced Indexing with map
You can use the map function to create indexes based on computed or transformed values:
import { KeyvSecondary } from 'keyv-secondary'
type Person = { age: number; firstName: string; lastName: string }
type Indexes = 'byInitial' | 'byAgeGroup'
const kv = new KeyvSecondary<string, Person, Indexes>(
{},
{
indexes: [
{
// Index by first letter of firstName
map: person => person.firstName[0],
filter: () => true,
name: 'byInitial',
},
{
// Index by age group (adult/minor)
map: person => (person.age >= 18 ? 'adult' : 'minor'),
filter: () => true,
name: 'byAgeGroup',
},
],
}
)
await kv.set('1', { age: 30, firstName: 'Galina', lastName: 'Ivanova' })
await kv.set('2', { age: 17, firstName: 'Stepan', lastName: 'Lukov' })
// Get all adults
const adults = await kv.getByIndex('byAgeGroup', 'adult')
// Get all people whose first name starts with 'G'
const gNames = await kv.getByIndex('byInitial', 'G')Multiple Indexes on Same Field
type Person = { age: number; firstName: string; lastName: string }
type Indexes = 'byYoungAge' | 'byOldAge'
const kv = new KeyvSecondary<string, Person, Indexes>(
{},
{
indexes: [
{
field: 'age',
filter: ({ age }) => age >= 40,
name: 'byOldAge',
},
{
field: 'age',
filter: ({ age }) => age < 40,
name: 'byYoungAge',
},
],
}
)
await kv.set('1', { age: 30, firstName: 'Galina', lastName: 'Ivanova' })
await kv.set('2', { age: 59, firstName: 'Zinaida', lastName: 'Petrovna' })
const youngPeople = await kv.getByIndex('byYoungAge', 30)
const oldPeople = await kv.getByIndex('byOldAge', 59)Concurrency Handling
Internally, KeyvSecondary uses a p-queue with a concurrency limit of 1 to ensure that operations such as set and delete are processed in a serialized manner. This avoids race conditions when multiple asynchronous operations are performed on the same instance.
If you want to provide your own concurrency control mechanism, you can use the options.locker parameter:
const customLocker = async <T>(cb: () => T): T => {
// Custom concurrency logic
return cb()
}
const kv = new KeyvSecondary<Id, Person, Indexes>(
{}, // Keyv options
{
locker: customLocker, // Pass your custom locker function
// ... indexes
}
)Keyv 6 Storage Layout
Keyv 6 no longer applies an implicit keyv namespace or a keyv: key prefix. Secondary indexes are stored under $secondary-index:<name>:<value> and primary keys are stored as-is. When a namespace is configured, the final layout is adapter-specific (for example, namespace::key for the in-memory adapter). Data written by Keyv 5 must be migrated following Keyv's v5 to v6 migration guide.
Index Generation for Postgres
Keyv 6
select
'$secondary-index:byCountry:'
|| (value->'value'->>'country')::text as key,
jsonb_build_object(
'value',
array_agg(key::int)
) as value
from keyv_user
where not key like '%$secondary-index%'
group by value->'value'->>'country'Keyv < 6
select
'keyv:$secondary-index:byCountry:'
|| (value->'value'->>'country')::text as key,
jsonb_build_object(
'value',
array_agg(trim('keyv:' from key)::int)
) as value
from keyv_user
where not key like '%$secondary-index%'
group by value->'value'->>'country'