Skip to content

Commit

Permalink
feat: 优化简化 entry 和 trigger 配置代码;忽略读取数据事件;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 10, 2021
1 parent 560f4e8 commit fe06c74
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 59 deletions.
34 changes: 7 additions & 27 deletions src/entry/admin.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
import { Router } from 'express'
import { Entry, MongoAccessor, Ruler } from 'less-api'
import { Entry, Ruler } from 'less-api'
import Config from '../config'
import { getPermissions } from '../lib/api/permission'
import { scheduler } from '../lib/faas'
import { accessor } from '../lib/db'
import { getLogger } from '../lib/logger'
import { getAccessRules } from '../lib/rules'
import { convertActionType } from '../lib/util'

const logger = getLogger('admin:entry')
const router = Router()

const accessor = new MongoAccessor(Config.db.database, Config.db.uri, {
poolSize: Config.db.poolSize,
useNewUrlParser: true,
useUnifiedTopology: true,
})

accessor.on('result', data => {
const { params, result } = data
const op = convertActionType(params.action)

// 触发数据事件
const event = `/db/${params.collection}#${op}`
scheduler.emit(event, result)
})

const ruler = new Ruler(accessor)
accessor.init()
.then(() => {
return getAccessRules('admin', accessor)
})
.then(rules => {
ruler.load(rules)
})
accessor.ready.then(async () => {
const rules = await getAccessRules('admin', accessor)
ruler.load(rules)
})

export const entry = new Entry(accessor, ruler)
entry.setLogger(getLogger('admin:less-api', 'warning'))

const logger = getLogger('admin:entry')
router.post('/entry', async (req, res) => {
const requestId = req['requestId']
const auth = req['auth'] ?? {}
Expand Down
37 changes: 8 additions & 29 deletions src/entry/app.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
import { Router } from 'express'
import { Entry, MongoAccessor, Ruler } from 'less-api'
import Config from '../config'
import { scheduler } from '../lib/faas'
import { Entry, Ruler } from 'less-api'
import { accessor } from '../lib/db'
import { getLogger } from '../lib/logger'
import { getAccessRules } from '../lib/rules'
import { convertActionType } from '../lib/util'

const logger = getLogger('app:entry')
const router = Router()

router.all('*', function (_req, _res, next) {
next()
})

const accessor = new MongoAccessor(Config.db.database, Config.db.uri, {
poolSize: Config.db.poolSize,
useNewUrlParser: true,
useUnifiedTopology: true,
})

accessor.on('result', data => {
const { params, result } = data
const op = convertActionType(params.action)

// 触发数据事件
const event = `/db/${params.collection}#${op}`
scheduler.emit(event, result)
})

const ruler = new Ruler(accessor)
accessor.init()
.then(() => {
return getAccessRules('app', accessor)
})
.then(rules => {
ruler.load(rules)
})
accessor.ready.then(async () => {
const rules = await getAccessRules('app', accessor)
ruler.load(rules)
})

export const entry = new Entry(accessor, ruler)
entry.setLogger(getLogger('app:less-api', 'warning'))

const logger = getLogger('app:entry')
router.post('/entry', async (req, res) => {
const requestId = req['requestId']

Expand All @@ -66,7 +45,7 @@ router.post('/entry', async (req, res) => {
try {
const data = await entry.execute(params)
logger.trace(`[${requestId}] executed query: `, data)

return res.send({
code: 0,
data
Expand Down
2 changes: 1 addition & 1 deletion src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import Config from '../config'
import { getLogger } from './logger'

export const accessor = new MongoAccessor(Config.db.database, Config.db.uri, {
poolSize: Config.db.poolSize,
useNewUrlParser: true,
useUnifiedTopology: true
})

accessor.setLogger(getLogger('server:db', 'warning'))

// 为了在外部能够等待异步的数据库连接
accessor.init()

// 获取 db 对象
Expand Down
16 changes: 14 additions & 2 deletions src/lib/faas/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,23 @@ accessor.ready.then(() => {
scheduler.init()
})

accessor.on('result', data => {
accessor.on('result', AccessorEventCallBack)

/**
* 数据操作事件回调
* @param data
*/
export function AccessorEventCallBack(data: any) {
const { params, result } = data

const op = convertActionType(params.action)

// 忽略的数据事件
if (['read', 'count', 'watch'].includes(op)) {
return
}

// 触发数据事件
const event = `/db/${params.collection}#${op}`
scheduler.emit(event, result)
})
}

0 comments on commit fe06c74

Please sign in to comment.