-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
112 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
const mongodbCore = require('../mongodb/mongodb.js') | ||
const { ObjectId} = require('mongodb') | ||
const mongodbCore = require("../mongodb/mongodb.js"); | ||
const { ObjectId } = require("mongodb"); | ||
|
||
const shortLinkDb = { | ||
async getList() { | ||
const db = mongodbCore.getDb() | ||
// 查询所有数据 | ||
const data = await db.collection('short-link').find({}).toArray(); | ||
return data | ||
}, | ||
async getList(queryStr, { pageIndex, pageSize }) { | ||
const db = mongodbCore.getDb(); | ||
// 查询列表数据 | ||
let queryRule = {}; | ||
if (queryStr) { | ||
queryRule = { | ||
$or: [ | ||
{ shortLink: { $regex: queryStr } }, | ||
{ redirect: { $regex: queryStr } }, | ||
], | ||
}; | ||
} | ||
// 页码 - skip数据量 - 公式 | ||
// 1 0 (pageIndex - 1) * pageSize | ||
// 2 pageSize | ||
// 3 | ||
const list = await db | ||
.collection("short-link") | ||
.find(queryRule) | ||
.limit(pageSize) | ||
.skip((pageIndex - 1) * pageSize) | ||
.toArray(); | ||
|
||
async add(payload) { | ||
const db = mongodbCore.getDb() | ||
// 插入数据时 _id 会自动增加 | ||
const insertResult = await db.collection('short-link').insertMany([ | ||
payload | ||
]); | ||
return insertResult | ||
}, | ||
const total = await db.collection("short-link").find(queryRule).count(); | ||
return {list, total}; | ||
}, | ||
|
||
async edit(payload) { | ||
const db = mongodbCore.getDb() | ||
const { shortLink, redirect, _id } = payload | ||
const updateResult = await db.collection('short-link').updateOne({ | ||
_id: ObjectId(_id) | ||
}, { | ||
$set: { shortLink, redirect } | ||
}) | ||
return updateResult | ||
}, | ||
|
||
async del(id) { | ||
const db = mongodbCore.getDb() | ||
const deleteResult = await db.collection('short-link').deleteMany({ | ||
_id: ObjectId(id) | ||
}) | ||
return deleteResult | ||
} | ||
} | ||
async add(payload) { | ||
const db = mongodbCore.getDb(); | ||
// 插入数据时 _id 会自动增加 | ||
const insertResult = await db | ||
.collection("short-link") | ||
.insertMany([payload]); | ||
return insertResult; | ||
}, | ||
|
||
async edit(payload) { | ||
const db = mongodbCore.getDb(); | ||
const { shortLink, redirect, _id } = payload; | ||
const updateResult = await db.collection("short-link").updateOne( | ||
{ | ||
_id: ObjectId(_id), | ||
}, | ||
{ | ||
$set: { shortLink, redirect }, | ||
} | ||
); | ||
return updateResult; | ||
}, | ||
|
||
async del(id) { | ||
const db = mongodbCore.getDb(); | ||
const deleteResult = await db.collection("short-link").deleteMany({ | ||
_id: ObjectId(id), | ||
}); | ||
return deleteResult; | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
shortLinkDb | ||
} | ||
shortLinkDb, | ||
}; |