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