@kravc/dos 1.9.2 → 1.9.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.9.2",
3
+ "version": "1.9.4",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
@@ -12,6 +12,7 @@
12
12
  "Lambda Function"
13
13
  ],
14
14
  "main": "src/index.js",
15
+ "types": "src/index.d.ts",
15
16
  "repository": {
16
17
  "type": "git",
17
18
  "url": "http://github.com/alexkravets/dos.git"
package/src/Document.js CHANGED
@@ -61,6 +61,14 @@ class Document extends Component {
61
61
  this._bodySchema = schema
62
62
  }
63
63
 
64
+ static _extendWithPartition(context, parameters) {
65
+ if (!this.getPartition) {
66
+ return
67
+ }
68
+
69
+ parameters.partition = this.getPartition(context, parameters)
70
+ }
71
+
64
72
  static async create(context, query, mutation) {
65
73
  /* NOTE: existing document in the context allows to return document without
66
74
  duplicate been created */
@@ -96,6 +104,7 @@ class Document extends Component {
96
104
  }
97
105
 
98
106
  mutation[this.idKey] = this.createId(mutation)
107
+ this._extendWithPartition(context, mutation)
99
108
 
100
109
  const isCreated = await this._create(mutation)
101
110
 
package/src/Service.js CHANGED
@@ -12,15 +12,27 @@ const OperationNotFoundError = require('./errors/OperationNotFoundError')
12
12
  const { get, uniq, compact } = require('lodash')
13
13
 
14
14
  const ROOT_PATH = process.cwd()
15
+ const DEFAULT_URL = 'http://localhost:3000/'
16
+ const DEFAULT_SERVICE_PATH = `${ROOT_PATH}/src`
17
+ const DEFAULT_SKIP_OPERATIONS = []
15
18
 
16
19
  class Service {
17
- constructor(modules, url = 'http://localhost:3000/', path = `${ROOT_PATH}/src`) {
20
+ constructor(modules, options = {}) {
21
+ let {
22
+ url = DEFAULT_URL,
23
+ path = DEFAULT_SERVICE_PATH,
24
+ skipOperations = DEFAULT_SKIP_OPERATIONS,
25
+ } = options
26
+
18
27
  if (!url.endsWith('/')) { url = url + '/' }
19
28
 
20
29
  const schemasMap = createSchemasMap(path)
21
30
 
22
31
  let components = modules.filter(Component => !Component.types)
23
- let operations = modules.filter(Component => !!Component.types)
32
+
33
+ let operations = modules
34
+ .filter(Component => !!Component.types)
35
+ .filter(({ id }) => !skipOperations.includes(id))
24
36
 
25
37
  const operationComponents = compact(operations.map(({ Component }) => Component))
26
38
  components = uniq([ ...components, ...operationComponents ])
@@ -35,7 +35,11 @@ describe('Service', () => {
35
35
 
36
36
  describe('Service.constructor(modules, url, path = \'/src\')', () => {
37
37
  it('initializes service', () => {
38
- const service = new Service(modules, 'https://example.com/api', `${ROOT_PATH}/examples`)
38
+ const service = new Service(modules, {
39
+ url: 'https://example.com/api',
40
+ path: `${ROOT_PATH}/examples`
41
+ })
42
+
39
43
  expect(service).to.exist
40
44
  })
41
45
 
@@ -71,8 +75,12 @@ describe('Service', () => {
71
75
  })
72
76
 
73
77
  describe('.handler(request)', () => {
74
- const service = new Service(modules, 'https://example.com/api/', `${ROOT_PATH}/examples`)
75
- const exec = test.execute(service)
78
+ const service = new Service(modules, {
79
+ url: 'https://example.com/api/',
80
+ path: `${ROOT_PATH}/examples`
81
+ })
82
+
83
+ const exec = test.execute(service)
76
84
 
77
85
  const authorization = test.createAccessToken({}, { group: 'Administrators' })
78
86
 
package/src/index.d.ts ADDED
@@ -0,0 +1,126 @@
1
+
2
+ export declare function getOrFail(object: Record<string, any>, path: string): any;
3
+
4
+ export declare type SortOrder = 'asc' | 'desc';
5
+ export declare type Context = Record<string, any>;
6
+ export declare type QueryMap = Record<string, any>;
7
+ export declare type CreateMutationMap = Record<string, any>;
8
+ export declare type UpdateMutationMap = Record<string, any>;
9
+
10
+ export declare interface IndexOptions {
11
+ sort?: SortOrder;
12
+ limit?: number;
13
+ index?: string;
14
+ exclusiveStartKey?: string;
15
+ }
16
+
17
+ export declare interface IndexAllOptions {
18
+ sort?: SortOrder;
19
+ index?: string;
20
+ }
21
+
22
+ export declare type DocumentConstructor<T, D extends Document<T> = Document<T>> = {
23
+ new(context: Context, attributes: T): D;
24
+ };
25
+
26
+ export declare class Document<T> {
27
+ constructor(context: Context, attributes: T);
28
+
29
+ static get idKey(): string;
30
+ static get sortBy(): string;
31
+ static get idKeyPrefix(): string;
32
+ static get documentName(): string;
33
+
34
+ static getPartition(
35
+ context: Context,
36
+ parameters: Record<string, any>
37
+ ): string;
38
+
39
+ static index<T, D extends Document<T> = Document<T>>(
40
+ this: DocumentConstructor<T, D>,
41
+ context: Context,
42
+ query?: QueryMap,
43
+ options?: IndexOptions,
44
+ ): Promise<{
45
+ count: number;
46
+ objects: D[];
47
+ lastEvaluatedKey: string;
48
+ }>;
49
+
50
+ static read<T, D extends Document<T> = Document<T>>(
51
+ this: DocumentConstructor<T, D>,
52
+ context: Context,
53
+ query: QueryMap,
54
+ ): Promise<D>;
55
+
56
+ static create<T, D extends Document<T> = Document<T>>(
57
+ this: DocumentConstructor<T, D>,
58
+ context: Context,
59
+ query: QueryMap | CreateMutationMap,
60
+ mutation?: CreateMutationMap
61
+ ): Promise<D>;
62
+
63
+ static update<T, D extends Document<T> = Document<T>>(
64
+ this: DocumentConstructor<T, D>,
65
+ context: Context,
66
+ query: QueryMap,
67
+ mutation: UpdateMutationMap,
68
+ originalDocument?: D
69
+ ): Promise<D>;
70
+
71
+ static delete(
72
+ context: Context,
73
+ query: QueryMap
74
+ ): Promise<void>;
75
+
76
+ static beforeCreate(
77
+ context: Context,
78
+ query: QueryMap,
79
+ mutation: CreateMutationMap
80
+ ): Promise<void>;
81
+
82
+ static afterCreate<T, D extends Document<T> = Document<T>>(
83
+ context: Context,
84
+ query: QueryMap,
85
+ mutation: CreateMutationMap,
86
+ originalDocument: D
87
+ ): Promise<void>;
88
+
89
+ static beforeUpdate(
90
+ context: Context,
91
+ query: QueryMap,
92
+ mutation: UpdateMutationMap,
93
+ ): Promise<void>;
94
+
95
+ static afterUpdate<T, D extends Document<T> = Document<T>>(
96
+ context: Context,
97
+ query: QueryMap,
98
+ mutation: UpdateMutationMap,
99
+ originalDocument: D
100
+ ): Promise<void>;
101
+
102
+ static beforeDelete(
103
+ context: Context,
104
+ query: QueryMap,
105
+ ): Promise<void>;
106
+
107
+ static afterDelete<T, D extends Document<T> = Document<T>>(
108
+ context: Context,
109
+ query: QueryMap,
110
+ originalDocument: D
111
+ ): Promise<void>;
112
+
113
+ get id(): string;
114
+ get context(): Context;
115
+ get attributes(): T;
116
+ get componentId(): string;
117
+ get originalDocument(): this;
118
+ get _query(): Record<string, any>;
119
+
120
+ _attributes: Record<string, any>;
121
+
122
+ update(mutation: UpdateMutationMap, shouldMutate?: boolean): Promise<this>;
123
+ delete(): Promise<void>;
124
+ hasAttributeChanged(attributePath: string): boolean;
125
+ toJSON(): Record<string, any>;
126
+ }