<commit_before>const { Knorm, KnormError } = require('@knorm/knorm');
const { Pool } = require('pg');
const sqlBricksPostgres = require('sql-bricks-postgres');

class KnormPostgresError extends KnormError {}

class KnormPostgres {
  constructor({
    connection,
    initClient,
    restoreClient,
    name = 'postgres'
  } = {}) {
    this.name = name;
    this.pool = new Pool(
      typeof connection === 'string'
        ? { connectionString: connection }
        : connection
    );

    if (initClient) {
      this._initClient = initClient;
    }

    if (restoreClient) {
      this._restoreClient = restoreClient;
    }
  }

  // TODO: throw custom errors
  async acquireClient() {
    const client = await this.pool.connect();

    if (this._initClient) {
      try {
        await this._initClient(client);
      } catch (e) {
        client.release();
        throw e;
      }
    }

    return client;
  }

  // TODO: throw custom errors
  async releaseClient(client) {
    if (this._restoreClient) {
      try {
        await this._restoreClient(client);
      } catch (e) {
        client.release();
        throw e;
      }
    }

    client.release();
  }

  updateField(knorm) {
    const { Field } = knorm;

    knorm.Field = knorm.Model.Field = class PostgresField extends Field {
      cast(value, modelInstance, options) {
        if (this.type !== 'json' && this.type !== 'jsonb') {
          return super.cast(value, modelInstance, options);
        }

        if (value !== null && options.forSave) {
          if (this.castors && this.castors.forSave) {
            return super.cast(value, modelInstance, options);
          }
          return JSON.stringify(value);
        }
      }

      validateIsString(value, type) {
        super.validateIsString(value, type);
        this.validateMaxLengthIs(value, 255);
      }
    };
  }

  updateTransation(knorm) {
    const { Transaction } = knorm;
    const knormPostgres = this;

    knorm.Transaction = class PostgresTransaction extends Transaction {
      constructor(...args) {
        super(...args);

        const transaction = this;

        this.Field = knorm.Field;
        this.Model = class TransactionModel extends knorm.Model {};
        this.Query = this.Model.Query = class TransactionQuery extends knorm.Query {
          async query(sql) {
            return transaction.query(sql);
          }
        };

        Object.entries(knorm.models).forEach(([name, model]) => {
          this[name] = class extends model {
            static get name() {
              return name;
            }
          };
          this[name].Query = class TransactionQuery extends this[name].Query {
            async query(sql) {
              return transaction.query(sql);
            }
          };
        });
      }

      async query(sql) {
        const result = await this.client.query(
          typeof sql === 'string' ? sql : sql.toParams()
        );
        return result.rows;
      }

      async acquireClient() {
        this.client = await knormPostgres.acquireClient();
      }

      async releaseClient() {
        return knormPostgres.releaseClient(this.client);
      }

      // TODO: throw custom errors
      async execute() {
        let result;
<commit_msg>feat: hash all transaction models by name<commit_after>const { Knorm, KnormError } = require('@knorm/knorm');
const { Pool } = require('pg');
const sqlBricksPostgres = require('sql-bricks-postgres');

class KnormPostgresError extends KnormError {}

class KnormPostgres {
  constructor({
    connection,
    initClient,
    restoreClient,
    name = 'postgres'
  } = {}) {
    this.name = name;
    this.pool = new Pool(
      typeof connection === 'string'
        ? { connectionString: connection }
        : connection
    );

    if (initClient) {
      this._initClient = initClient;
    }

    if (restoreClient) {
      this._restoreClient = restoreClient;
    }
  }

  // TODO: throw custom errors
  async acquireClient() {
    const client = await this.pool.connect();

    if (this._initClient) {
      try {
        await this._initClient(client);
      } catch (e) {
        client.release();
        throw e;
      }
    }

    return client;
  }

  // TODO: throw custom errors
  async releaseClient(client) {
    if (this._restoreClient) {
      try {
        await this._restoreClient(client);
      } catch (e) {
        client.release();
        throw e;
      }
    }

    client.release();
  }

  updateField(knorm) {
    const { Field } = knorm;

    knorm.Field = knorm.Model.Field = class PostgresField extends Field {
      cast(value, modelInstance, options) {
        if (this.type !== 'json' && this.type !== 'jsonb') {
          return super.cast(value, modelInstance, options);
        }

        if (value !== null && options.forSave) {
          if (this.castors && this.castors.forSave) {
            return super.cast(value, modelInstance, options);
          }
          return JSON.stringify(value);
        }
      }

      validateIsString(value, type) {
        super.validateIsString(value, type);
        this.validateMaxLengthIs(value, 255);
      }
    };
  }

  updateTransation(knorm) {
    const { Transaction } = knorm;
    const knormPostgres = this;

    knorm.Transaction = class PostgresTransaction extends Transaction {
      constructor(...args) {
        super(...args);

        this.models = {};
        const transaction = this;

        this.Field = knorm.Field;
        this.Model = class TransactionModel extends knorm.Model {};
        this.Query = this.Model.Query = class TransactionQuery extends knorm.Query {
          async query(sql) {
            return transaction.query(sql);
          }
        };

        Object.entries(knorm.models).forEach(([name, model]) => {
          this[name] = this.models[name] = class extends model {
            static get name() {
              return name;
            }
          };
          this[name].Query = class TransactionQuery extends this[name].Query {
            async query(sql) {
              return transaction.query(sql);
            }
          };
        });
      }

      async query(sql) {
        const result = await this.client.query(
          typeof sql === 'string' ? sql : sql.toParams()
        );
        return result.rows;
      }

      async acquireClient() {
        this.client = await knormPostgres.acquireClient();
      }

      async releaseClient() {
        return knormPostgres.releaseClient(this.client);
      }

      // TODO: throw custom errors
      async execute() {
        let result;

{'fraction_non_alphanumeric': 0.1175045427013931, 'fraction_numerical': 0.0009085402786190188, 'mean_word_length': 1.9618834080717489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1416976', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1835, 'n_words': 418}