This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbwrapper.py
60 lines (60 loc) · 2.25 KB
/
dbwrapper.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
57
58
59
60
import motor.motor_asyncio as amotor
import asyncio
url = "mongodb+srv://botuser:[email protected]/Francium?retryWrites=true&w=majority"
class DB:
def __init__(self,db):
self.client\
=amotor.AsyncIOMotorClient(url)
self.db=self.client[db]
self.collections=[]
self.collection=""
async def add_collection(self,name:str):
self.collections+=[name]
self.collections=list(set(self.collections))
self.collection=name
if name not in (await self.db.list_collection_names()):
await self.db.create_collection(name=name)
async def remove_collection(self,name:str):
self.collections.remove(name)
await self.db.drop_collection(name)
def set_collection(self,name:str):
self.collection=name
async def insert(self,**kwargs):
await self.db[self.collection].insert_one(kwargs)
async def insert_many(self,*items):
for i in items:
await self.insert(**i)
async def delete(self,**kwargs):
await self.db[self.collection].delete_many(kwargs)
async def find(self,length=1000,**kwargs):
cursor=self.db[self.collection].find(kwargs)
res=[]
for doc in await cursor.to_list(length=length):
doc.setdefault("")
res.append(doc)
return res
async def insertnorepeat(self,**kwargs):
if not (await self.find(**kwargs)):
await self.insert(**kwargs)
async def update(self,objid,**kwargs):
await self.db[self.collection].update_one({"_id":objid},{"$set":kwargs})
async def print_db(self):
res=[]
for i in (await self.find()):
temp=[]
for x in i.keys():
if i!="_id":temp+=[x+":"+str(i[x])]
res+=[",".join(temp)]
return "\n".join(res)
"""
async def main():
db=DB("users")
await db.add_collection("currency")
await db.insert(name="Test",content="woop")
await db.insert_many({"name":"lol","content":"testing"},{"name":"lol","content":"testing"})
#await db.delete()
m=await db.print_db()
print(m)
if __name__=="__main__":
asyncio.get_event_loop().run_until_complete(main())
"""