def all(cls): db_wrap = DBWrapper.get_solo() rdb = db_wrap.rdb # conn = g.rdb_conn conn = db_wrap.connection return rdb.table(cls._table).run(conn)
def delete(cls, id): db_conn = DBWrapper.get_solo() rdb = db_conn.rdb conn = g.rdb_conn status = r.table(cls._table).get(id).delete().run(conn) if status['errors']: raise DatabaseProcessError("Could not complete the delete action") return True
def filter(cls, predicate, fields=None): db_wrap = DBWrapper.get_solo() rdb = db_wrap.rdb # conn = g.rdb_conn conn = db_wrap.connection if fields: return rdb.table( cls._table).filter(predicate).pluck(fields).run(conn) return rdb.table(cls._table).filter(predicate).run(conn)
def get(cls, id): db_wrap = DBWrapper.get_solo() rdb = db_wrap.rdb # conn = g.rdb_conn conn = db_wrap.connection data = rdb.table(cls._table).get(id).run(conn) obj = None # if data is not None: # obj = cls(**data) return data
def create(cls, **kwargs): try: db_wrap = DBWrapper.get_solo() conn = db_wrap.connection try: db_wrap.rdb.table_create(cls._table).run(conn) except Exception as e: print(f" * [-] Table '{cls._table}' is already exist") data = db_wrap.rdb.table(cls._table).insert(kwargs).run(conn) conn.close() obj = cls(**kwargs) obj.pk = data['generated_keys'][0] return obj except: print(f" * [-] {cls} object create error error")
from db.utils import DBWrapper, establish_connection from db.models.user import User, Book from rethinkdb import r db_wrap = DBWrapper() db_wrap.setup() conn = db_wrap.connection rdb = db_wrap.rdb print('TABLE', User.table, Book.table) # print(User.filter(r.row['age'].eq(21), fields=['age', 'name']))