@heybejs/query 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/dist/ast.d.ts +24 -0
- package/dist/ast.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/to-ast.d.ts +4 -0
- package/dist/to-ast.js +144 -0
- package/dist/to-expand.d.ts +2 -0
- package/dist/to-expand.js +12 -0
- package/dist/to-filter.d.ts +2 -0
- package/dist/to-filter.js +40 -0
- package/dist/to-sort.d.ts +2 -0
- package/dist/to-sort.js +9 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +1 -0
- package/package.json +27 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mr.san
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type Op = "=" | "!=" | ">" | ">=" | "<" | "<=" | "~" | "!~" | "?=" | "?!=" | "?>" | "?>=" | "?<" | "?<=" | "?~" | "?!~";
|
|
2
|
+
export type Operand = {
|
|
3
|
+
kind: "field";
|
|
4
|
+
path: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: "value";
|
|
7
|
+
value: string | number | boolean | null;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "macro";
|
|
10
|
+
name: string;
|
|
11
|
+
} | {
|
|
12
|
+
kind: "call";
|
|
13
|
+
name: string;
|
|
14
|
+
args: Operand[];
|
|
15
|
+
};
|
|
16
|
+
export type Expr = {
|
|
17
|
+
kind: "and" | "or";
|
|
18
|
+
items: Expr[];
|
|
19
|
+
} | {
|
|
20
|
+
kind: "cmp";
|
|
21
|
+
op: Op;
|
|
22
|
+
left: Operand;
|
|
23
|
+
right: Operand;
|
|
24
|
+
};
|
package/dist/ast.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { Expr, Op, Operand } from "./ast.js";
|
|
2
|
+
export type { CollectionName, Collections, Expand, ExpandOf, FindManyArgs, ListResult, NoExtraExpand, OrderBy, Relation, Where, WithExpand, } from "./types.js";
|
|
3
|
+
export { toAst } from "./to-ast.js";
|
|
4
|
+
export { toExpand } from "./to-expand.js";
|
|
5
|
+
export { toFilter } from "./to-filter.js";
|
|
6
|
+
export { toSort } from "./to-sort.js";
|
package/dist/index.js
ADDED
package/dist/to-ast.d.ts
ADDED
package/dist/to-ast.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export const MACROS = new Set([
|
|
2
|
+
"@now",
|
|
3
|
+
"@second",
|
|
4
|
+
"@minute",
|
|
5
|
+
"@hour",
|
|
6
|
+
"@weekday",
|
|
7
|
+
"@day",
|
|
8
|
+
"@month",
|
|
9
|
+
"@year",
|
|
10
|
+
"@yesterday",
|
|
11
|
+
"@tomorrow",
|
|
12
|
+
"@todayStart",
|
|
13
|
+
"@todayEnd",
|
|
14
|
+
"@monthStart",
|
|
15
|
+
"@monthEnd",
|
|
16
|
+
"@yearStart",
|
|
17
|
+
"@yearEnd",
|
|
18
|
+
]);
|
|
19
|
+
const OPERATORS = [
|
|
20
|
+
["eq", "="],
|
|
21
|
+
["neq", "!="],
|
|
22
|
+
["gt", ">"],
|
|
23
|
+
["gte", ">="],
|
|
24
|
+
["lt", "<"],
|
|
25
|
+
["lte", "<="],
|
|
26
|
+
["like", "~"],
|
|
27
|
+
["notLike", "!~"],
|
|
28
|
+
["anyEq", "?="],
|
|
29
|
+
["anyNeq", "?!="],
|
|
30
|
+
["anyGt", "?>"],
|
|
31
|
+
["anyGte", "?>="],
|
|
32
|
+
["anyLt", "?<"],
|
|
33
|
+
["anyLte", "?<="],
|
|
34
|
+
["anyLike", "?~"],
|
|
35
|
+
["anyNotLike", "?!~"],
|
|
36
|
+
];
|
|
37
|
+
const OPERATOR_KEYS = new Set([
|
|
38
|
+
...OPERATORS.map(([key]) => key),
|
|
39
|
+
"insensitive",
|
|
40
|
+
"length",
|
|
41
|
+
"each",
|
|
42
|
+
"empty",
|
|
43
|
+
]);
|
|
44
|
+
const isPlain = (value) => typeof value === "object" &&
|
|
45
|
+
value !== null &&
|
|
46
|
+
!Array.isArray(value) &&
|
|
47
|
+
!(value instanceof Date);
|
|
48
|
+
const isSkipped = (value) => !((typeof value === "string" && value !== "") ||
|
|
49
|
+
(typeof value === "number" && Number.isFinite(value)) ||
|
|
50
|
+
typeof value === "boolean" ||
|
|
51
|
+
(value instanceof Date && !Number.isNaN(value.getTime())));
|
|
52
|
+
const field = (path) => ({ kind: "field", path });
|
|
53
|
+
function toOperand(value, lower) {
|
|
54
|
+
if (value instanceof Date) {
|
|
55
|
+
return {
|
|
56
|
+
kind: "value",
|
|
57
|
+
value: value.toISOString().replace("T", " "),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
if (MACROS.has(value))
|
|
62
|
+
return { kind: "macro", name: value };
|
|
63
|
+
return { kind: "value", value: lower ? value.toLowerCase() : value };
|
|
64
|
+
}
|
|
65
|
+
return { kind: "value", value: value };
|
|
66
|
+
}
|
|
67
|
+
function operatorExprs(path, ops, insensitive) {
|
|
68
|
+
const lower = insensitive || ops.insensitive === true;
|
|
69
|
+
const exprs = [];
|
|
70
|
+
for (const [key, op] of OPERATORS) {
|
|
71
|
+
const value = ops[key];
|
|
72
|
+
if (isSkipped(value))
|
|
73
|
+
continue;
|
|
74
|
+
const fold = lower && typeof value === "string";
|
|
75
|
+
exprs.push({
|
|
76
|
+
kind: "cmp",
|
|
77
|
+
op,
|
|
78
|
+
left: field(fold ? `${path}:lower` : path),
|
|
79
|
+
right: toOperand(value, fold),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (typeof ops.empty === "boolean") {
|
|
83
|
+
exprs.push({
|
|
84
|
+
kind: "cmp",
|
|
85
|
+
op: ops.empty ? "=" : "!=",
|
|
86
|
+
left: field(path),
|
|
87
|
+
right: { kind: "value", value: "" },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
for (const modifier of ["length", "each"]) {
|
|
91
|
+
if (isPlain(ops[modifier])) {
|
|
92
|
+
exprs.push(...operatorExprs(`${path}:${modifier}`, ops[modifier], modifier === "each" && lower));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return exprs;
|
|
96
|
+
}
|
|
97
|
+
function group(kind, items) {
|
|
98
|
+
if (items.length === 0)
|
|
99
|
+
return null;
|
|
100
|
+
if (items.length === 1)
|
|
101
|
+
return items[0];
|
|
102
|
+
return {
|
|
103
|
+
kind,
|
|
104
|
+
items: items.flatMap((item) => (item.kind === kind ? item.items : [item])),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function whereExprs(where, prefix) {
|
|
108
|
+
const exprs = [];
|
|
109
|
+
for (const [key, value] of Object.entries(where)) {
|
|
110
|
+
if (key === "AND" || key === "OR") {
|
|
111
|
+
if (!Array.isArray(value))
|
|
112
|
+
continue;
|
|
113
|
+
const items = value.flatMap((child) => {
|
|
114
|
+
if (!isPlain(child))
|
|
115
|
+
return [];
|
|
116
|
+
const node = group("and", whereExprs(child, prefix));
|
|
117
|
+
return node ? [node] : [];
|
|
118
|
+
});
|
|
119
|
+
const node = group(key === "AND" ? "and" : "or", items);
|
|
120
|
+
if (node)
|
|
121
|
+
exprs.push(node);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
125
|
+
if (isPlain(value)) {
|
|
126
|
+
const isOperatorObject = Object.keys(value).some((k) => OPERATOR_KEYS.has(k));
|
|
127
|
+
exprs.push(...(isOperatorObject
|
|
128
|
+
? operatorExprs(path, value, false)
|
|
129
|
+
: whereExprs(value, path)));
|
|
130
|
+
}
|
|
131
|
+
else if (!isSkipped(value)) {
|
|
132
|
+
exprs.push({
|
|
133
|
+
kind: "cmp",
|
|
134
|
+
op: "=",
|
|
135
|
+
left: field(path),
|
|
136
|
+
right: toOperand(value, false),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return exprs;
|
|
141
|
+
}
|
|
142
|
+
export function toAst(where) {
|
|
143
|
+
return group("and", whereExprs(where, ""));
|
|
144
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// { author: true, comments_via_post: { user: true } } -> "author,comments_via_post.user"
|
|
2
|
+
export function toExpand(expand) {
|
|
3
|
+
const paths = (tree, prefix) => Object.entries(tree).flatMap(([key, value]) => {
|
|
4
|
+
if (value === true)
|
|
5
|
+
return [prefix + key];
|
|
6
|
+
if (typeof value !== "object" || value === null)
|
|
7
|
+
return [];
|
|
8
|
+
const nested = paths(value, `${prefix}${key}.`);
|
|
9
|
+
return nested.length > 0 ? nested : [prefix + key];
|
|
10
|
+
});
|
|
11
|
+
return expand ? paths(expand, "").join(",") : "";
|
|
12
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const ESCAPES = {
|
|
2
|
+
"\\": "\\\\",
|
|
3
|
+
'"': '\\"',
|
|
4
|
+
"\n": "\\n",
|
|
5
|
+
"\t": "\\t",
|
|
6
|
+
"\r": "\\r",
|
|
7
|
+
};
|
|
8
|
+
// Only the escapes the filter scanner decodes.
|
|
9
|
+
const quote = (value) => `"${value.replace(/[\\"\n\t\r]/g, (ch) => ESCAPES[ch])}"`;
|
|
10
|
+
function operand(node) {
|
|
11
|
+
switch (node.kind) {
|
|
12
|
+
case "field":
|
|
13
|
+
return node.path;
|
|
14
|
+
case "macro":
|
|
15
|
+
return node.name;
|
|
16
|
+
case "call":
|
|
17
|
+
return `${node.name}(${node.args.map(operand).join(",")})`;
|
|
18
|
+
case "value":
|
|
19
|
+
return typeof node.value === "string"
|
|
20
|
+
? quote(node.value)
|
|
21
|
+
: String(node.value);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function render(expr) {
|
|
25
|
+
if (expr.kind === "cmp") {
|
|
26
|
+
return `${operand(expr.left)}${expr.op}${operand(expr.right)}`;
|
|
27
|
+
}
|
|
28
|
+
const separator = expr.kind === "and" ? " && " : " || ";
|
|
29
|
+
const parts = expr.items.flatMap((item) => {
|
|
30
|
+
const text = render(item);
|
|
31
|
+
if (text === "")
|
|
32
|
+
return [];
|
|
33
|
+
const nested = item.kind !== "cmp" && item.kind !== expr.kind;
|
|
34
|
+
return [nested ? `(${text})` : text];
|
|
35
|
+
});
|
|
36
|
+
return parts.join(separator);
|
|
37
|
+
}
|
|
38
|
+
export function toFilter(expr) {
|
|
39
|
+
return expr ? render(expr) : "";
|
|
40
|
+
}
|
package/dist/to-sort.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function toSort(orderBy) {
|
|
2
|
+
if (!orderBy)
|
|
3
|
+
return "";
|
|
4
|
+
const entries = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
5
|
+
return entries
|
|
6
|
+
.flatMap((entry) => Object.entries(entry))
|
|
7
|
+
.map(([name, direction]) => (direction === "desc" ? `-${name}` : name))
|
|
8
|
+
.join(",");
|
|
9
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export interface Collections {
|
|
2
|
+
}
|
|
3
|
+
export type CollectionName = keyof Collections & string;
|
|
4
|
+
type Scalar = string | number | boolean | Date;
|
|
5
|
+
declare const relation: unique symbol;
|
|
6
|
+
type IsMarked<V> = typeof relation extends keyof V ? true : false;
|
|
7
|
+
export type Relation<C extends string = string> = string & {
|
|
8
|
+
readonly [relation]?: C;
|
|
9
|
+
};
|
|
10
|
+
type Operators<V> = {
|
|
11
|
+
eq?: V | null;
|
|
12
|
+
neq?: V | null;
|
|
13
|
+
gt?: V | null;
|
|
14
|
+
gte?: V | null;
|
|
15
|
+
lt?: V | null;
|
|
16
|
+
lte?: V | null;
|
|
17
|
+
like?: string | null;
|
|
18
|
+
notLike?: string | null;
|
|
19
|
+
/** On a multi-value field the filter `tags?="x"` matches nothing; use `each: { anyEq: "x" }` (`tags:each?="x"`) instead. */
|
|
20
|
+
anyEq?: V | null;
|
|
21
|
+
anyNeq?: V | null;
|
|
22
|
+
anyGt?: V | null;
|
|
23
|
+
anyGte?: V | null;
|
|
24
|
+
anyLt?: V | null;
|
|
25
|
+
anyLte?: V | null;
|
|
26
|
+
anyLike?: string | null;
|
|
27
|
+
anyNotLike?: string | null;
|
|
28
|
+
insensitive?: boolean;
|
|
29
|
+
length?: Operators<number>;
|
|
30
|
+
each?: Operators<V>;
|
|
31
|
+
empty?: boolean;
|
|
32
|
+
};
|
|
33
|
+
type FieldWhere<V> = [NonNullable<V>] extends [infer U] ? U extends readonly (infer E)[] ? E | Operators<Extract<E, Scalar>> : IsMarked<U> extends true ? U | Operators<string> | Record<string, unknown> : U extends Scalar ? U | Operators<U> : Record<string, unknown> : never;
|
|
34
|
+
export type Where<T> = string extends keyof T ? Record<string, unknown> : {
|
|
35
|
+
[K in keyof T]?: FieldWhere<T[K]> | null;
|
|
36
|
+
} & {
|
|
37
|
+
AND?: Where<T>[];
|
|
38
|
+
OR?: Where<T>[];
|
|
39
|
+
};
|
|
40
|
+
type ExpandTree = {
|
|
41
|
+
[key: string]: true | ExpandTree | undefined;
|
|
42
|
+
};
|
|
43
|
+
type IsRelation<V> = [V] extends [readonly (infer E)[]] ? IsMarked<E> extends true ? true : false : IsMarked<V> extends true ? true : false;
|
|
44
|
+
type RelationKeys<T> = {
|
|
45
|
+
[K in keyof T & string]-?: IsRelation<NonNullable<T[K]>> extends true ? K : never;
|
|
46
|
+
}[keyof T & string];
|
|
47
|
+
export type Expand<T> = unknown extends T ? ExpandTree : string extends keyof T ? ExpandTree : {
|
|
48
|
+
[K in RelationKeys<T>]?: true | ExpandTree;
|
|
49
|
+
} & {
|
|
50
|
+
[K in `${string}_via_${string}`]?: true | ExpandTree;
|
|
51
|
+
};
|
|
52
|
+
type Loose = Record<string, unknown>;
|
|
53
|
+
type RelationTarget<V> = NonNullable<V> extends readonly (infer E)[] ? E : NonNullable<V>;
|
|
54
|
+
type Resolved<V, Sub> = RelationTarget<V> extends Relation<infer C> ? C extends keyof Collections ? WithExpand<Collections[C], Sub extends object ? Sub : undefined> : Loose : Loose;
|
|
55
|
+
type ExpandEntry<T, K, Sub> = K extends keyof T ? NonNullable<T[K]> extends readonly unknown[] ? Resolved<T[K], Sub>[] : Resolved<T[K], Sub> : Loose | Loose[];
|
|
56
|
+
export type NoExtraExpand<T, E> = {
|
|
57
|
+
[K in Exclude<keyof NonNullable<E>, keyof Expand<T>>]: never;
|
|
58
|
+
};
|
|
59
|
+
export type ExpandOf<A> = "expand" extends keyof A ? A["expand" & keyof A] : undefined;
|
|
60
|
+
export type WithExpand<T, E> = [NonNullable<E>] extends [never] ? T : T & {
|
|
61
|
+
expand?: {
|
|
62
|
+
[K in keyof NonNullable<E> & string]?: ExpandEntry<T, K, NonNullable<E>[K]>;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
type Direction = "asc" | "desc";
|
|
66
|
+
type OrderEntry<T> = {
|
|
67
|
+
[K in keyof T & string]?: Direction;
|
|
68
|
+
};
|
|
69
|
+
export type OrderBy<T> = OrderEntry<T> | OrderEntry<T>[];
|
|
70
|
+
export interface FindManyArgs<T> {
|
|
71
|
+
where?: Where<T>;
|
|
72
|
+
orderBy?: OrderBy<T>;
|
|
73
|
+
page?: number;
|
|
74
|
+
perPage?: number;
|
|
75
|
+
count?: boolean;
|
|
76
|
+
expand?: Expand<T>;
|
|
77
|
+
}
|
|
78
|
+
export interface ListResult<T> {
|
|
79
|
+
page: number;
|
|
80
|
+
perPage: number;
|
|
81
|
+
totalItems: number;
|
|
82
|
+
totalPages: number;
|
|
83
|
+
items: T[];
|
|
84
|
+
}
|
|
85
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heybejs/query",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"author": "emresandikci",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/emresandikci/heybe.git",
|
|
20
|
+
"directory": "packages/query"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@heybejs/configs": "0.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
27
|
+
"release": "semantic-release -e semantic-release-monorepo",
|
|
28
|
+
"test": "bun test src"
|
|
29
|
+
}
|
|
6
30
|
}
|
package/README.md
DELETED