Skip to content

Commit bf191cc

Browse files
committed
convert static class into module
1 parent de4a1cc commit bf191cc

File tree

13 files changed

+419
-22
lines changed

13 files changed

+419
-22
lines changed

.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ DB_ADMIN=admin
3636
DB_ADMIN_PWD=changeit
3737

3838
# Redis
39-
REDIS_CACHE_PORT=6379
40-
REDIS_CACHE_PASSWORD=changeit
39+
REDIS_HOST=redis
40+
REDIS_PORT=6379
41+
REDIS_PASSWORD=changeit
4142

4243
# Log
4344
# Example '/home/node/logs'
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import Sample, { SampleModel } from '../model/Sample';
22
import { Types } from 'mongoose';
33

4-
export default class SampleRepo {
5-
public static findById(id: Types.ObjectId): Promise<Sample | null> {
6-
return SampleModel.findOne({ _id: id, status: true }).lean().exec();
7-
}
4+
async function findById(id: Types.ObjectId): Promise<Sample | null> {
5+
return SampleModel.findOne({ _id: id, status: true }).lean().exec();
6+
}
87

9-
public static async create(sample: Sample): Promise<Sample> {
10-
const now = new Date();
11-
sample.createdAt = now;
12-
sample.updatedAt = now;
13-
const created = await SampleModel.create(sample);
14-
return created.toObject();
15-
}
8+
async function create(sample: Sample): Promise<Sample> {
9+
const now = new Date();
10+
sample.createdAt = now;
11+
sample.updatedAt = now;
12+
const created = await SampleModel.create(sample);
13+
return created.toObject();
14+
}
1615

17-
public static update(sample: Sample): Promise<Sample | null> {
18-
sample.updatedAt = new Date();
19-
return SampleModel.findByIdAndUpdate(sample._id, sample, { new: true }).lean().exec();
20-
}
16+
async function update(sample: Sample): Promise<Sample | null> {
17+
sample.updatedAt = new Date();
18+
return SampleModel.findByIdAndUpdate(sample._id, sample, { new: true }).lean().exec();
2119
}
20+
21+
export default {
22+
findById,
23+
create,
24+
update,
25+
};

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ services:
5555
restart: unless-stopped
5656
env_file: .env
5757
ports:
58-
- '$REDIS_CACHE_PORT:6379'
59-
command: redis-server --save 20 1 --loglevel warning --requirepass $REDIS_CACHE_PASSWORD
58+
- '$REDIS_PORT:6379'
59+
command: redis-server --save 20 1 --loglevel warning --requirepass $REDIS_PASSWORD
6060
volumes:
6161
- cache:/data/cache
6262

package-lock.json

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"multer": "^1.4.5-lts.1",
3737
"newrelic": "^9.7.2",
3838
"qs": "^6.11.0",
39+
"redis": "^4.5.1",
3940
"winston": "^3.8.2",
4041
"winston-daily-rotate-file": "^4.7.1"
4142
},

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Logger from './core/Logger';
33
import cors from 'cors';
44
import { corsUrl, environment } from './config';
55
import './database'; // initialize database
6+
import './cache'; // initialize cache
67
import { NotFoundError, ApiError, InternalError, ErrorType } from './core/ApiError';
78
import routesV1 from './routes/v1';
89

src/cache/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { redis } from '../config';
2+
import { createClient } from 'redis';
3+
import Logger from '../core/Logger';
4+
5+
const redisURL = `redis://${redis.password}@${redis.host}:${redis.port}`;
6+
7+
const client = createClient({ url: redisURL });
8+
9+
client.on('connect', () => Logger.info('Cache is connecting'));
10+
client.on('ready', () => Logger.info('Cache is ready'));
11+
client.on('end', () => Logger.info('Cache disconnected'));
12+
client.on('reconnecting', () => Logger.info('Cache is reconnecting'));
13+
client.on('error', (e) => Logger.error(e));
14+
15+
(async () => {
16+
await client.connect();
17+
})();
18+
19+
// If the Node process ends, close the Cache connection
20+
process.on('SIGINT', async () => {
21+
await client.disconnect();
22+
});
23+
24+
export default client;

src/cache/keys.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export enum Key {
2+
BLOGS_LATEST = 'BLOGS_LATEST',
3+
}
4+
5+
export enum DynamicKey {
6+
BLOGS_TAG = 'BLOGS_TAG',
7+
BLOG = 'BLOG',
8+
}
9+
10+
export type DynamicKeyType = `${DynamicKey}_${string}`;
11+
12+
export function getDynamicKey(key: DynamicKey, suffix: string) {
13+
const dynamic: DynamicKeyType = `${key}_${suffix}`;
14+
return dynamic;
15+
}

0 commit comments

Comments
 (0)