forked from dynamodb-toolbox/dynamodb-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.ts
1487 lines (1229 loc) · 51.9 KB
/
Table.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* DynamoDB Toolbox: A simple set of tools for working with Amazon DynamoDB
* @author Jeremy Daly <[email protected]>
* @license MIT
*/
// TODO: Check duplicate entity names code
// Import libraries, types, and classes
import Entity from './Entity'
import DynamoDb, { DocumentClient } from 'aws-sdk/clients/dynamodb'
import { parseTable, ParsedTable } from '../lib/parseTable'
import parseFilters from '../lib/expressionBuilder'
import validateTypes from '../lib/validateTypes'
import { FilterExpressions } from '../lib/expressionBuilder'
import parseProjections, { ProjectionAttributes, ProjectionAttributesTable } from '../lib/projectionBuilder'
import { ParsedEntity } from '../lib/parseEntity'
// Import standard error handler
import { error, conditonError } from '../lib/utils'
import { Document } from 'aws-sdk/clients/textract'
// Declare Table types
export interface TableConstructor {
name: string
alias?: string | null
partitionKey: string
sortKey?: string | null
entityField?: boolean | string
attributes?: TableAttributes
indexes?: TableIndexes
autoExecute?: boolean
autoParse?: boolean
DocumentClient?: DynamoDb.DocumentClient
entities?: {} // improve - not documented
removeNullAttributes?: boolean
}
export type DynamoDBTypes = 'string' | 'boolean' | 'number' | 'list' | 'map' | 'binary' | 'set'
export type DynamoDBSetTypes = 'string' | 'number' | 'binary'
export interface executeParse {
execute?: boolean
parse?: boolean
}
export interface TableAttributeConfig {
type: DynamoDBTypes,
setType?: DynamoDBSetTypes
}
export interface TableAttributes {
[attr: string]: DynamoDBTypes | TableAttributeConfig
}
export interface ParsedTableAttribute {
[attr: string]: TableAttributeConfig & { mappings: {} }
}
export interface TableIndexes {
[index: string]: { partitionKey?: string; sortKey?: string }
}
export interface queryOptions {
index?: string
limit?: number
reverse?: boolean
consistent?: boolean
capacity?: DocumentClient.ReturnConsumedCapacity
select?: DocumentClient.Select
eq?: string | number
lt?: string | number
lte?: string | number
gt?: string | number
gte?: string | number
between?: [string, string] | [number, number]
beginsWith?: string
filters?: FilterExpressions
attributes?: ProjectionAttributes
startKey?: {}
entity?: string
execute?: boolean
parse?: boolean
}
export interface scanOptions {
index?: string
limit?: number
consistent?: boolean
capacity?: DocumentClient.ReturnConsumedCapacity
select?: DocumentClient.Select
filters?: FilterExpressions
attributes?: ProjectionAttributes
startKey?: {}
segments?: number
segment?: number
entity?: string
execute?: boolean
parse?: boolean
}
interface batchGetOptions {
consistent?: boolean
capacity?: DocumentClient.ReturnConsumedCapacity
attributes?: ProjectionAttributes
include?: string[]
execute?: boolean
parse?: boolean
}
interface batchGetParamsMeta {
payload: any
Tables: { [key: string]: Table }
EntityProjections: { [key: string]: any },
TableProjections: { [key:string]: string[] }
}
interface batchWriteOptions {
capacity?: DocumentClient.ReturnConsumedCapacity
metrics?: DocumentClient.ReturnItemCollectionMetrics
execute?: boolean
parse?: boolean
}
interface transactGetParamsOptions {
capacity?: DocumentClient.ReturnConsumedCapacity
}
type transactGetOptions = transactGetParamsOptions & executeParse
interface transactWriteParamsOptions {
capacity?: DocumentClient.ReturnConsumedCapacity
metrics?: DocumentClient.ReturnItemCollectionMetrics
token?: string
}
type transactWriteOptions = transactWriteParamsOptions & executeParse
interface transactGetParamsMeta {
Entities: (any | undefined)[]
payload: DocumentClient.TransactGetItemsInput
}
// Declare Table class
class Table {
private _execute: boolean = true
private _parse: boolean = true
public _removeNulls: boolean = true
private _docClient?: DocumentClient
private _entities: string[] = []
public Table!: ParsedTable['Table']
public name!: string
public alias?: string
[key:string]: any
// Declare constructor (table config and optional entities)
constructor(table: TableConstructor) {
// Sanity check the table definition
if (typeof table !== 'object' || Array.isArray(table))
error('Please provide a valid table definition')
// Parse the table and merge into this
Object.assign(this,parseTable(table))
} // end constructor
// Sets the auto execute mode (default to true)
set autoExecute(val) { this._execute = typeof val === 'boolean' ? val : true }
// Gets the current auto execute mode
get autoExecute() { return this._execute }
// Sets the auto parse mode (default to true)
set autoParse(val) { this._parse = typeof val === 'boolean' ? val : true }
// Gets the current auto execute mode
get autoParse() { return this._parse }
// Sets the auto execute mode (default to true)
set removeNullAttributes(val) { this._removeNulls = typeof val === 'boolean' ? val : true }
// Gets the current auto execute mode
get removeNullAttributes() { return this._removeNulls }
// Retrieves the document client
get DocumentClient() { return this._docClient }
// Validate and sets the document client (extend with options.convertEmptyValues because it's not typed)
set DocumentClient(docClient: (DocumentClient & { options?: { convertEmptyValues: boolean}}) | undefined) {
// If a valid document client
if (docClient) {
// Automatically set convertEmptyValues to true, unless false
if (docClient.options!.convertEmptyValues !== false)
docClient.options!.convertEmptyValues = true
this._docClient = docClient
} else {
error('Invalid DocumentClient')
}
} // end DocumentClient
/**
* Adds an entity to the table
* @param {Entity|Entity[]} Entity - An Entity or array of Entities to add to the table.
*/
addEntity(entity: ParsedEntity | ParsedEntity[]) {
// Coerce entity to array
let entities = Array.isArray(entity) ? entity : [entity]
// Loop through entities
for (let i in entities) {
let entity = entities[i]
// If an instance of Entity, add it
if (entity instanceof Entity) {
// Check for existing entity name
if (this._entities && this._entities.includes(entity.name)) {
error(`Entity name '${entity.name}' already exists`)
}
// Generate the reserved words list
const reservedWords = Object.getOwnPropertyNames(this)
.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(this)))
// Check for reserved word
if (reservedWords.includes(entity.name)) {
error(`'${entity.name}' is a reserved word and cannot be used to name an Entity`)
}
// Check for sortKeys (if applicable)
if (!this.Table.sortKey && entity.schema.keys.sortKey) {
error(`${entity.name} entity contains a sortKey, but the Table does not`)
} else if (this.Table.sortKey && !entity.schema.keys.sortKey) {
error(`${entity.name} entity does not have a sortKey defined`)
}
// Process Entity index keys
for (const key in entity.schema.keys) {
// Set the value of the key
const attr = entity.schema.keys[key]
// Switch based on key type (pk, sk, or index)
switch(key) {
// For the primary index
case 'partitionKey':
case 'sortKey':
// If the attribute's name doesn't match the table's pk/sk name
if (attr !== this.Table[key] && this.Table[key]) {
// If the table's index attribute name does not conflict with another entity attribute
if (!entity.schema.attributes[this.Table[key]!]) { // FIX: better way to do this?
// Add the attribute using the same config and add alias
entity.schema.attributes[this.Table[key]!] = Object.assign(
{},
entity.schema.attributes[attr],
{ alias: attr }
) // end assign
// Add a map from the attribute to the new index attribute
entity.schema.attributes[attr].map = this.Table[key]
// Otherwise, throw an error
} else {
error(`The Table's ${key} name (${this.Table[key]}) conflicts with an Entity attribute name`)
} // end if-else
} // end if
break
// For secondary indexes
default:
// Verify that the table has this index
if (!this.Table.indexes[key]) error(`'${key}' is not a valid secondary index name`)
// Loop through the key types (pk/sk) defined in the key mapping
for (const keyType in attr) {
// Make sure the table index contains the defined key types
// @ts-ignore
if (!this.Table.indexes[key][keyType])
error(`${entity.name} contains a ${keyType}, but it is not used by ${key}`)
// console.log(key,keyType,this.Table.indexes[key])
// If the attribute's name doesn't match the indexes attribute name
// @ts-ignore
if (attr[keyType] !== this.Table.indexes[key][keyType]) {
// If the indexes attribute name does not conflict with another entity attribute
// @ts-ignore
if (!entity.schema.attributes[this.Table.indexes[key][keyType]]) {
// If there is already a mapping for this attribute, make sure they match
// TODO: Figure out if this is even possible anymore. I don't think it is.
if (entity.schema.attributes[attr[keyType]].map
// @ts-ignore
&& entity.schema.attributes[attr[keyType]].map !== this.Table.indexes[key][keyType])
error(`${key}'s ${keyType} cannot map to the '${attr[keyType]}' alias because it is already mapped to another table attribute`)
// Add the index attribute using the same config and add alias
// @ts-ignore
entity.schema.attributes[this.Table.indexes[key][keyType]] = Object.assign(
{},
entity.schema.attributes[attr[keyType]],
{ alias: attr[keyType] }
) // end assign
// Add a map from the attribute to the new index attribute
// @ts-ignore
entity.schema.attributes[attr[keyType]].map = this.Table.indexes[key][keyType]
} else {
// @ts-ignore
const config = entity.schema.attributes[this.Table.indexes[key][keyType]]
// If the existing attribute isn't used by this index
if (
(!config.partitionKey && !config.sortKey)
|| (config.partitionKey && !config.partitionKey.includes(key))
|| (config.sortKey && !config.sortKey.includes(key))
) {
// @ts-ignore
error(`${key}'s ${keyType} name (${this.Table.indexes[key][keyType]}) conflicts with another Entity attribute name`)
} // end if
} // end if-else
} // end if
} // end for
// Check that composite keys define both keys
// TODO: This only checks for the attribute, not the explicit assignment
if (this.Table.indexes[key].partitionKey && this.Table.indexes[key].sortKey
&& (
!entity.schema.attributes[this.Table.indexes[key].partitionKey!]
|| !entity.schema.attributes[this.Table.indexes[key].sortKey!]
)) {
error(`${key} requires mappings for both the partitionKey and the sortKey`)
}
break
} // end switch
} // end for
// Loop through the Entity's attributes and validate their types against the Table definition
// Add attribute to table if not defined
for (let attr in entity.schema.attributes) {
// If an entity field conflicts with the entityField or its alias, throw an error
if (this.Table.entityField && (attr === this.Table.entityField || attr === entity._etAlias)) {
error(`Attribute or alias '${attr}' conflicts with the table's 'entityField' mapping or entity alias`)
// If the atribute already exists in the table definition
} else if (this.Table.attributes[attr]) {
// If type is specified, check for attribute match
if (this.Table.attributes[attr].type
&& this.Table.attributes[attr].type !== entity.schema.attributes[attr].type)
error(`${entity.name} attribute type for '${attr}' (${entity.schema.attributes[attr].type}) does not match table's type (${this.Table.attributes[attr].type})`)
// Add entity mappings
this.Table.attributes[attr].mappings[entity.name] = Object.assign({
[entity.schema.attributes[attr].alias || attr]: entity.schema.attributes[attr].type
},
// Add setType if type 'set'
entity.schema.attributes[attr].type === 'set'
? { _setType: entity.schema.attributes[attr].setType }
: {}
)
// else if the attribute doesn't exist
} else if (!entity.schema.attributes[attr].map) {
// Add type and entity map
this.Table.attributes[attr] = Object.assign(
{
mappings: {
[entity.name]: Object.assign({
[entity.schema.attributes[attr].alias || attr]: entity.schema.attributes[attr].type
},
// Add setType if type 'set'
entity.schema.attributes[attr].type === 'set'
? { _setType: entity.schema.attributes[attr].setType }
: {}
)
}
},
entity.schema.attributes[attr].partitionKey || entity.schema.attributes[attr].sortKey
? { type: entity.schema.attributes[attr].type } : null
) // end assign
} // end if-else Table attribute exists
} // end for loop to check/add attributes
// Add the Entity to the Table's entities list
this._entities.push(entity.name)
// Add the entity to the Table object
this[entity.name] = entity
// Set the Entity's table by reference
entity.table = this
} else {
error('Invalid Entity')
}
} // end for
} // end addEntity
// Deprecation notice
set entities(entity: any) {
this.addEntity(entity)
//error(`Setting entities by assignment has been deprecated. Please use 'addEntity' instead.`)
}
get entities() {
return this._entities
}
// ----------------------------------------------------------------//
// Table actions
// ----------------------------------------------------------------//
async query(
pk: any,
options: queryOptions = {},
params: Partial<DocumentClient.QueryInput> = {}
) {
// Generate query parameters with projection data
const {
payload,
EntityProjections,
TableProjections
} = this.queryParams(pk,options,params,true)
// If auto execute enabled
if (options.execute || (this.autoExecute && options.execute !== false)) {
const result = await this.DocumentClient!.query(payload).promise()
// If auto parse enable
if (options.parse || (this.autoParse && options.parse !== false)) {
return Object.assign(
result,
{
Items: result.Items && result.Items.map(item => {
if (this[item[String(this.Table.entityField)]]) {
return this[item[String(this.Table.entityField)]].parse(
item,
// Array.isArray(options.omit) ? options.omit : [],
EntityProjections[item[String(this.Table.entityField)]] ? EntityProjections[item[String(this.Table.entityField)]]
: TableProjections ? TableProjections
: []
)
} else {
return item
}
})
},
// If last evaluated key, return a next function
result.LastEvaluatedKey ? {
next: () => {
return this.query(
pk,
Object.assign(options, { startKey: result.LastEvaluatedKey }),
params
)
}
} : null
)
} else {
return result
}
} else {
return payload
} // end if-else
}
// Query the table
queryParams(
pk: any,
options: queryOptions = {},
params: Partial<DocumentClient.QueryInput> = {},
projections = false
) {
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions
// Deconstruct valid options
const {
index,
limit,
reverse, // ScanIndexForward
consistent, // ConsistentRead (boolean)
capacity, // ReturnConsumedCapacity (none, total, or indexes)
select, // Select (all_attributes, all_projected_attributes, specific_attributes, count)
eq, // =
lt, // <
lte, // <=
gt, // >
gte, // >=
between, // between
beginsWith, // begins_with,
filters, // filter object,
attributes, // Projections
startKey,
entity, // optional entity name to filter aliases
..._args // capture extra arguments
} = options
// Remove other valid options from options
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
// Error on extraneous arguments
if (args.length > 0)
error(`Invalid query options: ${args.join(', ')}`)
// Verify pk
if ((typeof pk !== 'string' && typeof pk !== 'number') || (typeof pk === 'string' && pk.trim().length === 0))
error(`Query requires a string, number or binary 'partitionKey' as its first parameter`)
// Verify index
if (index !== undefined && !this.Table.indexes[index])
error(`'${index}' is not a valid index name`)
// Verify limit
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0))
error(`'limit' must be a positive integer`)
// Verify reverse
if (reverse !== undefined && typeof reverse !== 'boolean')
error(`'reverse' requires a boolean`)
// Verify consistent read
if (consistent !== undefined && typeof consistent !== 'boolean')
error(`'consistent' requires a boolean`)
// Verify select
// TODO: Make dependent on whether or not an index is supplied
if (select !== undefined
&& (typeof select !== 'string'
|| !['ALL_ATTRIBUTES', 'ALL_PROJECTED_ATTRIBUTES', 'SPECIFIC_ATTRIBUTES', 'COUNT'].includes(select.toUpperCase())))
error(`'select' must be one of 'ALL_ATTRIBUTES', 'ALL_PROJECTED_ATTRIBUTES', 'SPECIFIC_ATTRIBUTES', OR 'COUNT'`)
// Verify entity
if (entity !== undefined && (typeof entity !== 'string' || !(entity in this)))
error(`'entity' must be a string and a valid table Entity name`)
// Verify capacity
if (capacity !== undefined
&& (typeof capacity !== 'string' || !['NONE','TOTAL','INDEXES'].includes(capacity.toUpperCase())))
error(`'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'`)
// Verify startKey
// TODO: validate startKey shape
if (startKey && (typeof startKey !== 'object' || Array.isArray(startKey)))
error(`'startKey' requires a valid object`)
// Default names and values
let ExpressionAttributeNames: { [key: string]: any } = { '#pk': (index && this.Table.indexes[index].partitionKey) || this.Table.partitionKey }
let ExpressionAttributeValues: { [key: string]: any } = { ':pk': pk }
let KeyConditionExpression = '#pk = :pk'
let FilterExpression // init FilterExpression
let ProjectionExpression // init ProjectionExpression
let EntityProjections = {}
let TableProjections // FIXME: removed default
// Parse sortKey condition operator and value
let operator, value: any, f: string = ''
if (eq) { value = eq; f = 'eq'; operator = '=' }
if (lt) { value = value ? conditonError(f) : lt; f = 'lt'; operator = '<' }
if (lte) { value = value ? conditonError(f) : lte; f = 'lte'; operator = '<=' }
if (gt) { value = value ? conditonError(f) : gt; f = 'gt'; operator = '>' }
if (gte) { value = value ? conditonError(f) : gte; f = 'gte'; operator = '>=' }
if (beginsWith) { value = value ? conditonError(f) : beginsWith; f = 'beginsWith'; operator = 'BEGINS_WITH' }
if (between) { value = value ? conditonError(f) : between; f = 'between'; operator = 'BETWEEN' }
// If a sortKey condition was set
if (operator) {
// Get sortKey configuration
const sk = index ?
(
this.Table.indexes[index].sortKey ? (
this.Table.attributes[this.Table.indexes[index].sortKey!] || { type: 'string'}
)
: error(`Conditional expressions require the index to have a sortKey`)
)
: this.Table.sortKey ? this.Table.attributes[this.Table.sortKey]
: error(`Conditional expressions require the table to have a sortKey`)
// Init validateType
const validateType = validateTypes(this.DocumentClient!)
// Add the sortKey attribute name
ExpressionAttributeNames['#sk'] = (index && this.Table.indexes[index].sortKey) || this.Table.sortKey
// If between operation
if (operator === 'BETWEEN') {
// Verify array input
if (!Array.isArray(value) || value.length !== 2)
error(`'between' conditions requires an array with two values.`)
// Add values and special key condition
ExpressionAttributeValues[':sk0'] = validateType(sk,f+'[0]',value[0])
ExpressionAttributeValues[':sk1'] = validateType(sk,f+'[1]',value[1])
KeyConditionExpression += ' and #sk between :sk0 and :sk1'
} else {
// Add value
ExpressionAttributeValues[':sk'] = validateType(sk,f,value)
// If begins_with, add special key condition
if (operator === 'BEGINS_WITH') {
KeyConditionExpression += ' and begins_with(#sk,:sk)'
} else {
KeyConditionExpression += ` and #sk ${operator} :sk`
}
} // end if-else
} // end if operator
// If filter expressions
if (filters) {
// Parse the filter
const {
expression,
names,
values
} = parseFilters(filters,this,entity)
if (Object.keys(names).length > 0) {
// TODO: alias attribute field names
// console.log(names)
// Merge names and values and add filter expression
ExpressionAttributeNames = Object.assign(ExpressionAttributeNames,names)
ExpressionAttributeValues = Object.assign(ExpressionAttributeValues,values)
FilterExpression = expression
} // end if names
} // end if filters
// If projections
if (attributes) {
const { names, projections, entities, tableAttrs } = parseProjections(attributes,this,entity!,true)
if (Object.keys(names).length > 0) {
// Merge names and add projection expression
ExpressionAttributeNames = Object.assign(ExpressionAttributeNames,names)
ProjectionExpression = projections
EntityProjections = entities
TableProjections = tableAttrs
} // end if names
} // end if projections
// Generate the payload
const payload = Object.assign(
{
TableName: this.name,
KeyConditionExpression,
ExpressionAttributeNames,
ExpressionAttributeValues
},
FilterExpression ? { FilterExpression } : null,
ProjectionExpression ? { ProjectionExpression } : null,
index ? { IndexName: index } : null,
limit ? { Limit: String(limit) } : null,
reverse ? { ScanIndexForward: !reverse } : null,
consistent ? { ConsistentRead: consistent } : null,
capacity ? { ReturnConsumedCapacity: capacity.toUpperCase() } : null,
select ? { Select: select.toUpperCase() } : null,
startKey ? { ExclusiveStartKey: startKey } : null,
typeof params === 'object' ? params : null
)
return projections ? { payload, EntityProjections, TableProjections } : payload
} // end query
// SCAN the table
async scan(
options: scanOptions = {},
params: Partial<DocumentClient.ScanInput> = {}
) {
// Generate query parameters with meta data
const {
payload,
EntityProjections,
TableProjections
} = this.scanParams(options,params,true)
// If auto execute enabled
if (options.execute || (this.autoExecute && options.execute !== false)) {
const result = await this.DocumentClient!.scan(payload).promise()
// If auto parse enable
if (options.parse || (this.autoParse && options.parse !== false)) {
return Object.assign(
result,
{
Items: result.Items && result.Items.map(item => {
if (this[item[String(this.Table.entityField)]]) {
return this[item[String(this.Table.entityField)]].parse(
item,
EntityProjections[item[String(this.Table.entityField)]] ? EntityProjections[item[String(this.Table.entityField)]]
: TableProjections ? TableProjections
: []
)
} else {
return item
}
})
},
// If last evaluated key, return a next function
result.LastEvaluatedKey ? {
next: () => {
return this.scan(
Object.assign(options, { startKey: result.LastEvaluatedKey }),
params
)
}
} : null
)
} else {
return result
}
} else {
return payload
} // end if-else
}
// Generate SCAN Parameters
scanParams(
options: scanOptions = {},
params: Partial<DocumentClient.ScanInput> = {},
meta=false
) {
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions
// Deconstruct valid options
const {
index,
limit,
consistent, // ConsistentRead (boolean)
capacity, // ReturnConsumedCapacity (none, total, or indexes)
select, // Select (all_attributes, all_projected_attributes, specific_attributes, count)
filters, // filter object,
attributes, // Projections
segments, // Segments,
segment, // Segment
startKey,
entity, // optional entity name to filter aliases
..._args // capture extra arguments
} = options
// Remove other valid options from options
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
// Error on extraneous arguments
if (args.length > 0)
error(`Invalid scan options: ${args.join(', ')}`)
// Verify index
if (index !== undefined && !this.Table.indexes[index])
error(`'${index}' is not a valid index name`)
// Verify limit
if (limit !== undefined && (!Number.isInteger(limit) || limit < 0))
error(`'limit' must be a positive integer`)
// Verify consistent read
if (consistent !== undefined && typeof consistent !== 'boolean')
error(`'consistent' requires a boolean`)
// Verify select
// TODO: Make dependent on whether or not an index is supplied
if (select !== undefined
&& (typeof select !== 'string'
|| !['ALL_ATTRIBUTES', 'ALL_PROJECTED_ATTRIBUTES', 'SPECIFIC_ATTRIBUTES', 'COUNT'].includes(select.toUpperCase())))
error(`'select' must be one of 'ALL_ATTRIBUTES', 'ALL_PROJECTED_ATTRIBUTES', 'SPECIFIC_ATTRIBUTES', OR 'COUNT'`)
// Verify entity
if (entity !== undefined && (typeof entity !== 'string' || !(entity in this)))
error(`'entity' must be a string and a valid table Entity name`)
// Verify capacity
if (capacity !== undefined
&& (typeof capacity !== 'string' || !['NONE','TOTAL','INDEXES'].includes(capacity.toUpperCase())))
error(`'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'`)
// Verify startKey
// TODO: validate startKey shape
if (startKey && (typeof startKey !== 'object' || Array.isArray(startKey)))
error(`'startKey' requires a valid object`)
// Verify consistent segments
if (segments !== undefined && (!Number.isInteger(segments) || segments < 1))
error(`'segments' must be an integer greater than 1`)
if (segment !== undefined && (!Number.isInteger(segment) || segment < 0 || segment >= segments!))
error(`'segment' must be an integer greater than or equal to 0 and less than the total number of segments`)
if ((segments !== undefined && segment === undefined) || (segments === undefined && segment !== undefined))
error(`Both 'segments' and 'segment' must be provided`)
// Default names and values
let ExpressionAttributeNames = {}
let ExpressionAttributeValues = {}
let FilterExpression // init FilterExpression
let ProjectionExpression // init ProjectionExpression
let EntityProjections = {}
let TableProjections
// If filter expressions
if (filters) {
// Parse the filter
const {
expression,
names,
values
} = parseFilters(filters,this,entity)
if (Object.keys(names).length > 0) {
// TODO: alias attribute field names
// console.log(names)
// Merge names and values and add filter expression
ExpressionAttributeNames = Object.assign(ExpressionAttributeNames,names)
ExpressionAttributeValues = Object.assign(ExpressionAttributeValues,values)
FilterExpression = expression
} // end if names
} // end if filters
// If projections
if (attributes) {
const { names, projections, entities, tableAttrs } = parseProjections(attributes,this,entity!,true)
if (Object.keys(names).length > 0) {
// Merge names and add projection expression
ExpressionAttributeNames = Object.assign(ExpressionAttributeNames,names)
ProjectionExpression = projections
EntityProjections = entities
TableProjections = tableAttrs
} // end if names
} // end if projections
// Generate the payload
const payload = Object.assign(
{
TableName: this.name,
},
Object.keys(ExpressionAttributeNames).length ? { ExpressionAttributeNames } : null,
Object.keys(ExpressionAttributeValues).length ? { ExpressionAttributeValues } : null,
FilterExpression ? { FilterExpression } : null,
ProjectionExpression ? { ProjectionExpression } : null,
index ? { IndexName: index } : null,
segments ? { TotalSegments: segments } : null,
Number.isInteger(segment) ? { Segment: segment } : null,
limit ? { Limit: String(limit) } : null,
consistent ? { ConsistentRead: consistent } : null,
capacity ? { ReturnConsumedCapacity: capacity.toUpperCase() } : null,
select ? { Select: select.toUpperCase() } : null,
startKey ? { ExclusiveStartKey: startKey } : null,
typeof params === 'object' ? params : null
)
return meta ? { payload, EntityProjections, TableProjections } : payload
} // end query
// BatchGet Items
async batchGet(
items: any,
options: batchGetOptions = {},
params: Partial<DocumentClient.BatchGetItemInput> = {}
) {
// Generate the payload with meta information
const {
payload, // batchGet payload
Tables, // table reference
EntityProjections,
TableProjections
} = this.batchGetParams(items,options,params,true) as batchGetParamsMeta
// If auto execute enabled
if (options.execute || (this.autoExecute && options.execute !== false)) {
const result = await this.DocumentClient!.batchGet(payload).promise()
// If auto parse enable
if (options.parse || (this.autoParse && options.parse !== false)) {
// TODO: Left in for testing. Needs to be removed
// result.UnprocessedKeys = testUnprocessedKeys
return this.parseBatchGetResponse(result,Tables,EntityProjections,TableProjections,options)
} else {
return result
}
} else {
return payload
} // end-if
} // end batchGet
parseBatchGetResponse(
result: any,
Tables: { [key: string]: Table },
EntityProjections: { [key: string]: any },
TableProjections: { [key:string]: string[] },
options: batchGetOptions = {}
) {
return Object.assign(
result,
// If reponses exist
result.Responses ? {
// Loop through the tables
Responses: Object.keys(result.Responses).reduce((acc,table) => {
// Merge in tables
return Object.assign(acc,{
// Map over the items
[(Tables[table] && Tables[table].alias) || table]: result.Responses[table].map((item: Table) => {
// Check that the table has a reference, the entityField exists, and that the entity type exists on the table
if (Tables[table] && Tables[table][item[String(Tables[table].Table.entityField)]]) {
// Parse the item and pass in projection references
return Tables[table][item[String(Tables[table].Table.entityField)]].parse(
item,
EntityProjections[table] && EntityProjections[table][item[String(Tables[table].Table.entityField)]] ? EntityProjections[table][item[String(Tables[table].Table.entityField)]]
: TableProjections[table] ? TableProjections[table]
: []
)
// Else, just return the original item
} else {
return item
}
}) // end item map
}) // end assign
}, {}) // end table reduce
} : null, // end if Responses
// If UnprocessedKeys, return a next function
result.UnprocessedKeys && Object.keys(result.UnprocessedKeys).length > 0 ? {
next: async (): Promise<any> => {
const nextResult = await this.DocumentClient!.batchGet(Object.assign(
{ RequestItems: result.UnprocessedKeys },
options.capacity ? { ReturnConsumedCapacity: options.capacity.toUpperCase() } : null
)).promise()
return this.parseBatchGetResponse(nextResult,Tables,EntityProjections,TableProjections,options)
}
} : { next: () => false } // TODO: How should this return?
) // end parse assign
} // end parseBatchGetResponse
// Generate BatchGet Params
batchGetParams(
_items: any,
options: batchGetOptions = {},
params: Partial<DocumentClient.BatchGetItemInput> = {},
meta=false
) {
let items = Array.isArray(_items) ? _items : [_items]
// Error on no items
if (items.length === 0)
error(`No items supplied`)
const {
capacity,
consistent,
attributes,
..._args
} = options
// Remove other valid options from options
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
// Error on extraneous arguments
if (args.length > 0)
error(`Invalid batchGet options: ${args.join(', ')}`)
// Verify capacity
if (capacity !== undefined
&& (typeof capacity !== 'string' || !['NONE','TOTAL','INDEXES'].includes(capacity.toUpperCase())))
error(`'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'`)