Skip to content
Classcorev0.12.0

KvCollection

The records of one type, stored under one name in config.kv. Get one from kv.collection<T>('name').

Every record goes through JSON on its way in and out. A Date you store reads back from get() as an ISO string.

class KvCollection<TRecord>

Member visibility

Filter class members by access level.

constructor

Constructs a new instance of the KvCollection class

KvCollection(
    driver: KvDriver,
    prefix: string,
    updates: KeyedQueue
)

Showing members with Protected visibility and higher.

TRecord

TRecord

entries()

Yields [id, record] for every record in this collection that hasn't expired. Use it to read back what the bot saved before a restart.

public entries(): AsyncIterable<
    [id: string, record: Stored<TRecord>]
>

Throws:

A SeedcordError when the backend can't list its keys, like @keyv/memcache.

get()

Reads the record stored under id. Resolves to undefined when nothing is there or the record expired.

public async get(
    id: string
): Promise<Stored<TRecord> | undefined>

set()

Stores value under id and replaces whatever was there.

public async set(
    id: string,
    value: TRecord,
    ttlMs?: number
): Promise<void>
Parameter: ttlMs

How long the record lasts, in milliseconds. Leave it out to keep the record until you delete it.

update()

Stores what next returns for the record under id, then resolves to that value. next gets the current record, or undefined when there is none.

Use it over a get() then a set() when two calls can change one record at the same time. set(), delete(), and update() calls on one id run one after the other in this process. None of them loses another's change. Two processes sharing one backend can still both read before either writes.

public async update(
    id: string,
    next: (current: Stored<TRecord> | undefined) => TRecord,
    ttlMs?: number | ((value: TRecord) => number)
): Promise<TRecord>
Parameter: ttlMs

How many milliseconds the stored record lasts. Pass a function to work it out from the record. Leave it out to keep the record until you delete it.