一个易于使用且高效的连表查询方法,仅有2个Api,无侵入式修改,特别为初中级前端和初级后端使用,支持一对一,一对多,嵌套查询等业务中最常用的连表操作,可以极大程度的提高代码的可维护性和工作效率,使用形式上更像面向数据库的GraphQL
npm install link-query --save
const { decorator } = require('link-query');
const UserEnhance = decorator(User);
UserEnhance.addLinker({
...
})
UserEnhance.linkQuery({
name: 1,
address: 1,
friends: {
name: 1,
},
blog: {
title: 1,
author: {
name: 1,
},
comments: {
content: 1,
createdAt: 1,
},
tags: {
title: 1,
}
}
})
result:
[{
name: 'people name',
address: 'people address',
friends: [{
name: 'friend name'
}],
blog: [{
title: 'blog title',
author: {
name: 'author name',
},
comments: [{
content: 'comment content',
createdAt: '2010-10-10',
}],
tags: [{
title: 'tag title',
}]
}]
}]
增强原始collection,为其添加addLinker
const { decorator } = require('link-query');
const CollectionEnhance = const decorator(Collection, linkConfig);
- Collection mongodb collection或者 mongoose model
- linkConfig
- [virtualFieldName] 虚拟的字段名字
- type [default=one]
- one 一对一关系
- many 一对多关系
- foreignField [default=_id] 对应的传入的collection的字段,支持深层属性,例如"user._id"
- localField [default=_id] 对应添加虚拟字段的collection的字段 ,支持深层属性,例如"user.blog._id"
- Collection 原始Collection或者增强后的Collection,如果是增强后的collection,可以实现嵌套查询
添加关联关系
CollectionEnhance.addLinker(linkConfig)
- linkConfig 同decorator(Collection, linkConfig)的linkConfig
- 可以先增强后添加,使得属性可以嵌套查询
示例:
const UserEnhance = const decorator(User);
const BlogEnhance = const decorator(Blog);
UserEnhance.addLinker({
blog: {
collection: BlogEnhance,
type: 'many',
foreignField: 'userId'
}
})
BlogEnhance.addLinker({
user: {
collection: UserEnhance,
type: 'one',
localField: 'userId'
}
})
// 使用
UserEnhance.linkQuery({
name: 1,
blog: {
title: 1,
user: {
name: 1,
address: 1,
blog: {
... // 继续嵌套
}
}
}
}).fetch()
使用关联关系进行查询
const handle = UserEnhance.linkQuery({
$filters:{},
$options:{},
[projections]: 1,
[virtualFieldName]: {
$filters:{},
$options:{},
[projections]: 1
}
})
await handle.fetch(); // 获取全部
await handle.fetchOne(); // 获取第一个
- $filters Collection 的查询条件query,适用于当前调用的Collection,不支持在父级直接筛选子集的属性,
例如{ [virtualFieldName.age] : 18 } - $options
- projections 选取当前Collection需要的字段
- virtualFieldName 定义在当前Collection的虚拟字段
- $filters 同上,作用于被关联表
- $options 同上,作用于被关联表
- projections 同上,作用于被关联表
示例:
// 使用
UserEnhance.linkQuery({
$filters: {
name: "Moyye"
},
$options:{
limit: 1,
sort: {
createdAt: -1
},
},
name: 1,
blog: {
title: 1,
user: {
name: 1,
address: 1,
blog: {
... // 继续嵌套
}
}
}}).fetch()