Skip to content
Classcorev0.14.0

KvCollection

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

The collection stores every record as JSON. A Date you store reads back from get() as an ISO string.

class KvCollection<TRecord>
interface Profile { bio: string; joinedAt: Date }
const profiles = this.core.kv.collection<Profile>('profile');

await profiles.set(user.id, { bio: 'hi', joinedAt: new Date() });
const profile = await profiles.get(user.id); // { bio: string; joinedAt: string } | undefined
await profiles.delete(user.id);

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.

// schedules every saved reminder again after a restart
for await (const [id, reminder] of reminders.entries()) schedule(id, reminder);

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.

// counts a run of the command and resolves to the new count
const runs = await counts.update(user.id, (count) => (count ?? 0) + 1);