Query

Collects conditions to query

Parameters

(None)
(defined in /src/query.ts:121)
constructor(model: typeof BaseModel) {
    this._model = model;
    this._name = model._name;
    this._connection = model._connection;
    this._adapter = model._connection._adapter;
    this._ifs = [];
    this._current_if = true;
    this._conditions = [];
    this._includes = [];
    this._options = {
      conditions_of_group: [],
      lean: model.lean_query,
      select_single: false,
    };
  }

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.

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:342)
public cache(options: IQueryOptions['cache']): this {
    if (!this._current_if) {
      return this;
    }
    this._options.cache = options;
    return this;
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:438)
public async count(): Promise<number> {
    await this._model._checkReady();
    if (this._id || this._find_single_id) {
      this._conditions.push({ id: this._id });
      delete this._id;
    }
    return await this._adapter.count(this._name, this._conditions, this._options);
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:491)
public async delete(options?: any): Promise<number> {
    await this._model._checkReady();
    if (this._id || this._find_single_id) {
      this._conditions.push({ id: this._id });
      delete this._id;
    }
    if (!(options && options.skip_log)) {
      this._connection.log(this._name, 'delete', { conditions: this._conditions });
    }
    await this._doArchiveAndIntegrity(options);
    return await this._adapter.delete(this._name, this._conditions, { transaction: this._options.transaction });
  }

Parameters

(None)

Returns

NameTypeDescription
(Returns)Querythis

See Also:

(defined in /src/query.ts:325)
public endif() {
    this._ifs.pop();
    this._current_if = true;
    for (const condition of this._ifs) {
      this._current_if = this._current_if && condition;
    }
    return this;
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:371)
public async exec(options?: { skip_log?: boolean }) {
    await this._model._checkReady();
    if (this._options.cache && this._options.cache.key) {
      try {
        // try cache
        return await this._model._loadFromCache(this._options.cache.key, this._options.cache.refresh);
      } catch (error) {
        // no cache, execute query
        const records = await this._execAndInclude(options);
        // save result to cache
        await this._model._saveToCache(this._options.cache.key, this._options.cache.ttl, records);
        return records;
      }
    } else {
      return await this._execAndInclude(options);
    }
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:412)
public async explain() {
    this._options.cache = undefined;
    this._options.explain = true;
    this._includes = [];
    return await this.exec({ skip_log: true });
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:140)
public find(id: RecordID): IQuerySingle<M, T>;

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:159)
public findPreserve(ids: RecordID[]): IQueryArray<M, T> {
    if (!this._current_if) {
      return this;
    }
    this._id = _.uniq(ids);
    this._find_single_id = false;
    this._preserve_order_ids = ids;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:245)
public group<U>(group_by: string | string[] | null, fields?: object): IQuerySingle<M, U>;

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:314)
public if(condition: boolean): this {
    this._ifs.push(condition);
    this._current_if = this._current_if && condition;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:353)
public include(column: any, select: any): this {
    if (!this._current_if) {
      return this;
    }
    this._includes.push({ column, select });
    return this;
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:302)
public lean(lean = true): this {
    if (!this._current_if) {
      return this;
    }
    this._options.lean = lean;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:279)
public limit(limit: number): this {
    if (!this._current_if) {
      return this;
    }
    this._options.limit = limit;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:172)
public near(target: object): this {
    if (!this._current_if) {
      return this;
    }
    this._options.near = target;
    return this;
  }

This is different from limit(1). limit(1) returns array of length 1 while this returns an instance.

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:267)
public one(): this {
    if (!this._current_if) {
      return this;
    }
    this._options.limit = 1;
    this._options.one = true;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:234)
public order(orders?: string): this {
    if (!this._current_if) {
      return this;
    }
    this._options.orders = orders;
    return this;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:200)
public select<K extends ModelColumnNamesWithId<M>>(columns?: string | string[]): IQuerySingle<M, Pick<M, K>>;

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:290)
public skip(skip: number): this {
    if (!this._current_if) {
      return this;
    }
    this._options.skip = skip;
    return this;
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:394)
public stream(): stream.Readable {
    const transformer = new stream.Transform({ objectMode: true });
    transformer._transform = function(chunk, encoding, callback) {
      this.push(chunk);
      callback();
    };
    this._model._checkReady().then(() => {
      this._adapter.stream(this._name, this._conditions, this._getAdapterFindOptions())
        .on('error', (error) => {
          transformer.emit('error', error);
        }).pipe(transformer);
    });
    return transformer;
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:422)
public then<TResult1 = T, TResult2 = never>(
    onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) |
      ((value: T[]) => TResult1 | PromiseLike<TResult1>) | undefined | null,
    onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
  ): PromiseLike<TResult1 | TResult2>;

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:451)
public async update(updates: any): Promise<number> {
    await this._model._checkReady();
    const errors: any[] = [];
    const data = {};
    this._validateAndBuildSaveData(errors, data, updates, '', updates);
    if (errors.length > 0) {
      throw new Error(errors.join(','));
    }
    if (this._id || this._find_single_id) {
      this._conditions.push({ id: this._id });
      delete this._id;
    }
    this._connection.log(this._name, 'update', { data, conditions: this._conditions, options: this._options });
    return await this._adapter.updatePartial(this._name, data, this._conditions, this._options);
  }

Parameters

(None)

Returns

(Nothing)

See Also:

(defined in /src/query.ts:471)
public async upsert(updates: any): Promise<void> {
    await this._model._checkReady();
    const errors: any[] = [];
    const data = {};
    this._validateAndBuildSaveData(errors, data, updates, '', updates);
    if (errors.length > 0) {
      throw new Error(errors.join(','));
    }
    if (this._id || this._find_single_id) {
      this._conditions.push({ id: this._id });
      delete this._id;
    }
    this._connection.log(this._name, 'upsert', { data, conditions: this._conditions, options: this._options });
    return await this._adapter.upsert(this._name, data, this._conditions, this._options);
  }

Parameters

(None)

Returns

(Nothing)
(defined in /src/query.ts:183)
public where(condition?: object): this {
    if (!this._current_if) {
      return this;
    }
    if (Array.isArray(condition)) {
      condition.forEach((cond) => {
        this._addCondition(cond);
      });
    } else if (condition != null) {
      this._addCondition(condition);
    }
    return this;
  }
Fork me on GitHub