nodejs-quickstart-structure 1.19.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +320 -301
- package/LICENSE +15 -15
- package/README.md +45 -10
- package/bin/index.js +7 -1
- package/lib/generator.js +139 -139
- package/lib/modules/app-setup.js +401 -401
- package/lib/modules/config-files.js +151 -151
- package/lib/modules/database-setup.js +116 -116
- package/lib/modules/project-setup.js +32 -32
- package/lib/prompts.js +100 -100
- package/package.json +79 -78
- package/templates/clean-architecture/js/src/domain/models/User.js +9 -9
- package/templates/clean-architecture/js/src/errors/ApiError.js +14 -14
- package/templates/clean-architecture/js/src/index.js.ejs +55 -55
- package/templates/clean-architecture/js/src/infrastructure/config/env.js.ejs +47 -47
- package/templates/clean-architecture/js/src/infrastructure/log/logger.js +36 -36
- package/templates/clean-architecture/js/src/infrastructure/log/logger.spec.js.ejs +63 -63
- package/templates/clean-architecture/js/src/infrastructure/webserver/middleware/errorMiddleware.js +30 -30
- package/templates/clean-architecture/js/src/infrastructure/webserver/server.js.ejs +89 -89
- package/templates/clean-architecture/js/src/infrastructure/webserver/swagger.js.ejs +6 -6
- package/templates/clean-architecture/js/src/interfaces/graphql/context.js.ejs +13 -13
- package/templates/clean-architecture/js/src/interfaces/graphql/context.spec.js.ejs +31 -31
- package/templates/clean-architecture/js/src/interfaces/graphql/index.js.ejs +5 -5
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/index.js.ejs +6 -6
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/index.js.ejs +6 -6
- package/templates/clean-architecture/js/src/interfaces/routes/api.spec.js.ejs +38 -38
- package/templates/clean-architecture/js/src/usecases/CreateUser.js +14 -14
- package/templates/clean-architecture/js/src/usecases/CreateUser.spec.js.ejs +51 -51
- package/templates/clean-architecture/js/src/usecases/GetAllUsers.js +12 -12
- package/templates/clean-architecture/js/src/usecases/GetAllUsers.spec.js.ejs +61 -61
- package/templates/clean-architecture/js/src/utils/httpCodes.js +9 -9
- package/templates/clean-architecture/ts/src/config/env.ts.ejs +46 -46
- package/templates/clean-architecture/ts/src/config/swagger.ts.ejs +6 -6
- package/templates/clean-architecture/ts/src/domain/user.ts +7 -7
- package/templates/clean-architecture/ts/src/errors/ApiError.ts +15 -15
- package/templates/clean-architecture/ts/src/index.ts.ejs +139 -139
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.spec.ts.ejs +63 -63
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.ts +36 -36
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.spec.ts.ejs +32 -32
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.ts.ejs +17 -17
- package/templates/clean-architecture/ts/src/interfaces/graphql/index.ts.ejs +3 -3
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/index.ts.ejs +4 -4
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/index.ts.ejs +4 -4
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.spec.ts.ejs +40 -40
- package/templates/clean-architecture/ts/src/usecases/createUser.spec.ts.ejs +51 -51
- package/templates/clean-architecture/ts/src/usecases/createUser.ts +13 -13
- package/templates/clean-architecture/ts/src/usecases/getAllUsers.spec.ts.ejs +63 -63
- package/templates/clean-architecture/ts/src/usecases/getAllUsers.ts +10 -10
- package/templates/clean-architecture/ts/src/utils/errorMiddleware.ts.ejs +27 -27
- package/templates/clean-architecture/ts/src/utils/httpCodes.ts +7 -7
- package/templates/common/.cursorrules.ejs +60 -60
- package/templates/common/.dockerignore +12 -12
- package/templates/common/.env.example.ejs +41 -41
- package/templates/common/.gitlab-ci.yml.ejs +86 -86
- package/templates/common/.lintstagedrc +6 -6
- package/templates/common/.prettierrc +7 -7
- package/templates/common/Dockerfile +73 -73
- package/templates/common/Jenkinsfile.ejs +87 -87
- package/templates/common/SECURITY.md +20 -20
- package/templates/common/_github/workflows/ci.yml.ejs +46 -46
- package/templates/common/_github/workflows/security.yml.ejs +36 -36
- package/templates/common/_gitignore +5 -5
- package/templates/common/_husky/pre-commit +4 -4
- package/templates/common/caching/clean/js/CreateUser.js.ejs +29 -29
- package/templates/common/caching/clean/js/GetAllUsers.js.ejs +37 -37
- package/templates/common/caching/clean/ts/createUser.ts.ejs +27 -27
- package/templates/common/caching/clean/ts/getAllUsers.ts.ejs +34 -34
- package/templates/common/caching/js/memoryCache.js.ejs +60 -60
- package/templates/common/caching/js/memoryCache.spec.js.ejs +101 -101
- package/templates/common/caching/js/redisClient.js.ejs +75 -75
- package/templates/common/caching/js/redisClient.spec.js.ejs +147 -147
- package/templates/common/caching/ts/memoryCache.spec.ts.ejs +102 -102
- package/templates/common/caching/ts/redisClient.spec.ts.ejs +157 -157
- package/templates/common/database/js/database.js.ejs +19 -19
- package/templates/common/database/js/database.spec.js.ejs +56 -56
- package/templates/common/database/js/mongoose.js.ejs +33 -33
- package/templates/common/database/js/mongoose.spec.js.ejs +43 -43
- package/templates/common/database/ts/database.spec.ts.ejs +56 -56
- package/templates/common/database/ts/database.ts.ejs +21 -21
- package/templates/common/database/ts/mongoose.spec.ts.ejs +42 -42
- package/templates/common/database/ts/mongoose.ts.ejs +28 -28
- package/templates/common/docker-compose.yml.ejs +159 -159
- package/templates/common/ecosystem.config.js.ejs +40 -40
- package/templates/common/eslint.config.mjs.ejs +77 -77
- package/templates/common/health/js/healthRoute.spec.js.ejs +70 -70
- package/templates/common/health/ts/healthRoute.spec.ts.ejs +76 -76
- package/templates/common/jest.config.js.ejs +32 -32
- package/templates/common/kafka/js/config/kafka.js +9 -9
- package/templates/common/kafka/js/config/kafka.spec.js.ejs +27 -27
- package/templates/common/kafka/js/messaging/baseConsumer.spec.js.ejs +58 -58
- package/templates/common/kafka/js/messaging/userEventSchema.spec.js.ejs +27 -27
- package/templates/common/kafka/js/services/kafkaService.spec.js.ejs +106 -106
- package/templates/common/kafka/ts/config/kafka.spec.ts.ejs +27 -27
- package/templates/common/kafka/ts/config/kafka.ts +7 -7
- package/templates/common/kafka/ts/messaging/baseConsumer.spec.ts.ejs +50 -50
- package/templates/common/kafka/ts/messaging/baseConsumer.ts.ejs +27 -27
- package/templates/common/kafka/ts/services/kafkaService.spec.ts.ejs +81 -81
- package/templates/common/migrate-mongo-config.js.ejs +31 -31
- package/templates/common/migrations/init.js.ejs +23 -23
- package/templates/common/package.json.ejs +119 -118
- package/templates/common/prompts/add-feature.md.ejs +26 -26
- package/templates/common/prompts/project-context.md.ejs +43 -43
- package/templates/common/prompts/troubleshoot.md.ejs +28 -28
- package/templates/common/public/css/style.css +147 -147
- package/templates/common/scripts/run-e2e.js.ejs +63 -63
- package/templates/common/sonar-project.properties.ejs +27 -27
- package/templates/common/src/utils/errorMiddleware.spec.js.ejs +79 -79
- package/templates/common/src/utils/errorMiddleware.spec.ts.ejs +94 -94
- package/templates/common/tsconfig.json +22 -22
- package/templates/common/views/ejs/index.ejs +55 -55
- package/templates/common/views/pug/index.pug +40 -40
- package/templates/mvc/js/src/config/env.js.ejs +46 -46
- package/templates/mvc/js/src/config/swagger.js.ejs +6 -6
- package/templates/mvc/js/src/errors/ApiError.js +14 -14
- package/templates/mvc/js/src/graphql/context.js.ejs +7 -7
- package/templates/mvc/js/src/graphql/context.spec.js.ejs +29 -29
- package/templates/mvc/js/src/graphql/index.js.ejs +5 -5
- package/templates/mvc/js/src/graphql/resolvers/index.js.ejs +6 -6
- package/templates/mvc/js/src/graphql/typeDefs/index.js.ejs +6 -6
- package/templates/mvc/js/src/index.js.ejs +136 -136
- package/templates/mvc/js/src/utils/errorMiddleware.js +29 -29
- package/templates/mvc/js/src/utils/httpCodes.js +9 -9
- package/templates/mvc/js/src/utils/logger.js +40 -40
- package/templates/mvc/js/src/utils/logger.spec.js.ejs +63 -63
- package/templates/mvc/ts/src/config/env.ts.ejs +45 -45
- package/templates/mvc/ts/src/config/swagger.ts.ejs +6 -6
- package/templates/mvc/ts/src/errors/ApiError.ts +15 -15
- package/templates/mvc/ts/src/graphql/context.spec.ts.ejs +30 -30
- package/templates/mvc/ts/src/graphql/context.ts.ejs +12 -12
- package/templates/mvc/ts/src/graphql/index.ts.ejs +3 -3
- package/templates/mvc/ts/src/graphql/resolvers/index.ts.ejs +4 -4
- package/templates/mvc/ts/src/graphql/typeDefs/index.ts.ejs +4 -4
- package/templates/mvc/ts/src/utils/errorMiddleware.ts.ejs +27 -27
- package/templates/mvc/ts/src/utils/httpCodes.ts +7 -7
- package/templates/mvc/ts/src/utils/logger.spec.ts.ejs +63 -63
- package/templates/mvc/ts/src/utils/logger.ts +36 -36
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
jest.mock('node-cache', () => {
|
|
2
|
-
return jest.fn().mockImplementation(() => ({
|
|
3
|
-
get: jest.fn(),
|
|
4
|
-
set: jest.fn(),
|
|
5
|
-
del: jest.fn(),
|
|
6
|
-
}));
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
jest.mock('<%- loggerPath %>', () => ({
|
|
10
|
-
info: jest.fn(),
|
|
11
|
-
error: jest.fn(),
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
describe('Memory Cache Client', () => {
|
|
15
|
-
let memoryCacheService;
|
|
16
|
-
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
jest.clearAllMocks();
|
|
19
|
-
jest.resetModules();
|
|
20
|
-
memoryCacheService = require('<% if (architecture === "MVC") { %>@/config/memoryCache<% } else { %>@/infrastructure/caching/memoryCache<% } %>');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should get data from memory cache', async () => {
|
|
24
|
-
const mockData = { test: 'data' };
|
|
25
|
-
memoryCacheService.cache.get.mockReturnValue(JSON.stringify(mockData));
|
|
26
|
-
|
|
27
|
-
const data = await memoryCacheService.get('test-key');
|
|
28
|
-
expect(data).toEqual(mockData);
|
|
29
|
-
expect(memoryCacheService.cache.get).toHaveBeenCalledWith('test-key');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should return null when key is not found', async () => {
|
|
33
|
-
memoryCacheService.cache.get.mockReturnValue(undefined);
|
|
34
|
-
const data = await memoryCacheService.get('non-existent');
|
|
35
|
-
expect(data).toBeNull();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should return data as is if not a string', async () => {
|
|
39
|
-
const mockData = { test: 'data' };
|
|
40
|
-
memoryCacheService.cache.get.mockReturnValue(mockData);
|
|
41
|
-
const data = await memoryCacheService.get('binary-key');
|
|
42
|
-
expect(data).toEqual(mockData);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should handle errors in get', async () => {
|
|
46
|
-
memoryCacheService.cache.get.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
47
|
-
const data = await memoryCacheService.get('error-key');
|
|
48
|
-
expect(data).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should set data to memory cache without TTL', async () => {
|
|
52
|
-
const mockData = { test: 'data' };
|
|
53
|
-
await memoryCacheService.set('test-key', mockData);
|
|
54
|
-
expect(memoryCacheService.cache.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData));
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should handle errors in set', async () => {
|
|
58
|
-
memoryCacheService.cache.set.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
59
|
-
await memoryCacheService.set('test-key', 'value');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should handle errors in del', async () => {
|
|
63
|
-
memoryCacheService.cache.del.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
64
|
-
await memoryCacheService.del('test-key');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should use getOrSet and call fetcher if not cached', async () => {
|
|
68
|
-
memoryCacheService.get = jest.fn().mockResolvedValue(null);
|
|
69
|
-
memoryCacheService.set = jest.fn();
|
|
70
|
-
const fetcher = jest.fn().mockResolvedValue({ new: 'data' });
|
|
71
|
-
|
|
72
|
-
const result = await memoryCacheService.getOrSet('new-key', fetcher);
|
|
73
|
-
|
|
74
|
-
expect(result).toEqual({ new: 'data' });
|
|
75
|
-
expect(fetcher).toHaveBeenCalled();
|
|
76
|
-
// Result should be cached
|
|
77
|
-
expect(memoryCacheService.set).toHaveBeenCalledWith('new-key', { new: 'data' }, 3600);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('should return cached data in getOrSet', async () => {
|
|
81
|
-
const cachedData = { cached: 'data' };
|
|
82
|
-
memoryCacheService.get = jest.fn().mockResolvedValue(cachedData);
|
|
83
|
-
const fetcher = jest.fn();
|
|
84
|
-
|
|
85
|
-
const result = await memoryCacheService.getOrSet('cached-key', fetcher);
|
|
86
|
-
|
|
87
|
-
expect(result).toEqual(cachedData);
|
|
88
|
-
expect(fetcher).not.toHaveBeenCalled();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should handle falsy data from fetcher in getOrSet', async () => {
|
|
92
|
-
memoryCacheService.get = jest.fn().mockResolvedValue(null);
|
|
93
|
-
memoryCacheService.set = jest.fn();
|
|
94
|
-
const fetcher = jest.fn().mockResolvedValue(null);
|
|
95
|
-
|
|
96
|
-
const result = await memoryCacheService.getOrSet('empty-key', fetcher);
|
|
97
|
-
|
|
98
|
-
expect(result).toBeNull();
|
|
99
|
-
expect(memoryCacheService.set).not.toHaveBeenCalled();
|
|
100
|
-
});
|
|
101
|
-
});
|
|
1
|
+
jest.mock('node-cache', () => {
|
|
2
|
+
return jest.fn().mockImplementation(() => ({
|
|
3
|
+
get: jest.fn(),
|
|
4
|
+
set: jest.fn(),
|
|
5
|
+
del: jest.fn(),
|
|
6
|
+
}));
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
jest.mock('<%- loggerPath %>', () => ({
|
|
10
|
+
info: jest.fn(),
|
|
11
|
+
error: jest.fn(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
describe('Memory Cache Client', () => {
|
|
15
|
+
let memoryCacheService;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
jest.clearAllMocks();
|
|
19
|
+
jest.resetModules();
|
|
20
|
+
memoryCacheService = require('<% if (architecture === "MVC") { %>@/config/memoryCache<% } else { %>@/infrastructure/caching/memoryCache<% } %>');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should get data from memory cache', async () => {
|
|
24
|
+
const mockData = { test: 'data' };
|
|
25
|
+
memoryCacheService.cache.get.mockReturnValue(JSON.stringify(mockData));
|
|
26
|
+
|
|
27
|
+
const data = await memoryCacheService.get('test-key');
|
|
28
|
+
expect(data).toEqual(mockData);
|
|
29
|
+
expect(memoryCacheService.cache.get).toHaveBeenCalledWith('test-key');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return null when key is not found', async () => {
|
|
33
|
+
memoryCacheService.cache.get.mockReturnValue(undefined);
|
|
34
|
+
const data = await memoryCacheService.get('non-existent');
|
|
35
|
+
expect(data).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should return data as is if not a string', async () => {
|
|
39
|
+
const mockData = { test: 'data' };
|
|
40
|
+
memoryCacheService.cache.get.mockReturnValue(mockData);
|
|
41
|
+
const data = await memoryCacheService.get('binary-key');
|
|
42
|
+
expect(data).toEqual(mockData);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should handle errors in get', async () => {
|
|
46
|
+
memoryCacheService.cache.get.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
47
|
+
const data = await memoryCacheService.get('error-key');
|
|
48
|
+
expect(data).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should set data to memory cache without TTL', async () => {
|
|
52
|
+
const mockData = { test: 'data' };
|
|
53
|
+
await memoryCacheService.set('test-key', mockData);
|
|
54
|
+
expect(memoryCacheService.cache.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should handle errors in set', async () => {
|
|
58
|
+
memoryCacheService.cache.set.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
59
|
+
await memoryCacheService.set('test-key', 'value');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should handle errors in del', async () => {
|
|
63
|
+
memoryCacheService.cache.del.mockImplementation(() => { throw new Error('Cache Error'); });
|
|
64
|
+
await memoryCacheService.del('test-key');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should use getOrSet and call fetcher if not cached', async () => {
|
|
68
|
+
memoryCacheService.get = jest.fn().mockResolvedValue(null);
|
|
69
|
+
memoryCacheService.set = jest.fn();
|
|
70
|
+
const fetcher = jest.fn().mockResolvedValue({ new: 'data' });
|
|
71
|
+
|
|
72
|
+
const result = await memoryCacheService.getOrSet('new-key', fetcher);
|
|
73
|
+
|
|
74
|
+
expect(result).toEqual({ new: 'data' });
|
|
75
|
+
expect(fetcher).toHaveBeenCalled();
|
|
76
|
+
// Result should be cached
|
|
77
|
+
expect(memoryCacheService.set).toHaveBeenCalledWith('new-key', { new: 'data' }, 3600);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should return cached data in getOrSet', async () => {
|
|
81
|
+
const cachedData = { cached: 'data' };
|
|
82
|
+
memoryCacheService.get = jest.fn().mockResolvedValue(cachedData);
|
|
83
|
+
const fetcher = jest.fn();
|
|
84
|
+
|
|
85
|
+
const result = await memoryCacheService.getOrSet('cached-key', fetcher);
|
|
86
|
+
|
|
87
|
+
expect(result).toEqual(cachedData);
|
|
88
|
+
expect(fetcher).not.toHaveBeenCalled();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should handle falsy data from fetcher in getOrSet', async () => {
|
|
92
|
+
memoryCacheService.get = jest.fn().mockResolvedValue(null);
|
|
93
|
+
memoryCacheService.set = jest.fn();
|
|
94
|
+
const fetcher = jest.fn().mockResolvedValue(null);
|
|
95
|
+
|
|
96
|
+
const result = await memoryCacheService.getOrSet('empty-key', fetcher);
|
|
97
|
+
|
|
98
|
+
expect(result).toBeNull();
|
|
99
|
+
expect(memoryCacheService.set).not.toHaveBeenCalled();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
const Redis = require('ioredis');
|
|
2
|
-
require('dotenv').config();
|
|
3
|
-
const logger = require('<%- loggerPath %>');
|
|
4
|
-
|
|
5
|
-
class RedisService {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.client = new Redis({
|
|
8
|
-
host: process.env.REDIS_HOST || 'localhost',
|
|
9
|
-
port: Number(process.env.REDIS_PORT) || 6379,
|
|
10
|
-
password: process.env.REDIS_PASSWORD || undefined,
|
|
11
|
-
retryStrategy: (times) => Math.min(times * 50, 2000),
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
this.client.on('connect', () => {
|
|
15
|
-
logger.info('Redis connected');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
this.client.on('error', (err) => {
|
|
19
|
-
logger.error('Redis error:', err);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static getInstance() {
|
|
24
|
-
if (!RedisService.instance) {
|
|
25
|
-
RedisService.instance = new RedisService();
|
|
26
|
-
}
|
|
27
|
-
return RedisService.instance;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async get(key) {
|
|
31
|
-
try {
|
|
32
|
-
const data = await this.client.get(key);
|
|
33
|
-
return data ? JSON.parse(data) : null;
|
|
34
|
-
} catch (error) {
|
|
35
|
-
logger.error(`Redis get error for key ${key}:`, error);
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async set(key, value, ttl) {
|
|
41
|
-
try {
|
|
42
|
-
const data = JSON.stringify(value);
|
|
43
|
-
if (ttl) {
|
|
44
|
-
await this.client.set(key, data, 'EX', ttl);
|
|
45
|
-
} else {
|
|
46
|
-
await this.client.set(key, data);
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
logger.error(`Redis set error for key ${key}:`, error);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async del(key) {
|
|
54
|
-
try {
|
|
55
|
-
await this.client.del(key);
|
|
56
|
-
} catch (error) {
|
|
57
|
-
logger.error(`Redis del error for key ${key}:`, error);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async getOrSet(key, fetcher, ttl = 3600) {
|
|
62
|
-
const cached = await this.get(key);
|
|
63
|
-
if (cached) return cached;
|
|
64
|
-
|
|
65
|
-
const data = await fetcher();
|
|
66
|
-
if (data) await this.set(key, data, ttl);
|
|
67
|
-
return data;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async quit() {
|
|
71
|
-
return await this.client.quit();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
module.exports = RedisService.getInstance();
|
|
1
|
+
const Redis = require('ioredis');
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
const logger = require('<%- loggerPath %>');
|
|
4
|
+
|
|
5
|
+
class RedisService {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.client = new Redis({
|
|
8
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
9
|
+
port: Number(process.env.REDIS_PORT) || 6379,
|
|
10
|
+
password: process.env.REDIS_PASSWORD || undefined,
|
|
11
|
+
retryStrategy: (times) => Math.min(times * 50, 2000),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
this.client.on('connect', () => {
|
|
15
|
+
logger.info('Redis connected');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
this.client.on('error', (err) => {
|
|
19
|
+
logger.error('Redis error:', err);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static getInstance() {
|
|
24
|
+
if (!RedisService.instance) {
|
|
25
|
+
RedisService.instance = new RedisService();
|
|
26
|
+
}
|
|
27
|
+
return RedisService.instance;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async get(key) {
|
|
31
|
+
try {
|
|
32
|
+
const data = await this.client.get(key);
|
|
33
|
+
return data ? JSON.parse(data) : null;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
logger.error(`Redis get error for key ${key}:`, error);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async set(key, value, ttl) {
|
|
41
|
+
try {
|
|
42
|
+
const data = JSON.stringify(value);
|
|
43
|
+
if (ttl) {
|
|
44
|
+
await this.client.set(key, data, 'EX', ttl);
|
|
45
|
+
} else {
|
|
46
|
+
await this.client.set(key, data);
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
logger.error(`Redis set error for key ${key}:`, error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async del(key) {
|
|
54
|
+
try {
|
|
55
|
+
await this.client.del(key);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
logger.error(`Redis del error for key ${key}:`, error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async getOrSet(key, fetcher, ttl = 3600) {
|
|
62
|
+
const cached = await this.get(key);
|
|
63
|
+
if (cached) return cached;
|
|
64
|
+
|
|
65
|
+
const data = await fetcher();
|
|
66
|
+
if (data) await this.set(key, data, ttl);
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async quit() {
|
|
71
|
+
return await this.client.quit();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = RedisService.getInstance();
|
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
jest.mock('ioredis', () => {
|
|
2
|
-
const mRedis = jest.fn(() => ({
|
|
3
|
-
on: jest.fn(),
|
|
4
|
-
get: jest.fn(),
|
|
5
|
-
set: jest.fn(),
|
|
6
|
-
del: jest.fn(),
|
|
7
|
-
quit: jest.fn().mockResolvedValue('OK'),
|
|
8
|
-
}));
|
|
9
|
-
return mRedis;
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
jest.mock('dotenv', () => ({
|
|
13
|
-
config: jest.fn()
|
|
14
|
-
}));
|
|
15
|
-
|
|
16
|
-
const mockLogger = {
|
|
17
|
-
info: jest.fn(),
|
|
18
|
-
error: jest.fn(),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
jest.mock('<%- loggerPath %>', () => mockLogger);
|
|
22
|
-
|
|
23
|
-
describe('Redis Service', () => {
|
|
24
|
-
let RedisService;
|
|
25
|
-
let logger;
|
|
26
|
-
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
jest.clearAllMocks();
|
|
29
|
-
jest.resetModules();
|
|
30
|
-
|
|
31
|
-
// Clean environment
|
|
32
|
-
const envVars = ['REDIS_HOST', 'REDIS_PORT', 'REDIS_PASSWORD'];
|
|
33
|
-
envVars.forEach(v => delete process.env[v]);
|
|
34
|
-
|
|
35
|
-
logger = require('<%- loggerPath %>');
|
|
36
|
-
RedisService = require('<%= redisClientPath %>');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should handle redis events', () => {
|
|
40
|
-
// Find the event handlers
|
|
41
|
-
const handlers = {};
|
|
42
|
-
RedisService.client.on.mock.calls.forEach(([event, handler]) => {
|
|
43
|
-
handlers[event] = handler;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
if (handlers['connect']) {
|
|
47
|
-
handlers['connect']();
|
|
48
|
-
expect(logger.info).toHaveBeenCalledWith('Redis connected');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (handlers['error']) {
|
|
52
|
-
handlers['error'](new Error('Test Error'));
|
|
53
|
-
expect(logger.error).toHaveBeenCalledWith('Redis error:', expect.any(Error));
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should get data from redis', async () => {
|
|
58
|
-
const mockData = { test: 'data' };
|
|
59
|
-
RedisService.client.get.mockResolvedValue(JSON.stringify(mockData));
|
|
60
|
-
|
|
61
|
-
const data = await RedisService.get('test-key');
|
|
62
|
-
expect(data).toEqual(mockData);
|
|
63
|
-
expect(RedisService.client.get).toHaveBeenCalledWith('test-key');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should return null when key not found in redis', async () => {
|
|
67
|
-
RedisService.client.get.mockResolvedValue(null);
|
|
68
|
-
const data = await RedisService.get('non-existent');
|
|
69
|
-
expect(data).toBeNull();
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should handle errors in get', async () => {
|
|
73
|
-
RedisService.client.get.mockRejectedValue(new Error('Redis Error'));
|
|
74
|
-
const result = await RedisService.get('test-key');
|
|
75
|
-
expect(result).toBeNull();
|
|
76
|
-
expect(logger.error).toHaveBeenCalled();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('should set data to redis without TTL', async () => {
|
|
80
|
-
const mockData = { test: 'data' };
|
|
81
|
-
await RedisService.set('test-key', mockData);
|
|
82
|
-
expect(RedisService.client.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData));
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('should set data to redis with TTL', async () => {
|
|
86
|
-
const mockData = { test: 'data' };
|
|
87
|
-
await RedisService.set('test-key', mockData, 3600);
|
|
88
|
-
expect(RedisService.client.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData), 'EX', 3600);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should handle errors in set', async () => {
|
|
92
|
-
RedisService.client.set.mockRejectedValue(new Error('Redis Error'));
|
|
93
|
-
await RedisService.set('test-key', 'value');
|
|
94
|
-
expect(logger.error).toHaveBeenCalled();
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('should delete data from redis', async () => {
|
|
98
|
-
await RedisService.del('test-key');
|
|
99
|
-
expect(RedisService.client.del).toHaveBeenCalledWith('test-key');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should handle errors in del', async () => {
|
|
103
|
-
RedisService.client.del.mockRejectedValue(new Error('Redis Error'));
|
|
104
|
-
await RedisService.del('test-key');
|
|
105
|
-
expect(logger.error).toHaveBeenCalled();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('should use getOrSet and call fetcher if not cached', async () => {
|
|
109
|
-
RedisService.get = jest.fn().mockResolvedValue(null);
|
|
110
|
-
RedisService.set = jest.fn();
|
|
111
|
-
const fetcher = jest.fn().mockResolvedValue({ new: 'data' });
|
|
112
|
-
|
|
113
|
-
const result = await RedisService.getOrSet('new-key', fetcher);
|
|
114
|
-
|
|
115
|
-
expect(result).toEqual({ new: 'data' });
|
|
116
|
-
expect(fetcher).toHaveBeenCalled();
|
|
117
|
-
expect(RedisService.set).toHaveBeenCalledWith('new-key', { new: 'data' }, 3600);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should use getOrSet and return cached data', async () => {
|
|
121
|
-
const cachedData = { cached: 'data' };
|
|
122
|
-
RedisService.get = jest.fn().mockResolvedValue(cachedData);
|
|
123
|
-
const fetcher = jest.fn();
|
|
124
|
-
|
|
125
|
-
const result = await RedisService.getOrSet('cached-key', fetcher);
|
|
126
|
-
|
|
127
|
-
expect(result).toEqual(cachedData);
|
|
128
|
-
expect(fetcher).not.toHaveBeenCalled();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('should handle falsy data from fetcher in getOrSet', async () => {
|
|
132
|
-
RedisService.get = jest.fn().mockResolvedValue(null);
|
|
133
|
-
RedisService.set = jest.fn();
|
|
134
|
-
const fetcher = jest.fn().mockResolvedValue(null);
|
|
135
|
-
|
|
136
|
-
const result = await RedisService.getOrSet('empty-key', fetcher);
|
|
137
|
-
|
|
138
|
-
expect(result).toBeNull();
|
|
139
|
-
expect(RedisService.set).not.toHaveBeenCalled();
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should quit the redis client', async () => {
|
|
143
|
-
const result = await RedisService.quit();
|
|
144
|
-
expect(result).toBe('OK');
|
|
145
|
-
expect(RedisService.client.quit).toHaveBeenCalled();
|
|
146
|
-
});
|
|
147
|
-
});
|
|
1
|
+
jest.mock('ioredis', () => {
|
|
2
|
+
const mRedis = jest.fn(() => ({
|
|
3
|
+
on: jest.fn(),
|
|
4
|
+
get: jest.fn(),
|
|
5
|
+
set: jest.fn(),
|
|
6
|
+
del: jest.fn(),
|
|
7
|
+
quit: jest.fn().mockResolvedValue('OK'),
|
|
8
|
+
}));
|
|
9
|
+
return mRedis;
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
jest.mock('dotenv', () => ({
|
|
13
|
+
config: jest.fn()
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const mockLogger = {
|
|
17
|
+
info: jest.fn(),
|
|
18
|
+
error: jest.fn(),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
jest.mock('<%- loggerPath %>', () => mockLogger);
|
|
22
|
+
|
|
23
|
+
describe('Redis Service', () => {
|
|
24
|
+
let RedisService;
|
|
25
|
+
let logger;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
jest.clearAllMocks();
|
|
29
|
+
jest.resetModules();
|
|
30
|
+
|
|
31
|
+
// Clean environment
|
|
32
|
+
const envVars = ['REDIS_HOST', 'REDIS_PORT', 'REDIS_PASSWORD'];
|
|
33
|
+
envVars.forEach(v => delete process.env[v]);
|
|
34
|
+
|
|
35
|
+
logger = require('<%- loggerPath %>');
|
|
36
|
+
RedisService = require('<%= redisClientPath %>');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should handle redis events', () => {
|
|
40
|
+
// Find the event handlers
|
|
41
|
+
const handlers = {};
|
|
42
|
+
RedisService.client.on.mock.calls.forEach(([event, handler]) => {
|
|
43
|
+
handlers[event] = handler;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (handlers['connect']) {
|
|
47
|
+
handlers['connect']();
|
|
48
|
+
expect(logger.info).toHaveBeenCalledWith('Redis connected');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (handlers['error']) {
|
|
52
|
+
handlers['error'](new Error('Test Error'));
|
|
53
|
+
expect(logger.error).toHaveBeenCalledWith('Redis error:', expect.any(Error));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should get data from redis', async () => {
|
|
58
|
+
const mockData = { test: 'data' };
|
|
59
|
+
RedisService.client.get.mockResolvedValue(JSON.stringify(mockData));
|
|
60
|
+
|
|
61
|
+
const data = await RedisService.get('test-key');
|
|
62
|
+
expect(data).toEqual(mockData);
|
|
63
|
+
expect(RedisService.client.get).toHaveBeenCalledWith('test-key');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return null when key not found in redis', async () => {
|
|
67
|
+
RedisService.client.get.mockResolvedValue(null);
|
|
68
|
+
const data = await RedisService.get('non-existent');
|
|
69
|
+
expect(data).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should handle errors in get', async () => {
|
|
73
|
+
RedisService.client.get.mockRejectedValue(new Error('Redis Error'));
|
|
74
|
+
const result = await RedisService.get('test-key');
|
|
75
|
+
expect(result).toBeNull();
|
|
76
|
+
expect(logger.error).toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should set data to redis without TTL', async () => {
|
|
80
|
+
const mockData = { test: 'data' };
|
|
81
|
+
await RedisService.set('test-key', mockData);
|
|
82
|
+
expect(RedisService.client.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should set data to redis with TTL', async () => {
|
|
86
|
+
const mockData = { test: 'data' };
|
|
87
|
+
await RedisService.set('test-key', mockData, 3600);
|
|
88
|
+
expect(RedisService.client.set).toHaveBeenCalledWith('test-key', JSON.stringify(mockData), 'EX', 3600);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should handle errors in set', async () => {
|
|
92
|
+
RedisService.client.set.mockRejectedValue(new Error('Redis Error'));
|
|
93
|
+
await RedisService.set('test-key', 'value');
|
|
94
|
+
expect(logger.error).toHaveBeenCalled();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should delete data from redis', async () => {
|
|
98
|
+
await RedisService.del('test-key');
|
|
99
|
+
expect(RedisService.client.del).toHaveBeenCalledWith('test-key');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should handle errors in del', async () => {
|
|
103
|
+
RedisService.client.del.mockRejectedValue(new Error('Redis Error'));
|
|
104
|
+
await RedisService.del('test-key');
|
|
105
|
+
expect(logger.error).toHaveBeenCalled();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should use getOrSet and call fetcher if not cached', async () => {
|
|
109
|
+
RedisService.get = jest.fn().mockResolvedValue(null);
|
|
110
|
+
RedisService.set = jest.fn();
|
|
111
|
+
const fetcher = jest.fn().mockResolvedValue({ new: 'data' });
|
|
112
|
+
|
|
113
|
+
const result = await RedisService.getOrSet('new-key', fetcher);
|
|
114
|
+
|
|
115
|
+
expect(result).toEqual({ new: 'data' });
|
|
116
|
+
expect(fetcher).toHaveBeenCalled();
|
|
117
|
+
expect(RedisService.set).toHaveBeenCalledWith('new-key', { new: 'data' }, 3600);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should use getOrSet and return cached data', async () => {
|
|
121
|
+
const cachedData = { cached: 'data' };
|
|
122
|
+
RedisService.get = jest.fn().mockResolvedValue(cachedData);
|
|
123
|
+
const fetcher = jest.fn();
|
|
124
|
+
|
|
125
|
+
const result = await RedisService.getOrSet('cached-key', fetcher);
|
|
126
|
+
|
|
127
|
+
expect(result).toEqual(cachedData);
|
|
128
|
+
expect(fetcher).not.toHaveBeenCalled();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should handle falsy data from fetcher in getOrSet', async () => {
|
|
132
|
+
RedisService.get = jest.fn().mockResolvedValue(null);
|
|
133
|
+
RedisService.set = jest.fn();
|
|
134
|
+
const fetcher = jest.fn().mockResolvedValue(null);
|
|
135
|
+
|
|
136
|
+
const result = await RedisService.getOrSet('empty-key', fetcher);
|
|
137
|
+
|
|
138
|
+
expect(result).toBeNull();
|
|
139
|
+
expect(RedisService.set).not.toHaveBeenCalled();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should quit the redis client', async () => {
|
|
143
|
+
const result = await RedisService.quit();
|
|
144
|
+
expect(result).toBe('OK');
|
|
145
|
+
expect(RedisService.client.quit).toHaveBeenCalled();
|
|
146
|
+
});
|
|
147
|
+
});
|