unicloud云函数的学习之入门三

2023-05-25  本文已影响0人  焚心123

学习JQL语法,更方便我们使用条件语句的查询,之前的查询需要我们使用下方的代码,使用JQL可以更简便

const db = uniCloud.database()
const dbCmd = db.command
let res = await db.collection('table1').where({
  field1: dbCmd.gt(0)
}).get()
<template>
    <view class="content">
        <button @click="createdDb">新增</button>
        <button @click="updateDb">修改</button>
        <button @click="login">登录</button>
        <button @click="loginOut">退出登录</button>

    </view>
</template>

<script>
import { mutations } from '../../uni_modules/uni-id-pages/common/store.js'
const db = uniCloud.database();
export default {
    setup() {
        const login = ()=>{
            uni.navigateTo({
                url:'/uni_modules/uni-id-pages/pages/login/login-withpwd'
            })
        }
        const loginOut= ()=>{
            mutations.logout()
        }
        const updateDb = ()=>{
            db.collection('test').doc('646c6d6609e2989198080bd6').update({
                title:'我是修改后的数据'
            }).then(res=>{
                console.log(res);
            })
        }
        const createdDb = () => {
            db.collection('test')
                .add({
                    title:'文章333',
                    content:'我是文章内容'
                })
                .then((res) => {
                    console.log(res);
                });
        };

        return {
            createdDb,
            updateDb,
            loginOut,
            login
        };
    }
};
</script>

<style lang="scss"></style>

db.collection('user').where({
 id:'1111'
}).get()

2、jql的查询(可以直接写我们要查询的条件)

//先进行添加
db.collection('test')
    .add({
    title: '文章333',
    content: '我是文章内容'
    })
    .then((res) => {
      console.log(res);
    });

//查询
db.collection('test')
  .where("title=='文章333'")
  .get()
  .then((res) => {
  console.log(res);
  });

注意: 使用schema表返回状态等是定义好的,我们要是想修改怎么办呢?只能使用action云函数进行修改,相当于是axios的拦截和响应进行处理我们需要的格式

// 一个action文件示例 uni-clientDB-actions/add-todo.js
module.exports = {
  // 在数据库操作之前执行
  before: async(state,event)=>{
  
  },
  // 在数据库操作之后执行
  after:async (state,event,error,result)=>{
    // state为当前数据库操作状态其格式见下方说明
    // event为传入云函数的event对象
    // error为执行操作的错误对象,如果没有错误error的值为null
    // result为执行command返回的结果
    console.log(state,event,error,result,'actions---------');
    if(error) {
      throw error
    }
    
    if(state.collection ==='test'){//请求的表为test表,具体的实现可以使用event我们传入的值进行判断也可以
        if(!error){
            result.errMsg='查询成功'
            result.errCode=200
            return result
        }else{
            result.errMsg='查询失败'
            result.errCode=500
            return result
        }
    }
   
    
    return result
  }
}

//err-msg这个就是我们创建的action文件名
db.action('err-msg').collection('test')
    .where("title=='文章333'")
    get()
    .then((res) => {
      console.log(res);
    });

联表查询,上面的查询是单表查询就是单独一张表进行查询,联表查询:意思就是可以两张表或多张表进行查询,但是必须都依赖同一个主表,这样才可以进行多张一起查询

// 直接关联多个表为虚拟联表再进行查询,旧写法,目前更推荐使用getTemp进行联表查询
const res = await db.collection('order,book').where('_id=="1"').get() // 直接关联order和book之后再过滤

// 使用getTemp先过滤处理获取临时表再联表查询,推荐用法
const order = db.collection('order').where('_id=="1"').getTemp() // 注意结尾的方法是getTemp,对order表过滤得到临时表
const res = await db.collection(order, 'book').get() // 将获取的order表的临时表和book表进行联表查询
const test = db.collection('test').field('userId,title,content').getTemp()//虚拟联表
const test1 = db.collection('test1').field('userId,title,num').getTemp()
const user = db.collection('uni-id-users').field('_id,username').getTemp()
db.collection(user,test,test1).get().then(res=>{
    console.log(res);
})

注意: 上面的db.collection(test,user)。test在前就会直接返回userId为属性的将user数据包裹在里面,反之亦然,要是user在前会以_id为属性,返回的结果是不一样的

const test = db.collection('test').field('title,content,userId').getTemp()//虚拟联表
            
const user = db.collection('uni-id-users').field('_id,username as name,nickname').getTemp()
db.collection(test,user).get().then(res=>{
    console.log(res);
})
image.png
上一篇 下一篇

猜你喜欢

热点阅读