@anthonylzq/simba.js 4.2.0 → 4.4.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.
@@ -0,0 +1,175 @@
1
+ const { platform } = require('os')
2
+ const { promisify } = require('util')
3
+ const exec = promisify(require('child_process').exec)
4
+ const writeFile = require('../../utils/writeFile')
5
+
6
+ /**
7
+ * @param {Object} args
8
+ * @param {String} projectName
9
+ */
10
+ const mongoF = async ({ projectName }) => {
11
+ const createFoldersCommand = `mkdir ${projectName}/src/database/mongo \
12
+ ${projectName}/src/database/mongo/models \
13
+ ${projectName}/src/database/mongo/queries`
14
+
15
+ if (platform() === 'win32')
16
+ await exec(createFoldersCommand.replaceAll('/', '\\'))
17
+ else await exec(createFoldersCommand)
18
+
19
+ const database = {
20
+ index: {
21
+ content: "export * from './mongo'\n",
22
+ file: `${projectName}/src/database/index.ts`
23
+ },
24
+ mongo: {
25
+ index: {
26
+ content: `export * from './models'
27
+ export * from './queries'
28
+ `,
29
+ file: `${projectName}/src/database/mongo/index.ts`
30
+ },
31
+ models: {
32
+ index: {
33
+ content: "export * from './user'\n",
34
+ file: `${projectName}/src/database/mongo/models/index.ts`
35
+ },
36
+ user: {
37
+ content: `import { model, Schema } from 'mongoose'
38
+
39
+ const UserSchema = new Schema<UserDBO>(
40
+ {
41
+ lastName: {
42
+ required: true,
43
+ type: String
44
+ },
45
+ name: {
46
+ required: true,
47
+ type: String
48
+ }
49
+ },
50
+ {
51
+ timestamps: true,
52
+ versionKey: false,
53
+ toObject: {
54
+ transform: (_, ret) => {
55
+ ret.id = ret._id.toString()
56
+ delete ret._id
57
+ }
58
+ }
59
+ }
60
+ )
61
+
62
+ const UserModel = model<UserDBO>('users', UserSchema)
63
+
64
+ export { UserModel }
65
+ `,
66
+ file: `${projectName}/src/database/mongo/models/user.ts`
67
+ }
68
+ },
69
+ queries: {
70
+ index: {
71
+ content: "export * from './user'\n",
72
+ file: `${projectName}/src/database/mongo/queries/index.ts`
73
+ },
74
+ user: {
75
+ content: `import { Document, Types } from 'mongoose'
76
+
77
+ import { UserModel } from '..'
78
+ import { UserDTO } from 'schemas'
79
+
80
+ const userDBOtoDTO = (
81
+ userDBO: Document<unknown, unknown, UserDBO> &
82
+ UserDBO & {
83
+ _id: Types.ObjectId
84
+ }
85
+ ): UserDTO => ({
86
+ ...userDBO.toObject(),
87
+ createdAt: userDBO.createdAt.toISOString(),
88
+ updatedAt: userDBO.updatedAt.toISOString()
89
+ })
90
+
91
+ const store = async (userData: UserDTO): Promise<UserDTO> => {
92
+ const user = new UserModel(userData)
93
+
94
+ await user.save()
95
+
96
+ return userDBOtoDTO(user)
97
+ }
98
+
99
+ const remove = async (
100
+ id: string | null = null
101
+ ): Promise<UserDTO | number | null> => {
102
+ if (id) {
103
+ const removedUser = await UserModel.findByIdAndRemove(id)
104
+
105
+ if (!removedUser) return null
106
+
107
+ return userDBOtoDTO(removedUser)
108
+ }
109
+
110
+ return (await UserModel.deleteMany({})).deletedCount
111
+ }
112
+
113
+ const get = async (
114
+ id: string | null = null
115
+ ): Promise<UserDTO[] | UserDTO | null> => {
116
+ if (id) {
117
+ const user = await UserModel.findById(id)
118
+
119
+ return user ? userDBOtoDTO(user) : null
120
+ }
121
+
122
+ const users = await UserModel.find({})
123
+
124
+ return users.map(u => userDBOtoDTO(u))
125
+ }
126
+
127
+ const update = async (userData: UserDTO): Promise<UserDTO | null> => {
128
+ const { id, ...rest } = userData
129
+ const user = await UserModel.findByIdAndUpdate(id, rest, { new: true })
130
+
131
+ return user ? userDBOtoDTO(user) : null
132
+ }
133
+
134
+ export { store, remove, get, update }
135
+ `,
136
+ file: `${projectName}/src/database/mongo/queries/user.ts`
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ await writeFile(database.index.file, database.index.content)
143
+ await writeFile(database.mongo.index.file, database.mongo.index.content)
144
+ await writeFile(
145
+ database.mongo.models.index.file,
146
+ database.mongo.models.index.content
147
+ )
148
+ await writeFile(
149
+ database.mongo.models.user.file,
150
+ database.mongo.models.user.content
151
+ )
152
+ await writeFile(
153
+ database.mongo.queries.index.file,
154
+ database.mongo.queries.index.content
155
+ )
156
+ await writeFile(
157
+ database.mongo.queries.user.file,
158
+ database.mongo.queries.user.content
159
+ )
160
+ }
161
+
162
+ /**
163
+ * @param {Object} args
164
+ * @param {Boolean|undefined} args.mongo
165
+ * @param {String} args.projectName
166
+ */
167
+ module.exports = async ({ mongo = true, projectName }) => {
168
+ const createFoldersCommand = `mkdir ${projectName}/src/database`
169
+
170
+ if (platform() === 'win32')
171
+ await exec(createFoldersCommand.replaceAll('/', '\\'))
172
+ else await exec(createFoldersCommand)
173
+
174
+ if (mongo) await mongoF({ projectName })
175
+ }
@@ -0,0 +1,220 @@
1
+ const os = require('os')
2
+ const util = require('util')
3
+ const exec = util.promisify(require('child_process').exec)
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')
10
+ const utils = require('./utils')
11
+ const writeFile = require('../../utils/writeFile')
12
+
13
+ /*
14
+ * Express api:
15
+ * src
16
+ * |- @types:
17
+ * |- |- custom:
18
+ * |- |- |- request: content, file
19
+ * |- |- |- response: content, file
20
+ * |- |- dto:
21
+ * |- |- |- user: content, file
22
+ * |- |- models:
23
+ * |- |- |- user: content, file
24
+ * | |- index: content, file
25
+ * |- database:
26
+ * | |- mongo:
27
+ * | |- |- models:
28
+ * | |- |- |- index: content, file
29
+ * | |- |- |- user: content, file
30
+ * | |- |- queries:
31
+ * | |- |- |- index: content, file
32
+ * | |- |- |- user: content, file
33
+ * | |- |- index: content, file
34
+ * | |- index: content, file
35
+ * |- network:
36
+ * | |- routes:
37
+ * | | |- schemas:
38
+ * | | | |- user: content, file
39
+ * | | | |- index: content, file
40
+ * | | |- home: content, file
41
+ * | | |- index: content, file
42
+ * | | |- user: content, file
43
+ * | |- response: content, file
44
+ * | |- router: content, file
45
+ * | |- server: content, file
46
+ * | |- index: content, file
47
+ * |- services:
48
+ * | |- utils:
49
+ * | | |- messages:
50
+ * | | | |- user: content, file
51
+ * | | | |- index: content, file
52
+ * | | |- index: content, file
53
+ * | |- user: content, file
54
+ * | |- index: content, file
55
+ * |- utils:
56
+ * | |- docs.json: content, file
57
+ * | |- index: content, file
58
+ * |- .env: content, file
59
+ * |- index: content, file
60
+ * index.http: content, file
61
+ */
62
+
63
+ /*
64
+ * Fastify api:
65
+ * src
66
+ * |- @types:
67
+ * |- |- dto:
68
+ * |- |- |- user: content, file
69
+ * |- |- models:
70
+ * |- |- |- user: content, file
71
+ * | |- index: content, file
72
+ * |- database:
73
+ * | |- mongo:
74
+ * | |- |- models:
75
+ * | |- |- |- index: content, file
76
+ * | |- |- |- user: content, file
77
+ * | |- |- queries:
78
+ * | |- |- |- index: content, file
79
+ * | |- |- |- user: content, file
80
+ * | |- |- index: content, file
81
+ * | |- index: content, file
82
+ * |- network:
83
+ * | |- routes:
84
+ * | | |- schemas:
85
+ * | | | |- user: content, file
86
+ * | | | |- index: content, file
87
+ * | | |- home: content, file
88
+ * | | |- user: content, file
89
+ * | | |- index: content, file
90
+ * | |- response: content, file
91
+ * | |- router: content, file
92
+ * | |- server: content, file
93
+ * | |- index: content, file
94
+ * |- services:
95
+ * | |- utils:
96
+ * | | |- messages:
97
+ * | | | |- user: content, file
98
+ * | | | |- index: content, file
99
+ * | | |- index: content, file
100
+ * | |- user: content, file
101
+ * | |- index: content, file
102
+ * |- .env: content, file
103
+ * |- index: content, file
104
+ * index.http: content, file
105
+ */
106
+
107
+ /**
108
+ * @param {Object} args
109
+ * @param {String} args.projectName
110
+ * @param {String} args.projectVersion
111
+ * @param {String} args.email
112
+ * @param {Boolean|undefined} args.fastify
113
+ * @param {Boolean|undefined} args.mongo
114
+ */
115
+ module.exports = async ({
116
+ projectName,
117
+ projectVersion,
118
+ email,
119
+ fastify = false,
120
+ mongo = true
121
+ }) => {
122
+ const data = {
123
+ test: {
124
+ index: {
125
+ content: `### Testing store a user
126
+ POST http://localhost:1996/api/users
127
+ Content-Type: application/json
128
+
129
+ {
130
+ "args": {
131
+ "lastName": "Lzq",
132
+ "name": "Anthony"
133
+ }
134
+ }
135
+
136
+ ### Testing getAll users
137
+ GET http://localhost:1996/api/users
138
+
139
+ ### Testing deleteAll users
140
+ DELETE http://localhost:1996/api/users
141
+
142
+ ### Testing getOne user
143
+ GET http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
144
+
145
+ ### Testing update user
146
+ PATCH http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
147
+ Content-Type: application/json
148
+
149
+ {
150
+ "args": {
151
+ "name": "Anthony",
152
+ "lastName": "Luzquiños"
153
+ }
154
+ }
155
+
156
+ ### Testing delete user
157
+ DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
158
+ `,
159
+ file: `${projectName}/index.http`
160
+ }
161
+ },
162
+ '.env': {
163
+ content: `MONGO_URI = ${
164
+ process.env.LOCAL
165
+ ? process.env.MONGO_URI
166
+ : `mongodb://mongo:mongo@mongo:27017/${projectName}`
167
+ }`,
168
+ file: `${projectName}/.env`
169
+ },
170
+ index: {
171
+ content: `import { Server } from './network'
172
+
173
+ Server.start()
174
+ `,
175
+ file: `${projectName}/src/index.ts`
176
+ }
177
+ }
178
+
179
+ const createFoldersCommands = `mkdir ${projectName}/src`
180
+
181
+ if (os.platform() === 'win32')
182
+ await exec(createFoldersCommands.replaceAll('/', '\\'))
183
+ else await exec(createFoldersCommands)
184
+
185
+ // /@types
186
+ types({
187
+ express: !fastify,
188
+ projectName
189
+ })
190
+ // /database
191
+ database({
192
+ mongo,
193
+ projectName
194
+ })
195
+ // /network
196
+ network({
197
+ fastify,
198
+ projectName
199
+ })
200
+ // /schemas
201
+ schemas({ projectName })
202
+ // /services
203
+ services({ projectName })
204
+ // /utils
205
+ utils({
206
+ express: !fastify,
207
+ projectName,
208
+ email,
209
+ projectVersion
210
+ })
211
+
212
+ // /test
213
+ await writeFile(data.test.index.file, data.test.index.content)
214
+
215
+ // .env
216
+ await writeFile(data['.env'].file, data['.env'].content)
217
+
218
+ // index
219
+ await writeFile(data.index.file, data.index.content)
220
+ }