@kravc/dos 1.9.3 → 1.9.5

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.3",
3
+ "version": "1.9.5",
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"
@@ -25,7 +26,7 @@
25
26
  "author": "Alexander Kravets <a@kra.vc>",
26
27
  "license": "ISC",
27
28
  "dependencies": {
28
- "@kravc/schema": "^2.7.1",
29
+ "@kravc/schema": "^2.7.2",
29
30
  "cookie": "^1.0.1",
30
31
  "js-yaml": "^4.1.0",
31
32
  "jsonwebtoken": "^9.0.2",
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
 
@@ -127,6 +136,8 @@ class Document extends Component {
127
136
  }
128
137
 
129
138
  static async read(context, query, options) {
139
+ this._extendWithPartition(context, query)
140
+
130
141
  const item = await this._read(query, options)
131
142
 
132
143
  if (!item) {
@@ -188,6 +199,8 @@ class Document extends Component {
188
199
  originalDocument = await this.read(context, query)
189
200
  }
190
201
 
202
+ this._extendWithPartition(context, query)
203
+
191
204
  const updatedItem = await this._update(query, mutation)
192
205
 
193
206
  const document = new this(context, updatedItem)
@@ -223,6 +236,8 @@ class Document extends Component {
223
236
  context so can be referenced in the after action helper */
224
237
  const originalDocument = await this.read(context, query)
225
238
 
239
+ this._extendWithPartition(context, query)
240
+
226
241
  await this._delete(query)
227
242
 
228
243
  if (this.afterDelete) {
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
+ }