sql-switch - v1.0.1
    Preparing search index...

    Class KeyProxy

    Terminal node of the fluent chain — bound to one schema:table:key triple. Returned by TableProxy.key.

    Index
    • Read the value at this key.

      Type Parameters

      • T = unknown

        Expected shape of the stored value.

      Returns Promise<T | null>

      The stored value, or null if the key doesn't exist.

      Read your writes => a value still sitting in the collector buffer wins over the stored row, so a queued set() is visible to the very next get() without waiting for the flush. The buffered copy goes through the same JSON round trip the driver does, so a value doesn't change shape depending on whether it landed yet.

      await db.schema('antinuke').table('settings').key('guild_1').set({ strict: true });
      // queued, not flushed yet — still reads back as { strict: true }
      const settings = await db.schema('antinuke').table('settings').key('guild_1').get();
    • Write a value at this key (upsert).

      The write is scheduled the moment you call set() => a fire and forget set(v) with no await and no .force() still lands. Awaiting it only decides whether you wait for the queue (or the driver, when the collector is off); .force() swaps the queued copy for an immediate durable write.

      Type Parameters

      • T = unknown

      Parameters

      • value: T

      Returns WriteOperation<void>

      InvalidValueError (a TypeError) for a value the two engines can't store identically => undefined, NaN/Infinity, a NUL character anywhere in a string or property name, a circular reference, or a BigInt.

      DatabaseUnavailableError at the call site when the collector's breaker is open.

      Eager on purpose (A2): the old behaviour was lazy => a set() that was never awaited & never forced quietly did nothing, which is the easiest way there is to lose a write. Now the queue happens synchronously, at the call, so the only thing await changes is when you find out it's done.

      Validation is synchronous too => a bad value (or an open breaker) throws right here, before anything is queued or written, so it can't sit in the buffer & blow up inside a flush where the only trace is a log line. undefined as an object property is still dropped, the way JSON.stringify always has.

      A value round trips through JSON, so two things are stored but come back reshaped rather than refused: an integer past Number.MAX_SAFE_INTEGER (2^53) loses precision the moment it's read back as a JS number (12345678901234567890 returns as ...567000), and a Buffer/typed array degrades to { type: 'Buffer', data: [...] }, never a Buffer again. store a big integer as a string & base64 encode binary yourself if you need either back intact => this is JSON's doing, not the engine's, so it's identical in local & cloud mode. only the id (key) is precision safe.

    • Delete this key.

      Returns WriteOperation<void>

      Deletes always run immediately, even when awaited without .force(). Routing them through the collector would mean a queued set() on the same key could land after the delete & resurrect the row. .force() is accepted for API symmetry.

      Any value still buffered for this key is dropped first, so a queued set() issued before the delete can't come back on the next flush.

    • Does this key exist?

      Returns Promise<boolean>

      true if a value is stored (or buffered) for this key, false otherwise.

      A queued set() counts as existing (read your writes), so the buffer is checked first. Past that it asks the driver's existence check rather than get() !== null => a row storing a literal null value still exists, and has() has to say so where get() can only return the null it can't tell apart from a missing key.

    • Add to the number stored at this key (a missing key counts as 0).

      Parameters

      • amount: number

        How much to add (may be negative).

      Returns Promise<number>

      The new total.

      if the stored value isn't a number.

      InvalidValueError if the result isn't finite (e.g. adding Infinity).

      Read-modify-write, not atomic => it reads (seeing its own buffered writes), computes, then writes back through the collector. Two un-awaited add()s on the same key can read the same base & one update is lost. Sequential awaited calls are fine; for contended counters under real concurrency you need a lock the DAL doesn't provide.

    • Subtract from the number stored at this key (a missing key counts as 0).

      Parameters

      • amount: number

        How much to subtract.

      Returns Promise<number>

      The new total.

      if the stored value isn't a number.

      Same non-atomic read-modify-write caveat as KeyProxy.add.

    • Append one or more items to the array stored at this key (a missing key starts a new array).

      Type Parameters

      • T = unknown

      Parameters

      • ...items: T[]

        Items to append.

      Returns Promise<T[]>

      The updated array.

      if the stored value isn't an array.

      Same non-atomic read-modify-write caveat as KeyProxy.add.

    • Prepend one or more items to the array stored at this key.

      Type Parameters

      • T = unknown

      Parameters

      • ...items: T[]

        Items to prepend, kept in the given order.

      Returns Promise<T[]>

      The updated array.

      if the stored value isn't an array.

      Same non-atomic read-modify-write caveat as KeyProxy.add.

    • Remove & return the last item of the array stored at this key.

      Type Parameters

      • T = unknown

      Returns Promise<T | undefined>

      The removed item, or undefined if the array is empty or the key is missing.

      if the stored value isn't an array.

      A missing key is left missing => popping from nothing returns undefined without creating an empty-array row. Same non-atomic read-modify-write caveat as KeyProxy.add.

    • Remove & return the first item of the array stored at this key.

      Type Parameters

      • T = unknown

      Returns Promise<T | undefined>

      The removed item, or undefined if the array is empty or the key is missing.

      if the stored value isn't an array.

      A missing key is left missing => shifting from nothing returns undefined without creating an empty-array row. Same non-atomic read-modify-write caveat as KeyProxy.add.

    • Remove every element of the array that matches, and store what's left.

      Type Parameters

      • T = unknown

      Parameters

      • match: T | ((item: T, index: number) => boolean)

        A value (removed by strict === equality) or a predicate (item, index) => boolean (removed when it returns truthy).

      Returns Promise<T[]>

      The array with the matches gone.

      if the stored value isn't an array.

      The value form is reference equality, so it won't match object elements => pass a predicate for those. A missing key returns [] without creating a row. Same non-atomic read-modify-write caveat as KeyProxy.add.