unicloud-database.uvue 9.4 KB
Newer Older
雪洛's avatar
雪洛 已提交
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
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @click="dbAdd">新增单条数据</button>
          <button type="primary" @click="dbBatchAdd">新增多条数据</button>
          <button type="primary" @click="dbUpdate">更新数据</button>
          <button type="primary" @click="dbGet">where传字符串获取数据</button>
          <button type="primary" @click="dbGetWithCommand">where传对象获取数据</button>
          <button type="primary" @click="dbRemove">删除数据</button>
          <button type="primary" @click="dbLookupInit">初始化联表查询数据</button>
          <button type="primary" @click="dbLookup">联表查询</button>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>
<script>
  export default {
    data() {
      return {
雪洛's avatar
雪洛 已提交
28 29 30 31 32 33 34 35 36
        title: 'ClientDB',
        addId: '',
        batchAddIds: [] as Array<string>,
        batchAddinserted: 0,
        updateUpdated: 0,
        getData: [] as Array<UTSJSONObject>,
        getWithCommandData: [] as Array<UTSJSONObject>,
        removeDeleted: 0,
        lookupData: [] as Array<UTSJSONObject>,
雪洛's avatar
雪洛 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      }
    },
    onLoad() {
    },
    onUnload() {
    },
    methods: {
      dbAdd: function () {
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
          .add({
            num: 1,
            tag: 'default-tag',
            date: new Date(),
            point: new db.Geo.Point(116, 38)
          })
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
59
            this.addId = res.id
雪洛's avatar
雪洛 已提交
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
            uni.showModal({
              content: `新增成功,id: ${res.id}`,
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
      dbBatchAdd() {
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
          .add([{
            num: 2,
            tag: 'default-tag',
          }, {
            num: 3,
            tag: 'default-tag',
          }])
          .then<void>((res) => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
91 92
            this.batchAddIds = res.ids
            this.batchAddinserted = res.inserted
雪洛's avatar
雪洛 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            uni.showModal({
              content: `新增成功,id列表: ${res.ids.join(',')}`,
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
雪洛's avatar
雪洛 已提交
108
      dbGet() {
雪洛's avatar
雪洛 已提交
109 110 111 112 113 114 115 116
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
          .where(
            'tag == "default-tag"'
          )
雪洛's avatar
雪洛 已提交
117 118 119 120 121
          .field('num, tag')
          .orderBy('num desc')
          .skip(1)
          .limit(2)
          .get()
雪洛's avatar
雪洛 已提交
122 123 124
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
125
            this.getData = res.data
雪洛's avatar
雪洛 已提交
126
            uni.showModal({
雪洛's avatar
雪洛 已提交
127
              content: `获取成功,取到了${res.data.length}条数据`,
雪洛's avatar
雪洛 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
雪洛's avatar
雪洛 已提交
141
      dbGetWithCommand() {
雪洛's avatar
雪洛 已提交
142 143 144 145 146
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
雪洛's avatar
雪洛 已提交
147 148 149
          .where({
            num: db.command.gt(1)
          })
雪洛's avatar
雪洛 已提交
150 151 152 153 154 155 156 157
          .field('num, tag')
          .orderBy('num desc')
          .skip(1)
          .limit(2)
          .get()
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
158
            this.getWithCommandData = res.data
雪洛's avatar
雪洛 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
            uni.showModal({
              content: `获取成功,取到了${res.data.length}条数据`,
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
雪洛's avatar
雪洛 已提交
174
      dbUpdate() {
雪洛's avatar
雪洛 已提交
175 176 177 178 179
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
雪洛's avatar
雪洛 已提交
180 181 182 183 184
          .where(
            'tag == "default-tag"'
          )
          .update({
            num: 4
雪洛's avatar
雪洛 已提交
185 186 187 188
          })
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
189
            this.updateUpdated = res.updated
雪洛's avatar
雪洛 已提交
190
            uni.showModal({
雪洛's avatar
雪洛 已提交
191
              content: `更新成功,更新了${res.updated}条数据`,
雪洛's avatar
雪洛 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
      dbRemove() {
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('type')
          .where(
            'tag == "default-tag"'
          )
          .remove()
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
218
            this.removeDeleted = res.deleted
雪洛's avatar
雪洛 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
            uni.showModal({
              content: `删除成功,删掉了${res.deleted}条数据`,
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
      dbLookupInit() {
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        db.collection('local')
          .where('tag == "default-tag"')
          .remove()
          .then(() : Promise<UniCloudDBRemoveResult> => {
            return db.collection('foreign')
              .where('tag == "default-tag"')
              .remove()
          })
          .then(() : Promise<UniCloudDBBatchAddResult> => {
            return db.collection('local')
              .add([{
                id: "local_1",
                name: "local_1_name",
                tag: "default-tag",
                foreign_id: "foreign_1"
              }, {
                id: "local_2",
                name: "local_2_name",
                tag: "default-tag",
                foreign_id: "foreign_2"
              }])
          })
          .then(() : Promise<UniCloudDBBatchAddResult> => {
            return db.collection('foreign')
              .add([{
                id: "foreign_1",
                name: "foreign_1_name",
                tag: "default-tag"
              }, {
                id: "foreign_2",
                name: "foreign_2_name",
                tag: "default-tag"
              }])
          })
          .then<void>((_) : void => {
            uni.hideLoading()
            uni.showModal({
              content: '数据初始化成功',
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            console.error(err)
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      },
      dbLookup() {
        uni.showLoading({
          title: '加载中...'
        })
        const db = uniCloud.databaseForJQL()
        const local = db.collection('local')
          .where('tag == "default-tag"')
          .getTemp()
        const foreign = db.collection('foreign')
          .where('tag == "default-tag"')
          .getTemp()
        db.collection(local, foreign)
          .get()
          .then<void>(res => {
            uni.hideLoading()
            console.log(res)
雪洛's avatar
雪洛 已提交
307
            this.lookupData = res.data
雪洛's avatar
雪洛 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            uni.showModal({
              content: `联表查询成功,取到了${res.data.length}条数据`,
              showCancel: false
            })
          })
          .catch<void>((err : any | null) => {
            uni.hideLoading()
            const error = err as UniCloudError
            uni.showModal({
              title: '错误',
              content: error.errMsg,
              showCancel: false
            })
          })
      }
    }
  }
</script>

<style>
</style>