very-express 0.1.0 → 0.1.1

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.
@@ -66,13 +66,12 @@ class {{interfaceName}}Controller {
66
66
  };
67
67
 
68
68
  // Get the selected fields from query string
69
- const fields = req.query.fields as string | undefined;
70
- const selectFields = ! fields ? undefined : await parseFieldsSelect(fields)
69
+ const selectedFields = await parseFieldsSelect(req.query.select)
71
70
  .catch((err) => {
72
71
  return res.status(400).json({ error: err });
73
72
  });
74
73
 
75
- const result = await {{interfaceName}}Model.find({}, selectFields);
74
+ const result = await {{interfaceName}}Model.find({}, selectedFields);
76
75
  if (!result) {
77
76
  return res.status(404).json({ error: "no data found" });
78
77
  }
@@ -121,6 +120,24 @@ class {{interfaceName}}Controller {
121
120
  }
122
121
  }
123
122
 
123
+ public async replace{{interfaceName}}(req: Request, res: Response): Promise<Response> {
124
+ try {
125
+ const validationError = validationResult(req);
126
+ if ( ! validationError.isEmpty() ) {
127
+ return res.status(400).json(validationError.array());
128
+ };
129
+ const result = await {{interfaceName}}Model.replaceOne({_id: req.params.id}, req.body, { new: true });
130
+ if (!result) {
131
+ return res.status(404).json({ error: "failed to update" });
132
+ }
133
+ else {
134
+ return res.status(200).json(result);
135
+ };
136
+ } catch (err:any) {
137
+ return res.status(500).json({ error: err.message });
138
+ }
139
+ }
140
+
124
141
  public async delete{{interfaceName}}(req: Request, res: Response): Promise<Response> {
125
142
  try {
126
143
  const validationError = validationResult(req);
@@ -120,7 +120,7 @@ function processSchema(options) {
120
120
  const validators = {
121
121
  [options.fieldName]: {
122
122
  in: options.param ? "params" : options.body ? "body" : [],
123
- optional: !options.required ? true : { options: { values: "falsy", checkFalsy: true } },
123
+ optional: !options.required ? { options: { values: "falsy", checkFalsy: true } } : false,
124
124
  notEmpty: true,
125
125
  },
126
126
  };
@@ -12,12 +12,19 @@ export function loadYaml(yamlFilePath: string) {
12
12
 
13
13
  /**
14
14
  * build mongoose select object
15
- * @param fieldsString stringified array of FieldPath
15
+ * @param fieldsString json stringified array of string
16
16
  * @error may throw an error if the fieldsString is not a valid JSON
17
17
  */
18
- export function parseFieldsSelect(fieldsString:string): Promise<{[key:string]:number}> {
18
+ export function parseFieldsSelect(fieldsString: any ): Promise<{[key:string]:number}> {
19
+
19
20
  return new Promise((resolve, reject) => {
21
+
22
+ if ( !fieldsString || typeof fieldsString !== "string"){
23
+ return reject("Invalid fields string : "+typeof fieldsString,);
24
+ }
25
+
20
26
  let fieldArr: string[] = [];
27
+
21
28
  try {
22
29
  fieldArr = JSON.parse(fieldsString);
23
30
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "very-express",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "based on json schema and openapi, generate Express RestApi server with mongoDb ",
5
5
  "author": "justin26l",
6
6
  "license": "MIT",