uni-app操作數(shù)據(jù)庫的三種方法總結(jié)
前端與后端(云端)分離實現(xiàn)數(shù)據(jù)庫的操作
1.使用云函數(shù)來操作數(shù)據(jù)庫
第一步:
創(chuàng)建一個云函數(shù)并部署
第二部:
在云函數(shù)中寫入數(shù)據(jù)庫的操作代碼:
const db = uniCloud.database(); exports.main = async (event, context) => { //event為客戶端上傳的參數(shù) //返回數(shù)據(jù)給客戶端 return await db.collection("News_system").add({ xingming:"張三" }) };
第三步:
前端代碼:
<template> <view> <button @click="add">添加</button> </view> </template> <script> // const db = uniCloud.database(); export default { data() { return { } }, onLoad() { }, methods: { add(){ // return db.collection("News_system").add({ // xingming:"1111" // }).then(res=>{ // console.log(res) // }) uniCloud.callFunction({ name:"hanshu1" }).then(res=>{ console.log(res) }) } } } </script> <style> </style>
顯示數(shù)據(jù)庫數(shù)據(jù)表信息添加情況:
2.使用云對象來操作數(shù)據(jù)庫
第一步:
先創(chuàng)建一個云對象并寫入相應的數(shù)據(jù)庫操作的代碼:
const db = uniCloud.database(); module.exports = { _before: function () { // 通用預處理器 }, add:()=>{ db.collection("News_system").add({ nianling:16 }) } }
第二步:
將其上傳部署!
第三步前端代碼:
<template> <view> <button @click="add">添加</button> </view> </template> <script> const duixiang1 = uniCloud.importObject("duixiang1") // const db = uniCloud.database(); export default { data() { return { } }, onLoad() { }, methods: { add(){ duixiang1.add().then(res=>{ console.log(res) }) // return db.collection("News_system").add({ // xingming:"1111" // }).then(res=>{ // console.log(res) // }) // uniCloud.callFunction({ // name:"hanshu1" // }).then(res=>{ // console.log(res) // }) } } } </script> <style> </style>
注意:我這里必選先調(diào)用一下我們寫的云對象,然后調(diào)用后直接對我們新賦值的參數(shù)進行操作即可!
const duixiang1 = uniCloud.importObject("duixiang1")
duixiang1.add().then(res=>{ console.log(res)
回到數(shù)據(jù)庫:我們可以發(fā)現(xiàn)我們點擊按鈕時,我們想添加的數(shù)據(jù)已經(jīng)在我們的數(shù)據(jù)庫中了!
前端與后端(云端)結(jié)合實現(xiàn)客戶端操作數(shù)據(jù)庫
3.使用DB Schema結(jié)構(gòu)規(guī)范實現(xiàn)客戶端對數(shù)據(jù)庫的操作
第一步:
我們線在數(shù)據(jù)庫中創(chuàng)建一個數(shù)據(jù)表。
第二步:
在 uniCloud中的database中右鍵,后選擇下載所有DB Schema及擴展校驗函數(shù)。
等在下載完畢后我們會發(fā)現(xiàn)
我們的數(shù)據(jù)表:News_system已經(jīng)在database中。
第三步:
然后我們打開它后,我們將原來的表文件改為:
注意:
將:
"read": true, "create": true,
是將我們數(shù)據(jù)表中的權(quán)限的”讀取“和”添加“打開!
properties中我們需要添加我們結(jié)合前端的需要在數(shù)據(jù)表中添加的數(shù)據(jù)(請結(jié)合第四步理解):
"xingming": { "bsonType": "string", "title": "姓名" }
第四步:
<template> <view> <button @click="add">添加</button> </view> </template> <script> const db = uniCloud.database(); export default { data() { return { } }, onLoad() { }, methods: { add(){ return db.collection("News_system").add({ xingming:"1111" }).then(res=>{ console.log(res) }) } } } </script> <style> </style>
我們直接在前端對數(shù)據(jù)庫進行一系列的操作:
const db = uniCloud.database();
return db.collection("News_system").add({ xingming:"1111" }).then(res=>{ console.log(res) })
我們這里想要添加的
xingming:"1111"
要必須在第三步中的數(shù)據(jù)表中進行xingming這個參數(shù)名的配置。
如第三步中的:
"xingming": { "bsonType": "string", "title": "姓名" }
回到前端頁面:
我們發(fā)現(xiàn),當我們點擊添加時,后臺已經(jīng)給我們打印出來了東西,說明我們的數(shù)據(jù)表的權(quán)限已經(jīng)處于打開狀態(tài),并且此時我們的想要添加的數(shù)據(jù)已經(jīng)成功的添加到數(shù)據(jù)庫中。
回到后端(云端)數(shù)據(jù)庫:
我們可以發(fā)現(xiàn),我們通過修改數(shù)據(jù)表權(quán)限的方式能實現(xiàn)通過純前端的操作就可以實現(xiàn)對數(shù)據(jù)庫進行操作!
總結(jié)
到此這篇關(guān)于uni-app操作數(shù)據(jù)庫的三種方法的文章就介紹到這了,更多相關(guān)uni-app操作數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
[Bootstrap-插件使用]Jcrop+fileinput組合實現(xiàn)頭像上傳功能實例代碼
這篇文章主要介紹了[Bootstrap-插件使用]Jcrop+fileinput組合實現(xiàn)頭像上傳功能實例代碼,非常具有實用價值,需要的朋友可以參考下。2016-12-12JavaScript設(shè)計模式經(jīng)典之命令模式
命令模式(Command)的定義是:用來對方法調(diào)用進行參數(shù)化處理和傳送,經(jīng)過這樣處理過的方法調(diào)用可以在任何需要的時候執(zhí)行。接下來通過本文給大家介紹JavaScript設(shè)計模式經(jīng)典之命令模式,需要的朋友參考下2016-02-02JavaScript+HTML5實現(xiàn)的日期比較功能示例
這篇文章主要介紹了JavaScript+HTML5實現(xiàn)的日期比較功能,涉及javascript結(jié)合HTML5針對日期的轉(zhuǎn)換與運算相關(guān)操作技巧,需要的朋友可以參考下2017-07-07JS實現(xiàn)數(shù)組去重及數(shù)組內(nèi)對象去重功能示例
這篇文章主要介紹了JS實現(xiàn)數(shù)組去重及數(shù)組內(nèi)對象去重功能,結(jié)合實例形式分析了ES5與ES6兩種版本針對數(shù)組去重的相關(guān)操作技巧,需要的朋友可以參考下2019-02-02