{"size": 2218, "ext": "js", "max_stars_repo_path": "src/server/preproc/pipelines/bolts/postgresql-storage.js", "max_stars_repo_name": "Omar-Rashed/x5gon", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/server/preproc/pipelines/bolts/postgresql-storage.js", "max_issues_repo_name": "Omar-Rashed/x5gon", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/server/preproc/pipelines/bolts/postgresql-storage.js", "max_forks_repo_name": "Omar-Rashed/x5gon", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.6176470588, "max_line_length": 115, "alphanum_fraction": 0.5973850316}
/********************************************************************
 * PostgresQL storage process
 * This component receives the verified OER material object and stores
 * it into postgresQL database.
 */

// configurations
const config = require('../../../../config/config');

// internal libraries
const Logger = require('../../../../lib/logging-handler')();
// create a logger instance for logging wikification process
const logger = Logger.createGroupInstance('postgresql-storage', 'preproc');

// postgres library
const pg = require('../../../../lib/postgresQL')(config.pg);

/**
 * Stores the OER material into PostgresQL database.
 */
class PostgresqlStorage {

    constructor(context) {
        this._name = null;
        this._onEmit = null;
    }

    init(name, config, context, callback) {
        this._name = name;
        this._onEmit = config.onEmit;
        // use other fields from config to control your execution
        callback();
    }

    heartbeat() {
        // do something if needed
    }

    shutdown(callback) {
        // prepare for gracefull shutdown, e.g. save state

        // close connection to postgres database
        pg.close();
        // shutdown component
        callback();
    }

    receive(material, stream_id, callback) {
        // log material insertion
        logger.info('start inserting material into oer_material database', { materialUrl: material.materialUrl });
        // takes the material and insert it into the OER material table
        pg.insert(material, 'oer_materials', (error, result) => {
            if (error) {
                // error when parsing response
                logger.error('error [postgresql.insert]: unable to insert material',
                    { error: error.message, materialUrl: material.materialUrl }
                );
            } else {
                // log successful material insertion
                logger.info('material inserted into oer_material database', { materialUrl: material.materialUrl });
            }
            // this is the end of the pre-processing pipeline
            return callback();
        });
    }
}

exports.create = function (context) { return new PostgresqlStorage(context); };