@anthonylzq/simba.js 6.2.2 → 7.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.
@@ -2,108 +2,11 @@ const os = require('os')
2
2
  const util = require('util')
3
3
  const exec = util.promisify(require('child_process').exec)
4
4
 
5
- const database = require('./database')
6
- const schemas = require('./schemas')
7
- const services = require('./services')
8
- const types = require('./types')
9
- const network = require('./network')
5
+ const express = require('./express')
6
+ const fastifyF = require('./fastify')
10
7
  const utils = require('./utils')
11
8
  const writeFile = require('../../utils/writeFile')
12
- const { ENVIRONMENTS_WITH_MONGO_URI } = require('../../utils/constants')
13
-
14
- /*
15
- * Express api:
16
- * src
17
- * |- @types:
18
- * |- |- custom:
19
- * |- |- |- request: content, file
20
- * |- |- |- response: content, file
21
- * |- |- dto:
22
- * |- |- |- user: content, file
23
- * |- |- models:
24
- * |- |- |- user: content, file
25
- * | |- index: content, file
26
- * |- database:
27
- * | |- mongo:
28
- * | |- |- models:
29
- * | |- |- |- index: content, file
30
- * | |- |- |- user: content, file
31
- * | |- |- queries:
32
- * | |- |- |- index: content, file
33
- * | |- |- |- user: content, file
34
- * | |- |- index: content, file
35
- * | |- index: content, file
36
- * |- network:
37
- * | |- routes:
38
- * | | |- schemas:
39
- * | | | |- user: content, file
40
- * | | | |- index: content, file
41
- * | | |- home: content, file
42
- * | | |- index: content, file
43
- * | | |- user: content, file
44
- * | |- response: content, file
45
- * | |- router: content, file
46
- * | |- server: content, file
47
- * | |- index: content, file
48
- * |- services:
49
- * | |- utils:
50
- * | | |- messages:
51
- * | | | |- user: content, file
52
- * | | | |- index: content, file
53
- * | | |- index: content, file
54
- * | |- user: content, file
55
- * | |- index: content, file
56
- * |- utils:
57
- * | |- docs.json: content, file
58
- * | |- index: content, file
59
- * |- .env: content, file
60
- * |- index: content, file
61
- * index.http: content, file
62
- */
63
-
64
- /*
65
- * Fastify api:
66
- * src
67
- * |- @types:
68
- * |- |- dto:
69
- * |- |- |- user: content, file
70
- * |- |- models:
71
- * |- |- |- user: content, file
72
- * | |- index: content, file
73
- * |- database:
74
- * | |- mongo:
75
- * | |- |- models:
76
- * | |- |- |- index: content, file
77
- * | |- |- |- user: content, file
78
- * | |- |- queries:
79
- * | |- |- |- index: content, file
80
- * | |- |- |- user: content, file
81
- * | |- |- index: content, file
82
- * | |- index: content, file
83
- * |- network:
84
- * | |- routes:
85
- * | | |- schemas:
86
- * | | | |- user: content, file
87
- * | | | |- index: content, file
88
- * | | |- home: content, file
89
- * | | |- user: content, file
90
- * | | |- index: content, file
91
- * | |- response: content, file
92
- * | |- router: content, file
93
- * | |- server: content, file
94
- * | |- index: content, file
95
- * |- services:
96
- * | |- utils:
97
- * | | |- messages:
98
- * | | | |- user: content, file
99
- * | | | |- index: content, file
100
- * | | |- index: content, file
101
- * | |- user: content, file
102
- * | |- index: content, file
103
- * |- .env: content, file
104
- * |- index: content, file
105
- * index.http: content, file
106
- */
9
+ const { ENVIRONMENTS_WITH_DB_URI } = require('../../utils/constants')
107
10
 
108
11
  /**
109
12
  * @param {Object} args
@@ -112,7 +15,7 @@ const { ENVIRONMENTS_WITH_MONGO_URI } = require('../../utils/constants')
112
15
  * @param {String} args.email
113
16
  * @param {Boolean} args.fastify
114
17
  * @param {Boolean} args.graphql
115
- * @param {Boolean|undefined} args.mongo
18
+ * @param {import('../../../').Config['database']} args.database
116
19
  */
