ts-db-helper
Version:
Simple ORM based on TypeScript
132 lines (131 loc) • 3.99 kB
TypeScript
import { IClause } from '../interfaces/i-clause.interface';
import { QueryPart } from './query-part.model';
/**
* @public
*
* @class Clause
*
* @description
* This entity is a single clause for where statement.
*
* @example
* ```typescript
* // Create a group of clauses
* const group = new ClauseGroup();
*
* // create clause to get Todo item where isDone === false
* const doneClause = new Clause();
* doneClause.key = 'isDone';
* doneClause.value = false;
* group.add(doneClause);
*
* // create clause to get Todo item where dueDate <= now
* const dueDateClause = new Clause();
* dueDateClause.key = 'dueDate';
* dueDateClause.value = (new Date()).getTime();
* dueDateClause.comparator = Clause.COMPARATORS.LTE;
* group.add(dueDateClause);
*
* // start select clause
* Select(Todo).where(group).exec().subscribe((QueryResult<Todo>) => {
* // do something with the result...
* }, (err) => {
* // do something with the error...
* });
* ```
*
* @author Olivier Margarit
* @since 0.1
*/
export declare class Clause implements IClause {
/**
* @public
* @property {string} operator is used as join clause condition
*
* @default ClauseOperators.AND
*/
operator: string;
private keyValue;
/**
* @public
* @property key is table column name corresponding to the field
* to compare with
*/
/**
* @public
* @method key is table column name corresponding to the field
* to compare with. Since 0.2, key support complex values. You can use an appended key with
* [optional]operator + [optional]not + column name + [optional]comparator with space separator
* or double underscore (__).
* Be carefull, get key will not return the value set for key. The get method only retrieve the column
* informations.
*
* @example:
* ```typescript
* // function to quickly get current timestamp
* function now(): number {
* return (new Date()).getTime()
* }
*
* // Select all done or passed todo
* Select(Todo).where({isDone: true, or__dueDate__lt: now()}).subcribe((qr: QueryResult<Todo>) => {
* const ids = <number[]>[];
* for (let i = 0; i < qr.rows.length; i++) {
* // check if todo really need to be deleted ...
* ids.push(qr.rows.item(i).id);
* }
*
* Delete(Todo).where(id__in:ids).exec().subscribe(() => {
* // todos are delete
* }, (err) => {
* console.error(err);
* })
* }, (err) => {
* console.error(err);
* });
*
* // delete directly without check
* Delete(Todo).where({isDone: true, or__dueDate__lt: now()}).subcribe(() => {
* // delete done
* }, (err) => {
* console.error(err);
* });
* ```
*/
key: string;
/**
* @public
* @property {any} value is the column value to compare with
*/
value: any;
/**
* @public
* @property {boolean} not is an additionnal operator to invert condition
*/
not: boolean;
/**
* @public
* @property {string} comparator is a comparator that define the type of
* relation with the value to compare with and the result.
*
* @default ClauseComparators.EQ
*/
comparator: string;
/**
* @public
* @constructor create a new instance of Clause
* @param {string} key the clause key
* @param {any} value the clause value
*
* @since 0.2
*/
constructor(key?: string, value?: any);
/**
* @public
* @method build should be removed to be a part of the private API
*
* @return {QueryPart} of the query with the string part and
* clauses params.
*/
build(): QueryPart;
}