def get(self, entity_id): with self.client.pipeline(transaction=True) as pipe: pipe.watch(hash(entity_id)) return Entity(**({ k.decode(): v.decode() for k, v in pipe.hgetall(hash(entity_id)).items() }))
def init_database(): """Initialize the database""" with APP.app_context(): DB.drop_all() DB.create_all() # add a few entities for name in MOCK_DATA: entity = Entity(name) DB.session.add(entity) DB.session.commit()
def get_by_id(self, id): r = self.repository.get_by_id(id) entity = r and Entity(int(r[0]), r[1]) or Entity() return json.dumps(entity, cls=EntityJsonEncoder)
def get_all(self): entities = [Entity(int(x[0]), x[1]) for x in self.repository.get_all()] return json.dumps(entities, cls=EntityJsonEncoder)
def save(self, json_body): with transaction(repository=Repository) as session: body = json.loads(json_body) entity = Entity(entity_value=body['value']) return json.dumps(session.save(entity=entity), cls=EntityJsonEncoder)
def get(self, id): response = self.client.select(space_name=self.space_id, key=id) return Entity(*response[0]) if response else Entity()
def update(self, entity): response = self.client.update(space_name=self.space_id, key=entity.e_id, op_list=[('=', 1, entity.e_val)]) return Entity(*response[0]) if response else Entity()
def remove(self, id): response = self.client.delete(space_name=self.space_id, key=id, index=0) return Entity(*response[0]) if response else Entity()
def create(self, entity): return Entity(*self.client.insert(space_name=self.space_id, values=entity.to_tuple())[0])