Skip to content

Commit

Permalink
fix(function): create db when not existed
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Sep 13, 2020
1 parent 64edb18 commit 906fac9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 47 deletions.
1 change: 0 additions & 1 deletion cloudbaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports = {
envId: envId,
functionRoot: './src/function',
functions: [
{ name: 'migrate', ...defaultFunctionConfig },
{ name: 'comment-get', ...defaultFunctionConfig },
{ name: 'comment-like', ...defaultFunctionConfig },
{ name: 'comment-submit', ...defaultFunctionConfig },
Expand Down
20 changes: 10 additions & 10 deletions src/function/comment-get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ exports.main = async (event, context) => {
try {
validate(event)
uid = await auth.getEndUserInfo().userInfo.uid
const data = await db
.collection('comment')
.where({
url: event.url,
isSpam: _.neq(true)
})
.orderBy('created', 'desc')
.get()
res.data = parse(data.data, uid)
} catch (e) {
res.data = []
res.message = e.message
return res
}
const data = await db
.collection('comment')
.where({
url: event.url,
isSpam: _.neq(true)
})
.orderBy('created', 'desc')
.get()
res.data = parse(data.data, uid)
return res
}

Expand Down
9 changes: 7 additions & 2 deletions src/function/comment-submit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ exports.main = async (event, context) => {
res.message = e.message
return res
}
await init()
try {
await readConfig()
} catch (e) {
await createCollections()
await readConfig()
}
const comment = await save(event)
res.id = comment.id
await sendMail(comment)
return res
}

async function init () {
async function readConfig () {
if (!config) {
config = await db
.collection('config')
Expand Down
25 changes: 21 additions & 4 deletions src/function/counter-get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ exports.main = async (event, context) => {
const res = {}
try {
validate(event)
const record = await db
.collection('counter')
.where({ url: event.url })
.get()
let record
try {
record = await read()
} catch (e) {
await createCollections()
record = await read()
}
res.data = record.data[0] ? record.data[0] : {}
res.time = res.data ? res.data.time : 0
res.updated = await inc(event)
Expand All @@ -26,6 +29,13 @@ exports.main = async (event, context) => {
return res
}

async function read () {
return await db
.collection('counter')
.where({ url: event.url })
.get()
}

/**
* 更新阅读数
* @param {String} event.url 文章地址
Expand Down Expand Up @@ -66,3 +76,10 @@ function validate (event) {
}
}
}

/**
* 建立数据库 collections
*/
async function createCollections () {
return await db.createCollection('counter')
}
22 changes: 0 additions & 22 deletions src/function/migrate/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/function/migrate/package.json

This file was deleted.

0 comments on commit 906fac9

Please sign in to comment.