-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
56 lines (45 loc) · 1.15 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
47
48
49
50
51
52
53
54
55
56
from pymongo import MongoClient
import os
from dotenv import load_dotenv
load_dotenv
def connect():
db_password = os.getenv('MONGO_DB_KEY')
client = MongoClient(db_password)
print("Successfully Connected")
collection = client.main
return collection.main
db = connect()
# Making a CRUD API
# Create
def write(data):
if type(data) == None:
print("No data")
return
# for loop to add multiple pieces of data
if type(data) == list:
for doc in data:
db.insert_one(doc)
return
db.insert_one(data)
# Read
def read(email):
if type(email) == None:
print("No email provided")
return
return db.find_one({"email": email})
# Update
def update(email, data, dataType):
if type(email) == None or type(data) == None:
print(f"Provide valid data\nEmail: {email} | Data: {data}")
return
db.update_one({"email": email}, {"$set":{dataType: data}})
# Delete
def delete(email):
if type(email) == None:
print("No email provided")
return
db.delete_one({"email": email})
object = {
"name": "Astra",
"email": "xyz@gmail.com"
}