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