File size: 1,105 Bytes
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from databases import Database
import datetime


def get_db_conn(config):
    db_url = f"{config.DB_URI}"
    return Database(db_url)


class Repository:
    def __init__(self, db_conn):
        self.db_conn = db_conn

    async def get_by_query(self, query, param):
        results = await self.db_conn.fetch_all(query, param)
        print("result get _by query", results)
        return [dict(result) for result in results]

    async def _fetch_one(self, query, param):
        result = await self.db_conn.fetch_one(query, param)
        return dict(result) if result is not None else result

    async def _exec(self, query, param):
        return await self.db_conn.execute(query, param)

    async def _exec_many(self, query, params):
        return await self.db_conn.execute_many(query, params)

    def update_params(self, params, update=False):
        current_time = datetime.datetime.now()
        if update == False:

            params.update({"createdAt": current_time, "updatedAt": current_time})
        else:
            params.update({"updatedAt": current_time})
        return params