aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/database.py
blob: 75f0137896a5cc9c56130ae0bdd77b614f0bc5cc (plain) (blame)
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
# coding=utf-8

import os

from flask import abort

import rethinkdb


class RethinkDB:

    def __init__(self, loop_type: str = "gevent"):
        self.host = os.environ.get("RETHINKDB_HOST", "127.0.0.1")
        self.port = os.environ.get("RETHINKDB_PORT", "28016")
        self.database = os.environ.get("RETHINKDB_DATABASE", "pythondiscord")
        self.conn = None

        rethinkdb.set_loop_type(loop_type)

        with self.get_connection(connect_database=False) as conn:
            try:
                rethinkdb.db_create(self.database).run(conn)
                print(f"Database created: {self.database}")
            except rethinkdb.RqlRuntimeError:
                print(f"Database found: {self.database}")

    def get_connection(self, connect_database: bool = True):
        if connect_database:
            return rethinkdb.connect(host=self.host, port=self.port, db=self.database)
        else:
            return rethinkdb.connect(host=self.host, port=self.port)

    def before_request(self):
        try:
            self.conn = self.get_connection()
        except rethinkdb.RqlDriverError:
            abort(503, "Database connection could not be established.")

    def teardown_request(self, _):
        try:
            self.conn.close()
        except AttributeError:
            pass