interface QueryDescription<T> extends ClauseDescription<T> {
fields?: FieldsValue[]
limit?: number
skip?: number
orderBy?: OrderDescription[]
}
interface ClauseDescription<T> {
where?: PredicateDescription<T>
}
interface OrderDescription {
fieldName: string
orderBy?: 'DESC' | 'ASC'
}
字段名 | 描述 |
fields | 查询哪些字段,数组,合法值为字符串或字面量对象 |
limit | 最多查询多少条记录,整数 |
skip | 跳过多少条记录,整数 |
orderBy | 排序,数组,合法值是 OrderDescription |
where | 查询条件,一个字面量对象。合法值是 PredicateDescription |
{
fields: ['_id', 'name', 'content', {
project: ['_id', 'name'],
executor: ['_id', 'name', 'avatarUrl']
}],
limit: 20,
skip: 40,
orderBy: [
{
fieldName: 'priority',
orderBy: 'ASC'
},
{
fieldName: 'dueDate',
orderBy: 'DESC'
}
],
where: {
dueDate: {
$lte: moment().add(7, 'day').startOf('day').valueOf()
},
startDate: {
$gte: moment().add(1, 'day').endOf('day').valueOf()
},
involveMembers: {
$has: 'xxxuserId'
}
}
}
interface OrderDescription {
fieldName: string
orderBy?: 'DESC' | 'ASC'
}
字段名 | 描述 |
fieldName | 排序的字段 |
orderBy | 排序方法。ASC 升序,DESC 降序 |
{
fieldName: 'priority',
orderBy: 'ASC'
}
type ValueLiteral = string | number | boolean
type VaildEqType = ValueLiteral | lf.schema.Column | lf.Binder
type PredicateDescription<T> = {
[P in keyof T & PredicateMeta<T>]?: Partial<PredicateMeta<T>> | ValueLiteral | PredicateDescription<T[P]>
}
interface PredicateMeta<T> {
$ne: ValueLiteral
$eq: ValueLiteral
$and: PredicateDescription<T>
$or: PredicateDescription<T> | PredicateDescription<T>[]
$not: PredicateDescription<T>
$lt: ValueLiteral
$lte: ValueLiteral
$gt: ValueLiteral
$gte: ValueLiteral
$match: RegExp
$notMatch: RegExp
$has: ValueLiteral
$between: [ number, number ]
$in: ValueLiteral[]
$isNull: boolean
$isNotNull: boolean
}
字面量对象,它的 key 为 PredicateMeta 的 key 时受到 PredicateMeta 接口的约束。 比如
{
// 只能为正则
$match: RegExp
}
当它的 key 为其它值时,它的值可能为新的 PredicateDescription
, PredicateMeta<T>
, ValueLiteral
, 它们可以一层层的递归的定义。
第一层定义的 key 默认用 $and
连接,比如:
{
dueDate: {
$lte: moment().add(7, 'day').endOf('day').valueOf()
},
startDate: {
$gte: moment().add(1, 'day').startOf('day').valueOf()
}
}
默认表示 lf.op.and(taskTable.dueDate.lte(...), taskTable.startDate.gte(...) )
{
$or: [
{
dueDate: {
$and: [
{ $lte: moment().add(7, 'day').startOf('day').valueOf() },
{ $gte: moment().add(1, 'day').endtOf('day').valueOf() }
]
},
startDate: {
$and: [
{ $lte: moment().add(7, 'day').startOf('day').valueOf() },
{ $gte: moment().add(1, 'day').endtOf('day').valueOf() }
]
},
}
],
involveMembers: {
$has: 'xxxuserId'
}
}