forked from ashishpatel546/aws-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsDyanmoDbService.js
134 lines (107 loc) · 4.12 KB
/
awsDyanmoDbService.js
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
const aws = require('aws-sdk')
module.exports = class AwsDynamoDBService {
constructor(region, accessKeyId, secretAccessKey) {
this.creds = {region, accessKeyId, secretAccessKey}
}
getDynamoDb (){
return new aws.DynamoDB(this.creds)
}
static from({region, accessKeyId, secretAccessKey}) {
return new AwsDynamoDBService(region, accessKeyId, secretAccessKey);
}
async createTable(createTableparams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.createTable(createTableparams).promise()
}
async delete_table(deleteTableparams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.deleteTable(deleteTableparams).promise()
}
async list_Tables(listParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.listTables(listParams).promise()
}
async describe_Table(tableParams){
const dynamoAccess = this.getDynamoDb()
const response = await dynamoAccess.describeTable(tableParams).promise()
const result = JSON.stringify(response.Table, null, 2)
return result
}
async update_Table(updateParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.updateTable(updateParams).promise()
}
async create_BackUp(backupParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.createBackup(backupParams).promise()
}
async delete_BackUp(backupParams){
const dynamoAccess = this.getDynamoDb()
const listBackUps = (await dynamoAccess.listBackups().promise()).BackupSummaries
let backUpArn = ""
for (let item of listBackUps){
if (item.BackupName === backupParams.BackupName) backUpArn = item.BackupArn
}
return await dynamoAccess.deleteBackup({BackupArn: backUpArn}).promise()
}
async put_Items(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.putItem(itemParams).promise()
}
async batch_Write_Item(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.batchWriteItem(itemParams).promise()
}
async get_Item(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.getItem(itemParams).promise()
}
async batch_Get_Item(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.batchGetItem(itemParams).promise()
}
async update_Item(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.updateItem(itemParams).promise()
}
async delete_Item(itemParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.deleteItem(itemParams).promise()
}
async query(queryParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.query(queryParams).promise()
}
async scan(scanParams){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.scan(scanParams).promise()
}
async execute_Statement(statement){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.executeStatement().promise()
}
async batchExecute_Statement(statements){
const dynamoAccess = this.getDynamoDb()
return await dynamoAccess.batchExecuteStatement().promise()
}
async getBackUps({}){
const dynamoAccess = this.getDynamoDb()
return await (await dynamoAccess.listBackups().promise()).BackupSummaries
}
async getRegions({}){
// if (this.creds.region)
this.creds.region = "us-west-2"
const ec2 = new aws.EC2(this.creds)
const result = await ec2.describeRegions({}).promise()
return result.Regions
}
async getTables({}){
const dynamoAccess = this.getDynamoDb()
const tables = await dynamoAccess.listTables({}).promise()
const results=[]
tables.TableNames.forEach(table=>{
results.push({TableName: table})
})
return results
}
}