摘要:全局安裝腳手架創(chuàng)建項目與是一個對象數(shù)據(jù)庫,是用來存儲數(shù)據(jù)的存儲的數(shù)據(jù)格式為。三封裝數(shù)據(jù)庫的在文件下新建采用封裝對數(shù)據(jù)庫的操作,避免回調(diào)地獄,使得代碼能夠更好的被讀懂和維護。
前奏
Express 是什么?
Express 是一個基于 Node.js 平臺的極簡、靈活的 web 應用開發(fā)框架,它提供一系列強大的特性,幫助你創(chuàng)建各種 Web 和移動設備應用。
全局安裝express腳手架
$ npm install express-generator -g
創(chuàng)建express項目
$ express myapp $ cd myapp $ npm install $ DEBUG=myapp npm start
MongoDB與Mongoose?
MongoDB是一個對象數(shù)據(jù)庫,是用來存儲數(shù)據(jù)的;存儲的數(shù)據(jù)格式為JSON。
Mongoose是封裝了MongoDB操作(增刪改查等)的一個對象模型庫,是用來操作這些數(shù)據(jù)的。
安裝MongoDB:
https://www.mongodb.com/download-center?jmp=nav
安裝Mongoose:
$ npm install mongoose --save一、連接MongoDB
在項目根目錄下新建/lib/mongo.js
var mongoose = require("mongoose");
var db = mongoose.connect("mongodb://localhost:27017/myblog");
module.exports = db
二、Schema要連接的數(shù)據(jù)庫為myblog
一種以文件形式存儲的數(shù)據(jù)庫模型骨架,無法直接通往數(shù)據(jù)庫端,不具備對數(shù)據(jù)庫的操作能力,僅僅只是數(shù)據(jù)庫模型在程序片段中的一種表現(xiàn),可以說是數(shù)據(jù)屬性模型(傳統(tǒng)意義的表結(jié)構(gòu)),又或著是“集合”的模型骨架
新建一個用戶Schema
在項目根目錄下新建/models/users.js
var mongoose = require("mongoose");
var db = require("../lib/mongo");
//一個用戶模型
var UserSchema = new mongoose.Schema({
username : { type:String },
password : {type: String},
avatar : {type: String},
age : { type:Number, default:0 },
description : { type: String},
email : { type: String },
github : { type: String },
time : { type:Date, default:Date.now }
});
//創(chuàng)建Model
var UserModel = db.model("user", UserSchema );
module.exports = UserModel
user:數(shù)據(jù)庫中的集合名稱,當我們對其添加數(shù)據(jù)時如果user已經(jīng)存在,則會保存到其目錄下,如果不存在,則會創(chuàng)建user集合,然后在保存數(shù)據(jù)。
擁有了Model,我們也就擁有了操作數(shù)據(jù)庫的金鑰匙,就可以使用Model來進行增刪改查的具體操作。
Entity
由Model創(chuàng)建的實體,使用save方法保存數(shù)據(jù),Model和Entity都有能影響數(shù)據(jù)庫的操作,但Model比Entity更具操作性。
var UserEntity = new UserModel({
name : "hzzly",
age : 21,
email: "hjingren@aliyun.com",
github: "https://github.com/hzzly"
});
UserEntity.save(function(error,doc){
if(error){
console.log("error :" + error);
}else{
console.log(doc);
}
});
三、封裝數(shù)據(jù)庫的CURD
在lib文件下新建api.js
采用Promise封裝對數(shù)據(jù)庫的操作,避免回調(diào)地獄,使得代碼能夠更好的被讀懂和維護。
var UserModel = require("../models/users");
module.exports = {
/**
* 添加數(shù)據(jù)
* @param {[type]} data 需要保存的數(shù)據(jù)對象
*/
save(data) {
return new Promise((resolve, reject) => {
//model.create(保存的對象,callback)
UserModel.create(data, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
},
find(data={}, fields=null, options={}) {
return new Promise((resolve, reject) => {
//model.find(需要查找的對象(如果為空,則查找到所有數(shù)據(jù)), 屬性過濾對象[可選參數(shù)], options[可選參數(shù)], callback)
UserModel.find(data, fields, options, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
},
findOne(data) {
return new Promise((resolve, reject) => {
//model.findOne(需要查找的對象,callback)
UserModel.findOne(data, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
},
findById(data) {
return new Promise((resolve, reject) => {
//model.findById(需要查找的id對象 ,callback)
UserModel.findById(data, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
},
update(conditions, update) {
return new Promise((resolve, reject) => {
//model.update(查詢條件,更新對象,callback)
UserModel.update(conditions, update, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
},
remove(conditions) {
return new Promise((resolve, reject) => {
//model.update(查詢條件,callback)
UserModel.remove(conditions, (error, doc) => {
if(error){
reject(error)
}else{
resolve(doc)
}
})
})
}
}
四、使用
在/routers/index.js中使用
var api = require("../lib/api");
router.post("/login", function(req, res, next) {
var user = {
username : req.body.username,
password: req.body.password
};
api.findOne(user)
.then(result => {
console.log(result)
})
})
router.post("/sign_up", function(req, res, next) {
var user = {
username : req.body.username,
password: req.body.password,
email: req.body.email
};
api.save(user)
.then(result => {
console.log(result)
})
})
router.get("/user_list", function(req, res, next) {
//返回所有用戶
api.find({})
.then(result => {
console.log(result)
})
//返回只包含一個鍵值name、age的所有記錄
api.find({},{name:1, age:1, _id:0})
.then(result => {
console.log(result)
})
//返回所有age大于18的數(shù)據(jù)
api.find({"age":{"$gt":18}})
.then(result => {
console.log(result)
})
//返回20條數(shù)據(jù)
api.find({},null,{limit:20})
.then(result => {
console.log(result)
})
//查詢所有數(shù)據(jù),并按照age降序順序返回數(shù)據(jù)
api.find({},null,{sort:{age:-1}}) //1是升序,-1是降序
.then(result => {
console.log(result)
})
})
項目Github地址: https://github.com/hzzly/expr...
如果對你有幫助,歡迎star
文章來源hzzly博客技術(shù)分享
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/18979.html