'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _sequelize = require('sequelize');

var _sequelize2 = _interopRequireDefault(_sequelize);

var _config = require('./../../config.json');

var _config2 = _interopRequireDefault(_config);

var _continuationLocalStorage = require('continuation-local-storage');

var _continuationLocalStorage2 = _interopRequireDefault(_continuationLocalStorage);

var _winstonLogs = require('./winstonLogs');

var _winstonLogs2 = _interopRequireDefault(_winstonLogs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
 * *******************************************************************************
 *  * Copyright (c) 2018 Edgeworx, Inc.
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Eclipse Public License v. 2.0 which is available at
 *  * http://www.eclipse.org/legal/epl-2.0
 *  *
 *  * SPDX-License-Identifier: EPL-2.0
 *  *******************************************************************************
 *
 */

var namespace = _continuationLocalStorage2.default.createNamespace('ioauthoring-namespace');

_sequelize2.default.cls = namespace;

//  `const` is a signal that the variable won’t be reassigned.
//  `let`, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm.
//  It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.
//  `var` is now the weakest signal available when you define a variable in JavaScript.
//  The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.
var sequelize = new _sequelize2.default(null, null, null, {
  dialect: 'sqlite',
  storage: __dirname + '/../../../../' + _config2.default.dbPath, //TODO: needs to improve this line of code
  logging: _winstonLogs2.default.debug,
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  }
});

sequelize.sync({
  force: false
}).then(function (err) {}, function (err) {
  console.log('Unable to connect to the database:', err);
});

//  export default is used in the case where a module exports a single value
//  hence import sequelize from 'sequelize' will return sequelize object
exports.default = sequelize;

/*
  FOR EXPORTING MORE THAN ONE VALUE FROM A MODULE
  export const sqrt = Math.sqrt;
  export function square(x) {
      return x * x;
  }
  export function diag(x, y) {
      return sqrt(square(x) + square(y));
  }

  //------ main.js ------
  import { square, diag } from 'lib';
*/