very-express 0.2.5 → 0.2.6
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.
|
@@ -63,19 +63,14 @@ class {{interfaceName}}Controller {
|
|
|
63
63
|
return res.status(400).json(validationError.array());
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
// Get mongoose filter query
|
|
67
66
|
const searchFilter = new MongoQS().parse(req.query);
|
|
67
|
+
let selectedFields : {[key: string]: number} | undefined = undefined;
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
try { selectedFields = parseFieldsSelect(req.query.select || '');}
|
|
70
|
+
catch (err:any) { return res.status(400).json({ error: err.message });};
|
|
71
71
|
|
|
72
72
|
const result = await {{interfaceName}}Model.find(searchFilter, selectedFields);
|
|
73
|
-
|
|
74
|
-
return res.status(404).json({ error: "no data found" });
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
return res.status(200).json(result);
|
|
78
|
-
};
|
|
73
|
+
return res.status(200).json(result);
|
|
79
74
|
} catch (err:any) {
|
|
80
75
|
return res.status(500).json({ error: err.message });
|
|
81
76
|
}
|
|
@@ -17,28 +17,32 @@ export function loadYaml(yamlFilePath: string) {
|
|
|
17
17
|
* @param fieldsString json stringified array of string
|
|
18
18
|
* @error may throw an error if the fieldsString is not a valid JSON
|
|
19
19
|
*/
|
|
20
|
-
export function parseFieldsSelect(
|
|
20
|
+
export function parseFieldsSelect(selectString: any) : { [key: string]: number } | undefined {
|
|
21
|
+
const ErrorArrStrMsg = "Invalid select string, only array of string accepted";
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
if (typeof selectString === "undefined" || selectString === "") {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
else if (typeof selectString !== "string") {
|
|
27
|
+
throw new Error(ErrorArrStrMsg);
|
|
28
|
+
}
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
return reject("Invalid fields string : "+typeof fieldsString,);
|
|
26
|
-
}
|
|
30
|
+
const fieldArr: string[] = JSON.parse(selectString);
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
if (!Array.isArray(fieldArr)) {
|
|
33
|
+
throw new Error(ErrorArrStrMsg);
|
|
34
|
+
}
|
|
29
35
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return reject("Invalid fields string");
|
|
36
|
+
for (let i = 0; i < fieldArr.length; i++) {
|
|
37
|
+
if (typeof fieldArr[i] !== "string") {
|
|
38
|
+
throw new Error(ErrorArrStrMsg);
|
|
34
39
|
}
|
|
40
|
+
}
|
|
41
|
+
// Convert the fieldArr to an object that can be used in the select method
|
|
42
|
+
const selectFields = fieldArr.reduce((obj: any, field: string) => {
|
|
43
|
+
obj[field] = 1;
|
|
44
|
+
return obj;
|
|
45
|
+
}, {} as Record<string, number>);
|
|
35
46
|
|
|
36
|
-
|
|
37
|
-
const selectFields = fieldArr.reduce((obj:any, field:string) => {
|
|
38
|
-
obj[field] = 1;
|
|
39
|
-
return obj;
|
|
40
|
-
}, {} as Record<string, number>);
|
|
41
|
-
|
|
42
|
-
resolve(selectFields);
|
|
43
|
-
});
|
|
47
|
+
return selectFields;
|
|
44
48
|
}
|