drizzle-cursor
Version:
Utils for Drizzle ORM cursor based pagination
109 lines (102 loc) • 4.52 kB
text/typescript
import { AnyColumn, SQL } from 'drizzle-orm';
type Cursor = {
order?: "ASC" | "DESC";
key: string;
schema: AnyColumn;
};
type CursorConfig = {
primaryCursor: Cursor;
cursors?: Array<Cursor>;
};
/**
* decoder: similar to `atob()` but compatible with UTF-8 strings
* converts a base64 encoded string to a UTF-8 string
* @link https://developer.mozilla.org/en-US/docs/Web/API/atob
*/
declare const decoder: (data: string) => string;
/**
* encoder: similar to `btoa()` but compatible with UTF-8 strings
* converts a UTF-8 string to a base64 encoded string
* @link https://developer.mozilla.org/en-US/docs/Web/API/btoa
*/
declare const encoder: (str: string) => string;
/**
* parser: similar to `JSON.parse()`
* converts a JSON string to a JavaScript object
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
*/
declare const parser: {
(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined): any;
(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined): unknown;
};
/**
* serializer: similar to `JSON.stringify()`
* converts a JavaScript object to a JSON string
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
*/
declare const serializer: {
(value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string;
(value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string;
};
/**
* parse:
* takes a cursor and returns a JavaScript object
* useful to retrive object from the FE client token
*/
declare function parse<T extends Record<string, unknown> = Record<string, unknown>>({ primaryCursor, cursors }: CursorConfig, cursor?: string | null, decoder?: (data: string) => string, parser?: {
(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined): any;
(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined): unknown;
}): T | null;
/**
* serialize:
* takes a JavaScript object and returns a cursor
* useful to generate token to the FE client
*/
declare function serialize<T extends Record<string, unknown> = Record<string, unknown>>({ primaryCursor, cursors }: CursorConfig, data?: T | null, encoder?: (str: string) => string, serializer?: {
(value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string;
(value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string;
}): string | null;
declare const generateCursor: (config: CursorConfig, options?: {
/**
* decoder: similar to `atob()` but compatible with UTF-8 strings
* converts a base64 encoded string to a UTF-8 string
* @link https://developer.mozilla.org/en-US/docs/Web/API/atob
*/
decoder?: typeof decoder;
/**
* encoder: similar to `btoa()` but compatible with UTF-8 strings
* converts a UTF-8 string to a base64 encoded string
* @link https://developer.mozilla.org/en-US/docs/Web/API/btoa
*/
encoder?: typeof encoder;
/**
* parser: similar to `JSON.parse()`
* converts a JSON string to a JavaScript object
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
*/
parser?: typeof parser;
/**
* serializer: similar to `JSON.stringify()`
* converts a JavaScript object to a JSON string
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
*/
serializer?: typeof serializer;
/**
* parse:
* takes a cursor and returns a JavaScript object
* useful to retrive object from the FE client token
*/
parse?: typeof parse;
/**
* serialize:
* takes a JavaScript object and returns a cursor
* useful to generate token to the FE client
*/
serialize?: typeof serialize;
}) => {
orderBy: SQL<unknown>[];
where: (lastPreviousItemData?: Record<string, unknown> | string | null) => SQL<unknown> | undefined;
parse: (cursor: string | null) => Record<string, unknown> | null;
serialize: (data?: Record<string, unknown> | null) => string | null;
};
export { type Cursor, type CursorConfig, decoder, generateCursor as default, encoder, generateCursor, parse, parser, serialize, serializer };