All files PouchDbActiveQuery.ts

85.71% Statements 24/28
66.67% Branches 12/18
100% Functions 4/4
80.95% Lines 17/21
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 501x     1x       6x 3x             3x     3x       3x 3x     3x     36x 21x             21x 6x   21x       21x 21x     1x  
import { ActiveQuery } from "@hke/activerecord";
import { PouchDbInstance } from "./PouchDbActiveRecord";
 
export class PouchDbActiveQuery extends ActiveQuery {
 
  db: PouchDbInstance;
 
  public async one(Emap: boolean = true) {
    let query = {
      fields: null,
      limit: 1,
      selector: this.params.where,
      sort: null,
      skip: 0
    };
    Iif (this.params.fields.length) {
      query.fields = this.params.fields;
    }
    Iif (this.params.sort.length) {
      query.sort = this.params.sort;
    }
 
    const res = await this.db.find(query);
    Iif (!res.docs.length) {
      return null;
    }
    return map ? new this.model(res.docs[0]) : res.docs[0];
  }
 
  public async all(map: boolean = true) {
    let query = {
      fields: null,
      limit: this.params.limit.end,
      selector: this.params.where,
      sort: null,
      skip: this.params.limit.start
    };
    if (this.params.fields.length) {
      query.fields = this.params.fields;
    }
    Iif (this.params.sort.length) {
      query.sort = this.params.sort;
    }
 
    const res = await this.db.find(query);
    return map ? res.docs.map((doc) => new this.model(doc)) : res.docs;
  }
 
}