-
Notifications
You must be signed in to change notification settings - Fork 208
/
redis.datasource.ts
45 lines (39 loc) · 1.24 KB
/
redis.datasource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: loopback4-example-shopping
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {inject, lifeCycleObserver, ValueOrPromise} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = {
name: 'redis',
connector: 'kv-redis',
host: '127.0.0.1',
port: 6379,
password: '',
db: 0,
};
function updateConfig(dsConfig: AnyObject) {
if (process.env.KUBERNETES_SERVICE_HOST) {
dsConfig.host = process.env.SHOPPING_APP_REDIS_MASTER_SERVICE_HOST;
dsConfig.port = +process.env.SHOPPING_APP_REDIS_MASTER_SERVICE_PORT!;
}
return dsConfig;
}
@lifeCycleObserver('datasource')
export class RedisDataSource extends juggler.DataSource {
static readonly dataSourceName = config.name;
static readonly defaultConfig = config;
constructor(
@inject('datasources.config.redis', {optional: true})
dsConfig: AnyObject = config,
) {
super(updateConfig(dsConfig));
}
/**
* Disconnect the datasource when application is stopped. This allows the
* application to be shut down gracefully.
*/
stop(): ValueOrPromise<void> {
return super.disconnect();
}
}