@kravc/dos 1.11.22 → 1.12.0
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.
- package/package.json +1 -1
- package/src/Document.js +13 -35
- package/src/helpers/getDefaultSchemaAttributes.js +32 -0
- package/src/index.d.ts +8 -0
- package/src/index.js +2 -0
package/package.json
CHANGED
package/src/Document.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { ulid }
|
|
3
|
+
const { ulid } = require('ulid')
|
|
4
|
+
const { get, omit, cloneDeep } = require('lodash')
|
|
5
|
+
|
|
4
6
|
const Component = require('./Component')
|
|
5
7
|
const getIdPrefix = require('./helpers/getIdPrefix')
|
|
6
|
-
const getComponentTitle = require('./helpers/getComponentTitle')
|
|
7
8
|
const DocumentExistsError = require('./errors/DocumentExistsError')
|
|
8
9
|
const DocumentNotFoundError = require('./errors/DocumentNotFoundError')
|
|
9
|
-
const
|
|
10
|
+
const getDefaultSchemaAttributes = require('./helpers/getDefaultSchemaAttributes')
|
|
10
11
|
|
|
11
12
|
const STORE = {}
|
|
12
13
|
const SYSTEM = 'SYSTEM'
|
|
@@ -35,34 +36,12 @@ class Document extends Component {
|
|
|
35
36
|
return this._schema
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
static get
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
id: {
|
|
43
|
-
description: capitalize(documentTitle) + ' ID',
|
|
44
|
-
required: true
|
|
45
|
-
},
|
|
46
|
-
createdAt: {
|
|
47
|
-
description: `Date and time when ${documentTitle} was created`,
|
|
48
|
-
format: 'date-time',
|
|
49
|
-
required: true
|
|
50
|
-
},
|
|
51
|
-
updatedAt: {
|
|
52
|
-
description: `Date and time when ${documentTitle} was updated`,
|
|
53
|
-
format: 'date-time'
|
|
54
|
-
},
|
|
55
|
-
createdBy: {
|
|
56
|
-
description: `ID of a user who created ${documentTitle}`
|
|
57
|
-
},
|
|
58
|
-
updatedBy: {
|
|
59
|
-
description: `ID of a user who updated ${documentTitle}`
|
|
60
|
-
}
|
|
61
|
-
}
|
|
39
|
+
static get _defaultSchemaAttributes() {
|
|
40
|
+
return getDefaultSchemaAttributes(this)
|
|
62
41
|
}
|
|
63
42
|
|
|
64
43
|
static set schema(schema) {
|
|
65
|
-
this._schema = schema.extend(this.
|
|
44
|
+
this._schema = schema.extend(this._defaultSchemaAttributes, this.id)
|
|
66
45
|
this._bodySchema = schema
|
|
67
46
|
}
|
|
68
47
|
|
|
@@ -74,10 +53,9 @@ class Document extends Component {
|
|
|
74
53
|
parameters.partition = this.getPartition(context, parameters)
|
|
75
54
|
}
|
|
76
55
|
|
|
77
|
-
static
|
|
56
|
+
static _extendWithCreatedStamps(context, mutation) {
|
|
78
57
|
const timestamp = new Date().toJSON()
|
|
79
58
|
mutation.createdAt = timestamp
|
|
80
|
-
mutation.updatedAt = timestamp
|
|
81
59
|
mutation.createdBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
|
|
82
60
|
}
|
|
83
61
|
|
|
@@ -101,7 +79,7 @@ class Document extends Component {
|
|
|
101
79
|
const { validator } = context
|
|
102
80
|
mutation = validator.normalize(mutation, this.id)
|
|
103
81
|
|
|
104
|
-
this.
|
|
82
|
+
this._extendWithCreatedStamps(context, mutation)
|
|
105
83
|
|
|
106
84
|
if (this.beforeCreate) {
|
|
107
85
|
await this.beforeCreate(context, query, mutation)
|
|
@@ -193,7 +171,7 @@ class Document extends Component {
|
|
|
193
171
|
return this._index(query)
|
|
194
172
|
}
|
|
195
173
|
|
|
196
|
-
static
|
|
174
|
+
static _extendWithUpdatedStamps(context, mutation) {
|
|
197
175
|
const timestamp = new Date().toJSON()
|
|
198
176
|
mutation.updatedAt = timestamp
|
|
199
177
|
mutation.updatedBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
|
|
@@ -202,7 +180,7 @@ class Document extends Component {
|
|
|
202
180
|
static async update(context, query, mutation, originalDocument = null) {
|
|
203
181
|
mutation = omit(mutation, [ this.idKey, 'createdAt', 'createdBy' ])
|
|
204
182
|
|
|
205
|
-
this.
|
|
183
|
+
this._extendWithUpdatedStamps(context, mutation)
|
|
206
184
|
|
|
207
185
|
if (this.beforeUpdate) {
|
|
208
186
|
await this.beforeUpdate(context, query, mutation)
|
|
@@ -253,7 +231,7 @@ class Document extends Component {
|
|
|
253
231
|
|
|
254
232
|
this._extendWithPartition(context, query)
|
|
255
233
|
|
|
256
|
-
await this._delete(
|
|
234
|
+
await this._delete(context, query)
|
|
257
235
|
|
|
258
236
|
if (this.afterDelete) {
|
|
259
237
|
await this.afterDelete(context, query, originalDocument)
|
|
@@ -262,7 +240,7 @@ class Document extends Component {
|
|
|
262
240
|
return originalDocument
|
|
263
241
|
}
|
|
264
242
|
|
|
265
|
-
static _delete({ id }) {
|
|
243
|
+
static _delete(context, { id }) {
|
|
266
244
|
const item = STORE[this.name][id]
|
|
267
245
|
|
|
268
246
|
/* istanbul ignore next: not used anymore by delete interface as read
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { capitalize } = require('lodash')
|
|
4
|
+
const getComponentTitle = require('./getComponentTitle')
|
|
5
|
+
|
|
6
|
+
const getDefaultSchemaAttributes = (Component) => {
|
|
7
|
+
const documentTitle = getComponentTitle(Component, false)
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
id: {
|
|
11
|
+
description: capitalize(documentTitle) + ' ID',
|
|
12
|
+
required: true
|
|
13
|
+
},
|
|
14
|
+
createdAt: {
|
|
15
|
+
description: `Date and time when ${documentTitle} was created`,
|
|
16
|
+
format: 'date-time',
|
|
17
|
+
required: true
|
|
18
|
+
},
|
|
19
|
+
updatedAt: {
|
|
20
|
+
description: `Date and time when ${documentTitle} was updated`,
|
|
21
|
+
format: 'date-time'
|
|
22
|
+
},
|
|
23
|
+
createdBy: {
|
|
24
|
+
description: `ID of a user who created ${documentTitle}`
|
|
25
|
+
},
|
|
26
|
+
updatedBy: {
|
|
27
|
+
description: `ID of a user who updated ${documentTitle}`
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = getDefaultSchemaAttributes
|
package/src/index.d.ts
CHANGED
|
@@ -36,8 +36,12 @@ export declare class Document<T> {
|
|
|
36
36
|
static set schema(schema: Schema);
|
|
37
37
|
static get schema(): Schema;
|
|
38
38
|
|
|
39
|
+
static get _defaultSchemaAttributes(): SchemaAttributes;
|
|
40
|
+
|
|
39
41
|
static _read(query: QueryMap): Promise<AttributesMap>;
|
|
42
|
+
static _extendWithCreatedStamps(context: Context, mutation: CreateMutationMap): void;
|
|
40
43
|
static _create(attributes: AttributesMap): Promise<Boolean>;
|
|
44
|
+
static _extendWithUpdatedStamps(context: Context, mutation: UpdateMutationMap): void;
|
|
41
45
|
static _update(query: QueryMap, mutation: UpdateMutationMap): Promise<AttributesMap>;
|
|
42
46
|
|
|
43
47
|
static createId(attributes: AttributesMap): string;
|
|
@@ -336,3 +340,7 @@ interface Identity {
|
|
|
336
340
|
export declare function authorize(Operation, Context): Promise<Identity>;
|
|
337
341
|
|
|
338
342
|
export declare function maskSecrets(object: Record<string, unknown>): Record<string, unknown>;
|
|
343
|
+
|
|
344
|
+
export declare function getComponentTitle(Component, isCapitalized?: boolean, isPlural?: boolean): string;
|
|
345
|
+
|
|
346
|
+
export declare function getDefaultSchemaAttributes(Component): SchemaAttributes;
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,8 @@ module.exports = {
|
|
|
16
16
|
getOrFail: require('./helpers/getOrFail'),
|
|
17
17
|
authorize: require('./helpers/authorize'),
|
|
18
18
|
maskSecrets: require('./helpers/maskSecrets'),
|
|
19
|
+
getComponentTitle: require('./helpers/getComponentTitle'),
|
|
20
|
+
getDefaultSchemaAttributes: require('./helpers/getDefaultSchemaAttributes'),
|
|
19
21
|
security: require('./security'),
|
|
20
22
|
verifyToken: require('./security/verifyToken'),
|
|
21
23
|
JwtAuthorization: require('./security/JwtAuthorization'),
|