<commit_before>import Knex from 'knex';
import {
  Identifiable,
  ConnectorConstructor,
  ModelStatic,
  ModelConstructor,
  Bindings,
  FilterSpecial,
  FilterProperty,
  Filter,
  FilterIn,
  FilterBetween,
  FilterCompare,
  FilterRaw,
  BaseType,
} from '@next-model/core';

export class NextModelKnexConnector<S extends Identifiable> implements ConnectorConstructor<S> {
  knex: Knex;

  constructor(options: Knex.Config) {
    this.knex = Knex(options);
  }

  private tableName(model: ModelStatic<S>): string {
    if (model.collectionName !== undefined) {
      return model.collectionName;
    } else {
      return model.lowerModelName;
    }
  }

  private table(model: ModelStatic<S>): Knex.QueryBuilder {
    return this.knex(this.tableName(model));
  }

  private propertyFilter(query: Knex.QueryBuilder, filter: FilterProperty<S>): Knex.QueryBuilder {
    return query.where(filter);
  }

  private andFilter(query: Knex.QueryBuilder, filters: Filter<S>[]): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private notFilter(query: Knex.QueryBuilder, filter: Filter<S>): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private orFilter(query: Knex.QueryBuilder, filters: Filter<S>[]): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private inFilter(query: Knex.QueryBuilder, filter: FilterIn<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.whereIn(key, <any>filter[key]);
    }
    throw '[TODO] Should not reach error';
  }

  private notInFilter(query: Knex.QueryBuilder, filter: FilterIn<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.whereNotIn(key, <any>filter[key]);
    }
    throw '[TODO] Should not reach error';
  }

  private nullFilter(query: Knex.QueryBuilder, key: keyof S): Knex.QueryBuilder {
    return query.whereNull(key);
  }

  private notNullFilter(query: Knex.QueryBuilder, key: keyof S): Knex.QueryBuilder {
    return query.whereNotNull(key);
  }

  private betweenFilter(query: Knex.QueryBuilder, filter: FilterBetween<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.andWhereBetween(key, [<any>filter[key].from, <any>filter[key].to])
    }
    throw '[TODO] Should not reach error';
  }

  private rawFilter(query: Knex.QueryBuilder, filter: FilterRaw): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private specialFilter(query: Knex.QueryBuilder, filter: FilterSpecial<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    if (filter.$and !== undefined)
      return this.andFilter(query, filter.$and);
    if (filter.$or !== undefined)
      return this.orFilter(query, filter.$or);
    if (filter.$not !== undefined)
      return this.notFilter(query, filter.$not);
    if (filter.$in !== undefined)
      return this.inFilter(query, filter.$in);
    if (filter.$notIn !== undefined)<commit_msg>Add not between filter<commit_after>import Knex from 'knex';
import {
  Identifiable,
  ConnectorConstructor,
  ModelStatic,
  ModelConstructor,
  Bindings,
  FilterSpecial,
  FilterProperty,
  Filter,
  FilterIn,
  FilterBetween,
  FilterCompare,
  FilterRaw,
  BaseType,
} from '@next-model/core';

export class NextModelKnexConnector<S extends Identifiable> implements ConnectorConstructor<S> {
  knex: Knex;

  constructor(options: Knex.Config) {
    this.knex = Knex(options);
  }

  private tableName(model: ModelStatic<S>): string {
    if (model.collectionName !== undefined) {
      return model.collectionName;
    } else {
      return model.lowerModelName;
    }
  }

  private table(model: ModelStatic<S>): Knex.QueryBuilder {
    return this.knex(this.tableName(model));
  }

  private propertyFilter(query: Knex.QueryBuilder, filter: FilterProperty<S>): Knex.QueryBuilder {
    return query.where(filter);
  }

  private andFilter(query: Knex.QueryBuilder, filters: Filter<S>[]): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private notFilter(query: Knex.QueryBuilder, filter: Filter<S>): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private orFilter(query: Knex.QueryBuilder, filters: Filter<S>[]): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private inFilter(query: Knex.QueryBuilder, filter: FilterIn<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.whereIn(key, <any>filter[key]);
    }
    throw '[TODO] Should not reach error';
  }

  private notInFilter(query: Knex.QueryBuilder, filter: FilterIn<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.whereNotIn(key, <any>filter[key]);
    }
    throw '[TODO] Should not reach error';
  }

  private nullFilter(query: Knex.QueryBuilder, key: keyof S): Knex.QueryBuilder {
    return query.whereNull(key);
  }

  private notNullFilter(query: Knex.QueryBuilder, key: keyof S): Knex.QueryBuilder {
    return query.whereNotNull(key);
  }

  private betweenFilter(query: Knex.QueryBuilder, filter: FilterBetween<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.andWhereBetween(key, [<any>filter[key].from, <any>filter[key].to])
    }
    throw '[TODO] Should not reach error';
  }

  private notBetweenFilter(query: Knex.QueryBuilder, filter: FilterBetween<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    for (const key in filter) {
      return query.andWhereNotBetween(key, [<any>filter[key].from, <any>filter[key].to])
    }
    throw '[TODO] Should not reach error';
  }

  private rawFilter(query: Knex.QueryBuilder, filter: FilterRaw): Knex.QueryBuilder {
    throw '[TODO] Not yet implemented';
  }

  private specialFilter(query: Knex.QueryBuilder, filter: FilterSpecial<S>): Knex.QueryBuilder {
    if (Object.keys(filter).length !== 1) throw '[TODO] Return proper error';
    if (filter.$and !== undefined)
      return this.andFilter(query, filter.$and);
    if (filter.$or !== undefined)
      return this.orFilter(query, filter.$or);
    if (filter.$not !== undefined)
      return this.notFilter(query, filter.$not);
    if (filter.$in !== undefined)
      return this.inFilter(query, filter.$in);
    if (filter.$notIn !== undefined)
{'fraction_non_alphanumeric': 0.13378819341441708, 'fraction_numerical': 0.0013349154553544942, 'mean_word_length': 4.0699248120300755, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, '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': '172277', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2090, 'n_words': 525}