detajs-sm
Version:
A small Deta Base wrapper with Typescript support.
191 lines (186 loc) • 5.58 kB
TypeScript
interface BaseDataProps extends Record<string, any> {
key: string;
__expires?: number;
}
interface PutResponse<T extends BaseDataProps> {
processed: {
items: T[];
};
failed: {
items: T[];
};
}
interface PutOptions {
expireIn?: number;
expireAt?: Date | number;
}
type GetResponse = BaseDataProps;
interface UpdatePayloadProps {
set: Record<string, any>;
increment: Record<string, number>;
append: Record<string, any[]>;
prepend: Record<string, any[]>;
delete: string[];
}
interface UpdateResponse extends UpdatePayloadProps {
key: string;
}
interface FetchOptions {
limit?: number;
last?: string;
}
interface FetchRawResponse<T extends GetResponse> {
paging: {
size: number;
last?: string;
};
items: T[];
}
interface FetchResponse<T extends GetResponse> {
/**
* List of items retrieved.
*/
items: T[];
/**
* The number of items in the response.
*/
count: number;
/**
* The last key seen in the fetch response. If `undefined`, there are no more items that can be retrieved.
*/
last?: string;
}
interface ClientErrorResponse {
errors: string[];
}
declare enum BaseUtilsActions {
Set = 0,
Increment = 1,
Append = 2,
Prepend = 3,
Delete = 4
}
declare class BaseAction<T = any> {
action: BaseUtilsActions;
value?: T;
constructor(action: BaseUtilsActions, value?: T);
}
declare class BaseUtils {
trim(): BaseAction<any>;
increment(value?: number): BaseAction<number>;
append<T>(values: T | T[]): BaseAction<T | T[]>;
prepend<T>(values: T | T[]): BaseAction<T | T[]>;
}
declare class Day {
private date;
/**
* Day constructor
*
* @param {Date} [date]
*/
constructor(date?: Date);
/**
* addSeconds returns new Day object
* by adding provided number of seconds.
*
* @param {number} seconds
* @returns {Day}
*/
addSeconds(seconds: number): Day;
/**
* getEpochSeconds returns number of seconds after epoch.
*
* @returns {number}
*/
getEpochSeconds(): number;
}
interface TTLResponse {
ttl?: number;
error?: Error;
}
/**
* getTTL computes and returns ttl value based on expireIn and expireAt params.
* expireIn and expireAt are optional params.
*
* @param {number} [expireIn]
* @param {Date | number} [expireAt]
* @returns {TTLResponse}
*/
declare function getTTL(expireIn?: number, expireAt?: Date | number): TTLResponse;
declare class _Base<K extends BaseDataProps> {
projectKey: string;
name: string;
baseUrl: string;
util: BaseUtils;
constructor(name: string, projectKey: string, projectId: string);
private request;
/**
* Store multiple items to the database.
*
* @param items Array / list of items to store to the database.
* @returns {Promise<PutResponse>}
*/
putMany(items: K[] | Omit<K, "key" | "__expires">[]): Promise<PutResponse<K>>;
/**
* Store an item in the database.
*
* It overwrite the item if the key already exists.
*
* @param item Item object to store / save to the database.
* @param key Key of the item to save.
* @param options Put options.
* @returns {Promise<string>}
*/
put(item: K | Omit<K, "key" | "__expires">, key?: string, options?: PutOptions): Promise<string>;
/**
* Get / retrieve a stored item from the database with the given key.
*
* @param key The key of the item to get.
* @returns {Promise<T | null>}
*/
get(key: string): Promise<K | null>;
/**
* Delete an item from the database with the given key.
*
* @param key The key of the item to delete / remove.
* @returns {Promise<null>}
*/
delete(key: string): Promise<null>;
/**
* Insert a single item into a Base.
*
* It will raise an error if the key already exists in the database.
* Note: `insert` is roughly 2x slower than `put`
*
* @param item The item to save / store to the database.
* @param key Key of the item if not included with the item object.
* @param options Insert options.
* @returns {Promise<GetResponse>}
*/
insert(item: K | Omit<K, "key">, key?: string, options?: PutOptions): Promise<GetResponse>;
/**
* Update an existing item from the database.
*
* @param updates A json object describing the updates on the item.
* @param key The key of the item to be updated.
* @returns {Promise<null>}
*/
update<T extends Record<string, any | BaseAction> | {
[k in keyof K]?: any | BaseAction;
}>(updates: T, key: string): Promise<null>;
/**
*
* @param query A single or a list of query objects. If omitted, will fetch all items in the database (up to 1mn)
* @param options
* @returns {Promise<FetchResponse<T>>}
*/
fetch<T extends GetResponse = K>(query?: Record<string, any> | Record<string, any>[], options?: FetchOptions): Promise<FetchResponse<T>>;
}
declare class _Deta {
projectKey: string;
projectId: string;
constructor(projectKey?: string);
Base<K extends BaseDataProps = BaseDataProps>(name: string): _Base<K>;
}
declare const Deta: (projectKey?: string) => _Deta;
export { BaseAction, BaseDataProps, BaseUtils, BaseUtilsActions, ClientErrorResponse, Day, Deta, FetchOptions, FetchRawResponse, FetchResponse, GetResponse, PutOptions, PutResponse, TTLResponse, UpdatePayloadProps, UpdateResponse, _Base, _Deta, getTTL };