anote-server-libs 0.6.8 → 0.6.10

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.
@@ -0,0 +1,3 @@
1
+ {
2
+ "typescript.tsdk": "node_modules\\typescript\\lib"
3
+ }
@@ -90,9 +90,9 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
90
90
  return Object.getOwnPropertyNames(storage).map(k => storage[k]);
91
91
  }
92
92
 
93
- update(instance: T, client: ClientBase | ConnectionPool | Transaction): Promise<R> {
93
+ update(instance: T, client: ClientBase | ConnectionPool | Transaction, on?: Date): Promise<R> {
94
94
  if((<any>instance).archivedOn) return Promise.reject('Record archived!');
95
- instance.updatedOn = new Date();
95
+ instance.updatedOn = on || new Date();
96
96
  if(this.pool) {
97
97
  const props = this.serialize(instance);
98
98
  props.push(instance.id);
@@ -105,8 +105,8 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
105
105
  }
106
106
  }
107
107
 
108
- create(instance: T, client?: ClientBase | Transaction): Promise<R> {
109
- (<any>instance).createdOn = instance.updatedOn = new Date();
108
+ create(instance: T, client?: ClientBase | Transaction, on?: Date): Promise<R> {
109
+ (<any>instance).createdOn = instance.updatedOn = on || new Date();
110
110
  if(this.pool) {
111
111
  const props = this.serialize(instance);
112
112
  return (<ClientBase | Pool>(client || this.pool)).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
@@ -126,9 +126,9 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
126
126
  }
127
127
  }
128
128
 
129
- createSeveral(instances: T[], client?: ClientBase): Promise<R[]> {
129
+ createSeveral(instances: T[], client?: ClientBase, on?: Date): Promise<R[]> {
130
130
  if(!instances.length) return Promise.resolve([]);
131
- const now = new Date();
131
+ const now = on || new Date();
132
132
  instances.forEach(instance => instance.updatedOn = now);
133
133
  const props = [].concat.apply([], instances.map(instance => this.serialize(instance)));
134
134
  return (client || this.pool).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anote-server-libs",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
4
4
  "description": "Helpers for express-TS servers",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -10,7 +10,7 @@ exports.ajv.addKeyword({
10
10
  schemaType: 'number',
11
11
  validate: (schema, data) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
12
12
  });
13
- function WithBody(schema) {
13
+ function WithBody(schema, collapseTopLevelNulls = true) {
14
14
  if (schema.type === 'object') {
15
15
  const required = [];
16
16
  const keys = Object.getOwnPropertyNames(schema.properties);
@@ -41,6 +41,12 @@ function WithBody(schema) {
41
41
  });
42
42
  }
43
43
  else {
44
+ if (collapseTopLevelNulls) {
45
+ Object.keys(req.body).forEach(k => {
46
+ if (req.body[k] === null)
47
+ delete req.body[k];
48
+ });
49
+ }
44
50
  const valid = validate(req.body);
45
51
  if (valid) {
46
52
  return previousMethod.call(this, req, res);
@@ -9,7 +9,7 @@ ajv.addKeyword({
9
9
  validate: (schema: number, data: any) => typeof data === 'number' && Math.round(data * 10 ** schema) / (10 ** schema) === data
10
10
  });
11
11
 
12
- export function WithBody(schema: any) {
12
+ export function WithBody(schema: any, collapseTopLevelNulls = true) {
13
13
  if(schema.type === 'object') {
14
14
  const required: string[] = [];
15
15
  const keys = Object.getOwnPropertyNames(schema.properties);
@@ -39,6 +39,11 @@ export function WithBody(schema: any) {
39
39
  }
40
40
  });
41
41
  } else {
42
+ if(collapseTopLevelNulls) {
43
+ Object.keys(req.body).forEach(k => {
44
+ if(req.body[k] === null) delete req.body[k];
45
+ });
46
+ }
42
47
  const valid = validate(req.body);
43
48
  if(valid) {
44
49
  return previousMethod.call(this, req, res);