diff --git a/packages/uni-components/lib/unicloud-db/unicloud-db.vue b/packages/uni-components/lib/unicloud-db/unicloud-db.vue index 801d7bc45d07f6dad1a36c9471d5ddae3673d10d..aa05712faa9d761eb67bc7565d337ad25a105907 100644 --- a/packages/uni-components/lib/unicloud-db/unicloud-db.vue +++ b/packages/uni-components/lib/unicloud-db/unicloud-db.vue @@ -77,7 +77,7 @@ export default { } }, collection: { - type: String, + type: [String, Array], default: '' }, action: { @@ -172,6 +172,21 @@ export default { errorMessage: '' } }, + computed: { + collectionArgs () { + return Array.isArray(this.collection) ? this.collection : [this.collection] + }, + isLookup () { + return (Array.isArray(this.collection) && this.collection.length > 1) || (typeof this.collection === 'string' && this.collection.indexOf(',') > -1) + }, + mainCollection () { + if (typeof this.collection === 'string') { + return this.collection.split(',')[0] + } + const mainQuery = JSON.parse(JSON.stringify(this.collection[0])) + return mainQuery.$db[0].$param[0] + } + }, created() { this._isEnded = false this.paginationInternal = { @@ -319,7 +334,7 @@ export default { db = db.action(action) } - db.collection(this._getCollection()).add(value).then((res) => { + db.collection(this.mainCollection).add(value).then((res) => { success && success(res) if (showToast) { uni.showToast({ @@ -393,7 +408,7 @@ export default { db = db.action(action) } - return db.collection(this._getCollection()).doc(id).update(value).then((res) => { + return db.collection(this.mainCollection).doc(id).update(value).then((res) => { success && success(res) if (showToast) { uni.showToast({ @@ -415,52 +430,7 @@ export default { complete && complete() }) }, - _execLoadData(callback, clear) { - if (this.loading) { - return - } - this.loading = true - this.errorMessage = '' - - return this._getExec().then((res) => { - this.loading = false - const { - data, - count - } = res.result - this._isEnded = count !== undefined ? (this.paginationInternal.current * this.paginationInternal.size >= count) : (data.length < this.pageSize) - this.hasMore = !this._isEnded - - const data2 = this.getone ? (data.length ? data[0] : undefined) : data - - if (this.getcount) { - this.paginationInternal.count = count - } - - callback && callback(data2, this._isEnded, this.paginationInternal) - this._dispatchEvent(events.load, data2) - - if (this.getone || this.pageData === pageMode.replace) { - this.dataList = data2 - } else { - if (clear) { - this.dataList = data2 - } else { - this.dataList.push(...data2) - } - } - - }).catch((err) => { - this.loading = false - this.errorMessage = err - callback && callback() - this.$emit(events.error, err) - if (process.env.NODE_ENV === 'development') { - console.error(err) - } - }) - }, - _getExec() { + getTemp(isTemp = true) { /* eslint-disable no-undef */ let db = uniCloud.database() @@ -468,7 +438,7 @@ export default { db = db.action(this.action) } - db = db.collection(this.collection) + db = db.collection(...this.collectionArgs) if (!(!this.where || !Object.keys(this.where).length)) { db = db.where(this.where) @@ -510,10 +480,77 @@ export default { if (this.gettreepath) { getOptions.getTreePath = treeOptions } - db = db.skip(size * (current - 1)).limit(size).get(getOptions) + db = db.skip(size * (current - 1)).limit(size) + + if (isTemp) { + db = db.getTemp(getOptions) + db.udb = this + } else { + db = db.get(getOptions) + } return db }, + setResult(result) { + if (result.code === 0) { + this._execLoadDataSuccess(result) + } else { + this._execLoadDataFail(new Error(result.message)) + } + }, + _execLoadData(callback, clear) { + if (this.loading) { + return + } + this.loading = true + this.errorMessage = '' + + return this._getExec().then((res) => { + this.loading = false + this._execLoadDataSuccess(res.result, callback, clear) + }).catch((err) => { + this.loading = false + this._execLoadDataFail(err, callback) + }) + }, + _execLoadDataSuccess(result, callback, clear) { + const { + data, + count + } = result + this._isEnded = count !== undefined ? (this.paginationInternal.current * this.paginationInternal.size >= count) : (data.length < this.pageSize) + this.hasMore = !this._isEnded + + const data2 = this.getone ? (data.length ? data[0] : undefined) : data + + if (this.getcount) { + this.paginationInternal.count = count + } + + callback && callback(data2, this._isEnded, this.paginationInternal) + this._dispatchEvent(events.load, data2) + + if (this.getone || this.pageData === pageMode.replace) { + this.dataList = data2 + } else { + if (clear) { + this.dataList = data2 + } else { + this.dataList.push(...data2) + } + } + }, + _execLoadDataFail(err, callback) { + this.errorMessage = err + callback && callback() + this.$emit(events.error, err) + if (process.env.NODE_ENV === 'development') { + console.error(err) + } + }, + _getExec() { + return this.getTemp(false) + }, _execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle) { if (!this.collection || !id) { return @@ -540,7 +577,7 @@ export default { exec = exec.action(action) } - exec.collection(this._getCollection()).where({ + exec.collection(this.mainCollection).where({ _id: dbCmd.in(ids) }).remove().then((res) => { success && success(res.result) @@ -564,11 +601,6 @@ export default { complete && complete() }) }, - _getCollection() { - const index = this.collection.indexOf(',') - const collection = index > 0 ? this.collection.substring(0, index) : this.collection - return collection - }, removeData(ids) { const il = ids.slice(0) const dl = this.dataList