@anthonylzq/simba.js 5.2.0 → 6.2.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.
@@ -5,9 +5,10 @@ const writeFile = require('../../utils/writeFile')
5
5
 
6
6
  /**
7
7
  * @param {Object} args
8
- * @param {String} projectName
8
+ * @param {String} args.projectName
9
+ * @param {Boolean} args.fastify
9
10
  */
10
- const mongoF = async ({ projectName }) => {
11
+ const mongoF = async ({ projectName, fastify }) => {
11
12
  const createFoldersCommand = `mkdir ${projectName}/src/database/mongo \
12
13
  ${projectName}/src/database/mongo/models \
13
14
  ${projectName}/src/database/mongo/queries`
@@ -22,9 +23,126 @@ ${projectName}/src/database/mongo/queries`
22
23
  file: `${projectName}/src/database/index.ts`
23
24
  },
24
25
  mongo: {
26
+ connection: {
27
+ content: fastify
28
+ ? `import { connect, connection } from 'mongoose'
29
+ import { FastifyLoggerInstance } from 'fastify'
30
+
31
+ const ENVIRONMENTS_WITHOUT_RECONNECTION = ['ci', 'local']
32
+ const dbConnection = async (
33
+ logger: FastifyLoggerInstance
34
+ ): Promise<{
35
+ connect: () => Promise<typeof import('mongoose')>
36
+ disconnect: () => Promise<void>
37
+ }> => {
38
+ const connectionConfig = {
39
+ keepAlive: true,
40
+ useNewUrlParser: true,
41
+ useUnifiedTopology: true
42
+ }
43
+
44
+ connection.on('connected', () => {
45
+ logger.info('Mongo connection established.')
46
+ })
47
+ connection.on('reconnected', () => {
48
+ logger.info('Mongo connection reestablished')
49
+ })
50
+ connection.on('disconnected', () => {
51
+ if (
52
+ !ENVIRONMENTS_WITHOUT_RECONNECTION.includes(
53
+ process.env.NODE_ENV as string
54
+ )
55
+ ) {
56
+ logger.info(
57
+ 'Mongo connection disconnected. Trying to reconnected to Mongo...'
58
+ )
59
+ setTimeout(() => {
60
+ connect(process.env.MONGO_URI as string, {
61
+ ...connection,
62
+ connectTimeoutMS: 3000,
63
+ socketTimeoutMS: 3000
64
+ })
65
+ }, 3000)
66
+ }
67
+ })
68
+ connection.on('close', () => {
69
+ logger.info('Mongo connection closed')
70
+ })
71
+ connection.on('error', (e: Error) => {
72
+ logger.info('Mongo connection error:')
73
+ logger.error(e)
74
+ })
75
+
76
+ return {
77
+ connect: () => connect(process.env.MONGO_URI as string, connectionConfig),
78
+ disconnect: () => connection.close()
79
+ }
80
+ }
81
+
82
+ export { dbConnection }
83
+ `
84
+ : `import { connect, connection } from 'mongoose'
85
+ import { HttpLogger } from 'express-pino-logger'
86
+
87
+ const ENVIRONMENTS_WITHOUT_RECONNECTION = ['ci', 'local']
88
+ const dbConnection = async (
89
+ logger: HttpLogger['logger']
90
+ ): Promise<{
91
+ connect: () => Promise<typeof import('mongoose')>
92
+ disconnect: () => Promise<void>
93
+ }> => {
94
+ const connectionConfig = {
95
+ keepAlive: true,
96
+ useNewUrlParser: true,
97
+ useUnifiedTopology: true
98
+ }
99
+
100
+ connection.on('connected', () => {
101
+ logger.info('Mongo connection established.')
102
+ })
103
+ connection.on('reconnected', () => {
104
+ logger.info('Mongo connection reestablished')
105
+ })
106
+ connection.on('disconnected', () => {
107
+ if (
108
+ !ENVIRONMENTS_WITHOUT_RECONNECTION.includes(
109
+ process.env.NODE_ENV as string
110
+ )
111
+ ) {
112
+ logger.info(
113
+ 'Mongo connection disconnected. Trying to reconnected to Mongo...'
114
+ )
115
+ setTimeout(() => {
116
+ connect(process.env.MONGO_URI as string, {
117
+ ...connection,
118
+ connectTimeoutMS: 3000,
119
+ socketTimeoutMS: 3000
120
+ })
121
+ }, 3000)
122
+ }
123
+ })
124
+ connection.on('close', () => {
125
+ logger.info('Mongo connection closed')
126
+ })
127
+ connection.on('error', (e: Error) => {
128
+ logger.info('Mongo connection error:')
129
+ logger.error(e)
130
+ })
131
+
132
+ return {
133
+ connect: () => connect(process.env.MONGO_URI as string, connectionConfig),
134
+ disconnect: () => connection.close()
135
+ }
136
+ }
137
+
138
+ export { dbConnection }
139
+ `,
140
+ file: `${projectName}/src/database/mongo/connection.ts`
141
+ },
25
142
  index: {
26
143
  content: `export * from './models'
27
144
  export * from './queries'
145
+ export * from './connection'
28
146
  `,
29
147
  file: `${projectName}/src/database/mongo/index.ts`
30
148
  },
@@ -141,21 +259,25 @@ export { store, remove, get, update }
141
259
  }
142
260
 
143
261
  await Promise.all([
144
- await writeFile(database.index.file, database.index.content),
145
- await writeFile(database.mongo.index.file, database.mongo.index.content),
146
- await writeFile(
262
+ writeFile(database.index.file, database.index.content),
263
+ writeFile(
264
+ database.mongo.connection.file,
265
+ database.mongo.connection.content
266
+ ),
267
+ writeFile(database.mongo.index.file, database.mongo.index.content),
268
+ writeFile(
147
269
  database.mongo.models.index.file,
148
270
  database.mongo.models.index.content
149
271
  ),
150
- await writeFile(
272
+ writeFile(
151
273
  database.mongo.models.user.file,
152
274
  database.mongo.models.user.content
153
275
  ),
154
- await writeFile(
276
+ writeFile(
155
277
  database.mongo.queries.index.file,
156
278
  database.mongo.queries.index.content
157
279
  ),
158
- await writeFile(
280
+ writeFile(
159
281
  database.mongo.queries.user.file,
160
282
  database.mongo.queries.user.content
161
283
  )
@@ -166,13 +288,14 @@ export { store, remove, get, update }
166
288
  * @param {Object} args
167
289
  * @param {Boolean|undefined} args.mongo
168
290
  * @param {String} args.projectName
291
+ * @param {Boolean} args.fastify
169
292
  */
170
- module.exports = async ({ mongo = true, projectName }) => {
293
+ module.exports = async ({ mongo = true, projectName, fastify }) => {
171
294
  const createFoldersCommand = `mkdir ${projectName}/src/database`
172
295
 
173
296
  if (platform() === 'win32')
174
297
  await exec(createFoldersCommand.replaceAll('/', '\\'))
175
298
  else await exec(createFoldersCommand)
176
299
 
177
- if (mongo) await mongoF({ projectName })
300
+ if (mongo) await mongoF({ projectName, fastify })
178
301
  }
@@ -9,6 +9,7 @@ const types = require('./types')
9
9
  const network = require('./network')
10
10
  const utils = require('./utils')
11
11
  const writeFile = require('../../utils/writeFile')
12
+ const { ENVIRONMENTS_WITH_MONGO_URI } = require('../../utils/constants')
12
13
 
13
14
  /*
14
15
  * Express api:
@@ -163,7 +164,7 @@ DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
163
164
  },
164
165
  '.env': {
165
166
  content: `MONGO_URI = ${
166
- process.env.LOCAL
167
+ ENVIRONMENTS_WITH_MONGO_URI.includes(process.env.NODE_ENV)
167
168
  ? process.env.MONGO_URI
168
169
  : `mongodb://mongo:mongo@mongo:27017/${projectName}`
169
170
  }`,
@@ -194,7 +195,8 @@ Server.start()
194
195
  // /database
195
196
  database({
196
197
  mongo,
197
- projectName
198
+ projectName,
199
+ fastify
198
200
  }),
199
201
  // /network
200
202
  network({
@@ -75,33 +75,35 @@ export { applyRoutes }
75
75
  server: {
76
76
  content: `import { Server as HttpServer } from 'http'
77
77
  import express from 'express'
78
- import mongoose from 'mongoose'
79
78
  import cors from 'cors'
80
79
  import pino, { HttpLogger } from 'express-pino-logger'
81
80
 
81
+ import { dbConnection } from 'database'
82
82
  import { applyRoutes } from './router'
83
83
 
84
84
  const PORT = (process.env.PORT as string) || 1996
85
+ const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']
85
86
 
86
87
  class Server {
87
88
  #app: express.Application
88
- #connection: mongoose.Connection | undefined
89
89
  #log: HttpLogger
90
90
  #server: HttpServer | undefined
91
+ #connection: Awaited<ReturnType<typeof dbConnection>> | undefined
91
92
 
92
93
  constructor() {
93
94
  this.#app = express()
94
95
  this.#log = pino({
95
- transport:
96
- process.env.ENVIRONMENT !== 'production'
97
- ? {
98
- target: 'pino-pretty',
99
- options: {
100
- translateTime: 'HH:MM:ss Z',
101
- ignore: 'pid,hostname'
102
- }
96
+ transport: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
97
+ process.env.NODE_ENV as string
98
+ )
99
+ ? {
100
+ target: 'pino-pretty',
101
+ options: {
102
+ translateTime: 'HH:MM:ss Z',
103
+ ignore: 'pid,hostname'
103
104
  }
104
- : undefined
105
+ }
106
+ : undefined
105
107
  })
106
108
  this.#config()
107
109
  }
@@ -130,57 +132,26 @@ class Server {
130
132
  applyRoutes(this.#app)
131
133
  }
132
134
 
133
- async #mongo(): Promise<void> {
134
- this.#connection = mongoose.connection
135
- const connection = {
136
- keepAlive: true,
137
- useNewUrlParser: true,
138
- useUnifiedTopology: true
139
- }
140
- this.#connection.on('connected', () => {
141
- this.#log.logger.info('Mongo connection established.')
142
- })
143
- this.#connection.on('reconnected', () => {
144
- this.#log.logger.info('Mongo connection reestablished')
145
- })
146
- this.#connection.on('disconnected', () => {
147
- this.#log.logger.info('Mongo connection disconnected')
148
- this.#log.logger.info('Trying to reconnected to Mongo...')
149
- setTimeout(() => {
150
- mongoose.connect(process.env.MONGO_URI as string, {
151
- ...connection,
152
- connectTimeoutMS: 3000,
153
- socketTimeoutMS: 3000
154
- })
155
- }, 3000)
156
- })
157
- this.#connection.on('close', () => {
158
- this.#log.logger.info('Mongo connection closed')
159
- })
160
- this.#connection.on('error', (e: Error) => {
161
- this.#log.logger.info('Mongo connection error:')
162
- this.#log.logger.error(e)
163
- })
164
- await mongoose.connect(process.env.MONGO_URI as string, connection)
135
+ async #dbConnection() {
136
+ this.#connection = await dbConnection(this.#log.logger)
165
137
  }
