crud-api-express 1.0.5 → 1.0.7
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/dist/index.js +2 -0
- package/package.json +1 -1
- package/src/index.ts +13 -11
package/dist/index.js
CHANGED
|
@@ -23,6 +23,8 @@ class CrudController {
|
|
|
23
23
|
this.router.post(path, applyMiddleware(async (req, res) => {
|
|
24
24
|
const method = 'POST';
|
|
25
25
|
try {
|
|
26
|
+
console.log("Request:", req);
|
|
27
|
+
console.log("Request body:", req.body);
|
|
26
28
|
const result = await this.model.create(req.body);
|
|
27
29
|
if (options.relatedModel && options.relatedMethods?.includes('POST')) {
|
|
28
30
|
await options.relatedModel.create({ [options.relatedField]: result._id, ...req.body });
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,10 +6,10 @@ interface CrudOptions<T extends Document> {
|
|
|
6
6
|
onSuccess?: (res: Response, method: string, result: T | T[]) => void;
|
|
7
7
|
onError?: (res: Response, method: string, error: Error) => void;
|
|
8
8
|
methods?: ('POST' | 'GET' | 'PUT' | 'DELETE')[];
|
|
9
|
-
relatedModel?: Model<any>;
|
|
9
|
+
relatedModel?: Model<any>;
|
|
10
10
|
relatedField?: string;
|
|
11
11
|
relatedMethods?: ('POST' | 'GET' | 'PUT' | 'DELETE')[];
|
|
12
|
-
aggregatePipeline?: object[];
|
|
12
|
+
aggregatePipeline?: object[];
|
|
13
13
|
customRoutes?: { method: 'post' | 'get' | 'put' | 'delete', path: string, handler: (req: Request, res: Response) => void }[];
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -50,12 +50,14 @@ class CrudController<T extends Document> {
|
|
|
50
50
|
this.router.post(path, applyMiddleware(async (req, res) => {
|
|
51
51
|
const method = 'POST';
|
|
52
52
|
try {
|
|
53
|
-
|
|
53
|
+
console.log("Request:", req);
|
|
54
|
+
console.log("Request body:", req.body);
|
|
55
|
+
const result: any = await this.model.create(req.body);
|
|
54
56
|
if (options.relatedModel && options.relatedMethods?.includes('POST')) {
|
|
55
57
|
await options.relatedModel.create({ [options.relatedField!]: result._id, ...req.body });
|
|
56
58
|
}
|
|
57
59
|
onSuccess(res, method, result);
|
|
58
|
-
} catch (error:any) {
|
|
60
|
+
} catch (error: any) {
|
|
59
61
|
onError(res, method, error);
|
|
60
62
|
}
|
|
61
63
|
}));
|
|
@@ -95,7 +97,7 @@ class CrudController<T extends Document> {
|
|
|
95
97
|
items = await this.model.find(query).sort(sortOrder).skip(skip).limit(pageSize);
|
|
96
98
|
}
|
|
97
99
|
onSuccess(res, method, items);
|
|
98
|
-
} catch (error:any) {
|
|
100
|
+
} catch (error: any) {
|
|
99
101
|
onError(res, method, error);
|
|
100
102
|
}
|
|
101
103
|
}));
|
|
@@ -129,7 +131,7 @@ class CrudController<T extends Document> {
|
|
|
129
131
|
return res.status(404).send();
|
|
130
132
|
}
|
|
131
133
|
onSuccess(res, method, item);
|
|
132
|
-
} catch (error:any) {
|
|
134
|
+
} catch (error: any) {
|
|
133
135
|
onError(res, method, error);
|
|
134
136
|
}
|
|
135
137
|
}));
|
|
@@ -150,7 +152,7 @@ class CrudController<T extends Document> {
|
|
|
150
152
|
await options.relatedModel.updateMany({ [options.relatedField!]: item._id }, req.body);
|
|
151
153
|
}
|
|
152
154
|
onSuccess(res, method, item);
|
|
153
|
-
} catch (error:any) {
|
|
155
|
+
} catch (error: any) {
|
|
154
156
|
onError(res, method, error);
|
|
155
157
|
}
|
|
156
158
|
}));
|
|
@@ -164,7 +166,7 @@ class CrudController<T extends Document> {
|
|
|
164
166
|
const method = 'DELETE';
|
|
165
167
|
try {
|
|
166
168
|
const query = req.query.filter ? JSON.parse(req.query.filter as string) : {};
|
|
167
|
-
const deleteResult:any = await this.model.deleteMany(query);
|
|
169
|
+
const deleteResult: any = await this.model.deleteMany(query);
|
|
168
170
|
if (deleteResult.deletedCount === 0) {
|
|
169
171
|
return res.status(404).send();
|
|
170
172
|
}
|
|
@@ -193,7 +195,7 @@ class CrudController<T extends Document> {
|
|
|
193
195
|
await options.relatedModel.deleteMany({ [options.relatedField!]: item._id });
|
|
194
196
|
}
|
|
195
197
|
onSuccess(res, method, item);
|
|
196
|
-
} catch (error:any) {
|
|
198
|
+
} catch (error: any) {
|
|
197
199
|
onError(res, method, error);
|
|
198
200
|
}
|
|
199
201
|
}));
|
|
@@ -206,10 +208,10 @@ class CrudController<T extends Document> {
|
|
|
206
208
|
this.router.get(path, applyMiddleware(async (req, res) => {
|
|
207
209
|
const method = 'GET (Aggregate)';
|
|
208
210
|
try {
|
|
209
|
-
const pipeline:any = options.aggregatePipeline ?? [];
|
|
211
|
+
const pipeline: any = options.aggregatePipeline ?? [];
|
|
210
212
|
const results = await this.model.aggregate(pipeline);
|
|
211
213
|
onSuccess(res, method, results);
|
|
212
|
-
} catch (error:any) {
|
|
214
|
+
} catch (error: any) {
|
|
213
215
|
onError(res, method, error);
|
|
214
216
|
}
|
|
215
217
|
}));
|