All files / src/Query QuerySegment.ts

100% Statements 51/51
100% Branches 21/21
100% Functions 23/23
100% Lines 51/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239        1x           1x               38x                     3x 3x               9x 9x                 7x 7x               84x     124x         19x 19x               1x               1x 1x                 4x 4x 4x                 2x 2x 2x                   3x 3x                 6x 6x                   19x 19x                     3x 3x                       3x 3x                   2x 2x                   2x 2x               4x 4x 4x               1x 1x 1x       50x 50x         1x           12x 12x             4x 4x       16x 16x        
/**
 * @module Query
 */ /** */
 
import { Query } from '.';
import { IContent, SavedContent } from '../Content';
 
/**
 * Represents a query expression segment
 */
export class QuerySegment<TReturns extends IContent> {
 
    /**
     * Escapes a String value (except '?' and '*' characters for wildcards)
     * @param {string} value The String value to be escaped
     * @returns {string} The escaped value
     */
    protected escapeValue(value: string): string {
        return typeof value === 'string' ? value.replace(/([\!\+\&\|\(\)\[\]\{\}\^\~\:\"])/g, '\\$1') : value;
    }
 
    protected _stringValue: string;
 
    /**
     * A '.SORT' Content Query segment
     * @param {K} field The name of the field
     * @param {boolean} reverse Sort in reverse order, false by default
     */
    public Sort<K extends keyof TReturns>(field: K, reverse: boolean = false) {
        this._stringValue = ` .${reverse ? 'REVERSESORT' : 'SORT'}:'${field}'`;
        return this.finializeSegment();
    }
 
    /**
     * A '.TOP' Content Query segment
     * @param {number} topCount The TOP item count
     */
    public Top(topCount: number) {
        this._stringValue = ` .TOP:${topCount}`;
        return this.finializeSegment();
    }
 
    /**
     * Adds a '.SKIP' Content Query segment
     * @param {number} skipCount Items to skip
     */
 
    public Skip(skipCount: number) {
        this._stringValue = ` .SKIP:${skipCount}`;
        return this.finializeSegment();
    }
 
    /**
     * @returns {string} a segment string value
     */
    // tslint:disable-next-line:naming-convention
    public toString() {
        return this._stringValue;
    }
 
    constructor(protected readonly _queryRef: Query<TReturns>) {
 
    }
 
    protected finializeSegment(): QuerySegment<TReturns> {
        this._queryRef.AddSegment(this);
        return new QuerySegment(this._queryRef);
    }
 
}
 
/**
 * Represents a sensenet Content Query expression
 */
export class QueryExpression<TReturns extends IContent> extends QuerySegment<TReturns> {
 
    /**
     * A plain string as Query term
     * @param {string} term The Query term
     * @returns { QueryOperator<TReturns> } The Next query operator (fluent)
     */
    public Term(term: string) {
        this._stringValue = term;
        return this.finialize();
    }
 
    /**
     * Adds an InTree content query expression
     * @param {string | Content } path The path string or content that will be used as a root
     * @returns { QueryOperator<TReturns> } The Next query operator (fluent)
     */
    public InTree(path: string | SavedContent) {
        const pathValue = this.escapeValue((path as SavedContent).Path ? (path as SavedContent).Path : path.toString());
        this._stringValue = `InTree:"${pathValue}"`;
        return this.finialize();
    }
 
    /**
     * Adds an InFolder content query expression
     * @param {string | Content } path The path string or content that will be used as a root
     * @returns { QueryOperator<TReturns> } The Next query operator (fluent)
     */
    public InFolder(path: string | SavedContent) {
        const pathValue = this.escapeValue((path as SavedContent).Path ? (path as SavedContent).Path : path.toString());
        this._stringValue = `InFolder:"${pathValue}"`;
        return this.finialize();
    }
 
    /**
     * Adds a Type content query expression and casts the rest of the expression to a new type
     * @param {{ new(...args: any[]): TNewType }} newTypeAssertion The path string or content that will be used as a root
     * @returns { QueryOperator<TNewType> } The Next query operator (fluent)
     */
 
    public Type<TNewType extends IContent = IContent>(newTypeAssertion: { new(...args: any[]): TNewType }) {
        this._stringValue = `Type:${newTypeAssertion.name}`;
        return this.finialize<TNewType>();
    }
 
    /**
     * Adds a TypeIs content query expression and casts the rest of the expression to a new type
     * @param {{ new(...args: any[]): TNewType }} newTypeAssertion The path string or content that will be used as a root
     * @returns { QueryOperator<TNewType> } The Next query operator (fluent)
     */
    public TypeIs<TNewType extends IContent = IContent>(newTypeAssertion: { new(...args: any[]): TNewType }) {
        this._stringValue = `TypeIs:${newTypeAssertion.name}`;
        return this.finialize<TNewType>();
    }
 
    /**
     * Field equality check content query expression (e.g. +FieldName:'value')
     * @param { K } FieldName The name of the Field to be checked
     * @param { TReturns[K] } value The value that will be checked. You can use '?' and '*' wildcards
     * @returns { QueryOperator<TReturns> } The Next query operator (fluent)
     */
    public Equals<K extends keyof TReturns>(fieldName: K | '_Text', value: TReturns[K]) {
        this._stringValue = `${fieldName}:'${this.escapeValue(value)}'`;
        return this.finialize();
    }
 
    /**
     * Field equality and NOT operator combination. (e.g. +NOT(FieldName:'value'))
     * @param { K } FieldName The name of the Field to be checked
     * @param { TReturns[K] } value The value that will be checked. You can use '?' and '*' wildcards
     * @returns { QueryOperator<TReturns> } The Next query operator (fluent)
     */
 
    public NotEquals<K extends keyof TReturns>(fieldName: K, value: TReturns[K]) {
        this._stringValue = `NOT(${fieldName}:'${this.escapeValue(value)}')`;
        return this.finialize();
    }
 
    /**
     * Range search query expression
     * @param { K } fieldName he name of the Field to be checked
     * @param { TReturns[K] } minValue The minimum allowed value
     * @param { TReturns[K] } maxValue The maximum allowed value
     * @param { boolean } minimumInclusive Lower limit will be inclusive / exclusive
     * @param { boolean } maximumInclusive Upper limit will be inclusive / exclusive
     */
    public Between<K extends keyof TReturns>(fieldName: K, minValue: TReturns[K], maxValue: TReturns[K], minimumInclusive: boolean = false, maximumInclusive: boolean = false) {
        this._stringValue = `${fieldName}:${minimumInclusive ? '[' : '{'}'${this.escapeValue(minValue)}' TO '${this.escapeValue(maxValue)}'${maximumInclusive ? ']' : '}'}`;
        return this.finialize();
    }
 
    /**
     * Greather than query expression (+FieldName:>'value')
     * @param { K } fieldName he name of the Field to be checked
     * @param { TReturns[K] } minValue The minimum allowed value
     * @param { boolean } minimumInclusive Lower limit will be inclusive / exclusive
     */
    public GreatherThan<K extends keyof TReturns>(fieldName: K, minValue: TReturns[K], minimumInclusive: boolean = false) {
        this._stringValue = `${fieldName}:>${minimumInclusive ? '=' : ''}'${this.escapeValue(minValue)}'`;
        return this.finialize();
    }
 
    /**
     * Less than query expression (+FieldName:<'value')
     * @param { K } fieldName he name of the Field to be checked
     * @param { TReturns[K] } maxValue The maximum allowed value
     * @param { boolean } maximumInclusive Upper limit will be inclusive / exclusive
     */
    public LessThan<K extends keyof TReturns>(fieldName: K, maxValue: TReturns[K], maximumInclusive: boolean = false) {
        this._stringValue = `${fieldName}:<${maximumInclusive ? '=' : ''}'${this.escapeValue(maxValue)}'`;
        return this.finialize();
    }
 
    /**
     * A Nested query expression
     * @param {(first: QueryExpression<TReturns>) => QuerySegment<TReturns>)} build The Expression builder method
     */
    public Query(build: (first: QueryExpression<TReturns>) => QuerySegment<TReturns>) {
        const innerQuery = new Query(build);
        this._stringValue = `(${innerQuery.toString()})`;
        return this.finialize();
    }
 
    /**
     * A Nested NOT query expression
     * @param {(first: QueryExpression<TReturns>) => QuerySegment<TReturns>)} build The Expression builder method
     */
    public Not(build: (first: QueryExpression<TReturns>) => QuerySegment<TReturns>) {
        const innerQuery = new Query(build);
        this._stringValue = `NOT(${innerQuery.toString()})`;
        return this.finialize();
    }
 
    protected finialize<TReturnsExtended extends IContent = TReturns>(): QueryOperators<TReturnsExtended> {
        this._queryRef.AddSegment(this);
        return new QueryOperators<TReturnsExtended>(this._queryRef as any as Query<TReturnsExtended>);
    }
}
 
// And, Or, Etc...
export class QueryOperators<TReturns extends IContent> extends QuerySegment<TReturns> {
 
    /**
     * AND Content Query operator
     */
    public get And() {
        this._stringValue = ' AND ';
        return this.finialize();
    }
 
    /**
     * OR Content Query operator
     */
    public get Or() {
        this._stringValue = ' OR ';
        return this.finialize();
    }
 
    protected finialize() {
        this._queryRef.AddSegment(this);
        return new QueryExpression(this._queryRef);
    }
 
}