后端搭建起来
This commit is contained in:
@@ -1,30 +1,37 @@
|
||||
const Service = require("egg").Service
|
||||
const {v4: uuid} = require("uuid");
|
||||
|
||||
class DbService extends Service {
|
||||
async addOrUpdate(table, form) {
|
||||
let result
|
||||
if (!form.id) {//创建
|
||||
form.id = uuid()
|
||||
result = await this.app.mysql.insert(table, form)
|
||||
} else {//更新
|
||||
result = await this.app.mysql.update(table, form)
|
||||
}
|
||||
return result === 1
|
||||
return result === 1 ? {code: 0, data: form.id} : {code: 1, data: null}
|
||||
}
|
||||
|
||||
async delete(table, id) {
|
||||
await this.app.mysql.delete(table, {id})
|
||||
return {code: 0, data: null}
|
||||
}
|
||||
|
||||
async detail(table, id) {
|
||||
return await this.app.mysql.get(table, {id})
|
||||
const data = await this.app.mysql.get(table, {id})
|
||||
return {code: 0, data}
|
||||
}
|
||||
|
||||
async list(table, params = {}) {
|
||||
return await this.app.mysql.select(table, {
|
||||
let {current = 0} = params
|
||||
const records = await this.app.mysql.select(table, {
|
||||
where: params,
|
||||
limit: params?.size || 10, // 返回数据量
|
||||
offset: Math.max(params?.current - 1, 0), // 数据偏移量
|
||||
offset: Math.max(--current, 0), // 数据偏移量
|
||||
})
|
||||
const total = (await this.app.mysql.select(table, {where: params}))?.length || 0
|
||||
return {code: 0, data: {records, total}}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user