Home Reference Source Repository

src/facade.js

import BBPromise from 'bluebird';
import PostgresqlAdapter from './adapter';
import PostgresqlSerializer from './serializer';
import PostgresqlRepository from './repository';
import PostgresqlMigrator from './migrator';
import PostgresqlDropper from './dropper';
import PostgresqlSyncer from './syncer';
import logger from 'debug';
var debug = logger('postgresql');

/**
 * PostgresqlFacade
 *
 * @class PostgresqlFacade
 */
class PostgresqlFacade {

  /**
   * Create a new PostgresqlFacade instance
   *
   * @param {Object} config
   */
  constructor(config) {
    debug('-> constructor', config);
    this.adapter = new PostgresqlAdapter(config);
    this.serializer = new PostgresqlSerializer();
    this.repository = new PostgresqlRepository(this.serializer, this.adapter);
    this.migrator = new PostgresqlMigrator(this.adapter, config);
    this.dropper = new PostgresqlDropper(config);
    this.syncer = new PostgresqlSyncer(config, this.adapter, this.serializer);
  }

  /**
   * Insert / update item.
   * @param  {Item} item          The item to store
   * @param  {Schema} schema      The schema for the name and primary properties.
   * @param  {Metadata} metadata  An object with a projectName and projectVersion property.
   * @return {Promise}
   */
  putItem(item, schema, metadata) {
    debug('-> putItem');
    return this.repository.putItem(item, schema, metadata);
  }

  /**
   * Remove an item.
   * @param  {String}   itemId      The id of the item
   * @param  {Schema}   schema      The schema for the name and primary properties.
   * @param  {Metadata} metadata    An object with a projectName and projectVersion property.
   * @return {Promise}
   */
  delItem(itemId, schema, metadata) {
    debug('-> delItem');
    return this.repository.delItem(itemId, schema, metadata);
  }

  /**
   * Create a new postgresql database and it's tables.
   *
   * @param  {String}   projectId
   * @param  {Project}  oldProject
   * @param  {Project}  project
   * @param  {Array}    changes
   *
   * @return {Promise}
   */
  migrate(projectId, oldProject, project, changes) {
    debug('-> migrate');
    return this.migrator.migrate(projectId, oldProject, project, changes)
      .then(() => {
        if (project.version > 1) {
          return this.syncer.sync(projectId, oldProject, project, changes);
        }
      });
  }

  /**
   * Drop postgresql database.
   *
   * @param  {String} projectId
   * @param  {Number} projectVersion
   *
   * @return {Promise}
   */
  drop(projectId, projectVersion) {
    debug('-> drop');
    return this.dropper.drop(projectId, projectVersion);
  }

}

export default PostgresqlFacade;