117
20
  module.exports = async ({
118
21
  projectName,
@@ -120,8 +23,9 @@ module.exports = async ({
120
23
  email,
121
24
  fastify,
122
25
  graphql,
123
- mongo = true
26
+ database
124
27
  }) => {
28
+ const dbIsSQL = database !== 'mongo'
125
29
  const data = {
126
30
  test: {
127
31
  index: {
@@ -163,11 +67,17 @@ DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
163
67
  }
164
68
  },
165
69
  '.env': {
166
- content: `MONGO_URI = ${
167
- ENVIRONMENTS_WITH_MONGO_URI.includes(process.env.NODE_ENV)
168
- ? process.env.MONGO_URI
169
- : `mongodb://mongo:mongo@mongo:27017/${projectName}`
170
- }`,
70
+ content: dbIsSQL
71
+ ? `DB_URI = ${
72
+ ENVIRONMENTS_WITH_DB_URI.includes(process.env.NODE_ENV)
73
+ ? process.env.DB_URI
74
+ : `${database}://${database}:${database}@${database}:27017/${projectName}`
75
+ }`
76
+ : `DB_URI = ${
77
+ ENVIRONMENTS_WITH_DB_URI.includes(process.env.NODE_ENV)
78
+ ? process.env.MONGO_URI
79
+ : `mongodb://mongo:mongo@mongo:27017/${projectName}`
80
+ }`,
171
81
  file: `${projectName}/.env`
172
82
  },
173
83
  index: {
@@ -186,47 +96,33 @@ Server.start()
186
96
  else await exec(createFoldersCommands)
187
97
 
188
98
  const processes = [
189
- // /@types
190
- types({
191
- express: !fastify,
192
- projectName,
193
- graphql
194
- }),
195
- // /database
196
- database({
197
- mongo,
198
- projectName,
199
- fastify
200
- }),
201
- // /network
202
- network({
203
- fastify,
204
- projectName,
205
- graphql
206
- }),
207
- // /schemas
208
- schemas({ projectName, graphql }),
209
- // /utils
210
- utils({
211
- express: !fastify,
212
- projectName,
213
- email,
214
- projectVersion,
215
- graphql
216
- }),
217
99
  // .env
218
100
  writeFile(data['.env'].file, data['.env'].content),
219
101
  // index
220
- writeFile(data.index.file, data.index.content)
102
+ writeFile(data.index.file, data.index.content),
103
+ writeFile(data.test.index.file, data.test.index.content)
221
104
  ]
222
105
 
223
- if (!graphql)
106
+ if (fastify)
107
+ processes.push(fastifyF({ projectName, graphQL: graphql, database }))
108
+ else
224
109
  processes.concat([
225
- // /services
226
- services({ projectName }),
110
+ express({ projectName, graphQL: graphql, database }),
111
+ // /utils
112
+ utils({
113
+ express: !fastify,
114
+ projectName,
115
+ email,
116
+ projectVersion,
117
+ graphql
118
+ })
119
+ ])
120
+
121
+ if (!graphql)
122
+ processes.push(
227
123
  // /test
228
124
  writeFile(data.test.index.file, data.test.index.content)
229
- ])
125
+ )
230
126
 
231
127
  await Promise.all(processes)
232
128
  }
@@ -203,14 +203,14 @@ export { ErrorForUser as EFU, MessageForUser as MFU }
203
203
  }
204
204
 
205
205
  await Promise.all([
206
- await writeFile(services.index.file, services.index.content),
207
- await writeFile(services.user.file, services.user.content),
208
- await writeFile(services.utils.index.file, services.utils.index.content),
209
- await writeFile(
206
+ writeFile(services.index.file, services.index.content),
207
+ writeFile(services.user.file, services.user.content),
208
+ writeFile(services.utils.index.file, services.utils.index.content),
209
+ writeFile(
210
210
  services['utils/messages'].index.file,
211
211
  services['utils/messages'].index.content
212
212
  ),
213
- await writeFile(
213
+ writeFile(
214
214
  services['utils/messages'].user.file,
215
215
  services['utils/messages'].user.content
216
216
  )
@@ -1,10 +1,13 @@
1
1
  const writeFile = require('../utils/writeFile')
2
2
 
3
3
  /**
4
- * @param {String} projectName
4
+ * @param {Object} args
5
+ * @param {String} args.projectName
6
+ * @param {Boolean} args.tests
7
+ * @param {Boolean} args.dbIsSQL
5
8
  * @returns {Promise<void>}
6
9
  */
7
- module.exports = async (projectName, tests) => {
10
+ module.exports = async ({ projectName, tests, dbIsSQL }) => {
8
11
  const data = {
9
12
  eslintContent: `{
10
13
  "env": {
@@ -44,7 +47,9 @@ module.exports = async (projectName, tests) => {
44
47
  {
45
48
  "devDependencies": [
46
49
  "**/*.test.ts",
47
- "webpack.config.js"${tests ? ',\n "jest.config.ts"' : ''}
50
+ "webpack.config.js"${tests ? ',\n "jest.config.ts"' : ''}${
51
+ dbIsSQL ? ',\n "src/database/postgres/config.js"' : ''
52
+ }
48
53
  ],
49
54
  "optionalDependencies": [
50
55
  "**/*.test.ts"
@@ -67,6 +67,7 @@ on: [push]
67
67
 
68
68
  jobs:
69
69
  test:
70
+ environment: Test
70
71
  name: Testing Simba.js API
71
72
  runs-on: ubuntu-latest
72
73
 
@@ -96,7 +97,7 @@ jobs:
96
97
  - name: Run test
97
98
  run: ${managerName === 'yarn' ? 'yarn' : 'npm run'} test:ci
98
99
  env:
99
- MONGO_URI: \${{ secrets.MONGO_URI }}
100
+ DB_URI: \${{ secrets.DB_URI }}
100
101
  NODE_ENV: ci
101
102
  `,
102
103
  file: `${projectName}/.github/workflows/test.yml`
@@ -2,15 +2,18 @@ const { platform } = require('os')
2
2
  const { promisify } = require('util')
3
3
  const exec = promisify(require('child_process').exec)
4
4
  const writeFile = require('../utils/writeFile')
5
- const { ENVIRONMENTS_WITH_MONGO_URI } = require('../utils/constants')
5
+ const { ENVIRONMENTS_WITH_DB_URI } = require('../utils/constants')
6
6
 
7
7
  /**
8
- * @param {String} projectName
9
- * @param {Boolean} graphql
8
+ * @param {Object} args
9
+ * @param {String} args.projectName
10
+ * @param {Boolean} args.graphql
11
+ * @param {Boolean} args.dbIsSQL
10
12
  * @returns {Promise<void>}
11
13
  */
12
- module.exports = async (projectName, graphql) => {
14
+ module.exports = async ({ projectName, graphql, dbIsSQL }) => {
13
15
  const createFoldersCommand = `mkdir ${projectName}/test`
16
+ const dbURI = dbIsSQL ? process.env.DB_URI : process.env.MONGO_URI
14
17
 
15
18
  if (platform() === 'win32')
16
19
  await exec(createFoldersCommand.replaceAll('/', '\\'))
@@ -54,7 +57,7 @@ const ajv = addFormats(new Ajv(), ['email'])
54
57
  .addKeyword('kind')
55
58
  .addKeyword('modifier')
56
59
 
57
- const BASE_URL = 'http://localhost:1996'
60
+ const BASE_URL = 'http://localhost:${process.env.PORT || 1996}'
58
61
  const validator = <T extends TProperties>(
59
62
  schema: TObject<T>,
60
63
  object: unknown
@@ -76,7 +79,7 @@ describe('Simba.js tests', () => {
76
79
  })
77
80
 
78
81
  describe('API endpoints tests', () => {
79
- let userID = ''
82
+ let userID = ${dbIsSQL ? 0 : "''"}
80
83
 
81
84
  describe('API: GET /', () => {
82
85
  let data: BaseResponseDTO
@@ -130,7 +133,7 @@ describe('Simba.js tests', () => {
130
133
 
131
134
  data = result.data
132
135
  status = result.status
133
- userID = result.data.data.user.id ?? ''
136
+ userID = result.data.data.user.id ?? userID
134
137
  expect(status).toBe(200)
135
138
  })
136
139
 
@@ -188,7 +191,7 @@ describe('Simba.js tests', () => {
188
191
 
189
192
  test('Should return 200 as status code', async () => {
190
193
  const result = await axios.post<GetOneUserDTO>(\`\${BASE_URL}/api\`, {
191
- query: \`query getUser($id: ID!) {
194
+ query: \`query getUser($id: ${dbIsSQL ? 'Int' : 'ID'}!) {
192
195
  user: getUser(id: $id) {
193
196
  id
194
197
  name
@@ -268,7 +271,7 @@ describe('Simba.js tests', () => {
268
271
 
269
272
  test('Should return 200 as status code', async () => {
270
273
  const result = await axios.post<DeleteUserDTO>(\`\${BASE_URL}/api\`, {
271
- query: \`mutation deleteUser($id: ID!) {
274
+ query: \`mutation deleteUser($id: ${dbIsSQL ? 'Int' : 'ID'}!) {
272
275
  result: deleteUser(id: $id)
273
276
  }\`,
274
277
  variables: {
@@ -353,7 +356,7 @@ const ajv = new Ajv({
353
356
  nullable: true
354
357
  })
355
358
 
356
- const BASE_URL = 'http://localhost:1996'
359
+ const BASE_URL = 'http://localhost:${process.env.PORT || 1996}'
357
360
  const validator = <T extends TProperties>(
358
361
  schema: TObject<T>,
359
362
  object: unknown
@@ -375,7 +378,7 @@ describe('Simba.js tests', () => {
375
378
  })
376
379
 
377
380
  describe('API endpoints tests', () => {
378
- let userID = ''
381
+ let userID = ${dbIsSQL ? 0 : "''"}
379
382
 
380
383
  describe('API: GET /', () => {
381
384
  let data: BaseResponseDTO
@@ -417,7 +420,7 @@ describe('Simba.js tests', () => {
417
420
 
418
421
  data = result.data
419
422
  status = result.status
420
- userID = result.data.message.id ?? ''
423
+ userID = result.data.message.id ?? userID
421
424
  expect(status).toBe(201)
422
425
  })
423
426
 
@@ -593,9 +596,11 @@ describe('Simba.js tests', () => {
593
596
  file: `${projectName}/test/jestGlobalSetup.ts`
594
597
  },
595
598
  setEnvVars: {
596
- content: `process.env.MONGO_URI =\n${
597
- ENVIRONMENTS_WITH_MONGO_URI.includes(process.env.NODE_ENV)
598
- ? ` '${process.env.MONGO_URI}'\n`
599
+ content: `process.env.DB_URI =\n${
600
+ ENVIRONMENTS_WITH_DB_URI.includes(process.env.NODE_ENV)
601
+ ? ` '${dbURI}'\n`
602
+ : dbIsSQL
603
+ ? ` 'postgres://postgres:postgres@postgres:5432/${projectName}'\n`
599
604
  : ` 'mongodb://mongo:mongo@mongo:27017/${projectName}'\n`
600
605
  }`,
601
606
  file: `${projectName}/test/setEnvVars.ts`
@@ -2,9 +2,10 @@ const writeFile = require('../utils/writeFile')
2
2
 
3
3
  /**
4
4
  * @param {String} projectName
5
+ * @param {Boolean} dbIsSQL
5
6
  * @returns {Promise<void>}
6
7
  */
7
- module.exports = async projectName => {
8
+ module.exports = async (projectName, dbIsSQL) => {
8
9
  const data = {
9
10
  base: {
10
11
  content: `{
@@ -73,7 +74,11 @@ module.exports = async projectName => {
73
74
  // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
74
75
 
75
76
  /* Experimental Options */
76
- // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
77
+ ${
78
+ dbIsSQL
79
+ ? ' "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */'
80
+ : ' // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */'
81
+ }
77
82
  // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
78
83
 
79
84
  /* Advanced Options */
package/lib/src/index.js CHANGED
@@ -35,7 +35,7 @@ const setOptions = process => {
35
35
  }
36
36
 
37
37
  /**
38
- * @param {Config} config configuration to build the project
38
+ * @param {import('../').Config} config configuration to build the project
39
39
  */
40
40
  module.exports = async ({
41
41
  author,
@@ -51,7 +51,8 @@ module.exports = async ({
51
51
  fastify,
52
52
  graphql,
53
53
  tests,
54
- ghat
54
+ ghat,
55
+ database
55
56
  }) => {
56
57
  const process = 4
57
58
  let i = 0
@@ -66,11 +67,37 @@ module.exports = async ({
66
67
  const fastifyProdPackages = `fastify@^3 @fastify/swagger@^6 @fastify/cors@^7 ${
67
68
  graphql ? 'apollo-server-fastify apollo-server-plugin-base' : ''
68
69
  }`
69
- const prodPackages = `${manager} @sinclair/typebox http-errors mongoose pino-pretty ${
70
+ let prodPackages = `${manager} @sinclair/typebox http-errors pino-pretty ${
70
71
  graphql
71
72
  ? '@graphql-tools/schema ajv ajv-formats apollo-server-core graphql'
72
73
  : 'ajv@^6'
73
74
  } ${fastify ? fastifyProdPackages : expressProdPackages}`
75
+
76
+ switch (database) {
77
+ case 'mongo':
78
+ prodPackages += ' mongoose'
79
+ break
80
+ case 'postgres':
81
+ prodPackages +=
82
+ ' sequelize sequelize-typescript sequelize-typescript-migration-lts pg pg-hstore'
83
+ break
84
+ case 'mysql':
85
+ prodPackages +=
86
+ ' sequelize sequelize-typescript sequelize-typescript-migration-lts mysql2'
87
+ break
88
+ case 'mariadb':
89
+ prodPackages +=
90
+ ' sequelize sequelize-typescript sequelize-typescript-migration-lts mariadb'
91
+ break
92
+ case 'sqlite':
93
+ prodPackages +=
94
+ ' sequelize sequelize-typescript sequelize-typescript-migration-lts sqlite3'
95
+ break
96
+ case 'sqlServer':
97
+ prodPackages +=
98
+ ' sequelize sequelize-typescript sequelize-typescript-migration-lts tedious'
99
+ }
100
+
74
101
  const expressDevPackages =
75
102
  '@types/express @types/swagger-ui-express @types/cors @types/express-pino-logger'
76
103
  const fastifyDevPackages = ''
@@ -114,6 +141,7 @@ webpack-node-externals`
114
141
  await exec(`mkdir ${projectName}`)
115
142
  bar.update(++i)
116
143
 
144
+ const dbIsSQL = database !== 'mongo'
117
145
  const functions = [
118
146
  packageJson({
119
147
  author: `${author} <${email}>`,
@@ -127,12 +155,12 @@ webpack-node-externals`
127
155
  readme(projectName, projectDescription),
128
156
  changelog(projectName),
129
157
  gitignore(projectName),
130
- tsconfig(projectName),
158
+ tsconfig(projectName, dbIsSQL),
131
159
  nodemon(projectName),
132
- eslint(projectName, tests),
160
+ eslint({ projectName, tests, dbIsSQL }),
133
161
  webpack(projectName),
134
162
  docker(projectName),
135
- api({ projectName, version, email, fastify, graphql }),
163
+ api({ projectName, version, email, fastify, graphql, database }),
136
164
  exec('git init', { cwd: `./${projectName}` })
137
165
  ]
138
166
 
@@ -149,7 +177,7 @@ webpack-node-externals`
149
177
 
150
178
  if (heroku) functions.push(herokuF(projectName))
151
179
 
152
- if (tests) functions.push(testsF(projectName, graphql))
180
+ if (tests) functions.push(testsF({ projectName, graphql, dbIsSQL }))
153
181
 
154
182
  if (ghat) functions.push(ghatF(projectName, manager))
155
183
 
@@ -164,29 +192,10 @@ webpack-node-externals`
164
192
 
165
193
  bar.stop()
166
194
  } catch (e) {
167
- console.error(e)
195
+ console.error('error', e)
168
196
  }
169
197
  }
170
198
 
171
- /**
172
- * @typedef {Object} Config configuration to initialize a project
173
- * @property {String} author author of the project
174
- * @property {String} email email of the project author
175
- * @property {String} projectName project name
176
- * @property {String} projectDescription project description
177
- * @property {Boolean} heroku true if the project will be deployed in heroku
178
- * @property {'unlicensed'|'mit'|'apache-2.0'|'mpl-2.0'|'lgpl-3.0'|'gpl-3.0'|'agpl-3.0'} license project license
179
- * @property {String} version project initial version
180
- * @property {String} licenseYear year when the license starts in format YYYY
181
- * @property {Boolean} npm true means that the package manager will be npm, otherwise yarn
182
- * @property {'yarn add'|'npm i'} manager command that will be used to install packages
183
- * @property {String} mainFile main file of the project
184
- * @property {Boolean} fastify true means that the project will be using Fastify
185
- * @property {Boolean} graphql true means that the project will be using GraphQL
186
- * @property {Boolean} tests true means that the project will have a basic suit of tests with Jest
187
- * @property {Boolean} ghat true means that the project will have a GitHub Action for the suit of tests
188
- */
189
-
190
199
  /**
191
200
  * @typedef {Object} CliOptions
192
201
  * @property {String} format cli format to show the progress to the user
@@ -1,3 +1,3 @@
1
- const ENVIRONMENTS_WITH_MONGO_URI = ['ci', 'local']
1
+ const ENVIRONMENTS_WITH_DB_URI = ['ci', 'local']
2
2
 
3
- module.exports = { ENVIRONMENTS_WITH_MONGO_URI }
3
+ module.exports = { ENVIRONMENTS_WITH_DB_URI }