1 2 3 4 5 6 7 8 | if (creds!==undefined){ nano=require('nano')(creds.url) } else { nano = require('nano')('http://localhost:5984'); } nano.db.create('storeall'); var db = nano.db.use('storeall'); |
1 2 3 4 | app.use(function(req,res,next){ req.db = db; next(); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | db.insert({itemname:itemname,itemvalue:itemvalue,itemremarks:itemremarks,itemdatecreated: itemdatecreated,itemdatemodified:itemdatemodified, itemusage:itemusage, itemvisible:itemvisible, crazy: true }, itemname,function(err, body, header) { if (err) { retObj.Result="ERROR"; retObj.Message="Could not create item or item already exists"; } else { retObj.Result = "OK"; retObj.Record=body; } res.send(json.stringify(retObj)); }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | db.insert({itemname:itemname,itemvalue:itemvalue,itemremarks:itemremarks,itemdatecreated: itemdatecreated,itemdatemodified:itemdatemodified, itemexpiration:itemexpiration, itemusage: itemusage, itemvisible:itemvisible, crazy: true, _rev:rev }, itemname,function(err, body, header) { if (err) { retObj.Result="ERROR"; retObj.Message="Error updating item...it was possibly updated by another user. Re-read it"; } else { retObj.Result = "OK"; retObj.Record=body; } res.send(json.stringify(retObj)); }); } else { retObj.Result="ERROR"; retObj.Message="Item not found"; res.send(json.stringify(retObj)); } }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | router.post('/itemlist', function(req, res) { var sorting_desceding = true; //determine the sort order from the querystring if (req.query.jtSorting !== undefined){ //Two possible values are ASC and DESC. If the last three characters of a value are ESC then //we assume they passed DESC else default to ASC sorting_descending = (req.query.jtSorting.substring(req.query.jtSorting.length-3)==='ESC'); } var db = req.db //jtPageSize = number of items to display on the table //jtStartIndex is the first item to display on the table, we tell cloudant to skip //x # of items so the start index //can be the index of the first item to display , params = {include_docs: true, skip: req.query.jtStartIndex, limit: req.query.jtPageSize, descending: sorting_descending}; db.list(params, function(error,body,headers) { var docs = []; var row; for (row in body.rows){ //the list functions displays all the databases along with the documents //so we extract the documents and send them to jtable //console.log('row text '+body.rows[row].id + ' and row=' + row); docs[row]=body.rows[row].doc; } var json_obj = {}; json_obj.Result = "OK"; json_obj.Records = docs; //adjust all the parameter that allows us to paginate the table json_obj.TotalRecordCount=body.total_rows; res.send(json.stringify(json_obj)); }); }); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |