createCursorCodec
Call Signature
Section titled “Call Signature”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T extends Record<string, unknown> |
Record<string, unknown> |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options? |
CursorCodecOptionsSync<T> |
Returns
Section titled “Returns”CursorCodec<T>
Examples
Section titled “Examples”Default codec (sync)
const codec = createCursorCodec();const token = codec.encode({ id: 7, createdAt: new Date() }); // stringconst 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 otherCall Signature
Section titled “Call Signature”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T extends Record<string, unknown> |
Record<string, unknown> |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
CursorCodecOptions<T> |
Returns
Section titled “Returns”Examples
Section titled “Examples”Default codec (sync)
const codec = createCursorCodec();const token = codec.encode({ id: 7, createdAt: new Date() }); // stringconst 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