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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| module.exports = { /** * params:{ * data:xxxx * } */ addItem: function (dbName, params){ if (!params){ console.error(`[数据库]${dbName} [新增记录],参数空`); } const data = { data: params } const db = wx.cloud.database(); return db.collection(dbName).add(data).then((res) => { console.log(`[数据库]${dbName} [新增记录] 成功,记录 _id:`, params, res._id) }).catch(err => { console.error(`[数据库]${dbName} [新增记录] 失败:`, params, err) }) }, /** * where:{ * _openid:openid * } */ queryItem:function(dbName,where){ const db = wx.cloud.database(); return new Promise((resolve,reject) => { db.collection(dbName).where(where).get().then(res => { const data = res.data; console.log(`[数据库]${dbName} [查询记录] 成功:`, where, data); resolve(data) }).catch(err => { console.log(`[数据库]${dbName} [查询记录] 失败:`, where, err); reject(err); }) }); }, /** * where:{ * _openid:openid * } */ queryLimitItem: function (dbName, where, { skip, limit }) { const db = wx.cloud.database(); return new Promise((resolve, reject) => { db.collection(dbName).where(where).skip(skip).orderBy('subIndex','asc').limit(limit).get().then(res => { const data = res.data; console.log(`[数据库]${dbName} [查询记录] 成功:`, where, data); resolve(data) }).catch(err => { console.log(`[数据库]${dbName} [查询记录] 失败:`, where, err); reject(err); }) }); }, /** * where:{ * _openid:openid * } * params:{ * data:xxx * } */ updateItem:function(dbName,where,params){ if (!params) { console.error(`[数据库]${dbName} [更新记录],参数空`); } const data = { data: params } const db = wx.cloud.database(); return db.collection(dbName).where(where).update(data).then(res => { console.log(`[数据库]${dbName} [更新记录] 成功:`, where, params ,res); }).catch(err => { console.error(`[数据库]${dbName} [更新记录] 失败:`, where, params, err); }); }, /** * where:{ * _openid:openid * } */ deleteItem: function(dbName, where) { const db = wx.cloud.database(); return db.collection(dbName).where(where).then(res => { console.log(`[数据库]${dbName} [删除记录]${uniqueId} 成功:`, params, res); }).catch(err => { onsole.error(`[数据库]${dbName} [删除记录] ${uniqueId} 失败:`, params, err); }); }, }
|