166
138
 
167
- public start(): void {
168
- this.#server = this.#app.listen(PORT, () => {
169
- this.#log.logger.info(\`Server running at port \${PORT}\`)
170
- })
171
-
139
+ public async start(): Promise<void> {
172
140
  try {
173
- this.#mongo()
141
+ await this.#dbConnection()
142
+ await this.#connection?.connect()
143
+ this.#server = this.#app.listen(PORT, () => {
144
+ this.#log.logger.info(\`Server running at port \${PORT}\`)
145
+ })
174
146
  } catch (e) {
175
147
  this.#log.logger.error(e)
176
148
  }
177
149
  }
178
150
 
179
- public stop(): void {
151
+ public async stop(): Promise<void> {
180
152
  try {
181
- if (this.#server) this.#server.close()
182
-
183
- process.exit(0)
153
+ await this.#connection?.disconnect()
154
+ this.#server?.close()
184
155
  } catch (e) {
185
156
  this.#log.logger.error(e)
186
157
  }
@@ -955,7 +926,6 @@ export { applyRoutes }
955
926
  server: {
956
927
  content: `import { createServer, Server as HttpServer } from 'http'
957
928
  import express from 'express'
958
- import mongoose from 'mongoose'
959
929
  import cors from 'cors'
960
930
  import pino, { HttpLogger } from 'express-pino-logger'
961
931
  import { ApolloServer } from 'apollo-server-express'
@@ -965,31 +935,34 @@ import {
965
935
  ApolloServerPluginLandingPageGraphQLPlayground
966
936
  } from 'apollo-server-core'
967
937
 
938
+ import { dbConnection } from 'database'
968
939
  import { mergedSchema as schema } from 'graphQL'
969
940
  import { applyRoutes } from './router'
970
941
 
971
942
  const PORT = (process.env.PORT as string) || 1996
943
+ const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']
972
944
 
973
945
  class Server {
974
946
  #app: express.Application
975
- #connection: mongoose.Connection | undefined
976
- #server: HttpServer
977
947
  #log: HttpLogger
948
+ #server: HttpServer
949
+ #connection: Awaited<ReturnType<typeof dbConnection>> | undefined
978
950
 
979
951
  constructor() {
980
952
  this.#app = express()
981
953
  this.#server = createServer(this.#app)
982
954
  this.#log = pino({
983
- transport:
984
- process.env.ENVIRONMENT !== 'production'
985
- ? {
986
- target: 'pino-pretty',
987
- options: {
988
- translateTime: 'HH:MM:ss Z',
989
- ignore: 'pid,hostname'
990
- }
955
+ transport: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
956
+ process.env.NODE_ENV as string
957
+ )
958
+ ? {
959
+ target: 'pino-pretty',
960
+ options: {
961
+ translateTime: 'HH:MM:ss Z',
962
+ ignore: 'pid,hostname'
991
963
  }
992
- : undefined
964
+ }
965
+ : undefined
993
966
  })
994
967
  this.#config()
995
968
  }
@@ -1017,38 +990,8 @@ class Server {
1017
990
  )
1018
991
  }
1019
992
 
1020
- async #mongo(): Promise<void> {
1021
- this.#connection = mongoose.connection
1022
- const connection = {
1023
- keepAlive: true,
1024
- useNewUrlParser: true,
1025
- useUnifiedTopology: true
1026
- }
1027
- this.#connection.on('connected', () => {
1028
- this.#log.logger.info('Mongo connection established.')
1029
- })
1030
- this.#connection.on('reconnected', () => {
1031
- this.#log.logger.info('Mongo connection reestablished')
1032
- })
1033
- this.#connection.on('disconnected', () => {
1034
- this.#log.logger.info('Mongo connection disconnected')
1035
- this.#log.logger.info('Trying to reconnected to Mongo...')
1036
- setTimeout(() => {
1037
- mongoose.connect(process.env.MONGO_URI as string, {
1038
- ...connection,
1039
- connectTimeoutMS: 3000,
1040
- socketTimeoutMS: 3000
1041
- })
1042
- }, 3000)
1043
- })
1044
- this.#connection.on('close', () => {
1045
- this.#log.logger.info('Mongo connection closed')
1046
- })
1047
- this.#connection.on('error', (e: Error) => {
1048
- this.#log.logger.info('Mongo connection error:')
1049
- this.#log.logger.error(e)
1050
- })
1051
- await mongoose.connect(process.env.MONGO_URI as string, connection)
993
+ async #dbConnection() {
994
+ this.#connection = await dbConnection(this.#log.logger)
1052
995
  }
1053
996
 
1054
997
  public async start(): Promise<void> {
@@ -1072,7 +1015,8 @@ class Server {
1072
1015
  path: '/api'
1073
1016
  })
1074
1017
  applyRoutes(this.#app)
1075
- await this.#mongo()
1018
+ await this.#dbConnection()
1019
+ await this.#connection?.connect()
1076
1020
  this.#server.listen(PORT, () => {
1077
1021
  this.#log.logger.info(\`Server listening at port: \${PORT}\`)
1078
1022
  this.#log.logger.info(
@@ -1084,11 +1028,10 @@ class Server {
1084
1028
  }
1085
1029
  }
1086
1030
 
1087
- public stop(): void {
1031
+ public async stop(): Promise<void> {
1088
1032
  try {
1089
- if (this.#server) this.#server.close()
1090
-
1091
- process.exit(0)
1033
+ await this.#connection?.disconnect()
1034
+ this.#server?.close()
1092
1035
  } catch (e) {
1093
1036
  this.#log.logger.error(e)
1094
1037
  }
@@ -1294,20 +1237,25 @@ export { applyRoutes }
1294
1237
  },
1295
1238
  server: {
1296
1239
  content: `import fastify, { FastifyInstance } from 'fastify'
1297
- import mongoose from 'mongoose'
1298
1240
 
1241
+ import { dbConnection } from 'database'
1299
1242
  import { applyRoutes } from './router'
1300
1243
  import { validatorCompiler } from './utils'
1301
1244
 
1302
1245
  const PORT = process.env.PORT ?? 1996
1246
+ const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']
1303
1247
 
1304
1248
  class Server {
1305
1249
  #app: FastifyInstance
1306
- #connection: mongoose.Connection | undefined
1250
+ #connection: Awaited<ReturnType<typeof dbConnection>> | undefined
1307
1251
 
1308
1252
  constructor() {
1309
1253
  this.#app = fastify({
1310
- logger: { prettyPrint: process.env.NODE_ENV !== 'production' }
1254
+ logger: {
1255
+ prettyPrint: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
1256
+ process.env.NODE_ENV as string
1257
+ )
1258
+ }
1311
1259
  })
1312
1260
  this.#config()
1313
1261
  }
@@ -1328,44 +1276,15 @@ class Server {
1328
1276
  applyRoutes(this.#app)
1329
1277
  }
1330
1278
 
1331
- async #mongo(): Promise<void> {
1332
- this.#connection = mongoose.connection
1333
- const connection = {
1334
- keepAlive: true,
1335
- useNewUrlParser: true,
1336
- useUnifiedTopology: true
1337
- }
1338
- this.#connection.on('connected', () => {
1339
- this.#app.log.info('Mongo connection established.')
1340
- })
1341
- this.#connection.on('reconnected', () => {
1342
- this.#app.log.info('Mongo connection reestablished')
1343
- })
1344
- this.#connection.on('disconnected', () => {
1345
- this.#app.log.info('Mongo connection disconnected')
1346
- this.#app.log.info('Trying to reconnected to Mongo...')
1347
- setTimeout(() => {
1348
- mongoose.connect(process.env.MONGO_URI as string, {
1349
- ...connection,
1350
- connectTimeoutMS: 3000,
1351
- socketTimeoutMS: 3000
1352
- })
1353
- }, 3000)
1354
- })
1355
- this.#connection.on('close', () => {
1356
- this.#app.log.info('Mongo connection closed')
1357
- })
1358
- this.#connection.on('error', (e: Error) => {
1359
- this.#app.log.info('Mongo connection error:')
1360
- this.#app.log.error(e)
1361
- })
1362
- await mongoose.connect(process.env.MONGO_URI as string, connection)
1279
+ async #dbConnection() {
1280
+ this.#connection = await dbConnection(this.#app.log)
1363
1281
  }
