UNPKG

4.13 kBJavaScriptView Raw
1'use strict';
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const Redis = require("ioredis");
13const _ = require("lodash");
14const typescript_ioc_1 = require("typescript-ioc");
15const configuration_1 = require("./configuration");
16let Database = Database_1 = class Database {
17 constructor() {
18 this.client = this.initializeRedis(this.config.database.redis);
19 this.events = this.initializeRedis(this.config.database.redis);
20 }
21 get redisClient() {
22 return this.client;
23 }
24 get redisEvents() {
25 return this.events;
26 }
27 disconnect() {
28 this.redisClient.disconnect();
29 this.redisEvents.disconnect();
30 }
31 async registerGatewayVersion() {
32 try {
33 const packageJson = require('../package.json');
34 await this.redisClient.set(Database_1.GATEWAY_VERSION_KEY, `${packageJson.version}`);
35 }
36 catch (err) {
37 throw new Error('It was not possible to register the Tree Gateway version.');
38 }
39 }
40 async getGatewayVersion() {
41 try {
42 return await this.redisClient.get(Database_1.GATEWAY_VERSION_KEY);
43 }
44 catch (err) {
45 throw (new Error('It was not possible to retrieve the Tree Gateway version.'));
46 }
47 }
48 async startGatewayUpdate() {
49 try {
50 return await this.redisClient.getset(Database_1.GATEWAY_VERSION_KEY, Database_1.UPDATING);
51 }
52 catch (err) {
53 throw (new Error('It was not possible to retrieve the Tree Gateway version.'));
54 }
55 }
56 initializeRedis(config) {
57 let client;
58 config = _.defaults(config, {
59 options: {}
60 });
61 if (config.cluster) {
62 config.cluster.forEach(node => {
63 node.port = _.toSafeInteger(node.port);
64 node.host = node.host;
65 });
66 client = new Redis.Cluster(config.cluster, {
67 redisOptions: config.options,
68 scaleReads: 'all'
69 });
70 }
71 else if (config.sentinel) {
72 const params = _.defaults(config.options, {
73 name: config.sentinel.name,
74 sentinels: config.sentinel.nodes
75 });
76 config.sentinel.nodes.forEach(node => {
77 node.port = _.toSafeInteger(node.port);
78 node.host = node.host;
79 });
80 client = new Redis(params);
81 }
82 else {
83 config.standalone = _.defaults(config.standalone, {
84 host: 'localhost',
85 port: 6379
86 });
87 if (config.standalone.password) {
88 config.options.password = config.standalone.password;
89 }
90 client = new Redis(_.toSafeInteger(config.standalone.port), config.standalone.host, config.options);
91 }
92 return client;
93 }
94};
95Database.GATEWAY_VERSION_KEY = '{config}:treegateway:version';
96Database.UPDATING = `0.0.0`;
97__decorate([
98 typescript_ioc_1.Inject,
99 __metadata("design:type", configuration_1.Configuration)
100], Database.prototype, "config", void 0);
101Database = Database_1 = __decorate([
102 typescript_ioc_1.Singleton,
103 typescript_ioc_1.AutoWired,
104 __metadata("design:paramtypes", [])
105], Database);
106exports.Database = Database;
107var Database_1;
108//# sourceMappingURL=database.js.map
\No newline at end of file