<commit_before>/*
{
  // the table's name, is required
  name: null,

  // table properties
  props: {
    key: 'id',
    // default key column, can be ['user_id', 'post_id'] for composite keys
    autoId: false,
    // by default we don't assume that you use an auto generated db id
    perPage: 25,
    // standard batch size per page used by `forPage` method
    // forPage method uses offset
    // avoid that and use a keyset in prod (http://use-the-index-luke.com/no-offset)
    timestamps: false
    // set to `true` if you want auto timestamps or
    // timestamps: ['created_at', 'updated_at'] (these are defaults when `true`)
    // will be assigned in this order only
  },

  // predefined scopes on the table
  scopes: {},
  // predefined joints on the table
  joints: {},
  // relations definitions for the table
  relations: {},
  // table methods defintions
  methods: {}
}
*/

const md5 = require('md5');
const uuid = require('uuid');
const isUsableObject = require('isusableobject');
const {
  isArray,
  isString,
  isDate,
  isNumber,
  isFunction,
  assign,
  merge,
  toPlainObject
} = require('lodash');

const Scope = require('./Scope');
const Track = require('./Track');

const HasOne = require('./relations/HasOne');
const HasMany = require('./relations/HasMany');
const HasManyThrough = require('./relations/HasManyThrough');
const BelongsTo = require('./relations/BelongsTo');
const ManyToMany = require('./relations/ManyToMany');
const MorphOne = require('./relations/MorphOne');
const MorphMany = require('./relations/MorphMany');
const MorphTo = require('./relations/MorphTo');

class Table {
  constructor(orm) {
    this.orm = orm;
    this.scopeTrack = new Track();

    if (this.orm.cache) {
      this.cache = this.orm.cache.hash(this.tableName());
    }
  }

  /**
   * get the tablename
   * @return {string} the tableName
   */
  tableName() {
    return this.name;
  }

  /**
   * knex.raw helper
   * @param  {string} expr     raw expression
   * @param  {Array}  bindings bindings for the expression
   * @return {knex.raw}        raw expr
   */
  raw(expr, bindings=[]) {
    return this.orm.raw(expr, bindings);
  }

  /**
   * just return a raw knex query for this table
   * @return {knex.query} a fresh knex query for table
   */
  rawQuery() {
    return this.orm.knex(this.tableName());
  }

  /**
   * get a new query instance for this table, with a few flags
   * set on the query object used by the orm
   * @return {knex.query} a fresh knex query for table with orm flags
   */
  newQuery() {
    return this.attachOrmNSToQuery(
      this.orm.knex(this.tableName())
    );
  }

  /**
   * attach _orm to knex query with required flags
   * @param  {knex.query} q knex query
   * @return {knex.query} query with namespace
   */
  attachOrmNSToQuery(q) {
    q._orm = {
      // used by cache processor
      cacheEnabled: false,
      cacheLifetime: null,
      destroyCache: false,

      // transaction being used on the query
      trx: null,

      // relations to be eagerloaded on the query
      eagerLoads: {}
    };

    return q;
  }

  /**
   * get a fully scoped query, with flags for whether this is a count query or not.
   * the counting function sets columns it counts on smartly
   * @return {knex.query} table's query object with scopeTrack applied
   */
  query() {
    const q = this.newQuery();

    // apply the scopeTrack on the query
    this.scopeTrack.apply(q);

    if (this.scopeTrack.hasScope('select')) {
      return q;
    } else {
      return q.select(this.c('*'));
    }
  }

  /**
   * load columns of the table
   * @return {Promise} a promise which resolves when table columns have loaded
   */
  load() {
    return this.newQuery().columnInfo().then((columns) => {
      this.orm.tableColumns.set(this.tableName(), columns);
      return this;
    });
  }

  /**
   * get a promise of columns in the table
   * @return {Promise} promise containing array of table's columnNames
   */
  columns() {
    if (this.orm.tableColumns.has(this.tableName())) {
      return Promise.resolve(
        Object.keys(this.orm.tableColumns.get(this.tableName()))
      );
    } else {
      return this.load().then(() => this.columns());
    }
  }

  /**
   * qualified column name helper
   * @param  {mixed} col column or [columns]
   * @return {mixed} qualified column name(s)
   */
  c(col) {
    if (isArray(col)) {
      return col.map((c) => this.c(c));
    } else if (isString(col)) {
      col = col.indexOf('.') > -1 ? col : `${this.tableName()}.${col}`;
    }

    return col;
  }

