Skip to content

createCursorCodec

function createCursorCodec<T>(options?): CursorCodec<T>;

Defined in: cursor-codec.ts:187

Create an opaque-cursor codec. Defaults to JSON + base64url, every stage replaceable — swap the encoder/decoder pair for encryption or signing, or the serializer/parser pair for superjson/msgpack-style formats.

Sync/async adaptive: all-sync stages (including the defaults) produce a CursorCodec whose encode/decode return plain values; if any stage is async the codec returns Promises — reflected in the types.

Date values survive a round-trip out of the box: JSON.stringify emits ISO strings and decode revives them (disable with reviveDates: false).

Type Parameter Default type
T extends Record<string, unknown> Record<string, unknown>
Parameter Type
options? CursorCodecOptionsSync<T>

CursorCodec<T>

Default codec (sync)

const codec = createCursorCodec();
const token = codec.encode({ id: 7, createdAt: new Date() }); // string
const data = codec.decode(token); // { id: 7, createdAt: Date }

Async stage (WebCrypto) — encode/decode become Promise-returning

const codec = createCursorCodec({
encoder: (s) => encryptWithSubtleCrypto(s), // returns Promise<string>
decoder: (t) => decryptWithSubtleCrypto(t),
});
const token = await codec.encode({ id: 7 });

Sharing custom stages with drizzle-cursor

const codec = createCursorCodec({ encoder, decoder });
const cursor = generateCursor(config, { encoder, decoder }); // drizzle-cursor
// tokens produced by either parse cleanly in the other
function createCursorCodec<T>(options): CursorCodecMaybeAsync<T>;

Defined in: cursor-codec.ts:190

Create an opaque-cursor codec. Defaults to JSON + base64url, every stage replaceable — swap the encoder/decoder pair for encryption or signing, or the serializer/parser pair for superjson/msgpack-style formats.

Sync/async adaptive: all-sync stages (including the defaults) produce a CursorCodec whose encode/decode return plain values; if any stage is async the codec returns Promises — reflected in the types.

Date values survive a round-trip out of the box: JSON.stringify emits ISO strings and decode revives them (disable with reviveDates: false).

Type Parameter Default type
T extends Record<string, unknown> Record<string, unknown>
Parameter Type
options CursorCodecOptions<T>

CursorCodecMaybeAsync<T>

Default codec (sync)

const codec = createCursorCodec();
const token = codec.encode({ id: 7, createdAt: new Date() }); // string
const data = codec.decode(token); // { id: 7, createdAt: Date }

Async stage (WebCrypto) — encode/decode become Promise-returning

const codec = createCursorCodec({
encoder: (s) => encryptWithSubtleCrypto(s), // returns Promise<string>
decoder: (t) => decryptWithSubtleCrypto(t),
});
const token = await codec.encode({ id: 7 });

Sharing custom stages with drizzle-cursor

const codec = createCursorCodec({ encoder, decoder });
const cursor = generateCursor(config, { encoder, decoder }); // drizzle-cursor
// tokens produced by either parse cleanly in the other