Skip to content

Commit 17c4b8d

Browse files
committed
mongo backend
1 parent 8c06e88 commit 17c4b8d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.pyc
22
*.sw*
33
*.DS_Store
4+
db/*

lib/databases/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import disk
2+
import mongo

lib/databases/mongo.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python2.7
2+
3+
import pymongo
4+
import lib
5+
6+
_DB = None
7+
_CLIENT = None
8+
def init(uri='mongodb://localhost:27017/', test=False):
9+
global _DB, _CLIENT
10+
_CLIENT = pymongo.MongoClient(uri)
11+
_DB = _CLIENT["bigdipster"]
12+
13+
14+
def save(table, data, require_all_fields=True, overwrite_id=False):
15+
global _DB
16+
try:
17+
item_id = _DB[table].insert(data)
18+
return str(item_id)
19+
except KeyError:
20+
raise lib.ItemNotFound(item_id)
21+
22+
def update(table, item_id, data, require_all_fields=True):
23+
global _DB
24+
try:
25+
_DB[table].update({"_id" : item_id}, {"$set" : data})
26+
except KeyError:
27+
raise lib.ItemNotFound(item_id)
28+
29+
def get(table, item_id):
30+
global _DB
31+
try:
32+
_DB[table].find_one({"_id" : item_id})
33+
except KeyError:
34+
raise lib.ItemNotFound(item_id)
35+
36+
def exists(table, item_id):
37+
global _DB
38+
try:
39+
_DB[table].find_one({"_id" : item_id})
40+
except KeyError:
41+
raise lib.ItemNotFound(item_id)

0 commit comments

Comments
 (0)