<commit_before>/**
  @module ember-flexberry-data
*/

import Ember from 'ember';
import FilterOperator from './filter-operator';
import { SimplePredicate, ComplexPredicate, StringPredicate, DetailPredicate } from './predicate';
import BaseAdapter from './base-adapter';
import { getAttributeFilterFunction, buildProjection, buildOrder, buildTopSkip, buildFilter } from './js-adapter';
import Information from '../utils/information';
import Dexie from 'npm:dexie';

/**
  Class of query language adapter that allows to load data from IndexedDB.

  @namespace Query
  @class IndexedDBAdapter
  @extends Query.BaseAdapter
*/
export default class extends BaseAdapter {
  /**
    @param {Dexie} db Dexie database instance.
    @class IndexedDBAdapter
    @constructor
  */
  constructor(db) {
    super();

    if (!db) {
      throw new Error('Database must be.');
    }

    this._db = db;
  }

  /**
    Loads data from IndexedDB.

    @method query
    @param {QueryObject} QueryObject instance to the adapter.
    @return {Promise} Promise with loaded data.
  */
  query(query) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      let isBadQuery = containsRelationships(query);
      let order = buildOrder(query);
      let topskip = buildTopSkip(query);
      let jsProjection = buildProjection(query);
      let table = this._db.table(query.modelName);
      let filter = query.predicate ? buildFilter(query.predicate, { booleanAsString: true }) : (data) => data;
      let projection = query.projectionName ? query.projectionName : query.projection ? query.projection : null;

      (isBadQuery ? table : updateWhereClause(table, query)).toArray().then((data) => {
        let length = data.length;
        if (!isBadQuery) {
          data = topskip(order(data));
        }

        Dexie.Promise.all(data.map(i => i.loadRelationships(projection))).then(() => {
          if (isBadQuery) {
            data = filter(data);
            length = data.length;
            data = topskip(order(data));
          }

          if (!projection) {
            data = jsProjection(data);
          }

          let response = { meta: {}, data: data };
          if (query.count) {
            response.meta.count = length;
          }

          resolve(response);
        });
      }).catch((error) => {
        reject(error);
      });
    });
  }
}

/**
  Builds Dexie `WhereClause` for filtering data.
  Filtering only with Dexie can applied only for simple cases (for `SimplePredicate`).
  For complex cases all logic implemened programmatically.

  @param {Dexie.Table} table Table instance for loading objects.
  @param {Query} query Query language instance for loading data.
  @returns {Dexie.Collection|Dexie.Table} Table or collection that can be used with `toArray` method.
*/
function updateWhereClause(table, query) {
  let predicate = query.predicate;

  if (query.id) {
    if (!predicate) {
      predicate = new SimplePredicate('id', FilterOperator.Eq, query.id);
    } else {
      predicate = predicate.and(new SimplePredicate('id', FilterOperator.Eq, query.id));
    }
  }

  if (!predicate) {<commit_msg>Fix indexeddb-adapter add missing reject<commit_after>/**
  @module ember-flexberry-data
*/

import Ember from 'ember';
import FilterOperator from './filter-operator';
import { SimplePredicate, ComplexPredicate, StringPredicate, DetailPredicate } from './predicate';
import BaseAdapter from './base-adapter';
import { getAttributeFilterFunction, buildProjection, buildOrder, buildTopSkip, buildFilter } from './js-adapter';
import Information from '../utils/information';
import Dexie from 'npm:dexie';

/**
  Class of query language adapter that allows to load data from IndexedDB.

  @namespace Query
  @class IndexedDBAdapter
  @extends Query.BaseAdapter
*/
export default class extends BaseAdapter {
  /**
    @param {Dexie} db Dexie database instance.
    @class IndexedDBAdapter
    @constructor
  */
  constructor(db) {
    super();

    if (!db) {
      throw new Error('Database must be.');
    }

    this._db = db;
  }

  /**
    Loads data from IndexedDB.

    @method query
    @param {QueryObject} QueryObject instance to the adapter.
    @return {Promise} Promise with loaded data.
  */
  query(query) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      let isBadQuery = containsRelationships(query);
      let order = buildOrder(query);
      let topskip = buildTopSkip(query);
      let jsProjection = buildProjection(query);
      let table = this._db.table(query.modelName);
      let filter = query.predicate ? buildFilter(query.predicate, { booleanAsString: true }) : (data) => data;
      let projection = query.projectionName ? query.projectionName : query.projection ? query.projection : null;

      (isBadQuery ? table : updateWhereClause(table, query)).toArray().then((data) => {
        let length = data.length;
        if (!isBadQuery) {
          data = topskip(order(data));
        }

        Dexie.Promise.all(data.map(i => i.loadRelationships(projection))).then(() => {
          if (isBadQuery) {
            data = filter(data);
            length = data.length;
            data = topskip(order(data));
          }

          if (!projection) {
            data = jsProjection(data);
          }

          let response = { meta: {}, data: data };
          if (query.count) {
            response.meta.count = length;
          }

          resolve(response);
        }, reject);
      }, reject);
    });
  }
}

/**
  Builds Dexie `WhereClause` for filtering data.
  Filtering only with Dexie can applied only for simple cases (for `SimplePredicate`).
  For complex cases all logic implemened programmatically.

  @param {Dexie.Table} table Table instance for loading objects.
  @param {Query} query Query language instance for loading data.
  @returns {Dexie.Collection|Dexie.Table} Table or collection that can be used with `toArray` method.
*/
function updateWhereClause(table, query) {
  let predicate = query.predicate;

  if (query.id) {
    if (!predicate) {
      predicate = new SimplePredicate('id', FilterOperator.Eq, query.id);
    } else {
      predicate = predicate.and(new SimplePredicate('id', FilterOperator.Eq, query.id));
    }
  }

  if (!predicate) {
{'fraction_non_alphanumeric': 0.11872802812400128, 'fraction_numerical': 0.0, 'mean_word_length': 2.90455396132252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1056722', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1780, 'n_words': 502}