mongodb - Python Eve: Add custom route, changing an object manually -
i started using eve , it's great getting full rest api run. however, i'm not entirely convinced rest perfect in cases, e.g. i'd have simple upvote route can increase counter of object. if manually retrieve object, increase counter, , update it, can run problems getting out-of-sync. i'd add simple extra-route, e.g. /resource/upvote increases upvote count 1 , returns object.
i don't know how "hacky" is, if it's over-the-top please tell me. don't see problem having custom routes important tasks work in restful way. know treat upvotes own resource, hey thought we're doing mongodb, let's not overly relational.
so here far got:
@app.route('/api/upvote/<type>/<id>') def upvote(type, id): obj = app.data.find_one_raw(type, id) obj['score'] += 1
- problem #1
find_one_raw
returns none time. guess have convert id parameter? (i'm using native mongodb objectid) - problem #2 how save object? don't see handy easy-to-use method
save_raw
- problem #3 can wrap whole thing in transaction or similar make sure it's thread-safe? (i'm new mongodb can tell).
1: type
happens python keyword. mean resource_type
?
2: there app.data.insert
(to create new) or app.data.update
(to update existing one)
3: apparently there no transactions in mongodb apparent this thread (as can tell, new mongodb myself)
Comments
Post a Comment