@nestjs-filter-grammar/mikroorm 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -0
- package/dist/apply-filter.d.ts +4 -0
- package/dist/apply-filter.d.ts.map +1 -0
- package/dist/apply-filter.js +72 -0
- package/dist/apply-filter.js.map +1 -0
- package/dist/apply-sort.d.ts +4 -0
- package/dist/apply-sort.d.ts.map +1 -0
- package/dist/apply-sort.js +20 -0
- package/dist/apply-sort.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/operators.d.ts +10 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +33 -0
- package/dist/operators.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -0
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# @nestjs-filter-grammar/mikroorm
|
|
2
|
+
|
|
3
|
+
MikroORM adapter for nestjs-filter-grammar. Translates `FilterTree` and `SortEntry[]` into MikroORM `FilterQuery` and `QueryOrderMap` objects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @nestjs-filter-grammar/mikroorm @nestjs-filter-grammar/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Peer Dependencies
|
|
12
|
+
|
|
13
|
+
- `@mikro-orm/core` >= 6.0.0
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
### `applyFilter(tree, options?): Record<string, MikroOrmQueryValue>`
|
|
18
|
+
|
|
19
|
+
Converts a `FilterTree` into a MikroORM `FilterQuery` object.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { applyFilter } from '@nestjs-filter-grammar/mikroorm';
|
|
23
|
+
|
|
24
|
+
const users = await em.find(User, applyFilter(filterTree));
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Output examples:**
|
|
28
|
+
|
|
29
|
+
| Input | Output |
|
|
30
|
+
|-------|--------|
|
|
31
|
+
| `status=active` | `{ status: { $eq: 'active' } }` |
|
|
32
|
+
| `status!=deleted` | `{ status: { $ne: 'deleted' } }` |
|
|
33
|
+
| `status=active,pending` | `{ status: { $in: ['active', 'pending'] } }` |
|
|
34
|
+
| `name*~john` | `{ name: { $ilike: '%john%' } }` |
|
|
35
|
+
| `status=active;age>=18` | `{ $and: [{ status: { $eq: 'active' } }, { age: { $gte: '18' } }] }` |
|
|
36
|
+
| `status=active\|status=pending` | `{ $or: [{ status: { $eq: 'active' } }, { status: { $eq: 'pending' } }] }` |
|
|
37
|
+
|
|
38
|
+
### `applySort(entries, options?): Record<string, MikroOrmQueryValue>`
|
|
39
|
+
|
|
40
|
+
Converts `SortEntry[]` into a MikroORM `QueryOrderMap`.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { applySort } from '@nestjs-filter-grammar/mikroorm';
|
|
44
|
+
|
|
45
|
+
const users = await em.find(User, {}, { orderBy: applySort(sortEntries) });
|
|
46
|
+
// orderBy: { name: 'asc', age: 'desc' }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Column Mapping
|
|
50
|
+
|
|
51
|
+
**String rename:**
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
applyFilter(tree, {
|
|
55
|
+
columnMap: { name: 'userName' },
|
|
56
|
+
});
|
|
57
|
+
// { userName: { $eq: 'John' } }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**Callback for nested relations:**
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
applyFilter(tree, {
|
|
64
|
+
columnMap: {
|
|
65
|
+
department: (operator, values) => ({
|
|
66
|
+
department: { name: { $eq: values[0].value } },
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Sort column mapping:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
applySort(entries, {
|
|
76
|
+
columnMap: {
|
|
77
|
+
department: (direction) => ({ department: { name: direction } }),
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
// { department: { name: 'asc' } }
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Full Example
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
@Controller('users')
|
|
87
|
+
export class UsersController {
|
|
88
|
+
constructor(private readonly em: EntityManager) {}
|
|
89
|
+
|
|
90
|
+
@Get()
|
|
91
|
+
async findAll(@Filter(UserQuery, { optional: true }) { filter, sort }: FilterResult) {
|
|
92
|
+
return this.em.find(User,
|
|
93
|
+
filter ? applyFilter(filter) : {},
|
|
94
|
+
{ orderBy: sort ? applySort(sort) : {} },
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FilterTree } from '@nestjs-filter-grammar/core';
|
|
2
|
+
import type { ApplyFilterOptions, MikroOrmQueryValue } from './types';
|
|
3
|
+
export declare function applyFilter(tree: FilterTree, options?: ApplyFilterOptions): Record<string, MikroOrmQueryValue>;
|
|
4
|
+
//# sourceMappingURL=apply-filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-filter.d.ts","sourceRoot":"","sources":["../src/apply-filter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EAGX,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,kBAAkB,EAAuB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE3F,wBAAgB,WAAW,CACzB,IAAI,EAAE,UAAU,EAChB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAEpC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyFilter = applyFilter;
|
|
4
|
+
const operators_1 = require("./operators");
|
|
5
|
+
function applyFilter(tree, options) {
|
|
6
|
+
return buildNode(tree, options?.columnMap);
|
|
7
|
+
}
|
|
8
|
+
function buildNode(node, columnMap) {
|
|
9
|
+
if ('field' in node) {
|
|
10
|
+
return buildCondition(node, columnMap);
|
|
11
|
+
}
|
|
12
|
+
return buildGroup(node, columnMap);
|
|
13
|
+
}
|
|
14
|
+
function buildGroup(group, columnMap) {
|
|
15
|
+
const key = group.type === 'AND' ? '$and' : '$or';
|
|
16
|
+
const children = group.conditions.map((c) => buildNode(c, columnMap));
|
|
17
|
+
return { [key]: children };
|
|
18
|
+
}
|
|
19
|
+
function buildCondition(condition, columnMap) {
|
|
20
|
+
// Check for column map callback
|
|
21
|
+
if (columnMap && condition.field in columnMap) {
|
|
22
|
+
const mapping = columnMap[condition.field];
|
|
23
|
+
if (typeof mapping === 'function') {
|
|
24
|
+
return mapping(condition.operator, condition.values);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Resolve column name
|
|
28
|
+
const column = (columnMap && typeof columnMap[condition.field] === 'string')
|
|
29
|
+
? columnMap[condition.field]
|
|
30
|
+
: condition.field;
|
|
31
|
+
const { values, operator } = condition;
|
|
32
|
+
const opMapping = (0, operators_1.getMikroOrmOperator)(operator);
|
|
33
|
+
const nullValues = values.filter((v) => v.type === 'null');
|
|
34
|
+
const scalarValues = values.filter((v) => v.type !== 'null');
|
|
35
|
+
// All null
|
|
36
|
+
if (scalarValues.length === 0 && nullValues.length > 0) {
|
|
37
|
+
return { [column]: { [opMapping.key]: null } };
|
|
38
|
+
}
|
|
39
|
+
// Multi-value: $in / $nin
|
|
40
|
+
if (scalarValues.length > 1) {
|
|
41
|
+
const vals = scalarValues.map((v) => v.value);
|
|
42
|
+
const inKey = opMapping.key === '$ne' ? '$nin' : '$in';
|
|
43
|
+
if (nullValues.length > 0) {
|
|
44
|
+
return {
|
|
45
|
+
$or: [
|
|
46
|
+
{ [column]: { [inKey]: vals } },
|
|
47
|
+
{ [column]: { $eq: null } },
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return { [column]: { [inKey]: vals } };
|
|
52
|
+
}
|
|
53
|
+
// Single value
|
|
54
|
+
const rawValue = scalarValues.length > 0 ? scalarValues[0].value : null;
|
|
55
|
+
let paramValue = rawValue;
|
|
56
|
+
if (opMapping.like && typeof rawValue === 'string') {
|
|
57
|
+
paramValue = (0, operators_1.wrapLikeValue)(rawValue, opMapping.like);
|
|
58
|
+
}
|
|
59
|
+
if (nullValues.length > 0 && rawValue !== null) {
|
|
60
|
+
return {
|
|
61
|
+
$or: [
|
|
62
|
+
{ [column]: { [opMapping.key]: paramValue } },
|
|
63
|
+
{ [column]: { $eq: null } },
|
|
64
|
+
],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (opMapping.negate) {
|
|
68
|
+
return { [column]: { $not: { [opMapping.key]: paramValue } } };
|
|
69
|
+
}
|
|
70
|
+
return { [column]: { [opMapping.key]: paramValue } };
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=apply-filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-filter.js","sourceRoot":"","sources":["../src/apply-filter.ts"],"names":[],"mappings":";;AAQA,kCAKC;AARD,2CAAiE;AAGjE,SAAgB,WAAW,CACzB,IAAgB,EAChB,OAA4B;IAE5B,OAAO,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,SAAS,CAChB,IAAgB,EAChB,SAA2C;IAE3C,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;QACpB,OAAO,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACzC,CAAC;IACD,OA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AAI,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACnD,UAAU,GAAG,IAAA,yBAAa,EAAC,QAAQ,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;IACvD,CAAC;IAED,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QAC/C,OAAO;YACL,GAAG,EAAE;gBACH,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,EAAE;gBAC7C,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;aAC5B;SACF,CAAC;IACJ,CAAC;IAED,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;IACjE,CAAC;IAED,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,EAAE,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SortEntry } from '@nestjs-filter-grammar/core';
|
|
2
|
+
import type { ApplySortOptions, MikroOrmQueryValue } from './types';
|
|
3
|
+
export declare function applySort(entries: SortEntry[], options?: ApplySortOptions): Record<string, MikroOrmQueryValue>;
|
|
4
|
+
//# sourceMappingURL=apply-sort.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-sort.d.ts","sourceRoot":"","sources":["../src/apply-sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAqB,MAAM,SAAS,CAAC;AAEvF,wBAAgB,SAAS,CACvB,OAAO,EAAE,SAAS,EAAE,EACpB,OAAO,CAAC,EAAE,gBAAgB,GACzB,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAkBpC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applySort = applySort;
|
|
4
|
+
function applySort(entries, options) {
|
|
5
|
+
const orderMap = {};
|
|
6
|
+
for (const entry of entries) {
|
|
7
|
+
if (options?.columnMap && entry.field in options.columnMap) {
|
|
8
|
+
const mapping = options.columnMap[entry.field];
|
|
9
|
+
if (typeof mapping === 'function') {
|
|
10
|
+
Object.assign(orderMap, mapping(entry.direction));
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
orderMap[mapping] = entry.direction;
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
orderMap[entry.field] = entry.direction;
|
|
17
|
+
}
|
|
18
|
+
return orderMap;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=apply-sort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-sort.js","sourceRoot":"","sources":["../src/apply-sort.ts"],"names":[],"mappings":";;AAGA,8BAqBC;AArBD,SAAgB,SAAS,CACvB,OAAoB,EACpB,OAA0B;IAE1B,MAAM,QAAQ,GAAuC,EAAE,CAAC;IAExD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC5B,IAAI,OAAO,EAAE,SAAS,IAAI,KAAK,CAAC,KAAK,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;YAC3D,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC/C,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;gBAClC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAG,OAA6B,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;gBACzE,SAAS;YACX,CAAC;YACD,QAAQ,CAAC,OAAiB,CAAC,GAAG,KAAK,CAAC,SAAS,CAAC;YAC9C,SAAS;QACX,CAAC;QAED,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,SAAS,CAAC;IAC1C,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { applyFilter } from './apply-filter';
|
|
2
|
+
export { applySort } from './apply-sort';
|
|
3
|
+
export type { MikroOrmQueryValue, MikroOrmColumnMapFn, MikroOrmColumnMap, MikroOrmSortMapFn, MikroOrmSortMap, ApplyFilterOptions, ApplySortOptions, } from './types';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EACV,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applySort = exports.applyFilter = void 0;
|
|
4
|
+
var apply_filter_1 = require("./apply-filter");
|
|
5
|
+
Object.defineProperty(exports, "applyFilter", { enumerable: true, get: function () { return apply_filter_1.applyFilter; } });
|
|
6
|
+
var apply_sort_1 = require("./apply-sort");
|
|
7
|
+
Object.defineProperty(exports, "applySort", { enumerable: true, get: function () { return apply_sort_1.applySort; } });
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAA6C;AAApC,2GAAA,WAAW,OAAA;AACpB,2CAAyC;AAAhC,uGAAA,SAAS,OAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FilterOperator } from '@nestjs-filter-grammar/core';
|
|
2
|
+
export interface MikroOrmOperatorMapping {
|
|
3
|
+
key: string;
|
|
4
|
+
like?: 'starts' | 'ends' | 'contains';
|
|
5
|
+
caseInsensitive?: boolean;
|
|
6
|
+
negate?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function getMikroOrmOperator(op: FilterOperator): MikroOrmOperatorMapping;
|
|
9
|
+
export declare function wrapLikeValue(value: string, like: 'starts' | 'ends' | 'contains'): string;
|
|
10
|
+
//# sourceMappingURL=operators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operators.d.ts","sourceRoot":"","sources":["../src/operators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,MAAM,WAAW,uBAAuB;IACtC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;IACtC,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAmBD,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,cAAc,GAAG,uBAAuB,CAE/E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,GAAG,MAAM,CAOzF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMikroOrmOperator = getMikroOrmOperator;
|
|
4
|
+
exports.wrapLikeValue = wrapLikeValue;
|
|
5
|
+
const core_1 = require("@nestjs-filter-grammar/core");
|
|
6
|
+
const OPERATOR_MAP = {
|
|
7
|
+
[core_1.FilterOperator.eq]: { key: '$eq' },
|
|
8
|
+
[core_1.FilterOperator.neq]: { key: '$ne' },
|
|
9
|
+
[core_1.FilterOperator.gt]: { key: '$gt' },
|
|
10
|
+
[core_1.FilterOperator.lt]: { key: '$lt' },
|
|
11
|
+
[core_1.FilterOperator.gte]: { key: '$gte' },
|
|
12
|
+
[core_1.FilterOperator.lte]: { key: '$lte' },
|
|
13
|
+
[core_1.FilterOperator.iEq]: { key: '$ilike', caseInsensitive: true },
|
|
14
|
+
[core_1.FilterOperator.iNeq]: { key: '$ilike', caseInsensitive: true, negate: true },
|
|
15
|
+
[core_1.FilterOperator.startsWith]: { key: '$like', like: 'starts' },
|
|
16
|
+
[core_1.FilterOperator.endsWith]: { key: '$like', like: 'ends' },
|
|
17
|
+
[core_1.FilterOperator.contains]: { key: '$like', like: 'contains' },
|
|
18
|
+
[core_1.FilterOperator.iStartsWith]: { key: '$ilike', like: 'starts', caseInsensitive: true },
|
|
19
|
+
[core_1.FilterOperator.iEndsWith]: { key: '$ilike', like: 'ends', caseInsensitive: true },
|
|
20
|
+
[core_1.FilterOperator.iContains]: { key: '$ilike', like: 'contains', caseInsensitive: true },
|
|
21
|
+
};
|
|
22
|
+
function getMikroOrmOperator(op) {
|
|
23
|
+
return OPERATOR_MAP[op];
|
|
24
|
+
}
|
|
25
|
+
function wrapLikeValue(value, like) {
|
|
26
|
+
const escaped = value.replace(/%/g, '\\%').replace(/_/g, '\\_');
|
|
27
|
+
switch (like) {
|
|
28
|
+
case 'starts': return `${escaped}%`;
|
|
29
|
+
case 'ends': return `%${escaped}`;
|
|
30
|
+
case 'contains': return `%${escaped}%`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=operators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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SAAS,CAAC,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE;IAClF,CAAC,qBAAc,CAAC,SAAS,CAAC,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE;CACvF,CAAC;AAEF,SAAgB,mBAAmB,CAAC,EAAkB;IACpD,OAAO,YAAY,CAAC,EAAE,CAAC,CAAC;AAC1B,CAAC;AAED,SAAgB,aAAa,CAAC,KAAa,EAAE,IAAoC;IAC/E,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAChE,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,QAAQ,CAAC,CAAC,OAAO,GAAG,OAAO,GAAG,CAAC;QACpC,KAAK,MAAM,CAAC,CAAC,OAAO,IAAI,OAAO,EAAE,CAAC;QAClC,KAAK,UAAU,CAAC,CAAC,OAAO,IAAI,OAAO,GAAG,CAAC;IACzC,CAAC;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FilterOperator, FilterValue } from '@nestjs-filter-grammar/core';
|
|
2
|
+
/** Recursive type representing a MikroORM FilterQuery/QueryOrderMap value */
|
|
3
|
+
export type MikroOrmQueryValue = string | number | boolean | null | MikroOrmQueryValue[] | {
|
|
4
|
+
[key: string]: MikroOrmQueryValue;
|
|
5
|
+
};
|
|
6
|
+
export type MikroOrmColumnMapFn = (operator: FilterOperator, values: FilterValue[]) => Record<string, MikroOrmQueryValue>;
|
|
7
|
+
export type MikroOrmColumnMap = Record<string, string | MikroOrmColumnMapFn>;
|
|
8
|
+
export type MikroOrmSortMapFn = (direction: 'asc' | 'desc') => Record<string, MikroOrmQueryValue>;
|
|
9
|
+
export type MikroOrmSortMap = Record<string, string | MikroOrmSortMapFn>;
|
|
10
|
+
export interface ApplyFilterOptions {
|
|
11
|
+
columnMap?: MikroOrmColumnMap;
|
|
12
|
+
}
|
|
13
|
+
export interface ApplySortOptions {
|
|
14
|
+
columnMap?: MikroOrmSortMap;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/E,6EAA6E;AAC7E,MAAM,MAAM,kBAAkB,GAC1B,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,kBAAkB,EAAE,GACpB;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB,CAAA;CAAE,CAAC;AAE1C,MAAM,MAAM,mBAAmB,GAAG,CAChC,QAAQ,EAAE,cAAc,EACxB,MAAM,EAAE,WAAW,EAAE,KAClB,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;AAExC,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,mBAAmB,CAAC,CAAC;AAE7E,MAAM,MAAM,iBAAiB,GAAG,CAC9B,SAAS,EAAE,KAAK,GAAG,MAAM,KACtB,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;AAExC,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC;AAEzE,MAAM,WAAW,kBAAkB;IACjC,SAAS,CAAC,EAAE,iBAAiB,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,CAAC,EAAE,eAAe,CAAC;CAC7B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nestjs-filter-grammar/mikroorm",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "MikroORM adapter for @nestjs-filter-grammar — translates FilterTree into MikroORM FilterQuery objects",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.build.json",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"test:watch": "vitest",
|
|
11
|
+
"typecheck": "tsc --noEmit"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@nestjs-filter-grammar/core": "workspace:*"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@mikro-orm/core": ">=6.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@mikro-orm/core": "^6.0.0"
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist"]
|
|
23
|
+
}
|