@aaroncadillac/crudify-mongo 1.7.0 → 1.8.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/README.md +3 -0
- package/crudify.js +35 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# Crudify Mongo
|
|
2
2
|
|
|
3
3
|
[](https://opensource.org/licenses/MPL-2.0)
|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
4
7
|
|
|
5
8
|
An automatic CRUD generator for [fastify](https://fastify.dev) and MongoDB, using [mongoose](https://mongoosejs.com),a nd adding basic features like pagination, filtering, and sorting.
|
|
6
9
|
|
package/crudify.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
function removeKeys(obj, keysToRemove) {
|
|
2
2
|
if (typeof obj !== 'object' || obj === null) return;
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const keys = Object.keys(obj);
|
|
5
|
+
for (const key of keys) {
|
|
5
6
|
if (keysToRemove.includes(key)) {
|
|
6
7
|
delete obj[key];
|
|
7
8
|
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
@@ -100,9 +101,9 @@ function Crudify (fastify, opts, next) {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
const paginateOptions = JSON.parse(req.query.pagination ||= '{"pagination": false}')
|
|
103
|
-
reply.type('application/json').code(200).send(await opts.Model.paginate(filterObject, paginateOptions))
|
|
104
|
+
return reply.type('application/json').code(200).send(await opts.Model.paginate(filterObject, paginateOptions))
|
|
104
105
|
} catch (err) {
|
|
105
|
-
|
|
106
|
+
return reply.type('application/json').code(500).send({ error: err.message || err })
|
|
106
107
|
}
|
|
107
108
|
})
|
|
108
109
|
|
|
@@ -110,31 +111,44 @@ function Crudify (fastify, opts, next) {
|
|
|
110
111
|
try {
|
|
111
112
|
const response = await opts.Model.findById(req.params.id)
|
|
112
113
|
if (!response) {
|
|
113
|
-
reply.type('application/json').code(404).send({
|
|
114
|
+
return reply.type('application/json').code(404).send({
|
|
114
115
|
status: 404,
|
|
115
116
|
error: 'Not Found',
|
|
116
117
|
message: `ID: ${ req.params.id } not found`
|
|
117
118
|
})
|
|
118
119
|
} else {
|
|
119
|
-
reply.type('application/json').code(200).send(response)
|
|
120
|
+
return reply.type('application/json').code(200).send(response)
|
|
120
121
|
}
|
|
121
122
|
} catch (err) {
|
|
122
|
-
|
|
123
|
+
return reply.type('application/json').code(500).send({ error: err.message || err })
|
|
123
124
|
}
|
|
124
125
|
})
|
|
125
126
|
|
|
126
127
|
fastify.post(`${opts.url}`, optsPost, async (req, reply) => {
|
|
127
|
-
|
|
128
|
+
try {
|
|
129
|
+
if (Array.isArray(req.body)) {
|
|
130
|
+
await Promise.all(req.body.map(item => opts.Model.validate(item)))
|
|
131
|
+
} else if (typeof req.body === 'object' && req.body !== null) {
|
|
128
132
|
await opts.Model.validate(req.body)
|
|
129
|
-
}
|
|
130
|
-
reply.type('application/json').code(400).send(
|
|
133
|
+
} else {
|
|
134
|
+
return reply.type('application/json').code(400).send({ error: "Invalid request body" })
|
|
131
135
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
+
} catch (err) {
|
|
137
|
+
return reply.type('application/json').code(400).send({ error: err.message || err })
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
let response;
|
|
141
|
+
if (Array.isArray(req.body)) {
|
|
142
|
+
await opts.Model.insertMany(req.body)
|
|
143
|
+
response = { message: "All documents created successfully" }
|
|
144
|
+
} else {
|
|
145
|
+
response = await opts.Model.create(req.body)
|
|
136
146
|
}
|
|
137
|
-
|
|
147
|
+
return reply.type('application/json').code(201).send(response)
|
|
148
|
+
} catch (err) {
|
|
149
|
+
return reply.type('application/json').code(500).send({ error: err.message || err })
|
|
150
|
+
}
|
|
151
|
+
})
|
|
138
152
|
|
|
139
153
|
fastify.put(`${opts.url}/:id`, optsGet, async (req, reply) => {
|
|
140
154
|
try {
|
|
@@ -143,13 +157,13 @@ function Crudify (fastify, opts, next) {
|
|
|
143
157
|
runValidators: true
|
|
144
158
|
})
|
|
145
159
|
if (!response) {
|
|
146
|
-
reply.type('application/json').code(404).send({
|
|
160
|
+
return reply.type('application/json').code(404).send({
|
|
147
161
|
status: 404,
|
|
148
162
|
error: 'Not Found',
|
|
149
163
|
message: `ID: ${ req.params.id } not found`
|
|
150
164
|
})
|
|
151
165
|
} else {
|
|
152
|
-
reply.type('application/json').code(200).send(response)
|
|
166
|
+
return reply.type('application/json').code(200).send(response)
|
|
153
167
|
}
|
|
154
168
|
} catch (err) {
|
|
155
169
|
const requiredErrors = []
|
|
@@ -159,13 +173,13 @@ function Crudify (fastify, opts, next) {
|
|
|
159
173
|
}
|
|
160
174
|
}
|
|
161
175
|
if (requiredErrors.length > 0) {
|
|
162
|
-
reply.type('application/json').code(400).send({
|
|
176
|
+
return reply.type('application/json').code(400).send({
|
|
163
177
|
status: 400,
|
|
164
178
|
error: 'Bad Request',
|
|
165
179
|
message: `Fields: [${ requiredErrors.join(', ') }] are required and cannot be empty`
|
|
166
180
|
})
|
|
167
181
|
} else {
|
|
168
|
-
|
|
182
|
+
return reply.type('application/json').code(500).send({ error: err.message || err })
|
|
169
183
|
}
|
|
170
184
|
}
|
|
171
185
|
})
|
|
@@ -174,16 +188,16 @@ function Crudify (fastify, opts, next) {
|
|
|
174
188
|
try {
|
|
175
189
|
const response = await opts.Model.findOneAndDelete({ _id: req.params.id })
|
|
176
190
|
if (!response) {
|
|
177
|
-
reply.type('application/json').code(404).send({
|
|
191
|
+
return reply.type('application/json').code(404).send({
|
|
178
192
|
status: 404,
|
|
179
193
|
error: 'Not Found',
|
|
180
194
|
message: `ID: ${ req.params.id } not found`
|
|
181
195
|
})
|
|
182
196
|
} else {
|
|
183
|
-
reply.type('application/json').code(200).send(response)
|
|
197
|
+
return reply.type('application/json').code(200).send(response)
|
|
184
198
|
}
|
|
185
199
|
} catch (err) {
|
|
186
|
-
|
|
200
|
+
return reply.type('application/json').code(500).send({ error: err.message || err })
|
|
187
201
|
}
|
|
188
202
|
})
|
|
189
203
|
|