-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion-3-complete.ts
155 lines (117 loc) · 3.75 KB
/
version-3-complete.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
import aws from 'aws-sdk'
import { DocumentClient, ScanInput } from 'aws-sdk/clients/dynamodb'
import pMap from 'p-map'
export interface Vehicle {
windows: number
name: string
canFly: boolean
}
interface Person {
hands: number
}
interface TableNameTypes {
"vehiclesTable": Vehicle,
"personTable": Person
}
const scanConcurrency = 8
const getSafeSelectByfieldParms = (field: string, value: string) => ({
KeyConditionExpression: `#fieldName = :${field}`,
ExpressionAttributeNames: { '#fieldName': String(field) },
ExpressionAttributeValues: {
[`:${field}`]: value,
},
})
const parallelScan = async <T>(scanParams: ScanInput): Promise<T[]> => {
// {{{
const dynamoDb = new aws.DynamoDB.DocumentClient({
httpOptions: {
timeout: 60000 * 5, // 5 minutes
},
})
const segmentIndexes = [...Array(scanConcurrency).keys()]
const docs: T[] = []
await pMap(segmentIndexes, async (_, segmentIndex) => {
let ExclusiveStartKey: DocumentClient.Key | undefined
const params: DocumentClient.ScanInput = {
...scanParams,
Segment: segmentIndex,
TotalSegments: scanConcurrency,
}
do {
if (ExclusiveStartKey) {
params.ExclusiveStartKey = ExclusiveStartKey
}
const { Items, LastEvaluatedKey } = await dynamoDb.scan(params).promise()
ExclusiveStartKey = LastEvaluatedKey
docs.push(...((Items ?? []) as T[]))
} while (ExclusiveStartKey)
})
return docs
// }}}
}
type ColumnsArray<D> = ReadonlyArray<keyof D>
type DataSubset<D, C extends ColumnsArray<D>> = Pick<D, C[number]>
type DataSubsetOrEverything<D, C extends ColumnsArray<D> | undefined> = C extends ColumnsArray<D>
? DataSubset<D, C>
: D
export class NewDataService<TN extends keyof TableNameTypes> {
private getTableParams(prefix: string) {
return { TableName: `${prefix}${process.env.SERVERLESS_STAGE}` }
}
private dynamoDb: DocumentClient
constructor(private tableNamePrefix: TN) {
this.dynamoDb = new aws.DynamoDB.DocumentClient()
}
async putItem(item: TableNameTypes[TN]): Promise<void> {
// {{{
await this.dynamoDb
.put({
...this.getTableParams(this.tableNamePrefix),
Item: item,
})
.promise()
// }}}
}
async waitForItemByFieldToBeConsistent(field: keyof TableNameTypes[TN], value: string): Promise<void> {
// {{{
await this.dynamoDb
.query({
...this.getTableParams(this.tableNamePrefix),
...getSafeSelectByfieldParms(String(field), value),
ConsistentRead: true,
})
.promise()
// }}}
}
async getItemsByField(field: string, value: string): Promise<TableNameTypes[TN][]> {
// {{{
const result = await this.dynamoDb
.query({
...this.getTableParams(this.tableNamePrefix),
...getSafeSelectByfieldParms(String(field), value),
})
.promise()
return (result.Items ?? []) as TableNameTypes[TN][]
// }}}
}
async getAll<C extends ColumnsArray<TableNameTypes[TN]>>(columns?: C): Promise<DataSubsetOrEverything<TableNameTypes[TN], C>[]> {
// {{{
interface ExpressionAttributeNames {
[key: string]: string
}
const expressionAttributeNames = columns?.reduce<ExpressionAttributeNames>((accum, item) => {
accum[`#${item}`] = String(item)
return accum
}, {})
const projectionExpression = Object.keys(expressionAttributeNames ?? {}).join(',')
const scanParams: ScanInput = columns
? {
...this.getTableParams(this.tableNamePrefix),
ProjectionExpression: projectionExpression,
ExpressionAttributeNames: expressionAttributeNames,
}
: this.getTableParams(this.tableNamePrefix)
return await parallelScan(scanParams)
// }}}
}
}