import { BehaviorSubject } from 'rxjs'; import { Api, ApiRequestOptions } from './api'; import { Meta, SliceInfo } from './meta'; import { Unsubscribe } from './socket'; export declare class ApiCall { private readonly api; readonly slice: number | string; private operation; private popField?; private request; /** Log response automatically */ debugging?: boolean; /** Get raw API request */ get raw(): Partial<{ [x: string]: any; $pop: string | undefined; }>; constructor(api: Api, slice: number | string); /** * Whitelist and alias fields. Alias of `fields()` * @example * ```ts * const id: {id: number, field2: any}[] = await new Slice(12345) * .select().alias({id: 'id', field1: 'field2'}).exec().keys(); * ``` * @param {object} aliasKeyVals List of properties to whitelist and what to rename them to * @return {Slice} */ alias: { (whitelist: string[]): this; (alias: { [key: string]: string | object; }): this; }; /** * Add an 'AND' condition inside the where argument * @example * ```ts * const rows: T[] = await new Slice(12345).select() * .where('field1', '>', 1) * .and() * .where({field2: 2, field3: 3}) * .exec().rows(); * ``` * @return {Slice} */ and(): this; /** * Count the returned rows * @example * ```ts * const count = await new Slice(12345).count() * .where({field1: 1}) * .exec().count(); * ``` * @param {object | string} arg Count argument * @return {Slice} */ count(arg?: object | string): this; /** * Output the formed request to the console for inspection * @param {boolean} enabled Enable/Disable console logging * @return {Slice} */ debug(enabled?: boolean): this; /** * Set the request type to delete * @example * ```ts * await new Slice(12345).delete(id).exec(); * ``` * @param {number | number[]} id ID(s) to delete * @return {Slice} */ delete(id?: number | number[]): this; /** * Filter rows from slice based on an excel expression * @example * ```ts * const rows: T[] = await new Slice(12345).select() * .excel('contains({property}, foobar)') * .exec().rows(); * ``` * @param formula Excel formula to use as where clause * @return {Slice} */ excel(formula: string): this; /** * Compile the request and send it * @param {ApiRequestOptions} options API Request options * @return {Promise} API response */ exec(options?: ApiRequestOptions): Promise; /** * Whitelist fields by passing an array of keys or alias fields by using a map * @example * ```ts * const id: {id: number, field1: any}[] = await new Slice(12345) * .select().fields(['id', 'field1']).exec().keys(); * ``` * @param {object} whitelist Either a list of keys to whitelist or a map of keys to rename * @return {Slice} */ fields(whitelist: string[]): this; fields(alias: { [key: string]: string | object; }): this; /** * Unwrap response returning the first ID * @return {Slice} */ id(): this; /** * Set the request type to insert * @example * ```ts * const id: number = await new Slice(12345).insert([ * {field1: 1}, * {field1: 2} * ]).exec().keys(); * ``` * @param {T | T[]} rows Rows to be inserted into the slice * @return {Slice} */ insert(rows: T | T[]): this; /** * Limit number of rows returned * @example * ```ts * const rows: T[] = await new Slice(12345).select().limit(10) * .exec().rows(); * ``` * @param {number} num Number of rows to return * @return {Slice} */ limit(num: number): this; /** * Add an 'OR' condition inside the where argument * @example * ```ts * const rows: T[] = await new Slice(12345).select() * .where('field1', '>', 1) * .or() * .where({field2: 2, field3: 3}) * .or() * .where(['$gt', ['$field', field4], 4) * .exec().rows(); * ``` * @return {Slice} */ or(): this; /** * Order rows by a field * @example * ```ts * const rows: T[] = new Slice(12345).select().order('field1', true) // true = ascending * .exec().rows(); * ``` * @param {string} field property name * @param {boolean} ascending Sort in ascending or descending order * @return {Slice} */ order(field: string, ascending?: boolean): this; /** * Unwrap response, returning the field * @param {string} field Colon seperated path: `rows:0` * @return {Slice} */ pop(field: string | null): this; /** * Unwrap response returning the first row * @return {Slice} */ row(): this; /** * Unwrap response returning the rows * @return {Slice} */ rows(): this; /** * Save multiple rows to the slice, automatically inserts/updates * @param {T} rows Rows to add to slice * @returns {this} */ save(rows: T | T[]): this; /** * Set the request type to select * @example * ```ts * const rows: T[] = new Slice(12345).select().exec().rows(); * const row: T = new Slice(12345).select(id).exec().row(); * ``` * @param {number | number[]} id ID(s) to select, leaving blank will return all rows * @return {Slice} */ select(id?: number | number[]): this; /** * Set the request type to update * @example * ```ts * const ids: number[] = await new Slice(12345).update([ * {id: 1, field1: 1}, * {id: 2, field1: 1} * ]).exec().keys(); * ``` * @param {T | T[]} rows Rows to be updated, each row must have an ID * @return {Slice} */ update(rows: T | T[]): this; /** * Add where condition to request * @example * ```ts * const rows: T[] = await new Slice(12345).select() * .where('field1', '>', 1) * .where({field2: 2, field3: 3}) // Automatic AND * .or() * .where(['$gt', ['$field', field4], 4) * .exec().rows(); * ``` * @param {string | object} field property to compare or a map of equality comparisons * @param {string} operator Operation to compare with. Accepts JS operators (>=, ==, !=, ...) as well as datalynk styax ($gte, $eq, $is, $not, ...) * @param {any} value value to compare against * @return {Slice} */ where(field: string | object, operator?: string, value?: any): this; } /** * An object to aid in constructing Datalynk requests */ export declare class Slice { private slice; private api; static idMap: { [key: number]: number; }; static nextId: number; private table?; private info?; private loaded; private pushing; private pushTimer; private pushBackoff; /** Cached slice data as an observable */ cache$: BehaviorSubject; /** Unsubscribe from changes, undefined if not subscribed */ unsubscribe?: Unsubscribe | null; /** Cached slice data */ get cache(): T[]; /** Set cached data & alert subscribers */ private set cache(value); /** * Whether this slice has local IndexedDB/offline support enabled. * * Offline-enabled slices can fall back to their local cache when either the * browser loses network connectivity or the Datalynk client enters an * unavailable recovery state after a returned server/API failure. */ get offlineEnabled(): boolean | undefined; /** * An object to aid in constructing requests * * @param {number} slice Slice ID to interact with * @param {Api} api Api to send the requests through */ constructor(slice: number | string, api: Api); private schedulePushChanges; private execWrapper; pushChanges(): Promise; /** * Get slice information * @param reload Ignore cache & reload info * @return {Promise} */ getInfo(reload?: boolean): Promise; /** * Synchronize the local slice cache with the server and subscribe to socket changes. * * For an offline-enabled slice, synchronization failures caused by network or * API unavailability are contained in the background rather than becoming * unhandled promise rejections. Local cached data remains usable while the * {@link Api} recovery loop determines when the failed API path works again. * When the client becomes online again, pending local changes are scheduled * for upload without overlapping push operations. * * Calling `sync(false)` unsubscribes from slice socket events. * * @example * ```ts * const slice: Slice = new Slice(Slices.Contact); * const rows$ = slice.sync(); * rows$?.subscribe((rows: T[]) => console.log(rows)); * ``` * @param on Enable or disable synchronization/socket events. * @returns The observable local cache when synchronization is enabled. */ sync(on?: boolean): BehaviorSubject | undefined; /** * {@inheritDoc ApiCall.count} */ count(arg?: object | string): ApiCall; /** * {@inheritDoc ApiCall.delete} */ delete(id: number | number[]): ApiCall; /** * {@inheritDoc ApiCall.insert} */ insert(rows: T | T[]): ApiCall; /** * {@inheritDoc ApiCall.save} */ save(rows: T | T[]): ApiCall; /** * {@inheritDoc ApiCall.select} */ select(id?: number | number[]): ApiCall; /** * {@inheritDoc ApiCall.update} */ update(rows: T | T[]): ApiCall; } //# sourceMappingURL=slice.d.ts.map