<commit_before>
import dynogels from 'dynogels'
import {
  NotImplementedException,
  UnauthorizedQueryException
} from '../../utils/exceptions'
import dbClient from '../databaseClient'

dynogels.documentClient(dbClient)

class BaseModel {
  constructor (obj) {
    if (!obj || typeof obj !== 'object') {
      return
    }
    const fieldNames = [].concat(Object.keys(this.constructor.schema),
      ['created', 'updated'])
    fieldNames.forEach((fieldName) => {
      // Set properties for each field on the model.
      // Set the value to the value passed in `obj` if one exists.
      // If not passed a value in `obj`, use the defualt value
      // defined in the `fieldDefaults` method, if it exists.
      if (obj[fieldName]) {
        this[fieldName] = obj[fieldName]
      } else if (this.constructor.fieldDefaults[fieldName]) {
        this[fieldName] = this.constructor.fieldDefaults[fieldName]
      }
    })
  }

  /**
   * The name of the model.
   * You are required to override this function on the child class.
   * @return {string} The name of the model.
   */
  static get name () {
    throw new NotImplementedException()
  }

  /**
   * The name of the database table.
   * You are required to override this function on the child class.
   * @return {string} The name of the database table.
   */
  static get tableName () {
    throw new NotImplementedException()
  }

  /**
   * The name of the hashKey for the DynamoDB table.
   * You are required to override this function on the child class.
   * @return {string} The name of the hashKey for the DynamoDB table.
   */
  static get hashKey () {
    throw new NotImplementedException()
  }

  /**
   * The name of the range key (if it exists) for the DynamoDB table.
   * @return {string} The name of the hashKey for the DynamoDB table.
   */
  static get rangeKey () {
    return null
  }

  /**
   * The table schema, used in dynogels.
   * You are required to override this function on the child class.
   * @return {object} The table schema.
   */
  static get schema () {
    throw new NotImplementedException()
  }

  /**
   * Default values for the fields in schema.
   * @return {object} A map of default values
   */
  static get fieldDefaults () {
    return {}
  }

  /**
   * The permissions object, used to check authorization for database
   * operations. By default, no operations are authorized.
   * @return {object} The permissions object, with a key for each
   *   operation name. Each property value is a function that receives
   *   a user object, item hashKey, and item rangeKey, and must return
   *   a boolean for whether the query is authorized.
   */
  static get permissions () {
    return {
      get: (user, hashKeyValue, rangeKeyValue) => false,
      getAll: () => false,
      update: (user, hashKeyValue, rangeKeyValue) => false,
      create: (user, hashKeyValue) => false
    }
  }

  /**
   * Register the model with dynogels. This must be called prior to
   * using any methods that query the database.
   * @return {undefined}
   */
  static register () {
    // console.log(`Registering model ${this.name} to table ${this.tableName}.`)
    const options = {
      hashKey: this.hashKey,
      tableName: this.tableName,

      // Add two timestamps, `created` and `updated`, to
      // the item's fields.
      timestamps: true,
      createdAt: 'created',
      updatedAt: 'updated',

      schema: this.schema
    }
    if (this.rangeKey) {
      options['rangeKey'] = this.rangeKey
    }
    this.dynogelsModel = dynogels.define(this.name, options)
  }

