1 // schema扩展相关文档请参阅:https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html 2 module.exports = { 3 trigger: { 4 // 写入JQL数据库的标准写法 5 // read触发器 6 beforeRead: async function({ 7 collection, 8 where, 9 field, 10 clientInfo, 11 userInfo, 12 result, 13 triggerContext, 14 subCollection, 15 rawWhere, 16 rawGeoNear, 17 skip, 18 limit, 19 sample, 20 docId, 21 operation 22 }) { 23 //非jql操作系统库不会触发数据库触发器 24 //const db = uniCloud.database() 25 26 //调用JQL操作数据库 27 /* const db = uniCloud.databaseForJQL({ 28 clientInfo, 29 skipTrigger: true 30 }) */ 31 32 //示例:修改文章更新时间 33 /* const id = where && where._id 34 if (typeof id === 'string' && (updateData.title || updateData.content)) { //如果字段较多,也可以不列举字段,删掉后半个判断 35 if (updateData.content) { 36 // updateData.summary = 'xxxx' // 根据content生成summary 37 } 38 updateData.update_date = Date.now() // 更新数据的update_date字段赋值为当前服务器时间 39 } */ 40 }, 41 afterRead: async function({ 42 collection, 43 where, 44 field, 45 clientInfo, 46 userInfo, 47 result, 48 triggerContext, 49 subCollection, 50 rawWhere, 51 rawGeoNear, 52 skip, 53 limit, 54 sample, 55 docId, 56 operation 57 }) { 58 //调用JQL操作数据库 59 /* const db = uniCloud.databaseForJQL({ 60 clientInfo, 61 skipTrigger: true 62 }) */ 63 64 //读取后触发实现阅读量加1 65 /* const db = uniCloud.database() 66 const id = where && where._id 67 // clientInfo.uniIdToken可以解出客户端用户信息,再进行判断是否应该加1。为了让示例简单清晰,此处省略相关逻辑 68 if (typeof id === 'string' && field.includes('content')) { 69 // 读取了content字段后view_count加1 70 await db.collection('article').where(where).update({ 71 view_count: db.command.inc(1) 72 }) 73 } */ 74 }, 75 76 //create触发器 77 beforeCreate: async function({ 78 collection, 79 where, 80 addDataList, 81 clientInfo, 82 userInfo, 83 result, 84 triggerContext, 85 operation 86 }) { 87 //调用JQL操作数据库 88 /* const db = uniCloud.databaseForJQL({ 89 clientInfo, 90 skipTrigger: true 91 }) */ 92 93 //示例:新增文章时自动添加摘要 94 /* for (let i = 0; i < addDataList.length; i++) { 95 const addDataItem = addDataList[i] 96 if (!addDataItem.content) { 97 throw new Error('缺少文章内容') 98 } 99 addDataItem.summary = addDataItem.content.slice(0, 100) 100 } */ 101 }, 102 afterCreate: async function({ 103 collection, 104 where, 105 addDataList, 106 clientInfo, 107 userInfo, 108 result, 109 triggerContext, 110 operation 111 }) { 112 //调用JQL操作数据库 113 /* const db = uniCloud.databaseForJQL({ 114 clientInfo, 115 skipTrigger: true 116 }) */ 117 }, 118 119 //update触发器 120 beforeUpdate: async function({ 121 collection, 122 where, 123 updateData, 124 clientInfo, 125 userInfo, 126 result, 127 triggerContext, 128 rawWhere, 129 docId, 130 operation 131 }) { 132 //调用JQL操作数据库 133 /* const db = uniCloud.databaseForJQL({ 134 clientInfo, 135 skipTrigger: true 136 }) */ 137 }, 138 afterUpdate: async function({ 139 collection, 140 where, 141 updateData, 142 clientInfo, 143 userInfo, 144 result, 145 triggerContext, 146 rawWhere, 147 docId, 148 operation 149 }) { 150 //调用JQL操作数据库 151 /* const db = uniCloud.databaseForJQL({ 152 clientInfo, 153 skipTrigger: true 154 }) */ 155 }, 156 157 //delete触发器 158 beforeDelete: async function({ 159 collection, 160 where, 161 clientInfo, 162 userInfo, 163 result, 164 triggerContext, 165 rawWhere, 166 docId, 167 operation 168 }) { 169 //调用JQL操作数据库 170 /* const db = uniCloud.databaseForJQL({ 171 clientInfo, 172 skipTrigger: true 173 }) */ 174 175 //示例:删除前备份 176 /* const db = uniCloud.database() 177 const id = where && where._id 178 if (typeof id !== 'string') { // 此处也可以加入管理员可以批量删除的逻辑 179 throw new Error('禁止批量删除') 180 } 181 const res = await db.collection('article').where(where).get() 182 const record = res.data[0] 183 if (record) { 184 await db.collection('article-archived').add(record) 185 } */ 186 }, 187 afterDelete: async function({ 188 collection, 189 where, 190 clientInfo, 191 userInfo, 192 result, 193 triggerContext, 194 rawWhere, 195 docId, 196 operation 197 }) { 198 //调用JQL操作数据库 199 /* const db = uniCloud.databaseForJQL({ 200 clientInfo, 201 skipTrigger: true 202 }) */ 203 }, 204 205 //count触发器 206 beforeCount: async function({ 207 collection, 208 where, 209 clientInfo, 210 userInfo, 211 result, 212 triggerContext, 213 operation 214 }) { 215 //调用JQL操作数据库 216 /* const db = uniCloud.databaseForJQL({ 217 clientInfo, 218 skipTrigger: true 219 }) */ 220 }, 221 afterCount: async function({ 222 collection, 223 where, 224 clientInfo, 225 userInfo, 226 result, 227 triggerContext, 228 operation 229 }) { 230 //调用JQL操作数据库 231 /* const db = uniCloud.databaseForJQL({ 232 clientInfo, 233 skipTrigger: true 234 }) */ 235 } 236 } 237 }
标签:触发器,clientInfo,const,数据库,db,collection,uniCloud,where,模板 From: https://www.cnblogs.com/wm218/p/17105737.html