nodester 0.5.0 → 0.5.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.
@@ -30,13 +30,14 @@ function extract(body, filter=null, model) {
30
30
 
31
31
  const bodyEntries = Object.entries(body);
32
32
 
33
+ // Unwrap statics:
33
34
  const { statics } = filter;
34
-
35
+ const staticAttributes = statics?.attributes ?? {};
35
36
 
36
37
  // Result object.
37
38
  const filteredBody = {};
38
39
 
39
- for (const [key, value] of bodyEntries) {
40
+ for (const [ key, value ] of bodyEntries) {
40
41
  const isInclude = availableIncludes.indexOf(key) > -1;
41
42
  const isAttribute = modelAttributes.indexOf(key) > -1;
42
43
 
@@ -47,6 +48,14 @@ function extract(body, filter=null, model) {
47
48
  }
48
49
 
49
50
  if (isAttribute) {
51
+ // If this attribute must have a static value,
52
+ // skip it.
53
+ // The static value will be set later:
54
+ if (typeof staticAttributes[key] !== 'undefined') {
55
+ continue;
56
+ }
57
+
58
+ // Sanitize value from the body:
50
59
  const column = model.rawAttributes[key];
51
60
  const typeName = column.type.constructor.name;
52
61
  // Optional validation.
@@ -99,6 +108,12 @@ function extract(body, filter=null, model) {
99
108
  err.status = httpCodes.NOT_ACCEPTABLE;
100
109
  throw err;
101
110
  }
111
+
112
+ // Set all static attributes:
113
+ const staticAttributeEntries = Object.entries(staticAttributes);
114
+ for (const [ key, value ] of staticAttributeEntries) {
115
+ filteredBody[key] = staticAttributes[key];
116
+ }
102
117
 
103
118
  return filteredBody;
104
119
  }
@@ -64,7 +64,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
64
64
  where: {},
65
65
  include: []
66
66
  };
67
-
67
+
68
68
  const {
69
69
  attributes,
70
70
  clauses,
@@ -184,18 +184,19 @@ function traverse(queryNode, filter=null, model=null, association=null) {
184
184
  const order = {};
185
185
 
186
186
  const clausesEntries = Object.entries(clauses);
187
- for (let [clauseName, value] of clausesEntries) {
187
+ for (const [ clauseName, value ] of clausesEntries) {
188
188
 
189
189
  // If clause is not available:
190
190
  if (filter != null) {
191
- if (filter.clauses.indexOf(clauseName) === -1)
191
+ if (filter.clauses.indexOf(clauseName) === -1) {
192
192
  continue;
193
+ }
193
194
  }
194
195
 
195
196
  switch(clauseName) {
196
197
  case 'limit': {
197
198
  const _value = _setValueWithBounds(value, 'number', filter.bounds.clauses.limit);
198
-
199
+
199
200
  // Do not set if -1:
200
201
  if (_value === -1)
201
202
  continue;
@@ -226,7 +227,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
226
227
 
227
228
  order.by = value;
228
229
  continue;
229
-
230
+
230
231
  default:
231
232
  continue;
232
233
  }
@@ -243,7 +244,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
243
244
  case 'limit':
244
245
  newQuery.limit = staticClauseValue;
245
246
  continue;
246
-
247
+
247
248
  case 'skip':
248
249
  newQuery.offset = staticClauseValue;
249
250
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodester",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "A versatile REST framework for Node.js",
5
5
  "directories": {
6
6
  "docs": "docs",