  /**
   * get the key of the table
   * @return {mixed} get the key property
   */
  key() {
    return this.props.key;<commit_msg>things<commit_after>/*
{
  // the table's name, is required
  name: null,

  // table properties
  props: {
    key: 'id',
    // default key column, can be ['user_id', 'post_id'] for composite keys
    autoId: false,
    // by default we don't assume that you use an auto generated db id
    perPage: 25,
    // standard batch size per page used by `forPage` method
    // forPage method uses offset
    // avoid that and use a keyset in prod (http://use-the-index-luke.com/no-offset)
    timestamps: false
    // set to `true` if you want auto timestamps or
    // timestamps: ['created_at', 'updated_at'] (these are defaults when `true`)
    // will be assigned in this order only
  },

  // predefined scopes on the table
  scopes: {},
  // predefined joints on the table
  joints: {},
  // relations definitions for the table
  relations: {},
  // table methods defintions
  methods: {}
}
*/

const md5 = require('md5');
const uuid = require('uuid');
const isUsableObject = require('isusableobject');
const {
  isArray,
  isString,
  isDate,
  isNumber,
  isFunction,
  assign,
  merge,
  toPlainObject
} = require('lodash');

const Scope = require('./Scope');
const Track = require('./Track');

const HasOne = require('./relations/HasOne');
const HasMany = require('./relations/HasMany');
const HasManyThrough = require('./relations/HasManyThrough');
const BelongsTo = require('./relations/BelongsTo');
const ManyToMany = require('./relations/ManyToMany');
const MorphOne = require('./relations/MorphOne');
const MorphMany = require('./relations/MorphMany');
const MorphTo = require('./relations/MorphTo');

class Table {
  constructor(orm) {
    this.orm = orm;
    this.scopeTrack = new Track();

    if (this.orm.cache) {
      this.cache = this.orm.cache.hash(this.tableName());
    }
  }

  /**
   * get the tablename
   * @return {string} the tableName
   */
  tableName() {
    return this.name;
  }

  /**
   * knex.raw helper
   * @param  {string} expr     raw expression
   * @param  {Array}  bindings bindings for the expression
   * @return {knex.raw}        raw expr
   */
  raw(expr, bindings=[]) {
    return this.orm.raw(expr, bindings);
  }

  /**
   * just return a raw knex query for this table
   * @return {knex.query} a fresh knex query for table
   */
  rawQuery() {
    return this.orm.knex(this.tableName());
  }

  /**
   * get a new query instance for this table, with a few flags
   * set on the query object used by the orm
   * @return {knex.query} a fresh knex query for table with orm flags
   */
  newQuery() {
    return this.attachOrmNSToQuery(
      this.orm.knex(this.tableName())
    );
  }

  /**
   * attach _orm to knex query with required flags
   * @param  {knex.query} q knex query
   * @return {knex.query} query with namespace
   */
  attachOrmNSToQuery(q) {
    q._orm = {
      // used by cache processor
      cacheEnabled: false,
      cacheLifetime: null,
      destroyCache: false,

      // transaction being used on the query
      trx: null,

      // relations to be eagerloaded on the query
      eagerLoads: {}
    };

    return q;
  }

  /**
   * get a fully scoped query, with flags for whether this is a count query or not.
   * the counting function sets columns it counts on smartly
   * @return {knex.query} table's query object with scopeTrack applied
   */
  query() {
    const q = this.newQuery();

    // apply the scopeTrack on the query
    this.scopeTrack.apply(q);

    if (this.scopeTrack.hasScope('select')) {
      return q;
    } else {
      return q.select(this.c('*'));
    }
  }

  /**
   * load columns of the table
   * @return {Promise} a promise which resolves when table columns have loaded
   */
  load() {
    return this.newQuery().columnInfo().then((columns) => {
      this.orm.tableColumns.set(this.tableName(), columns);
      return this;
    });
  }

  /**
   * get a promise of columns in the table
   * @return {Promise} promise containing array of table's columnNames
   */
  columns() {
    if (this.orm.tableColumns.has(this.tableName())) {
      return Promise.resolve(
        Object.keys(this.orm.tableColumns.get(this.tableName()))
      );
    } else {
      return this.load().then(() => this.columns());
    }
  }

  /**
   * qualified column name helper
   * @param  {mixed} col column or [columns]
   * @return {mixed} qualified column name(s)
   */
  c(col) {
    if (isArray(col)) {
      return col.map((c) => this.c(c));
    }

    if (isString(col)) {
      col = col.indexOf('.') > -1 ? col : `${this.tableName()}.${col}`;
      col = col.indexOf('->') > -1 ? this.raw(col) : col;
      return col;
    }

    return col;
  }

  /**
   * get the key of the table
   * @return {mixed} get the key property
   */
  key() {
    return this.props.key;
{'fraction_non_alphanumeric': 0.13676206568803465, 'fraction_numerical': 0.0011616855000528038, 'mean_word_length': 2.6311349693251533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2167427', 'n_tokens_mistral': 3064, 'n_tokens_neox': 2972, 'n_words': 913}