#Condition Tree
condition 是 oor 的中间层。
同一份条件树可以继续交给 SQL、ES 或 Drizzle。
#Node 结构
TypeScript
type Node = {
link: 'AND' | 'OR'
items: Array<Node | Atom>
}
type Atom = {
field: string
op:
| 'Equal'
| 'Not'
| 'More'
| 'MoreEqual'
| 'Less'
| 'LessEqual'
| 'Like'
| 'StartWith'
| 'EndWith'
| 'In'
| 'NotIn'
| 'Between'
| 'IsNull'
| 'NotNull'
value: unknown
}
#makeNode
TypeScript
import { makeNode } from 'oor'
const node = makeNode({
statusIn: ['active', 'pending'],
ageMin: 18
})
默认会得到一棵 AND 条件树。
#withSoft
TypeScript
import { withSoft } from 'oor'
const node = withSoft(
{
link: 'OR',
items: [
{ field: 'status', op: 'Equal', value: 'active' },
{ field: 'status', op: 'Equal', value: 'pending' }
]
},
{
soft: {
mode: 'flag',
field: 'isDeleted',
del: true,
keep: false
}
}
)
#soft
flag:isDeleted = falsetime:deletedAt IS NULL
#page 和 sort
TypeScript
import { page, sort } from 'oor'
const p = page({ page: 2, size: 10 })
const s = sort({ sort: 'createdAt', order: 'desc' })
page默认每页15- 支持
_start/_count和page/size - 支持
_order/_by和sort/order meta.page.size、meta.sort可作为默认值
TypeScript
makeNode(input, meta, { skipSoft: true })
withSoft(node, meta, { skipSoft: true })