-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdatabase.py
40 lines (26 loc) · 1.07 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
import os
from deta import Deta # pip install deta
#from dotenv import load_dotenv # pip install python-dotenv
# Load the environment variables
#load_dotenv(".env")
DETA_KEY = "a0slxdit_FLgQbPkQLth8ER8yHCfjv3rdUu97qWps"
# Initialize with a project key
deta = Deta(DETA_KEY)
# This is how to create/connect a database
db = deta.Base("users_mehedi_db")
def insert_user(username, name, password):
"""Returns the user on a successful user creation, otherwise raises and error"""
return db.put({"key": username, "name": name, "password": password})
def fetch_all_users():
"""Returns a dict of all users"""
res = db.fetch()
return res.items
def get_user(username):
"""If not found, the function will return None"""
return db.get(username)
def update_user(username, updates):
"""If the item is updated, returns None. Otherwise, an exception is raised"""
return db.update(updates, username)
def delete_user(username):
"""Always returns None, even if the key does not exist"""
return db.delete(username)