@gettersethya/mira-collection 0.1.0-alpha.10
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/README.md +164 -0
- package/dist/src/collection/auth.d.ts +79 -0
- package/dist/src/collection/auth.d.ts.map +1 -0
- package/dist/src/collection/auth.js +55 -0
- package/dist/src/collection/auth.js.map +1 -0
- package/dist/src/collection/base.d.ts +64 -0
- package/dist/src/collection/base.d.ts.map +1 -0
- package/dist/src/collection/base.js +45 -0
- package/dist/src/collection/base.js.map +1 -0
- package/dist/src/collection/bytes.d.ts +51 -0
- package/dist/src/collection/bytes.d.ts.map +1 -0
- package/dist/src/collection/bytes.js +51 -0
- package/dist/src/collection/bytes.js.map +1 -0
- package/dist/src/collection/errors.d.ts +29 -0
- package/dist/src/collection/errors.d.ts.map +1 -0
- package/dist/src/collection/errors.js +23 -0
- package/dist/src/collection/errors.js.map +1 -0
- package/dist/src/collection/field.d.ts +334 -0
- package/dist/src/collection/field.d.ts.map +1 -0
- package/dist/src/collection/field.js +201 -0
- package/dist/src/collection/field.js.map +1 -0
- package/dist/src/collection/index-builder.d.ts +59 -0
- package/dist/src/collection/index-builder.d.ts.map +1 -0
- package/dist/src/collection/index-builder.js +43 -0
- package/dist/src/collection/index-builder.js.map +1 -0
- package/dist/src/collection/index.d.ts +22 -0
- package/dist/src/collection/index.d.ts.map +1 -0
- package/dist/src/collection/index.js +21 -0
- package/dist/src/collection/index.js.map +1 -0
- package/dist/src/collection/serialize.d.ts +24 -0
- package/dist/src/collection/serialize.d.ts.map +1 -0
- package/dist/src/collection/serialize.js +133 -0
- package/dist/src/collection/serialize.js.map +1 -0
- package/dist/src/collection/types.d.ts +127 -0
- package/dist/src/collection/types.d.ts.map +1 -0
- package/dist/src/collection/types.js +2 -0
- package/dist/src/collection/types.js.map +1 -0
- package/dist/src/collection/view.d.ts +57 -0
- package/dist/src/collection/view.d.ts.map +1 -0
- package/dist/src/collection/view.js +54 -0
- package/dist/src/collection/view.js.map +1 -0
- package/dist/src/filter/builder.d.ts +127 -0
- package/dist/src/filter/builder.d.ts.map +1 -0
- package/dist/src/filter/builder.js +72 -0
- package/dist/src/filter/builder.js.map +1 -0
- package/dist/src/filter/compiler.d.ts +48 -0
- package/dist/src/filter/compiler.d.ts.map +1 -0
- package/dist/src/filter/compiler.js +132 -0
- package/dist/src/filter/compiler.js.map +1 -0
- package/dist/src/filter/index.d.ts +6 -0
- package/dist/src/filter/index.d.ts.map +1 -0
- package/dist/src/filter/index.js +4 -0
- package/dist/src/filter/index.js.map +1 -0
- package/dist/src/filter/types.d.ts +96 -0
- package/dist/src/filter/types.d.ts.map +1 -0
- package/dist/src/filter/types.js +44 -0
- package/dist/src/filter/types.js.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +4 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/rule/builder.d.ts +91 -0
- package/dist/src/rule/builder.d.ts.map +1 -0
- package/dist/src/rule/builder.js +138 -0
- package/dist/src/rule/builder.js.map +1 -0
- package/dist/src/rule/index.d.ts +3 -0
- package/dist/src/rule/index.d.ts.map +1 -0
- package/dist/src/rule/index.js +3 -0
- package/dist/src/rule/index.js.map +1 -0
- package/dist/src/rule/types.d.ts +132 -0
- package/dist/src/rule/types.d.ts.map +1 -0
- package/dist/src/rule/types.js +2 -0
- package/dist/src/rule/types.js.map +1 -0
- package/dist/tests/collection/auth.test.d.ts +2 -0
- package/dist/tests/collection/auth.test.d.ts.map +1 -0
- package/dist/tests/collection/auth.test.js +44 -0
- package/dist/tests/collection/auth.test.js.map +1 -0
- package/dist/tests/collection/base.test.d.ts +2 -0
- package/dist/tests/collection/base.test.d.ts.map +1 -0
- package/dist/tests/collection/base.test.js +70 -0
- package/dist/tests/collection/base.test.js.map +1 -0
- package/dist/tests/collection/bytes.test.d.ts +2 -0
- package/dist/tests/collection/bytes.test.d.ts.map +1 -0
- package/dist/tests/collection/bytes.test.js +9 -0
- package/dist/tests/collection/bytes.test.js.map +1 -0
- package/dist/tests/collection/field.test.d.ts +2 -0
- package/dist/tests/collection/field.test.d.ts.map +1 -0
- package/dist/tests/collection/field.test.js +165 -0
- package/dist/tests/collection/field.test.js.map +1 -0
- package/dist/tests/collection/index-type.test.d.ts +2 -0
- package/dist/tests/collection/index-type.test.d.ts.map +1 -0
- package/dist/tests/collection/index-type.test.js +28 -0
- package/dist/tests/collection/index-type.test.js.map +1 -0
- package/dist/tests/collection/serialize.test.d.ts +2 -0
- package/dist/tests/collection/serialize.test.d.ts.map +1 -0
- package/dist/tests/collection/serialize.test.js +193 -0
- package/dist/tests/collection/serialize.test.js.map +1 -0
- package/dist/tests/collection/view.test.d.ts +2 -0
- package/dist/tests/collection/view.test.d.ts.map +1 -0
- package/dist/tests/collection/view.test.js +66 -0
- package/dist/tests/collection/view.test.js.map +1 -0
- package/dist/tests/filter/compiler.test.d.ts +2 -0
- package/dist/tests/filter/compiler.test.d.ts.map +1 -0
- package/dist/tests/filter/compiler.test.js +156 -0
- package/dist/tests/filter/compiler.test.js.map +1 -0
- package/dist/tests/rule/builder.test.d.ts +2 -0
- package/dist/tests/rule/builder.test.d.ts.map +1 -0
- package/dist/tests/rule/builder.test.js +342 -0
- package/dist/tests/rule/builder.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
function toFieldOperand(field) {
|
|
2
|
+
return {
|
|
3
|
+
eq: (value) => ({ op: "eq", field, value }),
|
|
4
|
+
neq: (value) => ({ op: "neq", field, value }),
|
|
5
|
+
gt: (value) => ({ op: "gt", field, value }),
|
|
6
|
+
gte: (value) => ({ op: "gte", field, value }),
|
|
7
|
+
lt: (value) => ({ op: "lt", field, value }),
|
|
8
|
+
lte: (value) => ({ op: "lte", field, value }),
|
|
9
|
+
in: (values) => ({ op: "in", field, values }),
|
|
10
|
+
like: (value) => ({ op: "like", field, value }),
|
|
11
|
+
null: () => ({ op: "null", field }),
|
|
12
|
+
notNull: () => ({ op: "not_null", field }),
|
|
13
|
+
between: (lo, hi) => ({ op: "and", left: { op: "gte", field, value: lo }, right: { op: "lte", field, value: hi } })
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Filter DSL builder for constructing `FilterNode` expressions.
|
|
18
|
+
*
|
|
19
|
+
* Use `Filter.field(name)` to start a comparison chain, then call a comparison
|
|
20
|
+
* method (`.eq()`, `.neq()`, `.gt()`, etc.) to produce a `FilterNode`.
|
|
21
|
+
* Combine nodes with `Filter.and()`, `Filter.or()`, `Filter.not()`.
|
|
22
|
+
*
|
|
23
|
+
* This is the untyped variant — field names are plain strings and values are
|
|
24
|
+
* `unknown`. For type-safe field access, use `FilterBuilder<F>` via the
|
|
25
|
+
* `CollectionClient` callback pattern.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* import { Filter } from "@gettersethya/mira-collection"
|
|
29
|
+
*
|
|
30
|
+
* // Simple equality
|
|
31
|
+
* const published = Filter.field("published").eq(true)
|
|
32
|
+
*
|
|
33
|
+
* // Range check
|
|
34
|
+
* const popular = Filter.and(
|
|
35
|
+
* Filter.field("views").gte(100),
|
|
36
|
+
* Filter.field("views").lte(10_000)
|
|
37
|
+
* )
|
|
38
|
+
*
|
|
39
|
+
* // In-list
|
|
40
|
+
* const draftOrArchived = Filter.field("status").in(["draft", "archived"])
|
|
41
|
+
*
|
|
42
|
+
* // LIKE pattern
|
|
43
|
+
* const search = Filter.field("title").like("%effect%")
|
|
44
|
+
*
|
|
45
|
+
* // Null checks
|
|
46
|
+
* const hasDeletedAt = Filter.field("deletedAt").null()
|
|
47
|
+
* const notDeleted = Filter.field("deletedAt").notNull()
|
|
48
|
+
*
|
|
49
|
+
* // Complex combination
|
|
50
|
+
* const query = Filter.and(
|
|
51
|
+
* Filter.field("published").eq(true),
|
|
52
|
+
* Filter.or(
|
|
53
|
+
* Filter.field("category").eq("tech"),
|
|
54
|
+
* Filter.field("tags").contains("effect")
|
|
55
|
+
* )
|
|
56
|
+
* )
|
|
57
|
+
*
|
|
58
|
+
* @see FilterNode — the AST node produced by filter methods
|
|
59
|
+
* @see FilterBuilder — typed variant used in CollectionClient methods
|
|
60
|
+
* @see filterNodeToWhereClause — compiles FilterNode to SQL
|
|
61
|
+
*/
|
|
62
|
+
export const Filter = {
|
|
63
|
+
/** Start a filter chain for a field by name. Returns a `FieldFilterOperand` with comparison methods. */
|
|
64
|
+
field: (name) => toFieldOperand(name),
|
|
65
|
+
/** Combine two filter nodes with logical AND. Produces `(left) AND (right)` in SQL. */
|
|
66
|
+
and: (left, right) => ({ op: "and", left, right }),
|
|
67
|
+
/** Combine two filter nodes with logical OR. Produces `(left) OR (right)` in SQL. */
|
|
68
|
+
or: (left, right) => ({ op: "or", left, right }),
|
|
69
|
+
/** Negate a filter node with logical NOT. Produces `NOT (node)` in SQL. */
|
|
70
|
+
not: (node) => ({ op: "not", node })
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/filter/builder.ts"],"names":[],"mappings":"AA+CA,SAAS,cAAc,CAAC,KAAa;IACnC,OAAO;QACL,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAa,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACpD,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAc,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACtD,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAa,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACpD,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE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iB,EAAc,EAAE,CACvD,CAAC,EAAE,EAAE,EAAE,KAAc,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IAEvC,qFAAqF;IACrF,EAAE,EAAE,CAAC,IAAgB,EAAE,KAAiB,EAAc,EAAE,CACtD,CAAC,EAAE,EAAE,EAAE,IAAa,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IAEtC,2EAA2E;IAC3E,GAAG,EAAE,CAAC,IAAgB,EAAc,EAAE,CACpC,CAAC,EAAE,EAAE,EAAE,KAAc,EAAE,IAAI,EAAE,CAAC;CACjC,CAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { CollectionSchema } from "../collection/types.js";
|
|
3
|
+
import { ValidationError } from "../collection/errors.js";
|
|
4
|
+
import type { FilterNode, WhereClause } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Compile a `FilterNode` AST into a `WhereClause` (`{ sql, params }`) suitable
|
|
7
|
+
* for splicing into an Effect SQL template literal via `unsafeFragment`.
|
|
8
|
+
*
|
|
9
|
+
* Validates each referenced field against the collection schema. Unknown fields
|
|
10
|
+
* produce a `ValidationError` in the Effect error channel.
|
|
11
|
+
*
|
|
12
|
+
* Field names are SQL-identifier-quoted (double-quoted as `t."fieldName"`).
|
|
13
|
+
* Values use `?`-style positional placeholders with params in the returned array.
|
|
14
|
+
*
|
|
15
|
+
* Edge cases:
|
|
16
|
+
* - `IN ()` with an empty array produces `1 = 0` (always false) instead of
|
|
17
|
+
* invalid SQL like `IN ()`.
|
|
18
|
+
* - The `like` pattern is passed through verbatim — the caller is responsible
|
|
19
|
+
* for adding `%` wildcards.
|
|
20
|
+
* - All column references use the hardcoded table alias `t`.
|
|
21
|
+
*
|
|
22
|
+
* @param node - The filter AST to compile
|
|
23
|
+
* @param schema - The collection schema (used for field validation)
|
|
24
|
+
* @param collectionName - Collection name (used only in error messages)
|
|
25
|
+
* @returns Effect that resolves to a WhereClause, or fails with ValidationError
|
|
26
|
+
* if any field name in the filter does not exist in the schema
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { Filter, filterNodeToWhereClause } from "@gettersethya/mira-collection"
|
|
30
|
+
* import type { CollectionSchema } from "@gettersethya/mira-collection"
|
|
31
|
+
*
|
|
32
|
+
* const schema: CollectionSchema = {
|
|
33
|
+
* type: "object",
|
|
34
|
+
* properties: { title: { type: "string" } }
|
|
35
|
+
* } as CollectionSchema
|
|
36
|
+
*
|
|
37
|
+
* const effect = filterNodeToWhereClause(
|
|
38
|
+
* Filter.field("title").eq("hello"),
|
|
39
|
+
* schema,
|
|
40
|
+
* "posts"
|
|
41
|
+
* )
|
|
42
|
+
* // Resolves to: { sql: 't."title" = ?', params: ["hello"] }
|
|
43
|
+
*
|
|
44
|
+
* @see FilterNode — the AST type consumed by this function
|
|
45
|
+
* @see WhereClause — the output type
|
|
46
|
+
*/
|
|
47
|
+
export declare function filterNodeToWhereClause(node: FilterNode, schema: CollectionSchema, collectionName: string): Effect.Effect<WhereClause, ValidationError>;
|
|
48
|
+
//# sourceMappingURL=compiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../../../src/filter/compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,gBAAgB,EACxB,cAAc,EAAE,MAAM,GACrB,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,eAAe,CAAC,CAE7C"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { ValidationError } from "../collection/errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Compile a `FilterNode` AST into a `WhereClause` (`{ sql, params }`) suitable
|
|
5
|
+
* for splicing into an Effect SQL template literal via `unsafeFragment`.
|
|
6
|
+
*
|
|
7
|
+
* Validates each referenced field against the collection schema. Unknown fields
|
|
8
|
+
* produce a `ValidationError` in the Effect error channel.
|
|
9
|
+
*
|
|
10
|
+
* Field names are SQL-identifier-quoted (double-quoted as `t."fieldName"`).
|
|
11
|
+
* Values use `?`-style positional placeholders with params in the returned array.
|
|
12
|
+
*
|
|
13
|
+
* Edge cases:
|
|
14
|
+
* - `IN ()` with an empty array produces `1 = 0` (always false) instead of
|
|
15
|
+
* invalid SQL like `IN ()`.
|
|
16
|
+
* - The `like` pattern is passed through verbatim — the caller is responsible
|
|
17
|
+
* for adding `%` wildcards.
|
|
18
|
+
* - All column references use the hardcoded table alias `t`.
|
|
19
|
+
*
|
|
20
|
+
* @param node - The filter AST to compile
|
|
21
|
+
* @param schema - The collection schema (used for field validation)
|
|
22
|
+
* @param collectionName - Collection name (used only in error messages)
|
|
23
|
+
* @returns Effect that resolves to a WhereClause, or fails with ValidationError
|
|
24
|
+
* if any field name in the filter does not exist in the schema
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* import { Filter, filterNodeToWhereClause } from "@gettersethya/mira-collection"
|
|
28
|
+
* import type { CollectionSchema } from "@gettersethya/mira-collection"
|
|
29
|
+
*
|
|
30
|
+
* const schema: CollectionSchema = {
|
|
31
|
+
* type: "object",
|
|
32
|
+
* properties: { title: { type: "string" } }
|
|
33
|
+
* } as CollectionSchema
|
|
34
|
+
*
|
|
35
|
+
* const effect = filterNodeToWhereClause(
|
|
36
|
+
* Filter.field("title").eq("hello"),
|
|
37
|
+
* schema,
|
|
38
|
+
* "posts"
|
|
39
|
+
* )
|
|
40
|
+
* // Resolves to: { sql: 't."title" = ?', params: ["hello"] }
|
|
41
|
+
*
|
|
42
|
+
* @see FilterNode — the AST type consumed by this function
|
|
43
|
+
* @see WhereClause — the output type
|
|
44
|
+
*/
|
|
45
|
+
export function filterNodeToWhereClause(node, schema, collectionName) {
|
|
46
|
+
return compileNode(node, schema, collectionName);
|
|
47
|
+
}
|
|
48
|
+
function compileNode(node, schema, collection) {
|
|
49
|
+
switch (node.op) {
|
|
50
|
+
case "eq":
|
|
51
|
+
case "neq":
|
|
52
|
+
case "gt":
|
|
53
|
+
case "gte":
|
|
54
|
+
case "lt":
|
|
55
|
+
case "lte":
|
|
56
|
+
return compileComparison(node, collection, schema);
|
|
57
|
+
case "in":
|
|
58
|
+
return compileIn(node, collection, schema);
|
|
59
|
+
case "like":
|
|
60
|
+
return compileLike(node, collection, schema);
|
|
61
|
+
case "null":
|
|
62
|
+
case "not_null":
|
|
63
|
+
return compileNull(node, collection, schema);
|
|
64
|
+
case "and":
|
|
65
|
+
case "or":
|
|
66
|
+
return compileLogical(node, schema, collection);
|
|
67
|
+
case "not":
|
|
68
|
+
return compileNot(node, schema, collection);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function validateField(field, collection, schema) {
|
|
72
|
+
if (!schema.properties[field]) {
|
|
73
|
+
return Effect.fail(new ValidationError({
|
|
74
|
+
collection,
|
|
75
|
+
issues: [`Unknown field in filter: "${field}"`]
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
return Effect.void;
|
|
79
|
+
}
|
|
80
|
+
function compileComparison(node, collection, schema) {
|
|
81
|
+
const opMap = {
|
|
82
|
+
eq: "=",
|
|
83
|
+
neq: "!=",
|
|
84
|
+
gt: ">",
|
|
85
|
+
gte: ">=",
|
|
86
|
+
lt: "<",
|
|
87
|
+
lte: "<="
|
|
88
|
+
};
|
|
89
|
+
return Effect.gen(function* () {
|
|
90
|
+
yield* validateField(node.field, collection, schema);
|
|
91
|
+
return { sql: `t."${node.field}" ${opMap[node.op]} ?`, params: [node.value] };
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function compileIn(node, collection, schema) {
|
|
95
|
+
return Effect.gen(function* () {
|
|
96
|
+
yield* validateField(node.field, collection, schema);
|
|
97
|
+
if (node.values.length === 0)
|
|
98
|
+
return { sql: "1 = 0", params: [] };
|
|
99
|
+
const placeholders = node.values.map(() => "?").join(", ");
|
|
100
|
+
return { sql: `t."${node.field}" IN (${placeholders})`, params: [...node.values] };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function compileLike(node, collection, schema) {
|
|
104
|
+
return Effect.gen(function* () {
|
|
105
|
+
yield* validateField(node.field, collection, schema);
|
|
106
|
+
return { sql: `t."${node.field}" LIKE ?`, params: [node.value] };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function compileNull(node, collection, schema) {
|
|
110
|
+
return Effect.gen(function* () {
|
|
111
|
+
yield* validateField(node.field, collection, schema);
|
|
112
|
+
if (node.op === "null") {
|
|
113
|
+
return { sql: `t."${node.field}" IS NULL`, params: [] };
|
|
114
|
+
}
|
|
115
|
+
return { sql: `t."${node.field}" IS NOT NULL`, params: [] };
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function compileLogical(node, schema, collection) {
|
|
119
|
+
return Effect.gen(function* () {
|
|
120
|
+
const left = yield* compileNode(node.left, schema, collection);
|
|
121
|
+
const right = yield* compileNode(node.right, schema, collection);
|
|
122
|
+
const op = node.op.toUpperCase();
|
|
123
|
+
return { sql: `(${left.sql}) ${op} (${right.sql})`, params: [...left.params, ...right.params] };
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function compileNot(node, schema, collection) {
|
|
127
|
+
return Effect.gen(function* () {
|
|
128
|
+
const inner = yield* compileNode(node.node, schema, collection);
|
|
129
|
+
return { sql: `NOT (${inner.sql})`, params: inner.params };
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=compiler.js.map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KAAK,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,CAAA;QAC9D,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,CAAA;QAChE,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC,WAAW,EAAE,CAAA;QAChC,OAAO,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,KAAK,EAAE,KAAK,KAAK,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,EAAE,CAAA;IACjG,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,UAAU,CACjB,IAAkD,EAClD,MAAwB,EACxB,UAAkB;IAElB,OAAO,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACzB,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,CAAA;QAC/D,OAAO,EAAE,GAAG,EAAE,QAAQ,KAAK,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAA;IAC5D,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Filter } from "./builder.js";
|
|
2
|
+
export { filterNodeToWhereClause } from "./compiler.js";
|
|
3
|
+
export { FilterNodeSchema } from "./types.js";
|
|
4
|
+
export type { FilterBuilder, FieldFilterOperand } from "./builder.js";
|
|
5
|
+
export type { FilterNode, WhereClause } from "./types.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/filter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACrE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/filter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* A pre-compiled SQL WHERE fragment with positional `?` placeholders.
|
|
4
|
+
*
|
|
5
|
+
* Produced by `filterNodeToWhereClause()` and consumed by the repository layer.
|
|
6
|
+
* The `sql` string uses `?`-style placeholders; `params` supplies the values
|
|
7
|
+
* in positional order. Pass to `unsafeFragment(w.sql, w.params)` from
|
|
8
|
+
* `@effect/sql/Statement` to splice into a template literal.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Output of compiler for Filter.field("title").eq("hello"):
|
|
12
|
+
* const w: WhereClause = { sql: 't."title" = ?', params: ["hello"] }
|
|
13
|
+
*
|
|
14
|
+
* @see FilterNode — the AST type that compiles to WhereClause
|
|
15
|
+
* @see filterNodeToWhereClause — produces WhereClause from FilterNode
|
|
16
|
+
*/
|
|
17
|
+
export type WhereClause = {
|
|
18
|
+
sql: string;
|
|
19
|
+
params: ReadonlyArray<unknown>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* AST node for filter expressions. Discriminated union with 6 variant groups:
|
|
23
|
+
*
|
|
24
|
+
* | Variants | Fields | Description |
|
|
25
|
+
* |---|---|---|
|
|
26
|
+
* | `eq`, `neq`, `gt`, `gte`, `lt`, `lte` | `field`, `value` | Value comparisons |
|
|
27
|
+
* | `in` | `field`, `values` | Membership test |
|
|
28
|
+
* | `like` | `field`, `value` | SQL LIKE pattern match |
|
|
29
|
+
* | `null`, `not_null` | `field` | Null checks (no value operand) |
|
|
30
|
+
* | `and`, `or` | `left`, `right` | Binary logical combinators |
|
|
31
|
+
* | `not` | `node` | Unary negation |
|
|
32
|
+
*
|
|
33
|
+
* Construct via `Filter.field(name).eq(value)` or `Filter.and(left, right)`.
|
|
34
|
+
* Consumed by `filterNodeToWhereClause()` to produce SQL WHERE clauses.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* import { Filter } from "@gettersethya/mira-collection"
|
|
38
|
+
*
|
|
39
|
+
* // Single comparison
|
|
40
|
+
* const node: FilterNode = Filter.field("title").eq("hello")
|
|
41
|
+
*
|
|
42
|
+
* // Logical combination
|
|
43
|
+
* const complex: FilterNode = Filter.and(
|
|
44
|
+
* Filter.field("published").eq(true),
|
|
45
|
+
* Filter.field("views").gte(100)
|
|
46
|
+
* )
|
|
47
|
+
*
|
|
48
|
+
* // Null check
|
|
49
|
+
* const deleted: FilterNode = Filter.field("deletedAt").notNull()
|
|
50
|
+
*
|
|
51
|
+
* @see Filter — builder DSL for constructing FilterNode values
|
|
52
|
+
* @see filterNodeToWhereClause — compiles FilterNode to SQL
|
|
53
|
+
*/
|
|
54
|
+
export type FilterNode = {
|
|
55
|
+
op: "eq" | "neq" | "gt" | "gte" | "lt" | "lte";
|
|
56
|
+
field: string;
|
|
57
|
+
value: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
op: "in";
|
|
60
|
+
field: string;
|
|
61
|
+
values: ReadonlyArray<unknown>;
|
|
62
|
+
} | {
|
|
63
|
+
op: "like";
|
|
64
|
+
field: string;
|
|
65
|
+
value: string;
|
|
66
|
+
} | {
|
|
67
|
+
op: "null" | "not_null";
|
|
68
|
+
field: string;
|
|
69
|
+
} | {
|
|
70
|
+
op: "and" | "or";
|
|
71
|
+
left: FilterNode;
|
|
72
|
+
right: FilterNode;
|
|
73
|
+
} | {
|
|
74
|
+
op: "not";
|
|
75
|
+
node: FilterNode;
|
|
76
|
+
};
|
|
77
|
+
declare let _filterNodeSchema: Schema.Schema<FilterNode>;
|
|
78
|
+
/**
|
|
79
|
+
* Effect Schema for `FilterNode`. Used to decode filter query parameters
|
|
80
|
+
* from HTTP request JSON bodies.
|
|
81
|
+
*
|
|
82
|
+
* The schema is self-referential (recursive via `Schema.suspend`) to handle
|
|
83
|
+
* nested `and`/`or`/`not` nodes. This is why the underlying `let` variable
|
|
84
|
+
* exists — the recursive `suspend` needs a forward reference.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* import { Schema } from "effect"
|
|
88
|
+
*
|
|
89
|
+
* const raw = { op: "eq", field: "title", value: "hello" }
|
|
90
|
+
* const filter = Schema.decodeSync(FilterNodeSchema)(raw)
|
|
91
|
+
* // filter: FilterNode — { op: "eq", field: "title", value: "hello" }
|
|
92
|
+
*
|
|
93
|
+
* @see FilterNode — the TypeScript type this schema decodes to
|
|
94
|
+
*/
|
|
95
|
+
export { _filterNodeSchema as FilterNodeSchema };
|
|
96
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/filter/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAC/B,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,UAAU,GAClB;IAAE,EAAE,EAAE,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GACjF;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAAE,GAC3D;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,EAAE,EAAE,MAAM,GAAG,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC1C;IAAE,EAAE,EAAE,KAAK,GAAG,IAAI,CAAC;IAAC,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,GACzD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAA;AAEnC,QAAA,IAAI,iBAAiB,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AAiChD;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,CAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
let _filterNodeSchema;
|
|
3
|
+
_filterNodeSchema = Schema.Union(Schema.Struct({
|
|
4
|
+
op: Schema.Literal("eq", "neq", "gt", "gte", "lt", "lte"),
|
|
5
|
+
field: Schema.String,
|
|
6
|
+
value: Schema.Unknown
|
|
7
|
+
}), Schema.Struct({
|
|
8
|
+
op: Schema.Literal("in"),
|
|
9
|
+
field: Schema.String,
|
|
10
|
+
values: Schema.Array(Schema.Unknown)
|
|
11
|
+
}), Schema.Struct({
|
|
12
|
+
op: Schema.Literal("like"),
|
|
13
|
+
field: Schema.String,
|
|
14
|
+
value: Schema.String
|
|
15
|
+
}), Schema.Struct({
|
|
16
|
+
op: Schema.Literal("null", "not_null"),
|
|
17
|
+
field: Schema.String
|
|
18
|
+
}), Schema.Struct({
|
|
19
|
+
op: Schema.Literal("and", "or"),
|
|
20
|
+
left: Schema.suspend(() => _filterNodeSchema),
|
|
21
|
+
right: Schema.suspend(() => _filterNodeSchema)
|
|
22
|
+
}), Schema.Struct({
|
|
23
|
+
op: Schema.Literal("not"),
|
|
24
|
+
node: Schema.suspend(() => _filterNodeSchema)
|
|
25
|
+
}));
|
|
26
|
+
/**
|
|
27
|
+
* Effect Schema for `FilterNode`. Used to decode filter query parameters
|
|
28
|
+
* from HTTP request JSON bodies.
|
|
29
|
+
*
|
|
30
|
+
* The schema is self-referential (recursive via `Schema.suspend`) to handle
|
|
31
|
+
* nested `and`/`or`/`not` nodes. This is why the underlying `let` variable
|
|
32
|
+
* exists — the recursive `suspend` needs a forward reference.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* import { Schema } from "effect"
|
|
36
|
+
*
|
|
37
|
+
* const raw = { op: "eq", field: "title", value: "hello" }
|
|
38
|
+
* const filter = Schema.decodeSync(FilterNodeSchema)(raw)
|
|
39
|
+
* // filter: FilterNode — { op: "eq", field: "title", value: "hello" }
|
|
40
|
+
*
|
|
41
|
+
* @see FilterNode — the TypeScript type this schema decodes to
|
|
42
|
+
*/
|
|
43
|
+
export { _filterNodeSchema as FilterNodeSchema };
|
|
44
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/filter/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AA+D/B,IAAI,iBAA4C,CAAA;AAEhD,iBAAiB,GAAG,MAAM,CAAC,KAAK,CAC9B,MAAM,CAAC,MAAM,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,IA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"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,mBAAmB,uBAAuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { AnyCollectionDef, FieldsMap, InferFieldValue } from "../collection/types.js";
|
|
2
|
+
import type { ExprNode, OperandNode } from "./types.js";
|
|
3
|
+
type FieldOperandMethods<K extends string, V = unknown> = {
|
|
4
|
+
eq(right: OperandNode<any, V>): ExprNode<K>;
|
|
5
|
+
neq(right: OperandNode<any, V>): ExprNode<K>;
|
|
6
|
+
gt(right: OperandNode<any, V>): ExprNode<K>;
|
|
7
|
+
gte(right: OperandNode<any, V>): ExprNode<K>;
|
|
8
|
+
lt(right: OperandNode<any, V>): ExprNode<K>;
|
|
9
|
+
lte(right: OperandNode<any, V>): ExprNode<K>;
|
|
10
|
+
in(right: OperandNode<any, V | ReadonlyArray<V>>): ExprNode<K>;
|
|
11
|
+
startsWith(right: OperandNode<any, V>): ExprNode<K>;
|
|
12
|
+
contains(right: OperandNode<any, V>): ExprNode<K>;
|
|
13
|
+
};
|
|
14
|
+
/** An operand node with fluent comparison methods attached. */
|
|
15
|
+
export type FieldOperand<K extends string, V = unknown> = OperandNode<K, V> & FieldOperandMethods<K, V>;
|
|
16
|
+
/** Base rule methods shared by Rule and RuleBuilder (excludes field and subquery). */
|
|
17
|
+
type RuleBase = {
|
|
18
|
+
request: (source: "header" | "query" | "body", key: string) => FieldOperand<string, string>;
|
|
19
|
+
auth: <C extends AnyCollectionDef, K extends keyof C["fields"] & string>(_collection: C, field: K) => FieldOperand<string, string>;
|
|
20
|
+
authId: <C extends AnyCollectionDef>(_collection: C) => FieldOperand<any, string>;
|
|
21
|
+
/** The collection name of the authenticated user. Use to restrict access to a specific auth collection. */
|
|
22
|
+
authCollection: () => FieldOperand<any, string>;
|
|
23
|
+
literal: <V>(value: V) => OperandNode<any, V>;
|
|
24
|
+
now: () => OperandNode<any, string>;
|
|
25
|
+
dateAdd: (operand: OperandNode<any>, amount: number, unit: "day" | "hour" | "minute") => OperandNode<any, string>;
|
|
26
|
+
dateDiff: (left: OperandNode<any>, right: OperandNode<any>, unit: "day" | "hour" | "minute") => OperandNode<any, number>;
|
|
27
|
+
and: <K extends string = string>(...exprs: Array<ExprNode<K>>) => ExprNode<K>;
|
|
28
|
+
or: <K extends string = string>(...exprs: Array<ExprNode<K>>) => ExprNode<K>;
|
|
29
|
+
not: <K extends string = string>(expr: ExprNode<K>) => ExprNode<K>;
|
|
30
|
+
public: <K extends string = string>() => ExprNode<K>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* A narrowed version of the Rule builder where `field()` is constrained to field
|
|
34
|
+
* names defined in `F` and comparison operands are typed to match each field's value type.
|
|
35
|
+
* Used as the argument type for the `rules` callback in collection definers.
|
|
36
|
+
*/
|
|
37
|
+
export type RuleBuilder<F extends FieldsMap> = RuleBase & {
|
|
38
|
+
field<K extends keyof F & string>(name: K): FieldOperand<K, InferFieldValue<F[K]>>;
|
|
39
|
+
subquery: <C extends AnyCollectionDef, K extends keyof C["fields"] & string>(_collection: C, field: K) => {
|
|
40
|
+
where: (expr: ExprNode<any> | ((R: RuleBuilder<C["fields"]>) => ExprNode<any>)) => OperandNode<any, string>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Rule builder API.
|
|
45
|
+
*
|
|
46
|
+
* Constructs access rule expressions using a fluent builder pattern.
|
|
47
|
+
* Rules are compiled to SQL WHERE clauses at runtime by the rule compiler.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // Simple field comparison
|
|
51
|
+
* Rule.field("ownerId").eq(Rule.authId(Users))
|
|
52
|
+
*
|
|
53
|
+
* // Boolean combination
|
|
54
|
+
* Rule.or(
|
|
55
|
+
* Rule.field("isPublic").eq(Rule.literal(true)),
|
|
56
|
+
* Rule.field("ownerId").eq(Rule.authId(Users))
|
|
57
|
+
* )
|
|
58
|
+
*
|
|
59
|
+
* // Date-based rule
|
|
60
|
+
* Rule.field("createdAt").gte(Rule.dateAdd(Rule.now(), -30, "day"))
|
|
61
|
+
*
|
|
62
|
+
* // Subquery
|
|
63
|
+
* const memberTeams = Rule.subquery(Members, "teamId").where(
|
|
64
|
+
* Rule.field("userId").eq(Rule.authId(Users))
|
|
65
|
+
* )
|
|
66
|
+
* Rule.field("teamId").in(memberTeams)
|
|
67
|
+
*
|
|
68
|
+
* // Subquery with typed callback
|
|
69
|
+
* Rule.field("id").in(
|
|
70
|
+
* Rule.subquery(Members, "teamId").where(
|
|
71
|
+
* (Q) => Q.field("userId").eq(Rule.authId(Users))
|
|
72
|
+
* )
|
|
73
|
+
* )
|
|
74
|
+
*
|
|
75
|
+
* // Always allow
|
|
76
|
+
* Rule.public()
|
|
77
|
+
*/
|
|
78
|
+
export declare const Rule: RuleBase & {
|
|
79
|
+
field: <const K extends string>(name: K) => FieldOperand<K, unknown>;
|
|
80
|
+
subquery: <C extends AnyCollectionDef, K extends keyof C["fields"] & string>(_collection: C, field: K) => {
|
|
81
|
+
where: (expr: ExprNode<any> | ((R: RuleBuilder<C["fields"]>) => ExprNode<any>)) => OperandNode<any, string>;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Creates a typed `RuleBuilder<F>` that constrains `field()` to names in `F`
|
|
86
|
+
* and types each comparison operand to the field's value type.
|
|
87
|
+
* Used internally by collection definers and subquery typed callbacks.
|
|
88
|
+
*/
|
|
89
|
+
export declare function makeRuleBuilder<F extends FieldsMap>(): RuleBuilder<F>;
|
|
90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