@kravc/dos 1.11.22 → 1.12.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.
- package/package.json +1 -1
- package/src/Document.js +17 -39
- package/src/helpers/getDefaultSchemaAttributes.js +32 -0
- package/src/index.d.ts +42 -33
- 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)
|
|
@@ -243,17 +221,17 @@ class Document extends Component {
|
|
|
243
221
|
}
|
|
244
222
|
|
|
245
223
|
static async delete(context, query) {
|
|
246
|
-
if (this.beforeDelete) {
|
|
247
|
-
await this.beforeDelete(context, query)
|
|
248
|
-
}
|
|
249
|
-
|
|
250
224
|
/* NOTE: ensure that document to be removed exists and save it in the
|
|
251
225
|
context so can be referenced in the after action helper */
|
|
252
226
|
const originalDocument = await this.read(context, query)
|
|
253
227
|
|
|
228
|
+
if (this.beforeDelete) {
|
|
229
|
+
await this.beforeDelete(context, query, originalDocument)
|
|
230
|
+
}
|
|
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;
|
|
@@ -100,36 +104,37 @@ export declare class Document<T> {
|
|
|
100
104
|
mutation: CreateMutationMap
|
|
101
105
|
): Promise<void>;
|
|
102
106
|
|
|
103
|
-
static afterCreate<T, D extends Document<T> = Document<T>>(
|
|
104
|
-
context: Context,
|
|
105
|
-
query: QueryMap,
|
|
106
|
-
mutation: CreateMutationMap,
|
|
107
|
-
originalDocument: D
|
|
108
|
-
): Promise<void>;
|
|
109
|
-
|
|
110
107
|
static beforeUpdate(
|
|
111
108
|
context: Context,
|
|
112
109
|
query: QueryMap,
|
|
113
110
|
mutation: UpdateMutationMap,
|
|
114
111
|
): Promise<void>;
|
|
115
112
|
|
|
116
|
-
static
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
113
|
+
// static beforeDelete<T, D extends Document<T> = Document<T>>(
|
|
114
|
+
// context: Context,
|
|
115
|
+
// query: QueryMap,
|
|
116
|
+
// originalDocument: D
|
|
117
|
+
// ): Promise<void>;
|
|
118
|
+
|
|
119
|
+
// static afterCreate<T, D extends Document<T> = Document<T>>(
|
|
120
|
+
// context: Context,
|
|
121
|
+
// query: QueryMap,
|
|
122
|
+
// mutation: CreateMutationMap,
|
|
123
|
+
// originalDocument: D
|
|
124
|
+
// ): Promise<void>;
|
|
125
|
+
|
|
126
|
+
// static afterUpdate<T, D extends Document<T> = Document<T>>(
|
|
127
|
+
// context: Context,
|
|
128
|
+
// query: QueryMap,
|
|
129
|
+
// mutation: UpdateMutationMap,
|
|
130
|
+
// originalDocument: D
|
|
131
|
+
// ): Promise<void>;
|
|
132
|
+
|
|
133
|
+
// static afterDelete<T, D extends Document<T> = Document<T>>(
|
|
134
|
+
// context: Context,
|
|
135
|
+
// query: QueryMap,
|
|
136
|
+
// originalDocument: D
|
|
137
|
+
// ): Promise<void>;
|
|
133
138
|
|
|
134
139
|
get id(): string;
|
|
135
140
|
get context(): Context;
|
|
@@ -168,14 +173,14 @@ export declare class JwtAuthorization {
|
|
|
168
173
|
tokenVerificationMethod?: Function;
|
|
169
174
|
accessVerificationMethod?: (context: Context, payload: Record<string, unknown>) => [boolean, string?];
|
|
170
175
|
}): Record<string, any>
|
|
171
|
-
}
|
|
176
|
+
}
|
|
172
177
|
|
|
173
178
|
export declare class SystemAuthorization {
|
|
174
179
|
static createRequirement(options?: {
|
|
175
180
|
name?: string;
|
|
176
181
|
accessVerificationMethod?: (context: Context) => [boolean, string?];
|
|
177
182
|
}): Record<string, any>
|
|
178
|
-
}
|
|
183
|
+
}
|
|
179
184
|
|
|
180
185
|
export type ComponentConstructor = new (...args: any[]) => any;
|
|
181
186
|
type OperationConstructor = new (...args: any[]) => any;
|
|
@@ -302,31 +307,31 @@ export declare class CommonError extends Error {
|
|
|
302
307
|
constructor(code: string, message: string);
|
|
303
308
|
get code(): string;
|
|
304
309
|
get isCommonError(): boolean;
|
|
305
|
-
}
|
|
310
|
+
}
|
|
306
311
|
|
|
307
312
|
export declare class UnauthorizedError extends CommonError {
|
|
308
313
|
constructor(message: string);
|
|
309
|
-
}
|
|
314
|
+
}
|
|
310
315
|
|
|
311
316
|
export declare class AccessDeniedError extends CommonError {
|
|
312
317
|
constructor(message: string);
|
|
313
|
-
}
|
|
318
|
+
}
|
|
314
319
|
|
|
315
320
|
export declare class InvalidParametersError extends CommonError {
|
|
316
321
|
constructor(message: string)
|
|
317
|
-
}
|
|
322
|
+
}
|
|
318
323
|
|
|
319
324
|
export declare class UnprocessibleConditionError extends CommonError {
|
|
320
325
|
constructor(message: string)
|
|
321
|
-
}
|
|
326
|
+
}
|
|
322
327
|
|
|
323
328
|
export declare class DocumentExistsError extends CommonError {
|
|
324
329
|
constructor(Document: { name: string }, parameters: Record<string, unknown>);
|
|
325
|
-
}
|
|
330
|
+
}
|
|
326
331
|
|
|
327
332
|
export declare class DocumentNotFoundError extends CommonError {
|
|
328
333
|
constructor(Document: { name: string }, parameters: Record<string, unknown>);
|
|
329
|
-
}
|
|
334
|
+
}
|
|
330
335
|
|
|
331
336
|
interface Identity {
|
|
332
337
|
sub: string;
|
|
@@ -336,3 +341,7 @@ interface Identity {
|
|
|
336
341
|
export declare function authorize(Operation, Context): Promise<Identity>;
|
|
337
342
|
|
|
338
343
|
export declare function maskSecrets(object: Record<string, unknown>): Record<string, unknown>;
|
|
344
|
+
|
|
345
|
+
export declare function getComponentTitle(Component, isCapitalized?: boolean, isPlural?: boolean): string;
|
|
346
|
+
|
|
347
|
+
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'),
|