Skip to content

Commit

Permalink
feat: dbm 实现删除与创建集合索引接口;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 21, 2021
1 parent fd1d96c commit 405dfce
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
21 changes: 21 additions & 0 deletions http/dbm.http
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ Authorization: Bearer {{token}}

GET {{base_url}}/admin/collection/indexes?collection=functions
Content-Type: application/json
Authorization: Bearer {{token}}


### 创建集合索引

POST {{base_url}}/admin/collection/indexes?collection=functions
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}

{
"spec": {
"created_at": 1
},
"unique": true
}


### 删除集合索引

DELETE {{base_url}}/admin/collection/indexes?collection=functions&index=created_at_1
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}
4 changes: 4 additions & 0 deletions init/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ exports.permissions = [
{ name: 'trigger.edit', label: '编辑访触发器' },
{ name: 'trigger.delete', label: '删除触发器' },
{ name: 'trigger.apply', label: '应用触发器' },

{ name: 'collections.get', label: 'DBM-获取集合列表'},
{ name: 'collections.createIndex', label: 'DBM-创建集合索引'},
{ name: 'collections.deleteIndex', label: 'DBM-删除集合索引'}
]
113 changes: 110 additions & 3 deletions src/router/dbm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { getLogger } from '../../lib/logger'
export const DbmRouter = express.Router()
const logger = getLogger('admin:api')


/**
* 获取集合列表
*/
DbmRouter.get('/collections', async (req, res) => {
const requestId = req['requestId']
logger.info(`[${requestId}] get /collections`)
Expand All @@ -24,6 +26,10 @@ DbmRouter.get('/collections', async (req, res) => {
return res.send(names)
})


/**
* 获取集合索引列表
*/
DbmRouter.get('/collection/indexes', async (req, res) => {
const requestId = req['requestId']
logger.info(`[${requestId}] get /collection/indexes`)
Expand All @@ -36,9 +42,110 @@ DbmRouter.get('/collection/indexes', async (req, res) => {

const collectionName = req.query?.collection
if (!collectionName) {
return res.status(404).send('Collection not found')
return res.status(422).send('collection cannot be empty')
}

const r = await accessor.db.collection(collectionName as string).indexes()
return res.send(r)
})
})

/**
* 创建集合索引
*/
DbmRouter.post('/collection/indexes', async (req, res) => {
const requestId = req['requestId']
logger.info(`[${requestId}] post /collection/indexes`)

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'collections.createIndex')
if (code) {
return res.status(code).send()
}

const collectionName = req.query?.collection
if (!collectionName) {
return res.status(422).send('collection cannot be empty')
}

const unique = req.body?.unique ?? false
const spec = req.body.spec
if (!validIndexSpec(spec)) {
return res.status(422).send('invalid index spec')
}

try {
const r = await accessor.db
.collection(collectionName as string)
.createIndex(spec, {
background: true,
unique: unique as boolean
})

return res.send(r)
} catch (error) {
return res.status(400).send(error)
}
})

/**
* 删除集合索引
*/
DbmRouter.delete('/collection/indexes', async (req, res) => {
const requestId = req['requestId']
logger.info(`[${requestId}] post /collection/indexes`)

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'collections.deleteIndex')
if (code) {
return res.status(code).send()
}

const collectionName = req.query?.collection
if (!collectionName) {
return res.status(422).send('collection cannot be empty')
}

const indexName = req.query?.index
if (!indexName) {
return res.status(422).send('invalid index name')
}

try {
const r = await accessor.db
.collection(collectionName as string)
.dropIndex(indexName as string)

return res.send(r)
} catch (error) {
return res.status(400).send(error)
}
})


/**
* 检查 index spec 合法性
* @param spec
* @returns
*/
function validIndexSpec(spec: any) {
if (!spec) return false
if (typeof spec !== 'object') {
return false
}

const keys = Object.keys(spec)
if (!keys || !keys.length) {
return false
}

for (const k of keys) {
if (typeof k !== 'string') return false
if (k === '_id') return false

if ([1, -1].includes(spec[k]) === false) {
return false
}
}

return true
}

0 comments on commit 405dfce

Please sign in to comment.