74 lines
2.0 KiB
Markdown
74 lines
2.0 KiB
Markdown
# @windychen/trans-form-middleware
|
||
|
||
Node.js ESM library that parses filter arrays emitted by `@windychen/trans-form-component` into structured conditions. **Does not generate SQL** — ORM adapters consume the output.
|
||
|
||
> 版本 0.1.0 — 首个可发布版本:`parseFilters` / `mapOperators` / `toTree` / `splitMultiValue`,零依赖,纯 ESM。
|
||
|
||
## Install
|
||
|
||
```bash
|
||
npm install @windychen/trans-form-middleware
|
||
```
|
||
|
||
## Usage
|
||
|
||
```js
|
||
import { parseFilters } from '@windychen/trans-form-middleware';
|
||
|
||
const componentOutput = [
|
||
{ field: 'name', value: 'xx', query: 'EQ' },
|
||
{ field: 'tags', value: 'a,b,c', query: 'IN', logic: 'OR' },
|
||
];
|
||
|
||
const filters = parseFilters(componentOutput, {
|
||
allowedFields: ['name', 'tags', 'status'],
|
||
mode: 'pass-through',
|
||
unknownField: 'error',
|
||
skipEmpty: false,
|
||
});
|
||
```
|
||
|
||
See `DESIGN.md` for the full protocol and grill-me decisions (Q1–Q61).
|
||
|
||
## API
|
||
|
||
| Export | Description |
|
||
|--------|-------------|
|
||
| `parseFilters(conditions, options)` | Main parser |
|
||
| `mapOperators(query, operatorMap?)` | Map EQ/GT/… to op symbols |
|
||
| `toTree(flat)` | Nest flat array by logic |
|
||
| `splitMultiValue(query, value)` | Parse comma-separated multi-values |
|
||
| `DEFAULT_OPERATOR_MAP` | Built-in operator mapping |
|
||
|
||
### parseOptions
|
||
|
||
| Option | Default | Description |
|
||
|--------|---------|-------------|
|
||
| `allowedFields` | *(required)* | Whitelist of field names |
|
||
| `mode` | `'pass-through'` | `'flat'` \| `'tree'` \| `'pass-through'` |
|
||
| `unknownField` | `'error'` | `'error'` \| `'skip'` |
|
||
| `strict` | `false` | Validate item shape |
|
||
| `operatorMap` | `{}` | Merge/override operator mapping |
|
||
| `skipFlags` | `['disabled','hidden']` | Skip items with these truthy flags |
|
||
| `skipEmpty` | `false` | Skip items with `empty: true` |
|
||
|
||
### 状态标记默认行为(Q60/Q61)
|
||
|
||
| 标记 | 默认处理 |
|
||
|------|----------|
|
||
| `disabled: true` | 跳过 |
|
||
| `hidden: true` | 跳过 |
|
||
| `readOnly: true` | 正常解析 |
|
||
| `empty: true` | 保留(`skipEmpty: true` 时跳过) |
|
||
|
||
## Development
|
||
|
||
```bash
|
||
npm install
|
||
npm test
|
||
```
|
||
|
||
## License
|
||
|
||
MIT
|