首页 > 数据库 >数据库触发器模板

数据库触发器模板

时间:2023-02-09 16:35:58浏览次数:46  
标签:触发器 clientInfo const 数据库 db collection uniCloud where 模板

  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

相关文章

  • 云对象模板
    1//云对象教程:https://uniapp.dcloud.net.cn/uniCloud/cloud-obj2//jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/1293mo......
  • DM数据库备份与恢复
    1.环境介绍操作系统:CentOS7.6数据库:DM82.备份与恢复           参考:https://eco.dameng.com/document/dm/zh-cn/ops/physical-backup-restor......
  • 数据库必知必会:TiDB(4)TiKV分布式事务
    (数据库必知必会:TiDB(4)TiKV分布式事务)分布式事务存在的问题假设有这样一个事务:begin:updatepersonsetname='Tom'whereid=1;updatepersonsetname='Jack'......
  • 数据库/SQL教学推荐用什么样SQL工具?必须管理方便,轻松上手的
    SQL语言逐渐成为职场人士必备的能力。很多人一直走上职场才了解什么是SQL,而更多人在大学就已经开始学习。这些人一定对类似《数据库原理与应用》的课程不陌生。还记得你们......
  • 分布式数据库设计——数据一致性设计原则
    摘要数据一致性,因为它是复制一致性和分布式事务的理论基础。在现实世界中,分布式数据库的节点并不总是处于活动状态且相互能够通信的。但是,以上这些故障不应该影响数据库的可......
  • MYSQL——真实生产环境的数据库机器配置
    摘要介绍真实项目中数据库配置选型,机器压测指标等,以帮助项目设计构建MYSQL集群能够符合你的业务。以下是个人在真实生产环境总结的的相关的参数,仅仅供大家参考。一、生产环......
  • 数据库架构设计——数据库选型
    摘要架构师在工作中经常会遇到数据库存储选型的问题,而市面上数据库产品众多,往往会无从下手,甚至有时候从业务开发到上线运维过程中会多次更换底层数据库,给整个研发中心带来不......
  • 分布式数据库设计——超大规模数据存储
    摘要随着互联网时代,特别是移动互联网的到来,形形色色的企业都在将自己的系统平台快速升级迭代,以此作为向互联网转型的一部分。在此背景下,这类应用平台所依赖的数据库系统就需......
  • 分布式数据库设计——数据复制方案设计
    摘要分片技术,它主要的目的是提高数据容量和性能。复制的主要目的是在几个不同的数据库节点上保留相同数据的副本,从而提供一种数据冗余。这份冗余的数据可以提高数据查询性能......
  • flask web 项目5 连接数据库
    连接数据库pymysql、Flask_sqlalchemy #在app.config中设置好连接数据库的信息#然后使用SQLAlchemy(app)创建一个db对象#SQLAlchemy会自动读取app.config中连接数据......