import { QueryBase } from "./QueryBase"; import { WhereQuery } from "./WhereQuery"; import { OrderProperty } from "./OrderProperty"; import { QueryBuilder } from "./QueryBuilder"; /** * The query class is used for constructing queries. * @example * const query = new Sitefinity.Query(); * const query0 = query.where().and().endsWith("Title", "Record").done().or().startsWith('Title', 'asd').done().done(); * const query1 = query.where().or().endsWith("Record", "Title").ne('age', '15').eq('Content', 'test').done().done(); * const query2 = query.where().and().endsWith("Title", "Record").done().done(); * const query3 = query.where().endsWith("Record", "Title").or().ne('age', '15').eq('Content', 'test').done().done(); * const query4 = query.where().not().endsWith("Record", "Title").done().and().not().eq('Content', 'test').done().done().done(); * const query5 = query.where().not().and().endsWith("Record", "Title").eq('Content', 'test').done().done().done(); * const query6 = query.select("Id", "Title", "Content").expand('Tags').order("Title desc").where().ne('Title', 'zzz').eq('Title', 'newTitle').done()//.count(); * const query7 = query.order("Title desc").where().eq('Title', 'newTitleUPDATE1').done().select("Title", "Content"); * const query8 = query.where().eq('Title', 'newTitleN11').done(); */ export class Query extends QueryBase { orderProperties: OrderProperty[] = null; skipValue: number = null; takeValue: number = null; searchValue: string = null; inlineCount: boolean; filter: WhereQuery = null; constructor() { super(); } /** * Begins a where clause for filtering the items. */ where(): WhereQuery { const clone: Query = this.clone((newObj: Query) => { newObj.filter = new WhereQuery(newObj); }); return clone.filter; } /** * Specifies the order by clause. * @example * order by single field * order("Title desc") * @example * order by multiple fields * order("Title desc", "Description asc") */ order(...properties: string[]): Query { properties = properties ?? []; this.validatePropertyPaths(properties); return this.clone((newObj: Query) => { const orderItems: OrderProperty[] = []; for (let i = 0; i < properties.length; i++) { const member = properties[i]; const orderProp = new OrderProperty(member); orderItems.push(orderProp); } newObj.orderProperties = orderItems; }); } /** * Skips a certain number of items from the beginning before returning the rest of the items. Used for paging. */ skip(value: number): Query { return this.clone((newObj: Query) => { newObj.skipValue = value; }); } /** * Takes a specified number of items from the query result. Used for paging. */ take(value: number): Query { return this.clone((newObj: Query) => { newObj.takeValue = value; }); } /** * Searches for the items matching the specified expression. */ search(value: string): Query { return this.clone((newObj: Query) => { newObj.searchValue = value; }); } /** * Returns the total item count with the result. Used for paging. */ count(inline = true): Query { return this.clone((newObj: Query) => { newObj.inlineCount = inline; }); } build() { return new QueryBuilder(this).build(); } }