-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
56 lines (47 loc) · 1.44 KB
/
db.py
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
import boto3
class DataBase():
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
# Get the service client
dynamodb_client = boto3.client('dynamodb')
primary_key = None
def __init__(self, table_name):
if table_name not in self.dynamodb_client.list_tables()['TableNames']:
raise Exception(f"DynamoDB '{table_name}' does not exist!")
else:
self.table = self.dynamodb.Table(table_name)
self.primary_key = self.table.key_schema[0]['AttributeName']
return None
def write(self, primary_key, key, value):
self.table.put_item(
Item={
self.primary_key : primary_key,
key : value
}
)
def get(self, primary_key):
response = self.table.get_item(
Key={
self.primary_key : primary_key
}
)
try:
return response['Item']
except:
return None
def delete(self, primary_key):
self.table.delete_item(
Key={
self.primary_key : primary_key
}
)
def update(self, primary_key, key, value):
self.table.update_item(
Key={
self.primary_key : primary_key
},
UpdateExpression=f'SET {key} = :val1',
ExpressionAttributeValues={
':val1': value
}
)