Skip to navigation
Async pouchdb wrapper to put and get data
26.11.20
async function get_rev(db,doc_name) { try { let d1 = await db.get(doc_name); return d1['_rev']; } catch (err) { try { var r = await db.put({_id: '_local/'+ doc_name}); return r['rev']; } catch (err) { console.log(err); } } return false; } async function set_doc(db, doc_name, val, rev=false) { try { if(rev === false) { let rev = await get_rev(db, doc_name); } let r = await db.put({ _id: '_local/'+doc_name, _rev: rev, v: val, unix: unixTime = Math.floor(Date.now() / 1000) }); return r['rev']; } catch(err) { console.log(err); } return false; } async function get_doc(db, doc_name, api, exp=86400) { let data = []; try { let doc = await db.get('_local/' + doc_name); let unix = doc['unix']; let now = Math.floor(Date.now() / 1000); let to_exp = now - exp; let rev = doc['_rev']; //console.log(unix); if(unix > to_exp) { data = doc['v']; } else { data = JSON.parse(await aget_api(api)); await set_doc(db, doc_name, data, rev); } } catch (err) { data = JSON.parse(await aget_api(api)); await set_doc(db, doc_name, data); } return data; }
https://pouchdb.com/api.html#fetch_document
Reply
Anonymous
Information Epoch 1732567628
You can always add complexity.
Home
Notebook
Contact us