async def _do_insert(self): connection = get_connection() data = self.dump(self._data) self.validate(data) table = self.get_table() result = await table.insert(data, return_changes=True).run(connection) return result
async def _do_update(self): conn = get_connection() data = self.dump(self._data) self.validate(data) table = self.get_table() result = await table.get(data['id'] ).update(data, return_changes=True).run(conn) return result
async def delete(self): conn = get_connection() table = self.get_table() data = self.dump(self._data) try: res = await table.get(data['id']).delete(return_changes=True ).run(conn) except errors.ReqlOpFailedError: return res self._saved = False return res
async def get(cls, _id, raw=False): connection = get_connection() table = cls.get_table() result = await table.get(_id).run(connection) if raw: return result else: if result is None: return result else: return cls(saved=True, **result)
async def all(cls, raw=False): connection = get_connection() table = cls.get_table() try: users = await table.run(connection) except errors.ReqlOpFailedError as err: yield None async for user in users: if raw: yield user else: yield cls(saved=True, **user)
async def drop(cls): conn = get_connection() table = cls.get_table() result = await table.delete().run(conn) return result
async def create_table(cls): connection = get_connection() result = await r.table_create(cls._table).run(connection) return result