@krypton-org/krypton-auth
Version:
Express authentication middleware, using GraphQL and JSON Web Tokens.
62 lines • 2.5 kB
JavaScript
;
/**
* Module defining functions for Mongoose connection and disconnection.
* @module db/db
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose_1 = __importDefault(require("mongoose"));
const config_1 = __importDefault(require("../config"));
function init(cb) {
return __awaiter(this, void 0, void 0, function* () {
// Get Mongoose to use the global promise library
mongoose_1.default.Promise = global.Promise;
// CONNECTION EVENTS
// When successfully connected
mongoose_1.default.connection.on('connected', () => {
config_1.default.serviceReady({ isMongooseReady: true });
if (cb) {
cb();
}
});
// If the connection throws an error
mongoose_1.default.connection.on('error', err => {
config_1.default.dbConnectionFailed(err);
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', () => {
mongoose_1.default.connection.close(() => {
process.exit(0);
});
});
// Create the database connection
try {
yield mongoose_1.default.connect(config_1.default.dbAddress, config_1.default.dbConfig);
}
catch (err) {
config_1.default.dbConnectionFailed(err);
}
});
}
function close(cb) {
return __awaiter(this, void 0, void 0, function* () {
yield mongoose_1.default.connection.close(() => {
if (cb) {
cb();
}
});
});
}
exports.default = { init, close };
//# sourceMappingURL=db.js.map