MongoKit vs MongoEngine vs Flask-MongoAlchemy for Flask
누구나 MongoKit, MongoEngine 또는 Flask 용 Flask-MongoAlchemy에 대한 경험이 있습니까?
어느 것을 더 선호 해? 긍정적이거나 부정적인 경험?. Flask-Newbie에게는 너무 많은 옵션이 있습니다.
MongoDB 용으로 인기있는 Python ORM을 평가하는 데 많은 시간을 투자했습니다. 제가 정말 하나를 선택하고 싶었 기 때문에 이것은 철저한 운동이었습니다.
내 결론은 ORM이 MongoDB의 재미를 제거한다는 것입니다. 아무도 자연스럽지 않고, 처음에 관계형 데이터베이스에서 멀어지게 만든 것과 유사한 제한을 부과합니다.
다시 말하지만, 저는 정말로 ORM을 사용하고 싶었지만 이제는 pymongo
직접 사용 하는 것이 갈 길이 라고 확신합니다 . 이제 저는 MongoDB,, pymongo
Python 을 포괄하는 패턴을 따릅니다 .
리소스 지향 아키텍처는 매우 자연스러운 표현으로 이어집니다. 예를 들어 다음 사용자 리소스를 사용합니다.
from werkzeug.wrappers import Response
from werkzeug.exceptions import NotFound
Users = pymongo.Connection("localhost", 27017)["mydb"]["users"]
class User(Resource):
def GET(self, request, username):
spec = {
"_id": username,
"_meta.active": True
}
# this is a simple call to pymongo - really, do
# we need anything else?
doc = Users.find_one(spec)
if not doc:
return NotFound(username)
payload, mimetype = representation(doc, request.accept)
return Response(payload, mimetype=mimetype, status=200)
def PUT(self, request, username):
spec = {
"_id": username,
"_meta.active": True
}
operation = {
"$set": request.json,
}
# this call to pymongo will return the updated document (implies safe=True)
doc = Users.update(spec, operation, new=True)
if not doc:
return NotFound(username)
payload, mimetype = representation(doc, request.accept)
return Response(payload, mimetype=mimetype, status=200)
Resource
기본 클래스의 모습처럼
class Resource(object):
def GET(self, request, **kwargs):
return NotImplemented()
def HEAD(self, request, **kwargs):
return NotImplemented()
def POST(self, request, **kwargs):
return NotImplemented()
def DELETE(self, request, **kwargs):
return NotImplemented()
def PUT(self, request, **kwargs):
return NotImplemented()
def __call__(self, request, **kwargs):
handler = getattr(self, request.method)
return handler(request, **kwargs)
WSGI
사양을 직접 사용하고 Werkzeug
가능한 경우 활용 하십시오 (그런데 Flask
불필요한 복잡함을 추가 한다고 생각합니다 Werkzeug
).
이 함수 representation
는 요청의 Accept
헤더를 가져와 적절한 표현 (예 : application/json
, 또는 text/html
)을 생성합니다. 구현하는 것은 어렵지 않습니다. 또한 Last-Modified
헤더를 추가합니다 .
물론 입력 내용을 삭제해야하며 제시된대로 코드가 작동하지 않습니다 (예제로서 의미하지만 내 요점을 이해하는 것은 어렵지 않습니다).
다시 말하지만 모든 것을 시도했지만이 아키텍처는 내 코드를 유연하고 간단하며 확장 가능하게 만들었습니다.
참고URL : https://stackoverflow.com/questions/9447629/mongokit-vs-mongoengine-vs-flask-mongoalchemy-for-flask
'Program Tip' 카테고리의 다른 글
XAML (.NET 4 Framework 이전)에서 제네릭 형식을 지정할 수 있습니까? (0) | 2020.10.27 |
---|---|
자바 스크립트에서 전역 var와 window.variable의 차이점은 무엇입니까? (0) | 2020.10.27 |
새 창이 아닌 새 탭에서 링크를 열려면 어떻게해야합니까? (0) | 2020.10.27 |
Haskell의 Data.Typeable은 무엇입니까? (0) | 2020.10.27 |
Jenkins 편안한 API 참조는 어디에서 찾을 수 있습니까? (0) | 2020.10.27 |