def upsert_in_db(bucket: Bucket, *, user_in: UserCreate, persist_to=0): user_doc_id = get_doc_id(user_in.username) passwordhash = get_password_hash(user_in.password) user = UserInDB(**user_in.dict(), hashed_password=passwordhash) doc_data = jsonable_encoder(user) with bucket.durability(persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS): bucket.upsert(user_doc_id, doc_data) return user
def upsert( bucket: Bucket, *, doc_id: str, doc_in: PydanticModel, persist_to=0, ttl=0 ) -> Optional[PydanticModel]: doc_data = jsonable_encoder(doc_in) with bucket.durability( persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS ): result = bucket.upsert(doc_id, doc_data, ttl=ttl) if result.success: return doc_in return None
def update_in_db(bucket: Bucket, *, username: str, user_in: UserUpdate, persist_to=0): user_doc_id = get_doc_id(username) stored_user = get(bucket, username=username) stored_user = stored_user.copy(update=user_in.dict(skip_defaults=True)) if user_in.password: passwordhash = get_password_hash(user_in.password) stored_user.hashed_password = passwordhash data = jsonable_encoder(stored_user) with bucket.durability(persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS): bucket.upsert(user_doc_id, data) return stored_user
def update_user_in_db(bucket: Bucket, user_in: UserInUpdate, persist_to=0): user_doc_id = get_user_doc_id(user_in.username) stored_user = get_user(bucket, username=user_in.username) for field in stored_user.fields: if field in user_in.fields: value_in = getattr(user_in, field) if value_in is not None: setattr(stored_user, field, value_in) if user_in.password: passwordhash = get_password_hash(user_in.password) stored_user.hashed_password = passwordhash data = jsonable_encoder(stored_user) with bucket.durability(persist_to=persist_to, timeout=COUCHBASE_DURABILITY_TIMEOUT_SECS): bucket.upsert(user_doc_id, data) return stored_user
def remove( bucket: Bucket, *, doc_id: str, doc_model: Type[PydanticModel] = None, persist_to=0 ) -> Optional[Union[PydanticModel, bool]]: result = bucket.get(doc_id, quiet=True) if not result.value: return None if doc_model: model = doc_model(**result.value) with bucket.durability( persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS ): result = bucket.remove(doc_id) if not result.success: return None if doc_model: return model return True
def update( bucket: Bucket, *, doc_id: str, doc: PydanticModel, doc_updated: PydanticModel, persist_to=0, ttl=0, ): doc_updated = doc.copy(update=doc_updated.dict(skip_defaults=True)) data = jsonable_encoder(doc_updated) with bucket.durability( persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS ): result = bucket.upsert(doc_id, data, ttl=ttl) if result.success: return doc_updated