UNPKG

ts-db-helper

Version:
111 lines 3.83 kB
import { QueryManager } from '../../managers/query-manager'; import { ModelManager } from '../../managers/model-manager'; import { DbQuery } from '../db-query.model'; import { ClauseGroup } from './clause-group.model'; import { Clause } from './clause.model'; /** * @public * @class QueryDelete * * @description * For design reasons this class should not be used directly. Use this class through {@link Delete} function. * * @param T @extends DbHelperModel a model declared with table * and column annotations * * @example * ```typesrcript * // Delete a specific model using Delete * // assume that "todo" is declared before and is a model extending * // DbHelperModel and using Table + Column annotation * Delete(todo).exec().subscribe((qr: QueryResult<any>) => { * // the model is deleted... * }, (err) => { * // manage th error... * }); * // You could use Delete statement to delete many entries * Delete(Todo).where({isDone: true}).exec().subscribe((qr: QueryResult<any>) => { * // all done todos are deleted ! * }, (err) => { * // manage th error... * }); * ``` * * @author Olivier Margarit * @since 0.1 */ var QueryDelete = /** @class */ (function () { /** * @public * @constructor should not be use directly, see class header * * @param {T | {new(): T }} model DbHelperModel extention */ function QueryDelete(model) { this.model = model; /** * @private * @constant {string} type the type of statement, should not be modified */ this.type = 'DELETE'; } /** * @public * @method where is the method to add clauses to the where statement of the query * see {@link Clause} or {@link ClauseGroup} * * @param {Clause|Clause[]|ClauseGroup|CompositeClause|{[index: string]: any}} clauses list of clauses * * @return {QueryDelete<T>} this instance to chain query instructions */ QueryDelete.prototype.where = function (clauses) { if (!this.whereClauses) { this.whereClauses = new ClauseGroup(); } this.whereClauses.add(clauses); return this; }; /** * @public * @method build should be removed to be a part of the private API * * @return {DbQuery} of the query with the string part and * clauses params. */ QueryDelete.prototype.build = function () { var table = ModelManager.getInstance().getModel(this.model); var dbQuery = new DbQuery(); dbQuery.table = table.name; dbQuery.type = this.type; dbQuery.query += this.type; dbQuery.query += ' FROM ' + table.name; if (this.model.$$isDbHelperModel) { for (var _i = 0, _a = table.columnList; _i < _a.length; _i++) { var column = _a[_i]; if (column.primaryKey) { var clause = new Clause(); clause.key = column.name; clause.value = this.model[column.field]; this.where(clause); } } } if (this.whereClauses) { dbQuery.query += ' WHERE'; dbQuery.append(this.whereClauses.build()); } return dbQuery; }; /** * @public * @method exec to execute the query and asynchronously retreive result. * * @return {Observable<QueryResult<any>>} observable to subscribe and retrieve results */ QueryDelete.prototype.exec = function () { return QueryManager.getInstance().query(this.build()); }; return QueryDelete; }()); export { QueryDelete }; //# sourceMappingURL=query-delete.model.js.map