1364
1282
 
1365
1283
  public async start(): Promise<void> {
1366
1284
  try {
1285
+ await this.#dbConnection()
1286
+ await this.#connection?.connect()
1367
1287
  await this.#app.listen(PORT)
1368
- this.#mongo()
1369
1288
  } catch (e) {
1370
1289
  console.error(e)
1371
1290
  }
@@ -1373,8 +1292,8 @@ class Server {
1373
1292
 
1374
1293
  public async stop(): Promise<void> {
1375
1294
  try {
1295
+ await this.#connection?.disconnect()
1376
1296
  await this.#app.close()
1377
- process.exit(0)
1378
1297
  } catch (e) {
1379
1298
  console.error(e)
1380
1299
  }
@@ -2266,20 +2185,25 @@ import {
2266
2185
  ApolloServerPluginLandingPageDisabled
2267
2186
  } from 'apollo-server-core'
2268
2187
  import { ApolloServerPlugin } from 'apollo-server-plugin-base'
2269
- import mongoose from 'mongoose'
2188
+ import { dbConnection } from 'database'
2270
2189
 
2271
2190
  import { mergedSchema as schema } from 'graphQL'
2272
2191
  import { applyRoutes } from './router'
2273
2192
 
2274
2193
  const PORT = process.env.PORT ?? 1996
2194
+ const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']
2275
2195
 
2276
2196
  class Server {
2277
2197
  #app: FastifyInstance
2278
- #connection: mongoose.Connection | undefined
2198
+ #connection: Awaited<ReturnType<typeof dbConnection>> | undefined
2279
2199
 
2280
2200
  constructor() {
2281
2201
  this.#app = fastify({
2282
- logger: { prettyPrint: process.env.NODE_ENV !== 'production' }
2202
+ logger: {
2203
+ prettyPrint: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
2204
+ process.env.NODE_ENV as string
2205
+ )
2206
+ }
2283
2207
  })
2284
2208
  this.#config()
2285
2209
  }
@@ -2298,38 +2222,8 @@ class Server {
2298
2222
  applyRoutes(this.#app)
2299
2223
  }
2300
2224
 
2301
- async #mongo(): Promise<void> {
2302
- this.#connection = mongoose.connection
2303
- const connection = {
2304
- keepAlive: true,
2305
- useNewUrlParser: true,
2306
- useUnifiedTopology: true
2307
- }
2308
- this.#connection.on('connected', () => {
2309
- this.#app.log.info('Mongo connection established.')
2310
- })
2311
- this.#connection.on('reconnected', () => {
2312
- this.#app.log.info('Mongo connection reestablished')
2313
- })
2314
- this.#connection.on('disconnected', () => {
2315
- this.#app.log.info('Mongo connection disconnected')
2316
- this.#app.log.info('Trying to reconnected to Mongo...')
2317
- setTimeout(() => {
2318
- mongoose.connect(process.env.MONGO_URI as string, {
2319
- ...connection,
2320
- connectTimeoutMS: 3000,
2321
- socketTimeoutMS: 3000
2322
- })
2323
- }, 3000)
2324
- })
2325
- this.#connection.on('close', () => {
2326
- this.#app.log.info('Mongo connection closed')
2327
- })
2328
- this.#connection.on('error', (e: Error) => {
2329
- this.#app.log.info('Mongo connection error:')
2330
- this.#app.log.error(e)
2331
- })
2332
- await mongoose.connect(process.env.MONGO_URI as string, connection)
2225
+ async #dbConnection() {
2226
+ this.#connection = await dbConnection(this.#app.log)
2333
2227
  }
2334
2228
 
2335
2229
  #fastifyAppClosePlugin(): ApolloServerPlugin {
@@ -2368,7 +2262,8 @@ class Server {
2368
2262
  path: '/api'
2369
2263
  })
2370
2264
  )
2371
- await this.#mongo()
2265
+ await this.#dbConnection()
2266
+ await this.#connection?.connect()
2372
2267
  await this.#app.listen(PORT)
2373
2268
  this.#app.log.info(
2374
2269
  \`GraphQL server listening at: http://localhost:\${PORT}\${server.graphqlPath}\`
@@ -2380,8 +2275,8 @@ class Server {
2380
2275
 
2381
2276
  public async stop(): Promise<void> {
2382
2277
  try {
2278
+ await this.#connection?.disconnect()
2383
2279
  await this.#app.close()
2384
- process.exit(0)
2385
2280
  } catch (e) {
2386
2281
  console.error(e)
2387
2282
  }