///
import stream from 'stream';
import { BaseModel, ModelColumnNamesWithId } from './model';
import { Transaction } from './transaction';
import { RecordID } from './types';
interface QueryOptions {
lean: boolean;
orders?: string;
near?: any;
select_columns?: string[];
select_single: boolean;
conditions_of_group: any[];
group_fields?: any;
group_by?: string[];
limit?: number;
skip?: number;
one?: boolean;
explain?: boolean;
cache?: {
key: string;
ttl: number;
refresh?: boolean;
};
transaction?: Transaction;
node?: 'master' | 'read';
index_hint?: string;
}
export interface QuerySingle extends PromiseLike {
clone(): QuerySingle;
find(id: RecordID): QuerySingle;
find(id: RecordID[]): QueryArray;
findPreserve(id: RecordID[]): QueryArray;
near(target: object): QuerySingle;
where(condition?: object): QuerySingle;
select>(columns: K[]): QuerySingle>;
select>(columns?: string): QuerySingle>;
selectSingle>(column: K): QuerySingle;
order(orders?: string): QuerySingle;
group, F>(group_by: G | G[], fields?: F): QuerySingle>;
group(group_by: null, fields?: F): QuerySingle;
group(group_by: string | null, fields?: object): QuerySingle;
one(): QuerySingleNull;
limit(limit?: number): QuerySingle;
skip(skip?: number): QuerySingle;
lean(lean?: boolean): QuerySingle;
if(condition: boolean): QuerySingle;
endif(): QuerySingle;
cache(options: QueryOptions['cache']): QuerySingle;
include(column: string, select?: string): QuerySingle;
transaction(transaction?: Transaction): QuerySingle;
using(node: 'master' | 'read'): QuerySingle;
index_hint(hint: string): QuerySingle;
exec(options?: {
skip_log?: boolean;
}): PromiseLike;
stream(): stream.Readable;
explain(): PromiseLike;
count(): PromiseLike;
update(updates: object): PromiseLike;
upsert(updates: object): PromiseLike;
delete(options?: any): PromiseLike;
}
interface QuerySingleNull extends PromiseLike {
clone(): QuerySingleNull;
find(id: RecordID): QuerySingle;
find(id: RecordID[]): QueryArray;
findPreserve(id: RecordID[]): QueryArray;
near(target: object): QuerySingleNull;
where(condition?: object): QuerySingleNull;
select>(columns: K[]): QuerySingleNull>;
select>(columns?: string): QuerySingleNull>;
selectSingle>(column: K): QuerySingleNull;
order(orders?: string): QuerySingleNull;
group, F>(group_by: G | G[], fields?: F): QuerySingleNull>;
group(group_by: null, fields?: F): QuerySingleNull;
group(group_by: string | null, fields?: object): QuerySingleNull;
one(): QuerySingleNull;
limit(limit?: number): QuerySingleNull;
skip(skip?: number): QuerySingleNull;
lean(lean?: boolean): QuerySingleNull;
if(condition: boolean): QuerySingleNull;
endif(): QuerySingleNull;
cache(options: QueryOptions['cache']): QuerySingleNull;
include(column: string, select?: string): QuerySingleNull;
transaction(transaction?: Transaction): QuerySingleNull;
using(node: 'master' | 'read'): QuerySingleNull;
index_hint(hint: string): QuerySingleNull;
exec(options?: {
skip_log?: boolean;
}): PromiseLike;
stream(): stream.Readable;
explain(): PromiseLike;
count(): PromiseLike;
update(updates: object): PromiseLike;
upsert(updates: object): PromiseLike;
delete(options?: any): PromiseLike;
}
export interface QueryArray extends PromiseLike {
clone(): QueryArray;
find(id: RecordID): QuerySingle;
find(id: RecordID[]): QueryArray;
findPreserve(id: RecordID[]): QueryArray;
near(target: object): QueryArray;
where(condition?: object): QueryArray;
select>(columns: K[]): QueryArray>;
select>(columns?: string): QueryArray>;
selectSingle>(column: K): QueryArray;
order(orders?: string): QueryArray;
group, F>(group_by: G | G[], fields?: F): QueryArray>;
group(group_by: null, fields?: F): QueryArray;
group(group_by: string | null, fields?: object): QueryArray;
one(): QuerySingleNull;
limit(limit?: number): QueryArray;
skip(skip?: number): QueryArray;
lean(lean?: boolean): QueryArray;
if(condition: boolean): QueryArray;
endif(): QueryArray;
cache(options: QueryOptions['cache']): QueryArray;
include(column: string, select?: string): QueryArray;
transaction(transaction?: Transaction): QueryArray;
using(node: 'master' | 'read'): QueryArray;
index_hint(hint: string): QueryArray;
exec(options?: {
skip_log?: boolean;
}): PromiseLike;
stream(): stream.Readable;
explain(): PromiseLike;
count(): PromiseLike;
update(updates: object): PromiseLike;
upsert(updates: object): PromiseLike;
delete(options?: any): PromiseLike;
}
/**
* Collects conditions to query
*/
declare class Query implements QuerySingle, QueryArray {
private _model;
private _name;
private _connection;
private _adapter;
private _ifs;
private _current_if;
private _options;
private _conditions;
private _includes;
private _id;
private _find_single_id;
private _preserve_order_ids?;
private _used;
/**
* Creates a query instance
*/
constructor(model: typeof BaseModel);
clone(): Query;
/**
* Finds a record by id
*/
find(id: RecordID): QuerySingle;
find(id: RecordID[]): QueryArray;
/**
* Finds records by ids while preserving order.
*/
findPreserve(ids: RecordID[]): QueryArray;
/**
* Finds records near target
*/
near(target: object): this;
/**
* Finds records by condition
*/
where(condition?: object): this;
/**
* Selects columns for result
*/
select>(columns?: string | string[]): QuerySingle>;
select>(columns?: string | string[]): QueryArray>;
selectSingle>(column: K): QuerySingle;
selectSingle>(column: K): QueryArray;
/**
* Specifies orders of result
*/
order(orders?: string): this;
/**
* Groups result records
*/
group(group_by: string | string[] | null, fields?: object): QuerySingle;
group(group_by: string | string[] | null, fields?: object): QueryArray;
/**
* Returns only one record (or null if does not exists).
*
* This is different from limit(1). limit(1) returns array of length 1 while this returns an instance.
*/
one(): this;
/**
* Sets limit of query
*/
limit(limit: number): this;
/**
* Sets skip of query
*/
skip(skip: number): this;
/**
* Returns raw instances instead of model instances
* @see Query::exec
*/
lean(lean?: boolean): this;
/**
* Makes a part of the query chain conditional
* @see Query::endif
*/
if(condition: boolean): this;
/**
* Ends last if
* @chainable
* @see Query::if
*/
endif(): this;
/**
* Cache result.
*
* If cache of key exists, actual query does not performed.
* If cache does not exist, query result will be saved in cache.
*
* Redis is used to cache.
*/
cache(options: QueryOptions['cache']): this;
/**
* Returns associated objects also
*/
include(column: string, select?: string): this;
transaction(transaction?: Transaction): this;
using(node: 'master' | 'read'): this;
index_hint(hint: string): this;
/**
* Executes the query
* @see AdapterBase::findById
* @see AdapterBase::find
*/
exec(options?: {
skip_log?: boolean;
}): Promise;
/**
* Executes the query and returns a readable stream
* @see AdapterBase::findById
* @see AdapterBase::find
*/
stream(): stream.Readable;
/**
* Explains the query
*/
explain(): Promise;
/**
* Executes the query as a promise (.then == .exec().then)
*/
then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | ((value: T[]) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): PromiseLike;
/**
* Executes the query as a count operation
* @see AdapterBase::count
*/
count(): Promise;
/**
* Executes the query as a update operation
* @see AdapterBase::update
*/
update(updates: any): Promise;
/**
* Executes the query as an insert or update operation
* @see AdapterBase::upsert
*/
upsert(updates: any): Promise;
/**
* Executes the query as a delete operation
* @see AdapterBase::delete
*/
delete(options?: any): Promise;
private _exec;
private _getAdapterFindOptions;
private _execAndInclude;
private _validateAndBuildSaveData;
private _doIntegrityActions;
private _doArchiveAndIntegrity;
private _addCondition;
private _setUsed;
}
export { Query };