README
wx-cloud-db-falsework
a falsework for weixin miniProgram cloud database
微信小程序云开发数据库脚手架
Install
npm install wx-cloud-db-falsework
Quick Start
import { DB, Model } from 'path/wx-cloud-db-falsework.min'
class Order extends Model {
constructor(o) {
let t = super(o)
return t
}
}
// collectionName 默认为model名,如Order默认为order
// collectionKey 默认_id
// cloud 小程序端传 wx.cloud, 云函数端传require('wx-server-sdk')
const Odr = Order.init({ env: '环境ID', cloud, [, collectionName = '集合名称', collectionKey = '主键字段名'] })
let row = await Odr.doc('xxxx').get()
let row = await Odr.find('xxxx')
let row = await Odr.where({_id: 'xxxx'}).get()
let row = await Odr.find(`{_id: this._.eq('xxxx')}`)
let row = await Odr.find({_id: Odr._.eq('xxxx')})
if(row instanceof DB.DbError){
console.log('error', row)
}else if(row instanceof DB.DbRes){
console.log('execed sql ==> ', row.sql)
if(row instanceof DB.DbResultJson){
console.log('DbResultJson ==> ', row)
}else {
// row instanceof DB.DbResultArray
console.log('DbResultArray ==> ', row)
}
// update
let back = await row.update({
abc: 888
}) ==> // { updated:Number }
// remove
let back = await row.remove() ==> // { removed:Number }
}
Other Demo
await Odr.add({_id: 'xxxx', abc: 123})
await Odr.add(`{_id: 'xxxx'}`)
await Odr.add({ data:{_id: 'xxxx'}, success(res){}, fail(res){}, complete(res){} })
await Odr.update({abc: 456}, {_id: 'xxxx'})
await Odr.where({_id: 'xxxx'}).update({abc: 456})
await Odr.doc('xxxx').update({abc: 456})
await Odr.doc('xxxx').update({ data: {abc: 456}, success(res){}, fail(res){}, complete(res){} })
await Odr.aggregate().match({abc: 456}).end()
await Odr.doc('xxxx').remove()
await Odr.where({_id: 'xxxx'}).remove()
await Odr.remove({_id: 'xxxx'})
await Odr.remove({ where: {_id: 'xxxx'}, success(res){}, fail(res){}, complete(res){} })
await Odr.remove(`{_id: 'xxxx', abc: this._.eq(456)}`)
await Odr.remove({_id: 'xxxx', abc: Odr_.eq(456)})