def find_one_and_update(where: dict, updates: dict, name: str): codec_options = build_code_options() collection = client.get_collection(name, codec_options=codec_options) return collection.find_one_and_update( filter=build_mongo_where_expression(where), update=updates, upsert=True)
def page_all(sort, pageable, model, name) -> DataPage: codec_options = build_code_options() collection = client.get_collection(name, codec_options=codec_options) total = collection.find().count() skips = pageable.pageSize * (pageable.pageNumber - 1) cursor = collection.find().skip(skips).limit(pageable.pageSize).sort(build_mongo_order(sort)) return build_data_pages(pageable, [model.parse_obj(result) for result in list(cursor)], total)
def find_and_modify_topic_data(topic_name, query, update_data): collection_name = build_collection_name(topic_name) codec_options = build_code_options() collection = db.get_collection(collection_name, codec_options=codec_options) old_value = collection.find_one_and_update(filter=query, update=update_data, upsert=True) trigger_pipeline(topic_name, {pipeline_constants.NEW: update_data, pipeline_constants.OLD: old_value}, TriggerType.update)
def insert_topic_data(topic_name, mapping_result, pipeline_uid): collection_name = build_collection_name(topic_name) codec_options = build_code_options() collection = db.get_collection(collection_name, codec_options=codec_options) add_audit_columns(mapping_result, INSERT) add_trace_columns(mapping_result, "insert_row", pipeline_uid) collection.insert(mapping_result) trigger_pipeline(topic_name, {pipeline_constants.NEW: mapping_result, pipeline_constants.OLD: None}, TriggerType.insert)
def query_with_pagination(collection_name, pagination, base_model=None, query_dict=None, sort_dict=None) -> DataPage: codec_options = build_code_options() collections = client.get_collection(collection_name, codec_options=codec_options) items_count = __find_with_count(collections, query_dict) skips = pagination.pageSize * (pagination.pageNumber - 1) cursor = __sort(__find_with_page(collections, query_dict, pagination, skips), sort_dict) if base_model is not None: return build_data_pages(pagination, [base_model.parse_obj(result) for result in list(cursor)], items_count) else: return build_data_pages(pagination, list(cursor), items_count)
def update_topic_data(topic_name, mapping_result, target_data, pipeline_uid): collection_name = build_collection_name(topic_name) codec_options = build_code_options() collection = db.get_collection(collection_name, codec_options=codec_options) old_data = find_topic_data_by_id(collection, target_data["_id"]) add_audit_columns(mapping_result, UPDATE) add_trace_columns(mapping_result, "update_row", pipeline_uid) collection.update_one({"_id": target_data["_id"]}, {"$set": mapping_result}) data = {**target_data, **mapping_result} trigger_pipeline(topic_name, {pipeline_constants.NEW: data, pipeline_constants.OLD: old_data}, TriggerType.update)
def find_and_modify_topic_data(topic_name, query, update_data, target_data): collection_name = build_collection_name(topic_name) codec_options = build_code_options() collection = db.get_collection(collection_name, codec_options=codec_options) old_data = find_topic_data_by_id(collection, target_data["_id"]) collection.find_one_and_update(query=query, update=update_data) trigger_pipeline(topic_name, { pipeline_constants.NEW: update_data, pipeline_constants.OLD: old_data }, TriggerType.update)
def page_(where, sort, pageable, model, name) -> DataPage: codec_options = build_code_options() collection = client.get_collection(name, codec_options=codec_options) mongo_where = build_mongo_where_expression(where) # print(mongo_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( 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: return build_data_pages(pageable, list(cursor), total)
def save_topic_instance(topic_name, instance): codec_options = build_code_options() topic_instance_col = client.get_collection( build_collection_name(topic_name), codec_options=codec_options) topic_instance_col.insert(instance) return topic_name, instance
def query_topic_data(mongo_query, topic_name): codec_options = build_code_options() collection_name = build_collection_name(topic_name) collection = db.get_collection(collection_name, codec_options=codec_options) result = collection.find_one(mongo_query) return result
def topic_data_list_all(topic_name) -> list: codec_options = build_code_options() topic_data_col = client.get_collection(build_collection_name(topic_name), codec_options=codec_options) result = topic_data_col.find() return list(result)
def topic_data_find_(where, topic_name): codec_options = build_code_options() topic_data_col = client.get_collection(build_collection_name(topic_name), codec_options=codec_options) return topic_data_col.find(where)
def topic_data_find_by_id(id_, topic_name): codec_options = build_code_options() topic_data_col = 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_one(id_, one, topic_name): codec_options = build_code_options() topic_data_col = client.get_collection(build_collection_name(topic_name), codec_options=codec_options) topic_data_col.update_one({"_id": ObjectId(id_)}, {"$set": one})
def topic_data_insert_(data, topic_name): codec_options = build_code_options() topic_data_col = client.get_collection(build_collection_name(topic_name), codec_options=codec_options) topic_data_col.insert_many(data)
def save_topic_instances(topic_name, instances): codec_options = build_code_options() topic_instance_col = client.get_collection( build_collection_name(topic_name), codec_options=codec_options) topic_instance_col.insert_many(instances)
def topic_find_one_and_update(where: dict, updates: dict, name: str): codec_options = build_code_options() collection = client.get_collection(build_collection_name(name), codec_options=codec_options) return collection.find_one_and_update(filter=build_mongo_where_expression(where), update=updates, upsert=True,return_document=ReturnDocument.AFTER)
def update_topic_instance(topic_name, instance, instance_id): codec_options = build_code_options() topic_instance_col = client.get_collection( build_collection_name(topic_name), codec_options=codec_options) topic_instance_col.update_one({"_id": ObjectId(instance_id)}, {"$set": instance})
def topic_data_insert_one(one, topic_name): codec_options = build_code_options() topic_data_col = client.get_collection(build_collection_name(topic_name), codec_options=codec_options) topic_data_col.insert(one) return topic_name, one