UNPKG

harperdb

Version:

HarperDB is a distributed database, caching service, streaming broker, and application development platform focused on performance and ease of use.

145 lines (144 loc) 6.19 kB
import { ResourceInterface, SubscriptionRequest, Id, Context, Query } from './ResourceInterface'; import { Transaction } from './DatabaseTransaction'; import { IterableEventQueue } from './IterableEventQueue'; /** * This is the main class that can be extended for any resource in HarperDB and provides the essential reusable * uniform interface for interacting with data, defining the API for providing data (data sources) and for consuming * data. This interface is used pervasively in HarperDB and is implemented by database tables and can be used to define * sources for caching, real-data sources for messaging protocols, and RESTful endpoints, as well as any other types of * data aggregation, processing, or monitoring. * * This base Resource class provides a set of static methods that are main entry points for querying and updating data * in resources/tables. The static methods provide the default handling of arguments, context, and ensuring that * internal actions are wrapped in a transaction. The base Resource class intended to be extended, and the instance * methods can be overriden to provide specific implementations of actions like get, put, post, delete, and subscribe. */ export declare class Resource implements ResourceInterface { #private; static transactions: Transaction[] & { timestamp: number; }; static directURLMapping: boolean; constructor(identifier: Id, source: any); /** * The get methods are for directly getting a resource, and called for HTTP GET requests. */ static get(identifier: Id, context?: Context): Promise<Resource>; static get(query: Query, context?: Context): Promise<AsyncIterable<object>>; static get: typeof Resource.get; get?(query?: any): Promise<any>; /** * Store the provided record by the provided id. If no id is provided, it is auto-generated. */ static put: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static patch: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static delete(identifier: Id, context?: Context): Promise<boolean>; static delete(request: Context, context?: Context): Promise<object>; static delete: typeof Resource.delete; /** * Generate a new primary key for a resource; by default we use UUIDs (for now). */ static getNewId(): `${string}-${string}-${string}-${string}-${string}`; /** * Create a new resource with the provided record and id. If no id is provided, it is auto-generated. Note that this * facilitates creating a new resource, but does not guarantee that this is not overwriting an existing entry. * @param id_prefix * @param record * @param context */ static create(id_prefix: Id, record: any, context: Context): Promise<Id>; static create(record: any, context: Context): Promise<Id>; static invalidate: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static post: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static connect: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static subscribe(request: SubscriptionRequest): Promise<AsyncIterable<{ id: any; operation: string; value: object; }>>; static subscribe: typeof Resource.subscribe; static publish: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static search: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static query: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static copy: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; static move: { (id_or_query: string | Id, data_or_context?: any, context?: Context): any; reliesOnPrototype: boolean; }; post(new_record: any): Promise<any>; static isCollection(resource: any): any; get isCollection(): boolean; static coerceId(id: string): number | string; static parseQuery(search: any): any; static parsePath(path: any, context: any, query: any): any; /** * Gets an instance of a resource by id * @param id * @param request * @param options * @returns */ static getResource(id: Id, request: Context, options?: any): Resource | Promise<Resource>; /** * This is called by protocols that wish to make a subscription for real-time notification/updates. * This default implementation simply provides a streaming iterator that does not deliver any notifications * but implementors can call send with * @param query * @param options */ subscribe(options?: {}): AsyncIterable<{ id: any; operation: string; value: object; }>; connect(incomingMessages: IterableEventQueue, query?: {}): AsyncIterable<any>; allowRead(user: any): boolean | object; allowUpdate(user: any): boolean | object; allowCreate(user: any): boolean | object; allowDelete(user: any): boolean | object; /** * Get the primary key value for this resource. * @returns primary key */ getId(): Id; /** * Get the context for this resource * @returns context object with information about the current transaction, user, and more */ getContext(): Context; } export declare function snake_case(camelCase: string): string; /** * An array for ids that toString's back to slash-delimited string */ export declare class MultiPartId extends Array { toString(): string; } export declare function transformForSelect(select: any, resource: any): (object: any) => any;