-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.py
49 lines (38 loc) · 1.19 KB
/
database.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
import os
from datetime import datetime
from boto3.dynamodb.conditions import Key
import boto3
class DynamoDatabase:
def __init__(self):
dynamodb = boto3.resource('dynamodb')
self.table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
self.pk = 'URL'
self.sk_prefix = 'LINK#'
def save_url(self, url, title, summary):
self.table.put_item(
Item={
'pk': self.pk,
'sk': f'{self.sk_prefix}{datetime.now().isoformat()}#{url}',
'url': url,
'title': title,
'summary': summary,
}
)
def get_links(self):
response = self.table.query(
KeyConditionExpression=Key('pk').eq(self.pk) & Key('sk').begins_with(self.sk_prefix),
ScanIndexForward=False
)
return response['Items'] or []
class InMemoryDatabase:
def __init__(self):
self.data = []
def save_url(self, url, title, summary):
self.data.append({
'url': url,
'title': title,
'summary': summary,
})
def get_links(self):
# reverse chronological order
return self.data[::-1]