def collection_add_document(name): doc = request.get_json() print('\ncollection [%s] add %s\n' % (name, json.dumps(doc))) fp = os.path.join(DIR_COLLECTIONS, '%s.json' % name) ls = [] if os.path.exists(fp): ls = ioutils.loadJson(fp) doc['_id'] = uuid.uuid4().hex ls = ls + [doc] ioutils.saveText(fp, json.dumps(ls)) return doc['_id']
def collection_get_document_by_id(name, docid): print('\ncollection [%s] save %s\n' % (name, docid)) fp = os.path.join(DIR_COLLECTIONS, '%s.json' % name) ls = [] if os.path.exists(fp): ls = ioutils.loadJson(fp) for i in range(len(ls)): if ls[i]['_id'] == docid: return json.dumps(ls[i]) return abort(404)
def collection_delete_document_by_id(name, docid): print('\ncollection [%s] DELETE %s\n' % (name, docid)) fp = os.path.join(DIR_COLLECTIONS, '%s.json' % name) ls = [] if os.path.exists(fp): ls = ioutils.loadJson(fp) for i in range(len(ls)): if ls[i]['_id'] == docid: del ls[i] break ioutils.saveText(fp, json.dumps(ls)) return 'ok'
def collection_save_document_by_id(name, docid): doc = request.get_json() print('\ncollection [%s] save %s\n' % (name, docid)) fp = os.path.join(DIR_COLLECTIONS, '%s.json' % name) ls = [] if os.path.exists(fp): ls = ioutils.loadJson(fp) doc['_id'] = docid for i in range(len(ls)): if ls[i]['_id'] == docid: ls[i] = doc ls[i]['_id'] = docid ioutils.saveText(fp, json.dumps(ls)) return 'ok'
def collection_get(name): print('\n collection [%s] get all\n' % name) fp = os.path.join(DIR_COLLECTIONS, '%s.json' % name) if os.path.exists(fp): return json.dumps(ioutils.loadJson(fp)) return '[]'