UNPKG

chen-redis

Version:
65 lines 2.46 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 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; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; const core_1 = require('chen/core'); const connection_1 = require('./connection'); exports.RedisConnection = connection_1.RedisConnection; /** * Redis class */ let Redis_1 = class Redis { /** * Initialize hook */ init() { this.config = this.context.config.get('extensions.chen-redis', {}); if (!this.config.default) { throw new core_1.Exception(`config 'extensions.chen-redis.default' is required`); } if (!this.config.connections) { throw new core_1.Exception(`config 'extensions.chen-redis.connections' is required`); } } /** * Create and get connection * @param {string} name * @return {RedisConnection} */ connection(name) { name = name || this.config.default; if (!this.config.connections[name]) { throw new core_1.Exception(`'${name}' doesn't exists in chen redis connections config`); } if (!Redis_1.connections.has(name)) { Redis_1.connections.set(name, Redis_1.createConnection(this.config.connections[name])); } return Redis_1.connections.get(name) || null; } /** * Create new connection * @param {RedisConfig} config * @return {RedisConnection} */ static createConnection(config) { return new connection_1.RedisConnection(config); } }; let Redis = Redis_1; /** * Created connections * @type {Map<string, RedisConnection>} */ Redis.connections = new Map(); Redis = Redis_1 = __decorate([ core_1.injectable(), __metadata('design:paramtypes', function () { return []; }) ], Redis); exports.Redis = Redis; //# sourceMappingURL=index.js.map