micro-generate 1.0.1
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/README.md +104 -0
- package/dist/cli/generators/feature-generator.d.ts +2 -0
- package/dist/cli/generators/feature-generator.js +164 -0
- package/dist/cli/generators/feature-generator.js.map +1 -0
- package/dist/cli/generators/graphql-generator.d.ts +2 -0
- package/dist/cli/generators/graphql-generator.js +117 -0
- package/dist/cli/generators/graphql-generator.js.map +1 -0
- package/dist/cli/generators/init-generator.d.ts +2 -0
- package/dist/cli/generators/init-generator.js +400 -0
- package/dist/cli/generators/init-generator.js.map +1 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +87 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/utils/types.d.ts +10 -0
- package/dist/cli/utils/types.js +2 -0
- package/dist/cli/utils/types.js.map +1 -0
- package/dist/config/database.d.ts +2 -0
- package/dist/config/database.js +27 -0
- package/dist/config/database.js.map +1 -0
- package/dist/config/env.d.ts +12 -0
- package/dist/config/env.js +17 -0
- package/dist/config/env.js.map +1 -0
- package/dist/core/MicroserviceServer.d.ts +24 -0
- package/dist/core/MicroserviceServer.js +85 -0
- package/dist/core/MicroserviceServer.js.map +1 -0
- package/dist/database/redis.d.ts +31 -0
- package/dist/database/redis.js +115 -0
- package/dist/database/redis.js.map +1 -0
- package/dist/features/hello/resolvers.d.ts +5 -0
- package/dist/features/hello/resolvers.js +6 -0
- package/dist/features/hello/resolvers.js.map +1 -0
- package/dist/features/hello/typeDefs.d.ts +1 -0
- package/dist/features/hello/typeDefs.js +6 -0
- package/dist/features/hello/typeDefs.js.map +1 -0
- package/dist/features/index.d.ts +6 -0
- package/dist/features/index.js +5 -0
- package/dist/features/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.js +43 -0
- package/dist/server.js.map +1 -0
- package/dist/utils/logger.d.ts +3 -0
- package/dist/utils/logger.js +21 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cors from 'cors';
|
|
3
|
+
import helmet from 'helmet';
|
|
4
|
+
import { ApolloServer } from '@apollo/server';
|
|
5
|
+
import { expressMiddleware } from '@apollo/server/express4';
|
|
6
|
+
import { connectDatabase, disconnectDatabase } from '../config/database.js';
|
|
7
|
+
import { createRedisConnector } from '../database/redis.js';
|
|
8
|
+
import { logger } from '../utils/logger.js';
|
|
9
|
+
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
|
|
10
|
+
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
|
|
11
|
+
export class MicroserviceServer {
|
|
12
|
+
config;
|
|
13
|
+
app;
|
|
14
|
+
httpServer = null;
|
|
15
|
+
apolloServer;
|
|
16
|
+
redisConnector;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.app = express();
|
|
20
|
+
this.redisConnector = createRedisConnector({
|
|
21
|
+
host: config.redisHost,
|
|
22
|
+
port: config.redisPort,
|
|
23
|
+
password: config.redisPassword,
|
|
24
|
+
db: config.redisDb,
|
|
25
|
+
});
|
|
26
|
+
const plugins = [];
|
|
27
|
+
if (config.playground !== false) { // Default to true if undefined
|
|
28
|
+
plugins.push(ApolloServerPluginLandingPageLocalDefault());
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
plugins.push(ApolloServerPluginLandingPageDisabled());
|
|
32
|
+
}
|
|
33
|
+
this.apolloServer = new ApolloServer({
|
|
34
|
+
typeDefs: config.typeDefs,
|
|
35
|
+
resolvers: config.resolvers,
|
|
36
|
+
plugins: plugins,
|
|
37
|
+
introspection: config.introspection !== false, // Default to true if undefined
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async start() {
|
|
41
|
+
// 1. Database Connections
|
|
42
|
+
// We utilize a slight hack here to reuse the existing singleton-style connectDatabase
|
|
43
|
+
// In a pure library, we might want to refactor connectDatabase to be a method or class instances.
|
|
44
|
+
// For now, we assume the env vars are set or we manually handle connection if possible.
|
|
45
|
+
// However, connectDatabase uses 'env' directly. To make this truly reusable via config,
|
|
46
|
+
// we should update connectDatabase to accept a URI.
|
|
47
|
+
// Let's assume for this step we will make connectDatabase accept the URI.
|
|
48
|
+
await connectDatabase(this.config.mongoUri);
|
|
49
|
+
await this.redisConnector.connect();
|
|
50
|
+
if (this.config.onRedisConnected) {
|
|
51
|
+
this.config.onRedisConnected(this.redisConnector.getClient());
|
|
52
|
+
}
|
|
53
|
+
// 2. Express Config
|
|
54
|
+
this.app.use(helmet({
|
|
55
|
+
crossOriginEmbedderPolicy: false,
|
|
56
|
+
contentSecurityPolicy: false,
|
|
57
|
+
}));
|
|
58
|
+
this.app.use(cors());
|
|
59
|
+
this.app.use(express.json());
|
|
60
|
+
// 3. Apollo Server
|
|
61
|
+
await this.apolloServer.start();
|
|
62
|
+
this.app.use('/graphql', cors(), express.json(), expressMiddleware(this.apolloServer, {
|
|
63
|
+
context: async ({ req }) => ({ token: req.headers.token }),
|
|
64
|
+
}));
|
|
65
|
+
this.app.get('/', (req, res) => {
|
|
66
|
+
res.send('Microservice is running');
|
|
67
|
+
});
|
|
68
|
+
// 4. Start HTTP Server
|
|
69
|
+
this.httpServer = this.app.listen(this.config.port, () => {
|
|
70
|
+
logger.info(`🚀 Server ready at http://localhost:${this.config.port}/graphql`);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async stop() {
|
|
74
|
+
if (this.httpServer) {
|
|
75
|
+
this.httpServer.close();
|
|
76
|
+
}
|
|
77
|
+
await this.apolloServer.stop();
|
|
78
|
+
await this.redisConnector.disconnect();
|
|
79
|
+
await disconnectDatabase();
|
|
80
|
+
}
|
|
81
|
+
getRedisClient() {
|
|
82
|
+
return this.redisConnector.getClient();
|
|
83
|
+
}
|
|
84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AAiB,CAAC,IAAI,CAAC,YAAY,EAAE;YACjC,OAAO,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;SAC7D,CAAC,CACL,CAAC;QAEF,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YAC3B,GAAG,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;QAEH,uBAAuB;QACvB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;YACrD,MAAM,CAAC,IAAI,CAAC,uCAAuC,IAAI,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,CAAC;QACnF,CAAC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,IAAI;QACN,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QAC5B,CAAC;QACD,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QAC/B,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,EAAE,CAAC;QACvC,MAAM,kBAAkB,EAAE,CAAC;IAC/B,CAAC;IAED,cAAc;QACV,OAAO,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE,CAAC;IAC3C,CAAC;CACJ"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Redis, RedisOptions } from 'ioredis';
|
|
2
|
+
/**
|
|
3
|
+
* Class to manage Redis connection
|
|
4
|
+
*/
|
|
5
|
+
export declare class RedisConnector {
|
|
6
|
+
private client;
|
|
7
|
+
private options;
|
|
8
|
+
private url?;
|
|
9
|
+
private isConnected;
|
|
10
|
+
constructor(optionsOrUrl: RedisOptions | string);
|
|
11
|
+
/**
|
|
12
|
+
* Connect to Redis
|
|
13
|
+
*/
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Disconnect from Redis
|
|
17
|
+
*/
|
|
18
|
+
disconnect(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Get the native Redis client (ioredis)
|
|
21
|
+
*/
|
|
22
|
+
getClient(): Redis;
|
|
23
|
+
/**
|
|
24
|
+
* Check connection status
|
|
25
|
+
*/
|
|
26
|
+
get connected(): boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Factory to create Redis connector
|
|
30
|
+
*/
|
|
31
|
+
export declare function createRedisConnector(urlOrOptions: string | RedisOptions): RedisConnector;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Redis } from 'ioredis';
|
|
2
|
+
import { logger } from '../utils/logger.js';
|
|
3
|
+
/**
|
|
4
|
+
* Class to manage Redis connection
|
|
5
|
+
*/
|
|
6
|
+
export class RedisConnector {
|
|
7
|
+
client = null;
|
|
8
|
+
options = {};
|
|
9
|
+
url;
|
|
10
|
+
isConnected = false;
|
|
11
|
+
constructor(optionsOrUrl) {
|
|
12
|
+
if (typeof optionsOrUrl === 'string') {
|
|
13
|
+
this.url = optionsOrUrl;
|
|
14
|
+
this.options = {
|
|
15
|
+
retryStrategy: (times) => Math.min(times * 50, 2000)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
this.options = {
|
|
20
|
+
retryStrategy: (times) => Math.min(times * 50, 2000),
|
|
21
|
+
...optionsOrUrl
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Connect to Redis
|
|
27
|
+
*/
|
|
28
|
+
async connect() {
|
|
29
|
+
if (this.isConnected && this.client) {
|
|
30
|
+
logger.warn('Redis already connected');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
if (this.url) {
|
|
35
|
+
this.client = new Redis(this.url, {
|
|
36
|
+
...this.options,
|
|
37
|
+
lazyConnect: true
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.client = new Redis({
|
|
42
|
+
...this.options,
|
|
43
|
+
lazyConnect: true
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// Register events
|
|
47
|
+
this.client.on('connect', () => {
|
|
48
|
+
this.isConnected = true;
|
|
49
|
+
logger.info('🔴 Redis (ioredis) connected');
|
|
50
|
+
});
|
|
51
|
+
this.client.on('error', (err) => {
|
|
52
|
+
logger.error(err, 'Redis error');
|
|
53
|
+
this.isConnected = false;
|
|
54
|
+
});
|
|
55
|
+
this.client.on('close', () => {
|
|
56
|
+
this.isConnected = false;
|
|
57
|
+
logger.warn('Redis connection closed');
|
|
58
|
+
});
|
|
59
|
+
this.client.on('reconnecting', () => {
|
|
60
|
+
logger.info('Redis reconnecting...');
|
|
61
|
+
});
|
|
62
|
+
// Connect
|
|
63
|
+
await this.client.connect();
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
logger.error(error, 'Failed to connect to Redis');
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Disconnect from Redis
|
|
72
|
+
*/
|
|
73
|
+
async disconnect() {
|
|
74
|
+
if (!this.client) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
await this.client.quit();
|
|
79
|
+
this.isConnected = false;
|
|
80
|
+
this.client = null;
|
|
81
|
+
logger.info('Redis disconnected gracefully');
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
logger.error(error, 'Error disconnecting from Redis');
|
|
85
|
+
// Force disconnect if quit fails
|
|
86
|
+
if (this.client) {
|
|
87
|
+
this.client.disconnect();
|
|
88
|
+
this.client = null;
|
|
89
|
+
this.isConnected = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get the native Redis client (ioredis)
|
|
95
|
+
*/
|
|
96
|
+
getClient() {
|
|
97
|
+
if (!this.client) {
|
|
98
|
+
throw new Error('Redis not connected');
|
|
99
|
+
}
|
|
100
|
+
return this.client;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check connection status
|
|
104
|
+
*/
|
|
105
|
+
get connected() {
|
|
106
|
+
return this.isConnected && this.client?.status === 'ready';
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Factory to create Redis connector
|
|
111
|
+
*/
|
|
112
|
+
export function createRedisConnector(urlOrOptions) {
|
|
113
|
+
return new RedisConnector(urlOrOptions);
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=redis.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis.js","sourceRoot":"","sources":["../../src/database/redis.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAgB,MAAM,SAAS,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,cAAc;IACf,MAAM,GAAiB,IAAI,CAAC;IAC5B,OAAO,GAAiB,EAAE,CAAC;IAC3B,GAAG,CAAU;IACb,WAAW,GAAG,KAAK,CAAC;IAE5B,YAAY,YAAmC;QAC3C,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE,CAAC;YACnC,IAAI,CAAC,GAAG,GAAG,YAAY,CAAC;YACxB,IAAI,CAAC,OAAO,GAAG;gBACX,aAAa,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,EAAE,EAAE,IAAI,CAAC;aAC/D,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,OAAO,GAAG;gBACX,aAAa,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAI,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AAC,WAAW,IAAI,IAAI,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;IAC/D,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,YAAmC;IACpE,OAAO,IAAI,cAAc,CAAC,YAAY,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolvers.js","sourceRoot":"","sources":["../../../src/features/hello/resolvers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG;IACrB,KAAK,EAAE;QACH,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO;KACvB;CACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const typeDefs = "#graphql\n type Query {\n hello: String\n }\n";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeDefs.js","sourceRoot":"","sources":["../../../src/features/hello/typeDefs.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG;;;;CAIvB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/features/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,SAAS,IAAI,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEnE,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,aAAa,CAAC,CAAC;AACxC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,cAAc,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const startServer: () => Promise<void>;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cors from 'cors';
|
|
3
|
+
import helmet from 'helmet';
|
|
4
|
+
import { ApolloServer } from '@apollo/server';
|
|
5
|
+
import { expressMiddleware } from '@apollo/server/express4';
|
|
6
|
+
import { connectDatabase } from './config/database.js';
|
|
7
|
+
import { createRedisConnector } from './database/redis.js';
|
|
8
|
+
import { env } from './config/env.js';
|
|
9
|
+
import { logger } from './utils/logger.js';
|
|
10
|
+
import { typeDefs, resolvers } from './features/index.js';
|
|
11
|
+
export const startServer = async () => {
|
|
12
|
+
// Connect to MongoDB
|
|
13
|
+
await connectDatabase();
|
|
14
|
+
// Connect to Redis
|
|
15
|
+
const redisConnector = createRedisConnector({
|
|
16
|
+
host: env.REDIS_HOST,
|
|
17
|
+
port: parseInt(env.REDIS_PORT),
|
|
18
|
+
password: env.REDIS_PASSWORD || undefined,
|
|
19
|
+
});
|
|
20
|
+
await redisConnector.connect();
|
|
21
|
+
const app = express();
|
|
22
|
+
app.use(helmet({
|
|
23
|
+
crossOriginEmbedderPolicy: false,
|
|
24
|
+
contentSecurityPolicy: false,
|
|
25
|
+
}));
|
|
26
|
+
app.use(cors());
|
|
27
|
+
app.use(express.json());
|
|
28
|
+
const server = new ApolloServer({
|
|
29
|
+
typeDefs,
|
|
30
|
+
resolvers,
|
|
31
|
+
});
|
|
32
|
+
await server.start();
|
|
33
|
+
app.use('/graphql', cors(), express.json(), expressMiddleware(server, {
|
|
34
|
+
context: async ({ req }) => ({ token: req.headers.token }),
|
|
35
|
+
}));
|
|
36
|
+
app.get('/', (req, res) => {
|
|
37
|
+
res.send('Microservices Project API');
|
|
38
|
+
});
|
|
39
|
+
app.listen(env.PORT, () => {
|
|
40
|
+
logger.info(`🚀 Server ready at http://localhost:${env.PORT}/graphql`);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAE1D,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,IAAI,EAAE;IAClC,qBAAqB;IACrB,MAAM,eAAe,EAAE,CAAC;IAExB,mBAAmB;IACnB,MAAM,cAAc,GAAG,oBAAoB,CAAC;QACxC,IAAI,EAAE,GAAG,CAAC,UAAU;QACpB,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC;QAC9B,QAAQ,EAAE,GAAG,CAAC,cAAc,IAAI,SAAS;KAC5C,CAAC,CAAC;IAEH,MAAM,cAAc,CAAC,OAAO,EAAE,CAAC;IAE/B,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IAEtB,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC;QACX,yBAAyB,EAAE,KAAK;QAChC,qBAAqB,EAAE,KAAK;KAC/B,CAAC,CAAC,CAAC;IACJ,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IAChB,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;IAExB,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC;QAC5B,QAAQ;QACR,SAAS;KACZ,CAAC,CAAC;IAEH,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IAErB,GAAG,CAAC,GAAG,CACH,UAAU,EACV,IAAI,EAAoB,EACxB,OAAO,CAAC,IAAI,EAAE,EACd,iBAAiB,CAAC,MAAM,EAAE;QACtB,OAAO,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;KAC7D,CAAC,CACL,CAAC;IAEF,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACtB,GAAG,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE;QACtB,MAAM,CAAC,IAAI,CAAC,uCAAuC,GAAG,CAAC,IAAI,UAAU,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;AACP,CAAC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import pino from 'pino';
|
|
2
|
+
// En desarrollo usamos 'pino-pretty' para leer fácil.
|
|
3
|
+
// En producción, usamos el log JSON estándar para máxima velocidad.
|
|
4
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
5
|
+
export const logger = pino({
|
|
6
|
+
level: process.env.LOG_LEVEL || 'info',
|
|
7
|
+
transport: isDev
|
|
8
|
+
? {
|
|
9
|
+
target: 'pino-pretty',
|
|
10
|
+
options: {
|
|
11
|
+
colorize: true,
|
|
12
|
+
translateTime: 'SYS:standard',
|
|
13
|
+
ignore: 'pid,hostname', // Limpia la salida
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
: undefined,
|
|
17
|
+
});
|
|
18
|
+
export function setLogLevel(level) {
|
|
19
|
+
logger.level = level;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,sDAAsD;AACtD,oEAAoE;AACpE,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC;AAErD,MAAM,CAAC,MAAM,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,MAAM;IACtC,SAAS,EAAE,KAAK;QACZ,CAAC,CAAC;YACE,MAAM,EAAE,aAAa;YACrB,OAAO,EAAE;gBACL,QAAQ,EAAE,IAAI;gBACd,aAAa,EAAE,cAAc;gBAC7B,MAAM,EAAE,cAAc,EAAE,mBAAmB;aAC9C;SACJ;QACD,CAAC,CAAC,SAAS;CAClB,CAAC,CAAC;AAEH,MAAM,UAAU,WAAW,CAAC,KAAa;IACrC,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC;AACzB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "micro-generate",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Microservices project with Node.js, Express, Mongoose, Redis, and GraphQL",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"micro-generate": "dist/cli/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"dev": "tsc -w",
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"lint": "eslint src/**/*.ts",
|
|
20
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
21
|
+
"cli": "tsx src/cli/index.js",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"nodejs",
|
|
26
|
+
"express",
|
|
27
|
+
"mongoose",
|
|
28
|
+
"redis",
|
|
29
|
+
"graphql",
|
|
30
|
+
"microservices"
|
|
31
|
+
],
|
|
32
|
+
"author": "",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@apollo/server": "^4.10.0",
|
|
36
|
+
"chalk": "^5.6.2",
|
|
37
|
+
"commander": "^14.0.2",
|
|
38
|
+
"cors": "^2.8.5",
|
|
39
|
+
"dotenv": "^16.4.1",
|
|
40
|
+
"express": "^4.18.2",
|
|
41
|
+
"fs-extra": "^11.3.3",
|
|
42
|
+
"graphql": "^16.8.1",
|
|
43
|
+
"helmet": "^7.1.0",
|
|
44
|
+
"inquirer": "^13.2.0",
|
|
45
|
+
"ioredis": "^5.3.2",
|
|
46
|
+
"mongoose": "^8.1.1",
|
|
47
|
+
"pino": "^10.2.0",
|
|
48
|
+
"zod": "^3.22.4"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/cors": "^2.8.17",
|
|
52
|
+
"@types/express": "^4.17.21",
|
|
53
|
+
"@types/fs-extra": "^11.0.4",
|
|
54
|
+
"@types/inquirer": "^9.0.9",
|
|
55
|
+
"@types/node": "^20.11.16",
|
|
56
|
+
"nodemon": "^3.0.3",
|
|
57
|
+
"pino-pretty": "^13.1.3",
|
|
58
|
+
"ts-node": "^10.9.2",
|
|
59
|
+
"tsx": "^4.7.0",
|
|
60
|
+
"typescript": "^5.3.3"
|
|
61
|
+
}
|
|
62
|
+
}
|