xuebaoxin #21
4
app.js
4
app.js
@ -8,9 +8,11 @@ const app = new Koa();
|
|||||||
app.use(async (ctx,next)=>{
|
app.use(async (ctx,next)=>{
|
||||||
// await next();
|
// await next();
|
||||||
console.log(ctx.url)
|
console.log(ctx.url)
|
||||||
|
// console.time("asd")
|
||||||
// ctx.response.body = {a:1};
|
// ctx.response.body = {a:1};
|
||||||
await next()
|
await next()
|
||||||
console.log("执行")
|
// console.log("执行")
|
||||||
|
// console.timeEnd("asd")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
137
bin/mongodb.js
137
bin/mongodb.js
@ -1,109 +1,148 @@
|
|||||||
var MongoClient = require('mongodb').MongoClient;
|
var MongoClient = require('mongodb').MongoClient;
|
||||||
var dburl;
|
var dburl;
|
||||||
let add = (set,data,url = dburl) =>{
|
let idadd = async (set) => {
|
||||||
|
let id = 0
|
||||||
|
if (set == "idadd") {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
await find('idadd', { 'set': set }).then((res) => {
|
||||||
|
if (res.data.length != 0) {
|
||||||
|
id = res.data[0].num_key
|
||||||
|
} else {
|
||||||
|
id = 1
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == 1) {
|
||||||
|
await add('idadd', { set, 'num_key': 2 })
|
||||||
|
} else if (set != "idadd") {
|
||||||
|
console.log(id)
|
||||||
|
|
||||||
|
await update('idadd', { 'set': set }, { 'num_key': id + 1 })
|
||||||
|
}
|
||||||
|
console.log(id)
|
||||||
|
return id
|
||||||
|
|
||||||
|
}
|
||||||
|
let add = async (set, data, url = dburl) => {
|
||||||
|
if (set != "idadd") {
|
||||||
|
let id = await idadd(set)
|
||||||
|
data.num_key = id
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
|
||||||
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).insertOne(data, function(err, res) {
|
dbo.collection(set).insertOne(data, function (err, res) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
db.close();
|
db.close();
|
||||||
resolve({code:0});
|
resolve({ code: 0 });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let adds = (set,data,url = dburl) =>{
|
let adds = async (set, data, url = dburl) => {
|
||||||
|
|
||||||
|
if (set != "idadd") {
|
||||||
|
for (let i in data) {
|
||||||
|
let id = await idadd(set)
|
||||||
|
data[i].num_key = id
|
||||||
|
}
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
|
||||||
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).insertMany(data, function(err, res) {
|
dbo.collection(set).insertMany(data, function (err, res) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
db.close();
|
db.close();
|
||||||
resolve({code:0,num:res.insertedCount});
|
resolve({ code: 0, num: res.insertedCount });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let remove = (set,where,url = dburl) =>{
|
let remove = async (set, where, url = dburl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).deleteOne(where, function(err, obj) {
|
dbo.collection(set).deleteOne(where, function (err, obj) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
db.close();
|
db.close();
|
||||||
resolve({code:0});
|
resolve({ code: 0 });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let removes = (set,where,url = dburl) =>{
|
let removes = async (set, where, url = dburl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if(where == "" ||Object.keys(where).length == 0){
|
if (where == "" || Object.keys(where).length == 0) {
|
||||||
reject({code:1})
|
reject({ code: 1 })
|
||||||
}
|
}
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).deleteMany(where, function(err, obj) {
|
dbo.collection(set).deleteMany(where, function (err, obj) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
db.close();
|
db.close();
|
||||||
resolve({code:0,num:obj.result.n});
|
resolve({ code: 0, num: obj.result.n });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
|
let find = async (set, where = {}, page = 0, num = 0, url = dburl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if(num == 0){
|
if (num == 0) {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set). find(where).toArray(function(err, result) {
|
dbo.collection(set).find(where).toArray(function (err, result) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
resolve({code:0,data:result});
|
resolve({ code: 0, data: result });
|
||||||
db.close();
|
db.close();
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}else{
|
} else {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
try {
|
try {
|
||||||
dbo.collection(set). find(where).skip(num * page).limit(num).toArray(function(err, result) {
|
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
resolve({code:0,data:result});
|
resolve({ code: 0, data: result });
|
||||||
db.close();
|
db.close();
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -111,48 +150,50 @@ let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
|
|||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let update = (set,where = {},data,url = dburl) =>{
|
let update = async (set, where = {}, data, defin = true, url = dburl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
data = {$set:data}
|
if (defin) {
|
||||||
|
data = { $set: data }
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).updateOne(where, data, function(err, res) {
|
dbo.collection(set).updateOne(where, data, function (err, res) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
resolve({code:0});
|
resolve({ code: 0 });
|
||||||
db.close();
|
db.close();
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let updates = (set,where = {},data,url = dburl) =>{
|
let updates = async (set, where = {}, data, url = dburl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var dbo = db.db(url.dbname);
|
var dbo = db.db(url.dbname);
|
||||||
data = {$set:data}
|
data = { $set: data }
|
||||||
try {
|
try {
|
||||||
dbo.collection(set).updateMany(where, data, function(err, res) {
|
dbo.collection(set).updateMany(where, data, function (err, res) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
db.close();
|
db.close();
|
||||||
resolve({code:0,num:res.result.nModified});
|
resolve({ code: 0, num: res.result.nModified });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject({code:1,error})
|
reject({ code: 1, error })
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (url,dbname){
|
module.exports = function (url, dbname) {
|
||||||
dburl = {url,dbname};
|
dburl = { url, dbname };
|
||||||
return{
|
return {
|
||||||
add,
|
add,
|
||||||
adds,
|
adds,
|
||||||
remove,
|
remove,
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
|
||||||
var loginup = async (ctx,next) => {
|
var loginup = async (ctx,next) => {
|
||||||
let arr;
|
let arr;
|
||||||
//查询数据库 没有用户默认admin 123456
|
//查询数据库 没有用户默认admin 123456
|
||||||
|
console.time("asd")
|
||||||
|
|
||||||
await dbs.find('admin').then((res)=>{
|
await dbs.find('admin').then((res)=>{
|
||||||
arr = res.data
|
arr = res.data
|
||||||
})
|
})
|
||||||
@ -11,12 +12,13 @@ var loginup = async (ctx,next) => {
|
|||||||
arr = res.data
|
arr = res.data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
console.timeEnd("asd")
|
||||||
//判断用户名密码
|
//判断用户名密码
|
||||||
if(ctx.request.body.name == arr[0].name && ctx.request.body.pwd == arr[0].pwd){
|
if(ctx.request.body.name == arr[0].name && ctx.request.body.pwd == arr[0].pwd){
|
||||||
//颁发token
|
//颁发token
|
||||||
const token = jwt.sign({
|
const token = jwt.sign({
|
||||||
name: ctx.request.body.name,
|
name: ctx.request.body.name,
|
||||||
_id: 1
|
id: arr[0]._id
|
||||||
}, 'my_token', { expiresIn: '2h' });
|
}, 'my_token', { expiresIn: '2h' });
|
||||||
ctx.body={code:0,token}
|
ctx.body={code:0,token}
|
||||||
}else{
|
}else{
|
||||||
|
@ -41,7 +41,7 @@ find 查询数据
|
|||||||
```
|
```
|
||||||
update 更新单条数据
|
update 更新单条数据
|
||||||
```
|
```
|
||||||
参数(操作的集合名,条件,要改为的{}[,{数据库地址,数据库名}])
|
参数(操作的集合名,条件,要改为的{}[,默认$set 如果其他 为flase 要改为的加操作符,{数据库地址,数据库名}])
|
||||||
返回值类型 promise
|
返回值类型 promise
|
||||||
成功返回 {code:0},
|
成功返回 {code:0},
|
||||||
失败返回 {code:1}
|
失败返回 {code:1}
|
||||||
@ -53,3 +53,8 @@ updates 更新多条数据
|
|||||||
成功返回 {code:0,num:修改成功的数量},
|
成功返回 {code:0,num:修改成功的数量},
|
||||||
失败返回 {code:1}
|
失败返回 {code:1}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
####默认
|
||||||
|
|
||||||
|
|
||||||
|
默认idadd表为id存储表,num_key为id 自动递增 1开始
|
||||||
|
Loading…
Reference in New Issue
Block a user