def topic_data_page_(self, where, sort, pageable, model, name) -> DataPage: topic_collection_name = build_collection_name(name) codec_options = build_code_options() collection = self.client.get_collection(topic_collection_name, codec_options=codec_options) mongo_where = self.build_mongo_where_expression(where) total = collection.find(mongo_where).count() skips = pageable.pageSize * (pageable.pageNumber - 1) if sort is not None: cursor = collection.find(mongo_where).skip(skips).limit(pageable.pageSize).sort( self.build_mongo_order(sort)) else: cursor = collection.find(mongo_where).skip(skips).limit(pageable.pageSize) if model is not None: return build_data_pages(pageable, [model.parse_obj(result) for result in list(cursor)], total) else: results = [] if self.storage_template.check_topic_type(name) == RAW: for doc in cursor: results.append(doc['data_']) else: for doc in cursor: del doc['_id'] results.append(doc) return build_data_pages(pageable, results, total)
def topic_data_find_with_aggregate(self, where, topic_name, aggregate): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) for key, value in aggregate.items(): aggregate_ = {} if value == "sum": aggregate_ = {"$group": { "_id": "null", "value": {"$sum": f'${key}'} } } elif value == "count": return topic_data_col.count_documents(self.build_mongo_where_expression(where)) elif value == "avg": aggregate_ = {"$group": { "_id": "null", "value": {"$avg": f'${key}'} } } pipeline = [{"$match": self.build_mongo_where_expression(where)}, aggregate_] cursor = topic_data_col.aggregate(pipeline) for doc in cursor: result = doc["value"] return result
def topic_data_update_one_with_version(self, id_, version_, one, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) self.encode_dict(one) result = topic_data_col.update_one( self.build_mongo_where_expression({"_id": ObjectId(id_), "version_": version_}), self.build_mongo_updates_expression_for_update(one)) if result.modified_count == 0: raise OptimisticLockError("Optimistic lock error")
def topic_data_insert_one(self, one, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) self.encode_dict(one) try: result = topic_data_col.insert_one(self.build_mongo_updates_expression_for_insert(one)) except WriteError as we: if we.code == 11000: # E11000 duplicate key error raise InsertConflictError("InsertConflict") return result.inserted_id
def topic_data_list_all(self, topic_name) -> list: codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) result = topic_data_col.find() return list(result)
def topic_data_find_(self, where, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) return topic_data_col.find(self.build_mongo_where_expression(where))
def topic_data_find_by_id(self, id_, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) result = topic_data_col.find_one({"_id": ObjectId(id_)}) return result
def topic_data_update_(self, where, updates, name): codec_options = build_code_options() self.encode_dict(updates) collection = self.client.get_collection(build_collection_name(name), codec_options=codec_options) collection.update_many(self.build_mongo_where_expression(where), {"$set": self.__convert_to_dict(updates)})
def topic_data_update_one(self, id_, one, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) self.encode_dict(one) topic_data_col.update_one({"_id": ObjectId(id_)}, self.build_mongo_updates_expression_for_update(one))
def topic_data_insert_(self, data, topic_name): codec_options = build_code_options() topic_data_col = self.client.get_collection(build_collection_name(topic_name), codec_options=codec_options) for d in data: self.encode_dict(d) topic_data_col.insert_many(data)