Home Reference Source Repository

src/adapter.js

import knex from 'knex';

const VERSION_SEPERATOR = '@';

/**
 * PostgresqlAdapter
 *
 * @class PostgresqlAdapter
 *
 * @param {Object} config The postgresql connection configuration
 */
class PostgresqlAdapter {

  constructor(config) {
    this.config = config;
    this.connections = {};
  }

  /**
   * Get a postgresql connection.
   *
   * @param  {String} projectName
   * @param  {Number} projectVersion
   *
   * @return {Object} The knex.js connection
   */
  getConnection(projectName, projectVersion) {
    if (!this.connections[projectName + VERSION_SEPERATOR + projectVersion]) {
      var options = {
        client: 'postgresql',
        connection: {
          host: this.config.host,
          user: this.config.user,
          password: this.config.password,
          database: projectName + (projectVersion ? VERSION_SEPERATOR + projectVersion : '')
        },
        pool: {
          min: 0,
          max: 1
        }
      };
      this.connections[projectName + VERSION_SEPERATOR + projectVersion] = knex(options);
    }
    return this.connections[projectName + VERSION_SEPERATOR + projectVersion];
  }

}

export default PostgresqlAdapter;