  static get (user, hashKey, rangeKey, options) {
    const self = this
    let keys = [hashKey]
    if (rangeKey) {
      keys.push(rangeKey)
    }
    // console.log(`Getting obj with hashKey ${hashKey} from table ${this.tableName}.`)
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'get', hashKey, rangeKey)) {
        reject(new UnauthorizedQueryException())
        return
      }
      this.dynogelsModel.get(...keys, (err, data) => {
        if (err) {
          console.log(err)
          reject(err)
        } else {
          console.log('FETCHED DATA', data)
          resolve(self.deserialize(data))
        }
      })
    })
  }

  static getAll (user) {
    // console.log(`Getting all objs in table ${this.tableName}.`)
    const self = this
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'getAll')) {
        reject(new UnauthorizedQueryException())
        return
      }
      this.dynogelsModel.scan().exec((err, data) => {
        if (err) {
          console.log(err)
          reject(err)
        } else {
          resolve(self.deserialize(data.Items))
        }
      })
    })
  }

  static create (user, item) {
    // console.log(`Creating item in ${this.tableName}: ${JSON.stringify(item, null, 2)}`)
    const self = this
    const hashKey = item[this.hashKey]
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'create', hashKey)) {<commit_msg>Remove console log.<commit_after>
import dynogels from 'dynogels'
import {
  NotImplementedException,
  UnauthorizedQueryException
} from '../../utils/exceptions'
import dbClient from '../databaseClient'

dynogels.documentClient(dbClient)

class BaseModel {
  constructor (obj) {
    if (!obj || typeof obj !== 'object') {
      return
    }
    const fieldNames = [].concat(Object.keys(this.constructor.schema),
      ['created', 'updated'])
    fieldNames.forEach((fieldName) => {
      // Set properties for each field on the model.
      // Set the value to the value passed in `obj` if one exists.
      // If not passed a value in `obj`, use the defualt value
      // defined in the `fieldDefaults` method, if it exists.
      if (obj[fieldName]) {
        this[fieldName] = obj[fieldName]
      } else if (this.constructor.fieldDefaults[fieldName]) {
        this[fieldName] = this.constructor.fieldDefaults[fieldName]
      }
    })
  }

  /**
   * The name of the model.
   * You are required to override this function on the child class.
   * @return {string} The name of the model.
   */
  static get name () {
    throw new NotImplementedException()
  }

  /**
   * The name of the database table.
   * You are required to override this function on the child class.
   * @return {string} The name of the database table.
   */
  static get tableName () {
    throw new NotImplementedException()
  }

  /**
   * The name of the hashKey for the DynamoDB table.
   * You are required to override this function on the child class.
   * @return {string} The name of the hashKey for the DynamoDB table.
   */
  static get hashKey () {
    throw new NotImplementedException()
  }

  /**
   * The name of the range key (if it exists) for the DynamoDB table.
   * @return {string} The name of the hashKey for the DynamoDB table.
   */
  static get rangeKey () {
    return null
  }

  /**
   * The table schema, used in dynogels.
   * You are required to override this function on the child class.
   * @return {object} The table schema.
   */
  static get schema () {
    throw new NotImplementedException()
  }

  /**
   * Default values for the fields in schema.
   * @return {object} A map of default values
   */
  static get fieldDefaults () {
    return {}
  }

  /**
   * The permissions object, used to check authorization for database
   * operations. By default, no operations are authorized.
   * @return {object} The permissions object, with a key for each
   *   operation name. Each property value is a function that receives
   *   a user object, item hashKey, and item rangeKey, and must return
   *   a boolean for whether the query is authorized.
   */
  static get permissions () {
    return {
      get: (user, hashKeyValue, rangeKeyValue) => false,
      getAll: () => false,
      update: (user, hashKeyValue, rangeKeyValue) => false,
      create: (user, hashKeyValue) => false
    }
  }

  /**
   * Register the model with dynogels. This must be called prior to
   * using any methods that query the database.
   * @return {undefined}
   */
  static register () {
    // console.log(`Registering model ${this.name} to table ${this.tableName}.`)
    const options = {
      hashKey: this.hashKey,
      tableName: this.tableName,

      // Add two timestamps, `created` and `updated`, to
      // the item's fields.
      timestamps: true,
      createdAt: 'created',
      updatedAt: 'updated',

      schema: this.schema
    }
    if (this.rangeKey) {
      options['rangeKey'] = this.rangeKey
    }
    this.dynogelsModel = dynogels.define(this.name, options)
  }

  static get (user, hashKey, rangeKey, options) {
    const self = this
    let keys = [hashKey]
    if (rangeKey) {
      keys.push(rangeKey)
    }
    // console.log(`Getting obj with hashKey ${hashKey} from table ${this.tableName}.`)
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'get', hashKey, rangeKey)) {
        reject(new UnauthorizedQueryException())
        return
      }
      this.dynogelsModel.get(...keys, (err, data) => {
        if (err) {
          console.log(err)
          reject(err)
        } else {
          resolve(self.deserialize(data))
        }
      })
    })
  }

  static getAll (user) {
    // console.log(`Getting all objs in table ${this.tableName}.`)
    const self = this
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'getAll')) {
        reject(new UnauthorizedQueryException())
        return
      }
      this.dynogelsModel.scan().exec((err, data) => {
        if (err) {
          console.log(err)
          reject(err)
        } else {
          resolve(self.deserialize(data.Items))
        }
      })
    })
  }

  static create (user, item) {
    // console.log(`Creating item in ${this.tableName}: ${JSON.stringify(item, null, 2)}`)
    const self = this
    const hashKey = item[this.hashKey]
    return new Promise((resolve, reject) => {
      if (!this.isQueryAuthorized(user, 'create', hashKey)) {
{'fraction_non_alphanumeric': 0.11605550049554014, 'fraction_numerical': 0.00019821605550049553, 'mean_word_length': 2.5283216783216784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '228049', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2943, 'n_words': 973}