This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databasestuff.py
54 lines (52 loc) · 1.94 KB
/
databasestuff.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
import motor.motor_asyncio as amotor
from json import loads as l
import asyncio
url=l(open("configs.json").read())['data']['url']
class GuildDB:
def __init__(self):
self.client=amotor.AsyncIOMotorClient(url)
self.db=self.client['Guildstore']
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 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=GuildDB()
await db.add_collection("guilds")
await db.insert(name="Test",content="woop")
await db.insert_many({"name":"sebi","content":"gay"},{"name":"agg","content":"gayer"})
await db.delete()
m=await db.print_db()
if __name__=="__main__":
asyncio.get_event_loop().run_until_complete(main())'''