@kravc/dos 1.11.20 → 1.11.21

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Document.js +21 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.11.20",
3
+ "version": "1.11.21",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
package/src/Document.js CHANGED
@@ -35,10 +35,10 @@ class Document extends Component {
35
35
  return this._schema
36
36
  }
37
37
 
38
- static set schema(schema) {
38
+ static get _defaultSchemaProperties() {
39
39
  const documentTitle = getComponentTitle(this, false)
40
40
 
41
- this._schema = schema.extend({
41
+ return {
42
42
  id: {
43
43
  description: capitalize(documentTitle) + ' ID',
44
44
  required: true
@@ -58,8 +58,11 @@ class Document extends Component {
58
58
  updatedBy: {
59
59
  description: `ID of a user who updated ${documentTitle}`
60
60
  }
61
- }, this.id)
61
+ }
62
+ }
62
63
 
64
+ static set schema(schema) {
65
+ this._schema = schema.extend(this._defaultSchemaProperties, this.id)
63
66
  this._bodySchema = schema
64
67
  }
65
68
 
@@ -71,6 +74,13 @@ class Document extends Component {
71
74
  parameters.partition = this.getPartition(context, parameters)
72
75
  }
73
76
 
77
+ static _extendWithCreateStamps(mutation) {
78
+ const timestamp = new Date().toJSON()
79
+ mutation.createdAt = timestamp
80
+ mutation.updatedAt = timestamp
81
+ mutation.createdBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
82
+ }
83
+
74
84
  static async create(context, query, mutation) {
75
85
  /* NOTE: existing document in the context allows to return document without
76
86
  duplicate been created */
@@ -90,11 +100,8 @@ class Document extends Component {
90
100
 
91
101
  const { validator } = context
92
102
  mutation = validator.normalize(mutation, this.id)
93
- mutation.createdBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
94
103
 
95
- const timestamp = new Date().toJSON()
96
- mutation.createdAt = timestamp
97
- mutation.updatedAt = timestamp
104
+ this._extendWithCreateStamps(mutation)
98
105
 
99
106
  if (this.beforeCreate) {
100
107
  await this.beforeCreate(context, query, mutation)
@@ -186,12 +193,16 @@ class Document extends Component {
186
193
  return this._index(query)
187
194
  }
188
195
 
196
+ static _extendWithUpdateStamps(mutation) {
197
+ const timestamp = new Date().toJSON()
198
+ mutation.updatedAt = timestamp
199
+ mutation.updatedBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
200
+ }
201
+
189
202
  static async update(context, query, mutation, originalDocument = null) {
190
203
  mutation = omit(mutation, [ this.idKey, 'createdAt', 'createdBy' ])
191
- mutation.updatedBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
192
204
 
193
- const timestamp = new Date().toJSON()
194
- mutation.updatedAt = timestamp
205
+ this._extendWithUpdateStamps(mutation)
195
206
 
196
207
  if (this.beforeUpdate) {
197
208
  await this.beforeUpdate(context, query, mutation)