@lafken/dynamo 0.10.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/LICENCE +21 -0
- package/README.md +503 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +19 -0
- package/lib/main/index.d.ts +1 -0
- package/lib/main/index.js +17 -0
- package/lib/main/table/index.d.ts +2 -0
- package/lib/main/table/index.js +18 -0
- package/lib/main/table/table.d.ts +107 -0
- package/lib/main/table/table.js +141 -0
- package/lib/main/table/table.types.d.ts +368 -0
- package/lib/main/table/table.types.js +15 -0
- package/lib/resolver/index.d.ts +1 -0
- package/lib/resolver/index.js +17 -0
- package/lib/resolver/resolver.d.ts +12 -0
- package/lib/resolver/resolver.js +48 -0
- package/lib/resolver/resolver.types.d.ts +13 -0
- package/lib/resolver/resolver.types.js +2 -0
- package/lib/resolver/table/external/external.d.ts +14 -0
- package/lib/resolver/table/external/external.js +15 -0
- package/lib/resolver/table/internal/internal.d.ts +19 -0
- package/lib/resolver/table/internal/internal.js +224 -0
- package/lib/resolver/table/table.types.d.ts +4 -0
- package/lib/resolver/table/table.types.js +2 -0
- package/lib/service/client/client.d.ts +3 -0
- package/lib/service/client/client.js +10 -0
- package/lib/service/index.d.ts +3 -0
- package/lib/service/index.js +19 -0
- package/lib/service/query-builder/base/base.d.ts +19 -0
- package/lib/service/query-builder/base/base.js +151 -0
- package/lib/service/query-builder/base/base.types.d.ts +37 -0
- package/lib/service/query-builder/base/base.types.js +2 -0
- package/lib/service/query-builder/base/base.utils.d.ts +22 -0
- package/lib/service/query-builder/base/base.utils.js +89 -0
- package/lib/service/query-builder/batch-write/batch-write.d.ts +15 -0
- package/lib/service/query-builder/batch-write/batch-write.js +51 -0
- package/lib/service/query-builder/batch-write/batch-write.types.d.ts +9 -0
- package/lib/service/query-builder/batch-write/batch-write.types.js +2 -0
- package/lib/service/query-builder/bulk-create/bulk-create.d.ts +6 -0
- package/lib/service/query-builder/bulk-create/bulk-create.js +26 -0
- package/lib/service/query-builder/bulk-create/bulk-create.types.d.ts +4 -0
- package/lib/service/query-builder/bulk-create/bulk-create.types.js +2 -0
- package/lib/service/query-builder/bulk-delete/bulk-delete.d.ts +6 -0
- package/lib/service/query-builder/bulk-delete/bulk-delete.js +27 -0
- package/lib/service/query-builder/bulk-delete/bulk-delete.types.d.ts +7 -0
- package/lib/service/query-builder/bulk-delete/bulk-delete.types.js +2 -0
- package/lib/service/query-builder/create/create.d.ts +6 -0
- package/lib/service/query-builder/create/create.js +22 -0
- package/lib/service/query-builder/create/create.types.d.ts +6 -0
- package/lib/service/query-builder/create/create.types.js +2 -0
- package/lib/service/query-builder/delete/delete.d.ts +13 -0
- package/lib/service/query-builder/delete/delete.js +33 -0
- package/lib/service/query-builder/delete/delete.types.d.ts +7 -0
- package/lib/service/query-builder/delete/delete.types.js +2 -0
- package/lib/service/query-builder/dynamo-index/dynamo-index.d.ts +15 -0
- package/lib/service/query-builder/dynamo-index/dynamo-index.js +90 -0
- package/lib/service/query-builder/dynamo-index/dynamo-index.types.d.ts +5 -0
- package/lib/service/query-builder/dynamo-index/dynamo-index.types.js +2 -0
- package/lib/service/query-builder/find/find.d.ts +13 -0
- package/lib/service/query-builder/find/find.js +63 -0
- package/lib/service/query-builder/find/find.types.d.ts +8 -0
- package/lib/service/query-builder/find/find.types.js +2 -0
- package/lib/service/query-builder/find-all/find-all.d.ts +10 -0
- package/lib/service/query-builder/find-all/find-all.js +19 -0
- package/lib/service/query-builder/find-one/find-one.d.ts +9 -0
- package/lib/service/query-builder/find-one/find-one.js +20 -0
- package/lib/service/query-builder/index.d.ts +1 -0
- package/lib/service/query-builder/index.js +17 -0
- package/lib/service/query-builder/query-builder.types.d.ts +243 -0
- package/lib/service/query-builder/query-builder.types.js +3 -0
- package/lib/service/query-builder/scan/scan.d.ts +15 -0
- package/lib/service/query-builder/scan/scan.js +57 -0
- package/lib/service/query-builder/scan/scan.types.d.ts +6 -0
- package/lib/service/query-builder/scan/scan.types.js +2 -0
- package/lib/service/query-builder/update/update.d.ts +15 -0
- package/lib/service/query-builder/update/update.js +101 -0
- package/lib/service/query-builder/update/update.types.d.ts +6 -0
- package/lib/service/query-builder/update/update.types.js +2 -0
- package/lib/service/query-builder/update/update.utils.d.ts +11 -0
- package/lib/service/query-builder/update/update.utils.js +22 -0
- package/lib/service/query-builder/upsert/upsert.d.ts +14 -0
- package/lib/service/query-builder/upsert/upsert.js +40 -0
- package/lib/service/query-builder/upsert/upsert.types.d.ts +7 -0
- package/lib/service/query-builder/upsert/upsert.types.js +2 -0
- package/lib/service/repository/index.d.ts +3 -0
- package/lib/service/repository/index.js +19 -0
- package/lib/service/repository/repository.d.ts +3 -0
- package/lib/service/repository/repository.js +89 -0
- package/lib/service/repository/repository.types.d.ts +23 -0
- package/lib/service/repository/repository.types.js +2 -0
- package/lib/service/repository/repository.utils.d.ts +9 -0
- package/lib/service/repository/repository.utils.js +17 -0
- package/lib/service/transaction/index.d.ts +2 -0
- package/lib/service/transaction/index.js +18 -0
- package/lib/service/transaction/transaction.d.ts +4 -0
- package/lib/service/transaction/transaction.js +38 -0
- package/lib/service/transaction/transaction.types.d.ts +5 -0
- package/lib/service/transaction/transaction.types.js +2 -0
- package/package.json +99 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import type { ClassResource, DeepPartial, KeyOfClass, OnlyNumberString, OnlyOne } from '@lafken/common';
|
|
3
|
+
import type { FieldsMetadata, PrimaryPartition, TablePartition, TablePayPerRequest, TableProvisioned } from '../../main/table';
|
|
4
|
+
interface RequiredName {
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
export type ModelMetadata<T extends Function> = (TableProvisioned<T> & RequiredName) | (TablePayPerRequest<T> & RequiredName);
|
|
8
|
+
export type OperationExpression<E> = OnlyOne<{
|
|
9
|
+
lessThan: E;
|
|
10
|
+
lessOrEqualThan: E;
|
|
11
|
+
greaterThan: E;
|
|
12
|
+
greaterOrEqualThan: E;
|
|
13
|
+
between: [E, E];
|
|
14
|
+
}>;
|
|
15
|
+
export type InExpression<E extends string | number> = {
|
|
16
|
+
in: E[];
|
|
17
|
+
};
|
|
18
|
+
export type NullExpression = OnlyOne<{
|
|
19
|
+
exist: true;
|
|
20
|
+
notExist: true;
|
|
21
|
+
}>;
|
|
22
|
+
export type StringExpression = {
|
|
23
|
+
beginsWith: string;
|
|
24
|
+
};
|
|
25
|
+
export type StringFilterExpression = OnlyOne<{
|
|
26
|
+
contains: string;
|
|
27
|
+
notContains: string;
|
|
28
|
+
}>;
|
|
29
|
+
export type CommonExpression<E> = OnlyOne<{
|
|
30
|
+
notEqual: E;
|
|
31
|
+
} & NullExpression>;
|
|
32
|
+
export type Filter<E> = {
|
|
33
|
+
[key in keyof E]?: E[key] extends PrimaryPartition<number> ? number | OnlyOne<OperationExpression<number> & CommonExpression<number> & InExpression<number>> : E[key] extends PrimaryPartition<string> ? string | OnlyOne<StringExpression & StringFilterExpression & CommonExpression<string> & InExpression<string>> : E[key] extends string | number | boolean | Date | null ? E[key] | (E[key] extends number ? OnlyOne<OperationExpression<number> & CommonExpression<number> & InExpression<number>> : E[key] extends boolean ? CommonExpression<boolean> : E[key] extends null ? NullExpression : OnlyOne<StringExpression & StringFilterExpression & CommonExpression<string> & InExpression<string>>) : DeepPartial<Filter<E[key]>>;
|
|
34
|
+
};
|
|
35
|
+
export type OrFilter<E> = {
|
|
36
|
+
OR: Array<Filter<E> | AndFilter<E>>;
|
|
37
|
+
};
|
|
38
|
+
export type AndFilter<E> = {
|
|
39
|
+
AND: Filter<E> | OrFilter<E>;
|
|
40
|
+
};
|
|
41
|
+
export type SortDirectionType = 'asc' | 'desc';
|
|
42
|
+
export type KeyCondition<E> = {
|
|
43
|
+
partition: Partial<OnlyNumberString<E>>;
|
|
44
|
+
sort?: {
|
|
45
|
+
[key in keyof E as E[key] extends number | string ? key : never]?: E[key] extends PrimaryPartition<number> ? number | OperationExpression<E[key]> : E[key] extends PrimaryPartition<string> ? string | OnlyOne<OperationExpression<E[key]> & StringExpression> : E[key] | (E[key] extends number ? OperationExpression<E[key]> : OnlyOne<OperationExpression<E[key]> | StringExpression>);
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export type Item<E extends Function> = {
|
|
49
|
+
[key in keyof E['prototype']]: E['prototype'][key];
|
|
50
|
+
};
|
|
51
|
+
export type Cursor<E extends Function> = Partial<E>;
|
|
52
|
+
export type Projection<E extends Function> = KeyOfClass<E>[] | 'ALL';
|
|
53
|
+
export interface FindProps<E extends Function> {
|
|
54
|
+
/**
|
|
55
|
+
* Specifies which attributes are included in the query result.
|
|
56
|
+
* By limiting the projected attributes, the response size can be reduced
|
|
57
|
+
* and query performance improved.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* projection: ['foo', 'bar']
|
|
61
|
+
*/
|
|
62
|
+
projection?: Projection<E>;
|
|
63
|
+
/**
|
|
64
|
+
* Specifies the filter expression applied to the query or scan operation.
|
|
65
|
+
* This filter is evaluated after the items are read and is used to narrow
|
|
66
|
+
* down the result set based on non-key attributes.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* filter: {
|
|
70
|
+
* age: {
|
|
71
|
+
* lessThan: 25,
|
|
72
|
+
* },
|
|
73
|
+
* }
|
|
74
|
+
*/
|
|
75
|
+
filter?: Filter<E['prototype']> | OrFilter<E['prototype']>;
|
|
76
|
+
/**
|
|
77
|
+
* Specifies the sort order to apply to the query results.
|
|
78
|
+
* This determines whether items are returned in ascending or descending order.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
*
|
|
82
|
+
* sortDirection: 'asc'
|
|
83
|
+
*/
|
|
84
|
+
sortDirection?: SortDirectionType;
|
|
85
|
+
/**
|
|
86
|
+
* Specifies the cursor representing the last item evaluated in a previous query.
|
|
87
|
+
* It is used to resume the query from the same position, allowing subsequent
|
|
88
|
+
* requests to paginate through the query results consistently.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
*
|
|
92
|
+
* {
|
|
93
|
+
* cursor: {
|
|
94
|
+
* foo: 'some',
|
|
95
|
+
* bar: 'item'
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
*/
|
|
99
|
+
cursor?: Cursor<E['prototype']>;
|
|
100
|
+
/**
|
|
101
|
+
* Specifies the maximum number of items to be retrieved by the query.
|
|
102
|
+
* This value limits the size of the result set returned by DynamoDB.
|
|
103
|
+
*/
|
|
104
|
+
limit?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Specifies the name of the index to use when executing the query.
|
|
107
|
+
* If not provided, the index that best matches the attributes defined
|
|
108
|
+
* in the key condition will be selected automatically.
|
|
109
|
+
*/
|
|
110
|
+
indexName?: string;
|
|
111
|
+
}
|
|
112
|
+
export interface QueryProps<E extends Function> extends FindProps<E> {
|
|
113
|
+
/**
|
|
114
|
+
* Specifies the key condition used to execute the query.
|
|
115
|
+
* This defines the partition key and, optionally, the sort key
|
|
116
|
+
* that DynamoDB uses to locate the matching items.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* {
|
|
120
|
+
* keyCondition: {
|
|
121
|
+
* partition: {
|
|
122
|
+
* foo: 'value'
|
|
123
|
+
* },
|
|
124
|
+
* sort: {
|
|
125
|
+
* bar: {
|
|
126
|
+
* lessThan: 10
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
*/
|
|
132
|
+
keyCondition: KeyCondition<E['prototype']>;
|
|
133
|
+
}
|
|
134
|
+
export interface QueryOneProps<E extends Function> extends Omit<QueryProps<E>, 'limit'> {
|
|
135
|
+
}
|
|
136
|
+
export interface QueryResponse<E extends Function> {
|
|
137
|
+
data: E['prototype'][];
|
|
138
|
+
cursor?: Cursor<E['prototype']>;
|
|
139
|
+
}
|
|
140
|
+
export type ObjectToBoolean<T> = {
|
|
141
|
+
[K in keyof T]?: T[K] extends number | string | boolean | Array<any> | Date ? true : ObjectToBoolean<T[K]> | true;
|
|
142
|
+
};
|
|
143
|
+
interface ExistValue<T> {
|
|
144
|
+
ifNotExistValue: T;
|
|
145
|
+
}
|
|
146
|
+
interface NumericValues<T> extends ExistValue<T> {
|
|
147
|
+
incrementValue?: number;
|
|
148
|
+
decrementValue?: number;
|
|
149
|
+
}
|
|
150
|
+
export type NumericOrExist<T> = T extends number ? number | OnlyOne<NumericValues<T>> : T extends string | boolean | Array<any> | Date ? T | ExistValue<T> : (T & {
|
|
151
|
+
ifNotExistValue?: never;
|
|
152
|
+
}) | (ExistValue<T> & {
|
|
153
|
+
[K in keyof T]?: never;
|
|
154
|
+
});
|
|
155
|
+
export type ReplaceValue<T> = {
|
|
156
|
+
[K in keyof T]?: NumericOrExist<T[K]>;
|
|
157
|
+
};
|
|
158
|
+
export type DeepReplaceValue<T> = {
|
|
159
|
+
[K in keyof T]?: T[K] extends number | string | boolean | Array<any> | Date ? NumericOrExist<T[K]> : ExistValue<T[K]> | DeepReplaceValue<T[K]>;
|
|
160
|
+
};
|
|
161
|
+
export interface UpdateProps<E extends Function> extends UpsertProps<E> {
|
|
162
|
+
/**
|
|
163
|
+
* Specifies the key condition used to execute the update query.
|
|
164
|
+
* This defines the partition key and, optionally, the sort key
|
|
165
|
+
* that DynamoDB uses to locate the matching items.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* {
|
|
169
|
+
* keyCondition: {
|
|
170
|
+
* partition: {
|
|
171
|
+
* foo: 'value'
|
|
172
|
+
* },
|
|
173
|
+
* sort: {
|
|
174
|
+
* bar: {
|
|
175
|
+
* lessThan: 10
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* }
|
|
179
|
+
* }
|
|
180
|
+
*/
|
|
181
|
+
keyCondition: TablePartition<Item<E>>;
|
|
182
|
+
/**
|
|
183
|
+
* Specifies the attribute values to be replaced on the item.
|
|
184
|
+
* Unlike full updates, this allows replacing specific parts
|
|
185
|
+
* of the item without overwriting the entire object.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* {
|
|
189
|
+
* setValues: {
|
|
190
|
+
* foo: {
|
|
191
|
+
* bar: 2 // only replace bar value in foo element
|
|
192
|
+
* }
|
|
193
|
+
* }
|
|
194
|
+
* }
|
|
195
|
+
*/
|
|
196
|
+
setValues?: DeepReplaceValue<Item<E>>;
|
|
197
|
+
/**
|
|
198
|
+
* Specifies the values to be updated on the item.
|
|
199
|
+
* When a value is an object, the entire object is replaced
|
|
200
|
+
* rather than being merged.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* {
|
|
204
|
+
* replaceValues: {
|
|
205
|
+
* foo: {
|
|
206
|
+
* bar: 1,
|
|
207
|
+
* baz: [1, 2, 3]
|
|
208
|
+
* }
|
|
209
|
+
* }
|
|
210
|
+
* }
|
|
211
|
+
*/
|
|
212
|
+
replaceValues?: ReplaceValue<Item<E>>;
|
|
213
|
+
/**
|
|
214
|
+
* Specifies which attributes should be removed from the item
|
|
215
|
+
* during the update operation.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* {
|
|
219
|
+
* removeValues: {
|
|
220
|
+
* foo: true
|
|
221
|
+
* }
|
|
222
|
+
* }
|
|
223
|
+
*/
|
|
224
|
+
removeValues?: ObjectToBoolean<Item<E>>;
|
|
225
|
+
}
|
|
226
|
+
export interface UpsertProps<E extends Function> {
|
|
227
|
+
/**
|
|
228
|
+
* Specifies the condition expression used during an item update operation.
|
|
229
|
+
* The update is only executed if the provided condition evaluates to true.
|
|
230
|
+
*
|
|
231
|
+
* condition: {
|
|
232
|
+
* foo: 'bar'
|
|
233
|
+
* }
|
|
234
|
+
*/
|
|
235
|
+
condition?: Filter<E['prototype']>;
|
|
236
|
+
}
|
|
237
|
+
export interface ModelInformation<E extends ClassResource> {
|
|
238
|
+
modelProps: ModelMetadata<E>;
|
|
239
|
+
partitionKey: string;
|
|
240
|
+
fields: FieldsMetadata;
|
|
241
|
+
sortKey?: string;
|
|
242
|
+
}
|
|
243
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ScanCommandInput } from '@aws-sdk/client-dynamodb';
|
|
2
|
+
import type { ClassResource } from '@lafken/common';
|
|
3
|
+
import { QueryBuilderBase } from '../base/base';
|
|
4
|
+
import type { QueryResponse } from '../query-builder.types';
|
|
5
|
+
import type { ScanBuilderProps } from './scan.types';
|
|
6
|
+
export declare class ScanBuilder<E extends ClassResource> extends QueryBuilderBase<E> {
|
|
7
|
+
protected queryOptions: ScanBuilderProps<E>;
|
|
8
|
+
protected command: ScanCommandInput;
|
|
9
|
+
constructor(queryOptions: ScanBuilderProps<E>);
|
|
10
|
+
getCommand(): ScanCommandInput;
|
|
11
|
+
then<T>(resolve: (value: QueryResponse<E>) => T, reject: (reason: any) => T): Promise<T>;
|
|
12
|
+
private exec;
|
|
13
|
+
private scan;
|
|
14
|
+
private runQuery;
|
|
15
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScanBuilder = void 0;
|
|
4
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
+
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
6
|
+
const base_1 = require("../base/base");
|
|
7
|
+
class ScanBuilder extends base_1.QueryBuilderBase {
|
|
8
|
+
queryOptions;
|
|
9
|
+
command;
|
|
10
|
+
constructor(queryOptions) {
|
|
11
|
+
super(queryOptions);
|
|
12
|
+
this.queryOptions = queryOptions;
|
|
13
|
+
this.scan();
|
|
14
|
+
}
|
|
15
|
+
getCommand() {
|
|
16
|
+
return this.command;
|
|
17
|
+
}
|
|
18
|
+
then(resolve, reject) {
|
|
19
|
+
return this.exec().then(resolve, reject);
|
|
20
|
+
}
|
|
21
|
+
exec() {
|
|
22
|
+
return this.runQuery(this.command);
|
|
23
|
+
}
|
|
24
|
+
scan() {
|
|
25
|
+
const { limit, cursor, filter, projection = 'ALL' } = this.queryOptions.inputProps;
|
|
26
|
+
let filterExpression;
|
|
27
|
+
if (filter) {
|
|
28
|
+
filterExpression = this.getFilterExpression(filter || {});
|
|
29
|
+
}
|
|
30
|
+
this.command = {
|
|
31
|
+
TableName: this.queryOptions.modelProps.name,
|
|
32
|
+
FilterExpression: filterExpression,
|
|
33
|
+
ExclusiveStartKey: cursor ? (0, util_dynamodb_1.marshall)(cursor) : undefined,
|
|
34
|
+
Limit: limit,
|
|
35
|
+
ProjectionExpression: projection === 'ALL' ? undefined : projection.join(', '),
|
|
36
|
+
...this.getAttributesAndNames(),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async runQuery(input, data = []) {
|
|
40
|
+
const command = new client_dynamodb_1.ScanCommand(input);
|
|
41
|
+
const { Items = [], LastEvaluatedKey } = await this.queryOptions.client.send(command);
|
|
42
|
+
const resultData = Items.map((item) => (0, util_dynamodb_1.unmarshall)(item));
|
|
43
|
+
const items = data.concat(resultData);
|
|
44
|
+
if (LastEvaluatedKey && (!input.Limit || items.length < input.Limit)) {
|
|
45
|
+
return this.runQuery({
|
|
46
|
+
...input,
|
|
47
|
+
ExclusiveStartKey: LastEvaluatedKey,
|
|
48
|
+
Limit: input.Limit ? input.Limit - items.length : undefined,
|
|
49
|
+
}, items);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
data: items,
|
|
53
|
+
cursor: LastEvaluatedKey ? (0, util_dynamodb_1.unmarshall)(LastEvaluatedKey) : undefined,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.ScanBuilder = ScanBuilder;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ClassResource } from '@lafken/common';
|
|
2
|
+
import type { QueryBuilderProps } from '../base/base.types';
|
|
3
|
+
import type { FindProps } from '../query-builder.types';
|
|
4
|
+
export interface ScanBuilderProps<E extends ClassResource> extends QueryBuilderProps<E> {
|
|
5
|
+
inputProps: FindProps<E>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type UpdateItemCommandInput } from '@aws-sdk/client-dynamodb';
|
|
2
|
+
import type { ClassResource } from '@lafken/common';
|
|
3
|
+
import { QueryBuilderBase } from '../base/base';
|
|
4
|
+
import type { UpdateBuilderProps } from './update.types';
|
|
5
|
+
export declare class UpdateBuilder<E extends ClassResource> extends QueryBuilderBase<E> {
|
|
6
|
+
protected queryOptions: UpdateBuilderProps<E>;
|
|
7
|
+
protected command: UpdateItemCommandInput;
|
|
8
|
+
constructor(queryOptions: UpdateBuilderProps<E>);
|
|
9
|
+
getCommand(): UpdateItemCommandInput;
|
|
10
|
+
then<T>(resolve: (value: boolean) => T, reject: (reason: any) => T): Promise<T>;
|
|
11
|
+
private exec;
|
|
12
|
+
protected prepare(): void;
|
|
13
|
+
private setValues;
|
|
14
|
+
private removeValues;
|
|
15
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UpdateBuilder = void 0;
|
|
4
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
+
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
6
|
+
const base_1 = require("../base/base");
|
|
7
|
+
const update_utils_1 = require("./update.utils");
|
|
8
|
+
class UpdateBuilder extends base_1.QueryBuilderBase {
|
|
9
|
+
queryOptions;
|
|
10
|
+
command;
|
|
11
|
+
constructor(queryOptions) {
|
|
12
|
+
super(queryOptions);
|
|
13
|
+
this.queryOptions = queryOptions;
|
|
14
|
+
this.prepare();
|
|
15
|
+
}
|
|
16
|
+
getCommand() {
|
|
17
|
+
return this.command;
|
|
18
|
+
}
|
|
19
|
+
then(resolve, reject) {
|
|
20
|
+
return this.exec().then(resolve, reject);
|
|
21
|
+
}
|
|
22
|
+
async exec() {
|
|
23
|
+
const command = new client_dynamodb_1.UpdateItemCommand(this.command);
|
|
24
|
+
await this.queryOptions.client.send(command);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
prepare() {
|
|
28
|
+
const { removeValues = {}, replaceValues = {}, setValues = {}, condition, } = this.queryOptions.inputProps;
|
|
29
|
+
if (Object.keys(removeValues).length > 0 &&
|
|
30
|
+
Object.keys(replaceValues).length > 0 &&
|
|
31
|
+
Object.keys(setValues).length > 0) {
|
|
32
|
+
throw new Error('You must assign a value to update');
|
|
33
|
+
}
|
|
34
|
+
let setExpression = this.setValues(setValues, true);
|
|
35
|
+
if (Object.keys(replaceValues).length > 0) {
|
|
36
|
+
setExpression += ` ${this.setValues(replaceValues, false)}`;
|
|
37
|
+
}
|
|
38
|
+
const removeExpression = this.removeValues(removeValues);
|
|
39
|
+
const keyCondition = this.queryOptions.inputProps.keyCondition;
|
|
40
|
+
this.command = {
|
|
41
|
+
TableName: this.queryOptions.modelProps.name,
|
|
42
|
+
Key: (0, util_dynamodb_1.marshall)(keyCondition),
|
|
43
|
+
UpdateExpression: `${setExpression ? `SET ${setExpression}` : ''} ${removeExpression ? `REMOVE ${removeExpression}` : ''}`.trim(),
|
|
44
|
+
...this.getAttributesAndNames(),
|
|
45
|
+
ConditionExpression: condition ? this.getFilterExpression(condition) : undefined,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
setValues(values, isDeepReplace, names = [], counter = 0) {
|
|
49
|
+
counter += 1;
|
|
50
|
+
const filterExpression = [];
|
|
51
|
+
const index = 0;
|
|
52
|
+
for (const key in values) {
|
|
53
|
+
const currentKeyNames = [...names, key];
|
|
54
|
+
const keyName = currentKeyNames.join('.#');
|
|
55
|
+
const keyValue = `${currentKeyNames.join('_')}_${counter}_${index}`;
|
|
56
|
+
this.attributeNames[`#${key}`] = key;
|
|
57
|
+
const name = `#${keyName}`;
|
|
58
|
+
if (typeof values[key] === 'object' && !Array.isArray(values[key])) {
|
|
59
|
+
const keys = Object.keys(values[key] || {});
|
|
60
|
+
const updateResolverKey = keys.find((key) => update_utils_1.updateResolverKeys.has(key));
|
|
61
|
+
if (updateResolverKey) {
|
|
62
|
+
const resolverFn = update_utils_1.updateResolver[updateResolverKey];
|
|
63
|
+
const { attributeValues, expression } = resolverFn(name, keyValue, values[key][updateResolverKey]);
|
|
64
|
+
this.attributeValues = {
|
|
65
|
+
...this.attributeValues,
|
|
66
|
+
...attributeValues,
|
|
67
|
+
};
|
|
68
|
+
filterExpression.push(expression);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (isDeepReplace) {
|
|
72
|
+
filterExpression.push(this.setValues(values[key], true, currentKeyNames, counter));
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
filterExpression.push(`#${keyName} = :${keyValue}`);
|
|
76
|
+
this.attributeValues[`:${keyValue}`] = values[key];
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
filterExpression.push(`#${keyName} = :${keyValue}`);
|
|
80
|
+
this.attributeValues[`:${keyValue}`] = values[key];
|
|
81
|
+
}
|
|
82
|
+
return filterExpression.join(',').trim();
|
|
83
|
+
}
|
|
84
|
+
removeValues(values) {
|
|
85
|
+
const removedKeys = [];
|
|
86
|
+
for (const key in values) {
|
|
87
|
+
const keyValue = `#${key}`;
|
|
88
|
+
if (typeof values[key] === 'boolean') {
|
|
89
|
+
this.attributeNames[`#${key}`] = key;
|
|
90
|
+
removedKeys.push(keyValue);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (values[key] !== undefined) {
|
|
94
|
+
const removed = this.removeValues(values[key]);
|
|
95
|
+
removedKeys.push(removed);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return removedKeys.join(',');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.UpdateBuilder = UpdateBuilder;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ClassResource } from '@lafken/common';
|
|
2
|
+
import type { QueryBuilderProps } from '../base/base.types';
|
|
3
|
+
import type { UpdateProps } from '../query-builder.types';
|
|
4
|
+
export interface UpdateBuilderProps<E extends ClassResource> extends QueryBuilderProps<E> {
|
|
5
|
+
inputProps: UpdateProps<E>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const updateResolver: {
|
|
2
|
+
incrementValue: (key: string, valueName: string, value: any) => import("../base/base.utils").ExpressionResolverResult;
|
|
3
|
+
decrementValue: (key: string, valueName: string, value: any) => import("../base/base.utils").ExpressionResolverResult;
|
|
4
|
+
ifNotExistValue: (key: string, valueName: string, value: any) => {
|
|
5
|
+
expression: string;
|
|
6
|
+
attributeValues: {
|
|
7
|
+
[x: string]: any;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare const updateResolverKeys: Set<string>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateResolverKeys = exports.updateResolver = void 0;
|
|
4
|
+
const base_utils_1 = require("../base/base.utils");
|
|
5
|
+
exports.updateResolver = {
|
|
6
|
+
incrementValue: (key, valueName, value) => {
|
|
7
|
+
return (0, base_utils_1.expressionResolver)(key, valueName, `= ${key} + `, value);
|
|
8
|
+
},
|
|
9
|
+
decrementValue: (key, valueName, value) => {
|
|
10
|
+
return (0, base_utils_1.expressionResolver)(key, valueName, `= ${key} - `, value);
|
|
11
|
+
},
|
|
12
|
+
ifNotExistValue: (key, valueName, value) => {
|
|
13
|
+
const valueResolver = `:${valueName}`;
|
|
14
|
+
return {
|
|
15
|
+
expression: `${key} = if_not_exists(${key}, ${valueResolver})`,
|
|
16
|
+
attributeValues: {
|
|
17
|
+
[valueResolver]: value,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
exports.updateResolverKeys = new Set([...Object.keys(exports.updateResolver)]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type PutItemCommandInput } from '@aws-sdk/client-dynamodb';
|
|
2
|
+
import type { ClassResource } from '@lafken/common';
|
|
3
|
+
import { QueryBuilderBase } from '../base/base';
|
|
4
|
+
import type { Item } from '../query-builder.types';
|
|
5
|
+
import type { UpsertBuilderProps } from './upsert.types';
|
|
6
|
+
export declare class UpsertBuilder<E extends ClassResource> extends QueryBuilderBase<E> {
|
|
7
|
+
private queryOptions;
|
|
8
|
+
protected command: PutItemCommandInput;
|
|
9
|
+
constructor(queryOptions: UpsertBuilderProps<E>);
|
|
10
|
+
getCommand(): PutItemCommandInput;
|
|
11
|
+
then<T>(resolve: (value: Item<E>) => T, reject: (reason: any) => T): Promise<T>;
|
|
12
|
+
private exec;
|
|
13
|
+
protected prepare(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UpsertBuilder = void 0;
|
|
4
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
+
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
6
|
+
const base_1 = require("../base/base");
|
|
7
|
+
class UpsertBuilder extends base_1.QueryBuilderBase {
|
|
8
|
+
queryOptions;
|
|
9
|
+
command;
|
|
10
|
+
constructor(queryOptions) {
|
|
11
|
+
super(queryOptions);
|
|
12
|
+
this.queryOptions = queryOptions;
|
|
13
|
+
this.prepare();
|
|
14
|
+
}
|
|
15
|
+
getCommand() {
|
|
16
|
+
return this.command;
|
|
17
|
+
}
|
|
18
|
+
then(resolve, reject) {
|
|
19
|
+
return this.exec().then(resolve, reject);
|
|
20
|
+
}
|
|
21
|
+
async exec() {
|
|
22
|
+
const command = new client_dynamodb_1.PutItemCommand(this.command);
|
|
23
|
+
await this.queryOptions.client.send(command);
|
|
24
|
+
return this.queryOptions.item;
|
|
25
|
+
}
|
|
26
|
+
prepare() {
|
|
27
|
+
const { condition } = this.queryOptions.inputProps;
|
|
28
|
+
let conditionExpression;
|
|
29
|
+
if (condition) {
|
|
30
|
+
conditionExpression = this.getFilterExpression(condition);
|
|
31
|
+
}
|
|
32
|
+
this.command = {
|
|
33
|
+
TableName: this.queryOptions.modelProps.name,
|
|
34
|
+
Item: (0, util_dynamodb_1.marshall)(this.queryOptions.item),
|
|
35
|
+
ConditionExpression: conditionExpression,
|
|
36
|
+
...this.getAttributesAndNames(),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.UpsertBuilder = UpsertBuilder;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ClassResource } from '@lafken/common';
|
|
2
|
+
import type { QueryBuilderProps } from '../base/base.types';
|
|
3
|
+
import type { Item, UpsertProps } from '../query-builder.types';
|
|
4
|
+
export interface UpsertBuilderProps<E extends ClassResource> extends QueryBuilderProps<E> {
|
|
5
|
+
item: Item<E>;
|
|
6
|
+
inputProps: UpsertProps<E>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./repository"), exports);
|
|
18
|
+
__exportStar(require("./repository.types"), exports);
|
|
19
|
+
__exportStar(require("./repository.utils"), exports);
|