-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_by_id.ts
677 lines (637 loc) · 20.5 KB
/
update_by_id.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import { UpdateItemCommand, UpdateItemInput } from '@aws-sdk/client-dynamodb';
import get from 'lodash/get';
import createDebug from 'debug';
import { Context } from '../context';
import {
getRootCollection,
assemblePrimaryKeyValue,
unwrap,
assembleIndexedValue,
findMatchingPath,
transformTTLValue,
} from '../base/util';
import { KeyPath } from '../base/access_pattern';
import { WrappedDocument, DocumentWithId, Key, DynamoDBSet } from '../base/common';
import {
InvalidUpdatesException,
InvalidUpdateValueException,
IndexNotFoundException,
} from '../base/exceptions';
import { Collection } from '../base/collection';
import { SecondaryIndexLayout } from '../base/layout';
import debugDynamo from '../debug/debugDynamo';
import {
createNameMapper,
createValueMapper,
NameMapper,
ValueMapper,
} from '../base/mappers';
import { CompositeCondition } from '../base/conditions';
import { parseCompositeCondition } from '../base/conditions_parser';
import { isEqualKey } from './find';
import { unmarshall } from '@aws-sdk/util-dynamodb';
/** @internal */
const debug = createDebug('dynaglue:operations:updateById');
/**
* An update object, where the key paths are specified as keys and the values
* as the new field values.
*
* Keep in mind, that although this is a partial update, you need to specify
* all the keys for a composite key in an access pattern - it cannot partially
* update composite index values.
*/
export type SetValuesDocument = {
[path: string]: unknown;
};
/**
* A key path and value to set on a document as part
* of an update operation. Specified as part of [[UpdateChangesDocument]],
* this is a tuple in the form:
*
* `[key_path, new_value]`
*
* * *key_path* is the property path into the document to update (either
* as a dotted string path e.g. `'profile.name'` or a [[KeyPath]])
* * *new_value* is the value to update the property path to (it cannot be
* undefined -- if you want to clear the value, see [[DeleteChange]])
*/
export type SetChange = [string | KeyPath, unknown];
/**
* A property path to delete on a document as part of a [[UpdateChangesDocument]],
* specified either as a dotted string path (e.g. `'profile.name'`) or a [[KeyPath]]
*/
export type RemoveChange = string | KeyPath;
/**
* Add a number to a number property. Use negative values to subtract.
*/
export type AddValueChange = [string | KeyPath, number]
/**
* Append or delete the values from from to a set property.
*/
export type AppendDeleteSetChange = [string | KeyPath, DynamoDBSet];
/**
* A set of changes to perform in an [[updateById]] or [[updateChildById]]
* operation as an object. Each property is optional, but at
* least one change must be specified.
*/
export type OperationUpdates = {
/**
* The list of key paths to set a value. This is a list
* of tuples, in the form [key_path, new_value]. The
* key paths can be specified in a string form e.g. `'profile.name'`
* or array form e.g. `['profile', 'name']`
*/
$set?: SetChange[];
/**
* The list of key paths to clear the value. Key paths
* may be specified in string form e.g. `'profile.name'` or
* array form e.g. `['profile', 'name']`
*/
$remove?: RemoveChange[];
/**
* A set of key path + value pairs for which number
* properties are to be added to.
*
* Key paths are specified in string (e.g. `profile.count`) or array (e.g. `['profile', 'count']) form.
*/
$addValue?: AddValueChange[];
/**
* A set of key path + value pairs for which set
* properties are to be appended.
*
* Key paths are specified in string (e.g. `profile.roles`) or array (e.g. `['profile', 'roles']) form.
*/
$addToSet?: AppendDeleteSetChange[];
/**
* A set of key path + value pairs for which set
* properties are to be appended.
*
* Key paths are specified in string (e.g. `profile.roles`) or array (e.g. `['profile', 'roles']) form.
*/
$deleteFromSet?: AppendDeleteSetChange[];
};
export type ChangesUpdates = {
$setValues: SetValuesDocument;
}
/**
* The set of updates to apply to a document. This can be
* specified one of two ways:
*
* * an an object of key paths to values to set e.g. `{ 'profile.name': 'new name', 'status': 3 }`
* * an operator object of changes to perform (see [[UpdateChangesDocument]])
*/
export type Updates = OperationUpdates | ChangesUpdates;
const makeKeyPath = (pathOrPathArray: string | KeyPath): KeyPath =>
typeof pathOrPathArray === 'string'
? pathOrPathArray.split('.')
: pathOrPathArray;
/** @internal */
export type StrictSetChange = [KeyPath, any];
/** @internal */
export type StrictDeleteChange = KeyPath;
/** @internal */
export type StrictAppendDeleteSetChange = [KeyPath, DynamoDBSet];
/** @internal */
export type StrictAddValueChange = [KeyPath, number];
/** @internal */
export type StrictChangesDocument = {
$set: StrictSetChange[];
$delete: StrictDeleteChange[];
$addToSet: StrictAppendDeleteSetChange[];
$deleteFromSet: StrictAppendDeleteSetChange[];
$addValue: StrictAddValueChange[];
};
/**
* @internal
* Convert the Updates object to something normalised that
* is simpler to process internally
*/
export const normaliseUpdates = (
updatesToPerform: Updates
): StrictChangesDocument => {
if (
'$set' in updatesToPerform ||
'$remove' in updatesToPerform ||
'$addValue' in updatesToPerform ||
'$addToSet' in updatesToPerform ||
'$deleteFromSet' in updatesToPerform
) {
const changesDocument = updatesToPerform as OperationUpdates;
return {
$set:
changesDocument.$set?.map(([path, value]) => [
makeKeyPath(path),
value,
]) ?? [],
$delete: changesDocument.$remove?.map(makeKeyPath) ?? [],
$addToSet:
changesDocument.$addToSet?.map(([path, value]) => [
makeKeyPath(path),
value,
]) ?? [],
$deleteFromSet:
changesDocument.$deleteFromSet?.map(([path, value]) => [
makeKeyPath(path),
value,
]) ?? [],
$addValue:
changesDocument.$addValue?.map(([path, value]) => [
makeKeyPath(path),
value,
]) ?? [],
};
} else if ('$setValues' in updatesToPerform) {
const updatesDocument = updatesToPerform.$setValues;
return {
$set: Object.entries(updatesDocument).map(([key, value]) => [
makeKeyPath(key),
value,
]),
$delete: [],
$addToSet: [],
$deleteFromSet: [],
$addValue: [],
};
} else {
throw new InvalidUpdatesException('Unknown change set provided for update: must be one of OperationUpdates or ChangesUpdates');
}
};
/**
* @internal
*/
export const extractUpdateKeyPaths = (
changes: StrictChangesDocument
): KeyPath[] => [...changes.$set.map(([path]) => path), ...changes.$delete];
/**
* @internal
*/
export const getValueForUpdatePath = (
matchingUpdatePath: KeyPath,
keyPath: KeyPath,
changes: StrictChangesDocument
): any => {
// Work out if this was a $set update (in which case we get the index) or
// a delete path (which we perform by deduction)
const setPathIndex = changes.$set.findIndex(([setPath]) =>
isEqualKey(setPath, matchingUpdatePath)
);
let value = setPathIndex >= 0 ? changes.$set[setPathIndex][1] : undefined;
if (setPathIndex >= 0 && keyPath.length !== matchingUpdatePath.length) {
const difference = keyPath.slice(matchingUpdatePath.length);
value = get(value, difference);
}
return value;
};
/**
* @internal
*/
export const createUpdateActionForKey = (
collectionName: string,
keyType: 'partition' | 'sort',
keyPaths: KeyPath[],
indexLayout: SecondaryIndexLayout,
changes: StrictChangesDocument,
separator?: string
):
| { attributeName: string; value?: string; valueErasure: boolean }
| undefined => {
const updateKeyPaths = extractUpdateKeyPaths(changes);
const matchingUpdatePaths = keyPaths.map((partitionKey) =>
findMatchingPath(updateKeyPaths, partitionKey)
);
const attributeName =
keyType === 'sort'
? (indexLayout.sortKey as string)
: indexLayout.partitionKey;
debug(
'createUpdateActionForKey: collection=%s keyType=%s keyPaths=%o attributeName=%s',
collectionName,
keyType,
keyPaths,
attributeName
);
if (
matchingUpdatePaths.every((updatePath) => typeof updatePath === 'undefined')
) {
debug(
'createUpdateActionForKey: no updates to %s key in collection %s',
keyType,
collectionName
);
return undefined;
}
debug(
'createUpdateActionForKey: key to be updated matchingUpdatePaths=%o',
matchingUpdatePaths
);
const updateValues = keyPaths.map((keyPath, index) => {
const matchingUpdatePath = matchingUpdatePaths[index];
if (!matchingUpdatePath) {
return undefined;
}
return getValueForUpdatePath(matchingUpdatePath, keyPath, changes);
});
return {
attributeName,
value: assembleIndexedValue(
keyType,
collectionName,
updateValues,
separator
),
valueErasure: updateValues.every((value) => typeof value === 'undefined'),
};
};
/**
* @internal
*
* Create the update action for a TTL key path
*/
export const createUpdateActionForTTLKey = (
attributeName: string,
keyPath: KeyPath,
updates: StrictChangesDocument
): { attributeName: string; value?: number } | undefined => {
const updateKeyPaths = extractUpdateKeyPaths(updates);
const matchingUpdatePath = findMatchingPath(updateKeyPaths, keyPath);
if (matchingUpdatePath) {
const value = getValueForUpdatePath(matchingUpdatePath, keyPath, updates);
return {
attributeName,
value: value ? transformTTLValue(value) : undefined,
};
}
return undefined;
};
/**
* @internal
*/
export const findCollectionIndex = (
collection: Collection,
indexName: string
): SecondaryIndexLayout => {
const layout = collection.layout.findKeys?.find(
(fk) => fk.indexName === indexName
);
if (!layout) {
throw new IndexNotFoundException(indexName);
}
return layout;
};
/**
* @internal
*/
export type Action = {
action: string;
expressionAttributeValue: [string, any];
expressionAttributeNames: [string, string][];
};
/**
* @internal
*
* Given the set of updates, create the SET and DELETE
* actions for the access patterns that also have to be
* changed.
*/
export const mapAccessPatterns = (
collection: Collection,
{
nameMapper,
valueMapper,
}: { nameMapper: NameMapper; valueMapper: ValueMapper },
changes: StrictChangesDocument
): {
setActions: string[];
removeActions: string[];
} => {
const expressionSetActions: string[] = [];
const expressionRemoveActions: string[] = [];
const { accessPatterns = [], ttlKeyPath } = collection;
for (const { indexName, partitionKeys, sortKeys } of accessPatterns) {
let partitionKeyUpdateSet: boolean | undefined = undefined;
let sortKeyUpdated = false;
const layout = findCollectionIndex(collection, indexName);
if (partitionKeys.length > 0) {
const update = createUpdateActionForKey(
collection.name,
'partition',
partitionKeys,
layout,
changes,
collection.layout.indexKeySeparator
);
if (update) {
partitionKeyUpdateSet = !update.valueErasure;
debug(
'mapAccessPatterns: adding set/delete action for partition key in collection %s: %o',
collection.name,
update
);
const nameMapping = nameMapper.map(update.attributeName);
const valueMapping = valueMapper.map(update.value);
expressionSetActions.push(`${nameMapping} = ${valueMapping}`);
}
}
if (sortKeys && sortKeys.length > 0) {
const update = createUpdateActionForKey(
collection.name,
'sort',
sortKeys,
layout,
changes,
collection.layout.indexKeySeparator
);
if (update) {
debug(
'mapAccessPatterns: adding set/delete action for sort key in collection %s: %o',
collection.name,
update
);
sortKeyUpdated = true;
if (typeof update.value !== 'undefined') {
const nameMapping = nameMapper.map(update.attributeName);
const valueMapping = valueMapper.map(update.value);
expressionSetActions.push(`${nameMapping} = ${valueMapping}`);
} else {
const nameMapping = nameMapper.map(update.attributeName);
expressionRemoveActions.push(nameMapping);
}
}
} else if (typeof partitionKeyUpdateSet !== 'undefined' && layout.sortKey) {
// When only primary key has indexed paths (i.e. partitionKeys.length > 0,
// sortKeys.length === 0, and we applied an update for the partitionKey)
// make an empty update to the sort key to in-case it wasn't populated
const nameMapping = nameMapper.map(layout.sortKey);
if (partitionKeyUpdateSet) {
const valueMapping = valueMapper.map(
assembleIndexedValue(
'sort',
collection.name,
[],
collection.layout.indexKeySeparator
)
);
expressionSetActions.push(`${nameMapping} = ${valueMapping}`);
} else {
expressionRemoveActions.push(nameMapping);
}
}
// In the case the sort key is updated but there is no indexed partition key
// paths, make sure the partition key gets a value written
if (sortKeyUpdated && (!partitionKeys || partitionKeys.length === 0)) {
const nameMapping = nameMapper.map(layout.partitionKey);
const valueMapping = valueMapper.map(
assembleIndexedValue(
'partition',
collection.name,
[],
collection.layout.indexKeySeparator
)
);
expressionSetActions.push(`${nameMapping} = ${valueMapping}`);
}
}
if (ttlKeyPath) {
const updateAction = createUpdateActionForTTLKey(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
collection.layout.ttlAttribute!, // we've already asserted this in context creation
ttlKeyPath,
changes
);
if (updateAction) {
const nameMapping = nameMapper.map(updateAction.attributeName);
if (updateAction.value) {
const valueMapping = valueMapper.map(updateAction.value);
expressionSetActions.push(`${nameMapping} = ${valueMapping}`);
} else {
expressionRemoveActions.push(nameMapping);
}
}
}
return {
setActions: expressionSetActions,
removeActions: expressionRemoveActions,
};
};
/**
* @internal
*
* Performs an update operation for the given collection and key
* value. Shares most of the code between updateById and updateChildById
*
*/
export async function updateInternal<DocumentType extends DocumentWithId>(
ctx: Context,
collection: Collection,
key: Key,
updatesToPerform: Updates,
options: { condition?: CompositeCondition }
): Promise<DocumentType> {
const changes = normaliseUpdates(updatesToPerform);
if (
changes.$set.length === 0 &&
changes.$delete.length === 0 &&
changes.$addToSet.length === 0 &&
changes.$addValue.length === 0 &&
changes.$deleteFromSet.length === 0
) {
throw new InvalidUpdatesException(
'There must be at least one update path in the updates object'
);
}
const nameMapper = createNameMapper();
const valueMapper = createValueMapper();
let expressionSetActions: string[] = [];
let expressionRemoveActions: string[] = [];
const expressionAddActions: string[] = [];
const expressionDeleteActions: string[] = [];
for (const [path, newValue] of changes.$set.values()) {
if (typeof newValue === 'undefined') {
throw new InvalidUpdateValueException(
path.join('.'),
'value must not be undefined'
);
}
const valueName = valueMapper.map(newValue);
const expressionAttributeNameParts = [
nameMapper.map('value', '#value'),
...path.map((part) => nameMapper.map(part)),
];
expressionSetActions.push(
`${expressionAttributeNameParts.join('.')} = ${valueName}`
);
}
for (const path of changes.$delete) {
const expressionAttributeNameParts = [
nameMapper.map('value', '#value'),
...path.map((part) => nameMapper.map(part)),
];
expressionRemoveActions.push(`${expressionAttributeNameParts.join('.')}`);
}
for (const [path, val] of changes.$addToSet) {
const expressionAttributeNameParts = [
nameMapper.map('value', '#value'),
...path.map((part) => nameMapper.map(part)),
];
const valueName = valueMapper.map(val);
expressionAddActions.push(
`${expressionAttributeNameParts.join('.')} ${valueName}`
);
}
for (const [path, val] of changes.$deleteFromSet) {
const expressionAttributeNameParts = [
nameMapper.map('value', '#value'),
...path.map((part) => nameMapper.map(part)),
];
const valueName = valueMapper.map(val);
expressionDeleteActions.push(
`${expressionAttributeNameParts.join('.')} ${valueName}`
);
}
for (const [path, val] of changes.$addValue) {
const expressionAttributeNameParts = [
nameMapper.map('value', '#value'),
...path.map((part) => nameMapper.map(part)),
];
const valueName = valueMapper.map(val);
expressionAddActions.push(
`${expressionAttributeNameParts.join('.')} ${valueName}`
);
}
const {
setActions: additionalSetActions,
removeActions: additionalRemoveActions,
} = mapAccessPatterns(collection, { nameMapper, valueMapper }, changes);
expressionSetActions = [...expressionSetActions, ...additionalSetActions];
expressionRemoveActions = [
...expressionRemoveActions,
...additionalRemoveActions,
];
let conditionExpression;
if (options.condition) {
conditionExpression = parseCompositeCondition(options.condition, {
nameMapper,
valueMapper,
parsePath: [],
});
}
const expressionAttributeNames = nameMapper.get();
const expressionAttributeValues = valueMapper.get();
const updateExpression =
(expressionSetActions.length
? ` SET ${expressionSetActions.join(', ')}`
: '') +
(expressionRemoveActions.length
? ` REMOVE ${expressionRemoveActions.join(', ')}`
: '') +
(expressionAddActions.length
? ` ADD ${expressionAddActions.join(', ')}`
: '') +
(expressionDeleteActions.length
? ` DELETE ${expressionDeleteActions.join(', ')}`
: '');
const updateItem: UpdateItemInput = {
TableName: collection.layout.tableName,
Key: key,
ReturnValues: 'ALL_NEW',
ExpressionAttributeNames: expressionAttributeNames,
ExpressionAttributeValues: expressionAttributeValues,
UpdateExpression: updateExpression.trim(),
ConditionExpression: conditionExpression,
};
debugDynamo('UpdateItem', updateItem);
const command = new UpdateItemCommand(updateItem);
const result = await ctx.ddb.send(command);
const unmarshalledAttributes = unmarshall(result.Attributes!);
const updatedDocument = unwrap(
unmarshalledAttributes as WrappedDocument<DocumentType>
);
return updatedDocument;
}
/**
* Update a document using its `_id`.
*
* This operation allows you to do a partial update of a collection document i.e. without
* specifying all the values (it uses DynamoDB`s `UpdateItem` operation).
*
* At this time, the `updates` value just updates specified key paths on the target document.
*
* If some of the update key paths are indexed values, the indexes will also be updated. Because
* of this, you must specify all the key values in an access pattern to ensure indexes are
* updated consistently.
*
* @param ctx the context
* @param collectionName the collection to update
* @param objectId the `_id` value of the object to update
* @param updates the set of updates to apply.
* @returns the updated object value in its entirety.
* @throws {CollectionNotFoundException} collection not found
* @throws {InvalidUpdatesException} thrown when the updates object is invalid or incomplete
* @throws {InvalidUpdateValueException} thrown when one of the update values is an invalid type
*/
export async function updateById<DocumentType extends DocumentWithId>(
ctx: Context,
collectionName: string,
objectId: string,
updates: Updates,
options: { condition?: CompositeCondition } = {}
): Promise<DocumentType> {
const collection = getRootCollection(ctx, collectionName);
const key = {
[collection.layout.primaryKey.partitionKey]: {
S: assemblePrimaryKeyValue(
collectionName,
objectId,
collection.layout.indexKeySeparator
),
},
[collection.layout.primaryKey.sortKey]: {
S: assemblePrimaryKeyValue(
collectionName,
objectId,
collection.layout.indexKeySeparator
),
},
};
return updateInternal(ctx, collection, key, updates, options);
}