query-spec 0.1.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 +23 -0
- package/README.md +212 -0
- package/esm/index.js +110 -0
- package/index.d.ts +95 -0
- package/index.js +115 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# query-spec
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/constructive/blob/main/LICENSE">
|
|
12
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/query-spec">
|
|
15
|
+
<img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=packages%2Fquery-spec%2Fpackage.json"/>
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
The Constructive query language specification. One shared grammar for filters, ordering, and pagination — used by the generated GraphQL SDK/ORM clients, the PostGraphile connection filters, and the SQL query builder, so the same `where` JSON means the same thing at every layer of the stack.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install query-spec
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## The filter grammar
|
|
28
|
+
|
|
29
|
+
Sibling keys AND together; `or`/`not` introduce disjunction and negation, nestable arbitrarily:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import type { Filter } from 'query-spec';
|
|
33
|
+
|
|
34
|
+
const where: Filter = {
|
|
35
|
+
status: { in: ['queued', 'retry'] },
|
|
36
|
+
attempts: { lessThan: 5 },
|
|
37
|
+
completed_at: { isNull: true },
|
|
38
|
+
or: [
|
|
39
|
+
{ priority: { greaterThan: 0 } },
|
|
40
|
+
{ escalated: { equalTo: true } },
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The same shape works in a generated SDK client (`api.jobs.findMany({ where })`) and in the SQL query builder (`new QueryBuilder().table('jobs').where(where)`).
|
|
46
|
+
|
|
47
|
+
### Operand generics
|
|
48
|
+
|
|
49
|
+
`Filter`, `FieldFilter`, and `RelationalFilter` are generic over the operand type, so each layer constrains what a field may be compared against:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import type { Filter } from 'query-spec';
|
|
53
|
+
|
|
54
|
+
type JsonFilter = Filter<string | number | boolean | null>; // SDK layer
|
|
55
|
+
type SqlFilter = Filter<SqlValue | Expr | QueryBuilder>; // SQL layer
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Operators
|
|
59
|
+
|
|
60
|
+
| Group | Operators |
|
|
61
|
+
|-------|-----------|
|
|
62
|
+
| Null check | `isNull` |
|
|
63
|
+
| Comparison | `equalTo`, `notEqualTo`, `lessThan`, `lessThanOrEqualTo`, `greaterThan`, `greaterThanOrEqualTo` |
|
|
64
|
+
| Distinct | `distinctFrom`, `notDistinctFrom` |
|
|
65
|
+
| List membership | `in`, `notIn` |
|
|
66
|
+
| LIKE | `like`, `notLike`, `likeInsensitive`, `notLikeInsensitive` |
|
|
67
|
+
| Pattern sugar | `includes`, `startsWith`, `endsWith` (+ `not*` / `*Insensitive` variants) |
|
|
68
|
+
| Case-insensitive | `equalToInsensitive`, `notEqualToInsensitive`, `distinctFromInsensitive`, `notDistinctFromInsensitive`, `inInsensitive`, `notInInsensitive` |
|
|
69
|
+
| Array | `contains`, `containedBy`, `overlaps` |
|
|
70
|
+
| PostGIS | `intersects`, `within`, `touches`, `covers`, `bboxIntersects2D`, ... |
|
|
71
|
+
|
|
72
|
+
Each group is exported both as a `const` array (for runtime validation) and a string-literal union type:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import {
|
|
76
|
+
FILTER_OPERATORS, // readonly string[] of every operator
|
|
77
|
+
COMPARISON_FILTER_OPERATORS,
|
|
78
|
+
isFilterOperator, // (op: string) => op is FilterOperator
|
|
79
|
+
isLogicalOperator, // 'and' | 'or' | 'not'
|
|
80
|
+
} from 'query-spec';
|
|
81
|
+
|
|
82
|
+
isFilterOperator('equalTo'); // true
|
|
83
|
+
isFilterOperator('bogus'); // false
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Relational filters
|
|
87
|
+
|
|
88
|
+
GraphQL/ORM layers filter on related records with `every` / `some` / `none`:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
const where: Filter = {
|
|
92
|
+
tags: { some: { name: { startsWith: 'infra' } } },
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
(The SQL layer expresses the same thing with joins or `{ in: subquery }`.)
|
|
97
|
+
|
|
98
|
+
## Ordering and pagination
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import type { OrderByItem, PageInfo, ConnectionResult } from 'query-spec';
|
|
102
|
+
|
|
103
|
+
const orderBy: OrderByItem[] = [{ field: 'name', direction: 'asc' }];
|
|
104
|
+
|
|
105
|
+
const page: ConnectionResult<{ id: string }> = {
|
|
106
|
+
nodes: [{ id: '1' }],
|
|
107
|
+
totalCount: 1,
|
|
108
|
+
pageInfo: { hasNextPage: false, hasPreviousPage: false },
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## API Reference
|
|
113
|
+
|
|
114
|
+
### Types
|
|
115
|
+
|
|
116
|
+
| Type | Description |
|
|
117
|
+
|------|-------------|
|
|
118
|
+
| `Filter<TOperand>` | The main nestable filter shape (`and`/`or`/`not` + field filters) |
|
|
119
|
+
| `FieldFilter<TOperand>` | Operator → operand map for a single field |
|
|
120
|
+
| `RelationalFilter<TOperand>` | `every`/`some`/`none` filters on related records |
|
|
121
|
+
| `FilterOperator` | Union of every supported operator |
|
|
122
|
+
| `LogicalOperator` | `'and' \| 'or' \| 'not'` |
|
|
123
|
+
| `OrderByItem`, `OrderDirection` | Ordering specification |
|
|
124
|
+
| `PageInfo`, `ConnectionResult<T>` | Relay-style pagination shapes |
|
|
125
|
+
|
|
126
|
+
### Runtime
|
|
127
|
+
|
|
128
|
+
| Export | Description |
|
|
129
|
+
|--------|-------------|
|
|
130
|
+
| `FILTER_OPERATORS` | All operators as a readonly array |
|
|
131
|
+
| `NULL_FILTER_OPERATORS`, `COMPARISON_FILTER_OPERATORS`, `DISTINCT_FILTER_OPERATORS`, `LIST_FILTER_OPERATORS`, `LIKE_FILTER_OPERATORS`, `PATTERN_FILTER_OPERATORS`, `INSENSITIVE_FILTER_OPERATORS`, `ARRAY_FILTER_OPERATORS`, `POSTGIS_FILTER_OPERATORS` | Operator groups |
|
|
132
|
+
| `LOGICAL_OPERATORS` | `['and', 'or', 'not']` |
|
|
133
|
+
| `isFilterOperator(op)` | Type-guard for `FilterOperator` |
|
|
134
|
+
| `isLogicalOperator(op)` | Type-guard for `LogicalOperator` |
|
|
135
|
+
|
|
136
|
+
## Related
|
|
137
|
+
|
|
138
|
+
* [`@constructive-io/query-builder`](https://www.npmjs.com/package/@constructive-io/query-builder) — AST-backed SQL builder consuming this grammar for `WHERE`/`HAVING`
|
|
139
|
+
* [`@constructive-io/graphql-codegen`](https://www.npmjs.com/package/@constructive-io/graphql-codegen) — generates SDK/ORM clients whose `where` arguments conform to this grammar
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Education and Tutorials
|
|
144
|
+
|
|
145
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
146
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
147
|
+
|
|
148
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
149
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
150
|
+
|
|
151
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
152
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
153
|
+
|
|
154
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
155
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
156
|
+
|
|
157
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
158
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
159
|
+
|
|
160
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
161
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
162
|
+
|
|
163
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
164
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
165
|
+
|
|
166
|
+
## Related Constructive Tooling
|
|
167
|
+
|
|
168
|
+
### 📦 Package Management
|
|
169
|
+
|
|
170
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
171
|
+
|
|
172
|
+
### 🧪 Testing
|
|
173
|
+
|
|
174
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
175
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
176
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
177
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
178
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
179
|
+
|
|
180
|
+
### 🧠 Parsing & AST
|
|
181
|
+
|
|
182
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
183
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
184
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
185
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
186
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
187
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
188
|
+
|
|
189
|
+
### 📚 Documentation & Skills
|
|
190
|
+
|
|
191
|
+
* [constructive-skills](https://github.com/constructive-io/constructive-skills): **📖 Platform documentation and AI agent skills** — feature catalog, blueprint reference, SDK guides (i18n, billing, limits, events, uploads, security, entities, search, AI), and deployment guides.
|
|
192
|
+
|
|
193
|
+
Install skills for AI coding agents:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
# All platform skills (security, blueprints, codegen, billing, etc.)
|
|
197
|
+
npx skills add constructive-io/constructive-skills
|
|
198
|
+
|
|
199
|
+
# Individual repo skills (pgpm, testing, CLI, search, etc.)
|
|
200
|
+
npx skills add https://github.com/constructive-io/constructive --skill pgpm
|
|
201
|
+
npx skills add https://github.com/constructive-io/constructive --skill constructive-testing
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Credits
|
|
205
|
+
|
|
206
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
207
|
+
|
|
208
|
+
## Disclaimer
|
|
209
|
+
|
|
210
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
211
|
+
|
|
212
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Constructive query language specification.
|
|
3
|
+
*
|
|
4
|
+
* Defines the shared filter, ordering, and pagination grammar used across the
|
|
5
|
+
* stack: the generated GraphQL SDK/ORM clients, the PostGraphile connection
|
|
6
|
+
* filters, and the SQL query builder all conform to these shapes.
|
|
7
|
+
*
|
|
8
|
+
* The `Filter` grammar is generic over the operand type so each layer can
|
|
9
|
+
* constrain what a field may be compared against (plain JSON values in the
|
|
10
|
+
* SDK, parameterized values / expressions / subqueries in SQL).
|
|
11
|
+
*/
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Filter operators
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
/** Null check */
|
|
16
|
+
export const NULL_FILTER_OPERATORS = ['isNull'];
|
|
17
|
+
/** Binary comparisons */
|
|
18
|
+
export const COMPARISON_FILTER_OPERATORS = [
|
|
19
|
+
'equalTo',
|
|
20
|
+
'notEqualTo',
|
|
21
|
+
'lessThan',
|
|
22
|
+
'lessThanOrEqualTo',
|
|
23
|
+
'greaterThan',
|
|
24
|
+
'greaterThanOrEqualTo'
|
|
25
|
+
];
|
|
26
|
+
/** IS DISTINCT FROM / IS NOT DISTINCT FROM */
|
|
27
|
+
export const DISTINCT_FILTER_OPERATORS = ['distinctFrom', 'notDistinctFrom'];
|
|
28
|
+
/** List membership */
|
|
29
|
+
export const LIST_FILTER_OPERATORS = ['in', 'notIn'];
|
|
30
|
+
/** LIKE / ILIKE with caller-supplied patterns */
|
|
31
|
+
export const LIKE_FILTER_OPERATORS = [
|
|
32
|
+
'like',
|
|
33
|
+
'notLike',
|
|
34
|
+
'likeInsensitive',
|
|
35
|
+
'notLikeInsensitive'
|
|
36
|
+
];
|
|
37
|
+
/** Pattern sugar — the layer builds the `%` pattern from a plain string */
|
|
38
|
+
export const PATTERN_FILTER_OPERATORS = [
|
|
39
|
+
'includes',
|
|
40
|
+
'notIncludes',
|
|
41
|
+
'includesInsensitive',
|
|
42
|
+
'notIncludesInsensitive',
|
|
43
|
+
'startsWith',
|
|
44
|
+
'notStartsWith',
|
|
45
|
+
'startsWithInsensitive',
|
|
46
|
+
'notStartsWithInsensitive',
|
|
47
|
+
'endsWith',
|
|
48
|
+
'notEndsWith',
|
|
49
|
+
'endsWithInsensitive',
|
|
50
|
+
'notEndsWithInsensitive'
|
|
51
|
+
];
|
|
52
|
+
/** Case-insensitive equality/membership variants */
|
|
53
|
+
export const INSENSITIVE_FILTER_OPERATORS = [
|
|
54
|
+
'equalToInsensitive',
|
|
55
|
+
'notEqualToInsensitive',
|
|
56
|
+
'distinctFromInsensitive',
|
|
57
|
+
'notDistinctFromInsensitive',
|
|
58
|
+
'inInsensitive',
|
|
59
|
+
'notInInsensitive'
|
|
60
|
+
];
|
|
61
|
+
/** Array containment/overlap */
|
|
62
|
+
export const ARRAY_FILTER_OPERATORS = ['contains', 'containedBy', 'overlaps'];
|
|
63
|
+
/** PostGIS spatial operators */
|
|
64
|
+
export const POSTGIS_FILTER_OPERATORS = [
|
|
65
|
+
'intersects',
|
|
66
|
+
'intersects3D',
|
|
67
|
+
'containsProperly',
|
|
68
|
+
'coveredBy',
|
|
69
|
+
'covers',
|
|
70
|
+
'crosses',
|
|
71
|
+
'disjoint',
|
|
72
|
+
'orderingEquals',
|
|
73
|
+
'touches',
|
|
74
|
+
'within',
|
|
75
|
+
'bboxIntersects2D',
|
|
76
|
+
'bboxIntersects3D',
|
|
77
|
+
'bboxOverlapsOrLeftOf',
|
|
78
|
+
'bboxOverlapsOrRightOf',
|
|
79
|
+
'bboxOverlapsOrBelow',
|
|
80
|
+
'bboxOverlapsOrAbove',
|
|
81
|
+
'bboxLeftOf',
|
|
82
|
+
'bboxRightOf',
|
|
83
|
+
'bboxBelow',
|
|
84
|
+
'bboxAbove',
|
|
85
|
+
'bboxContains',
|
|
86
|
+
'bboxEquals'
|
|
87
|
+
];
|
|
88
|
+
/** All supported filter operators */
|
|
89
|
+
export const FILTER_OPERATORS = [
|
|
90
|
+
...NULL_FILTER_OPERATORS,
|
|
91
|
+
...COMPARISON_FILTER_OPERATORS,
|
|
92
|
+
...DISTINCT_FILTER_OPERATORS,
|
|
93
|
+
...LIST_FILTER_OPERATORS,
|
|
94
|
+
...LIKE_FILTER_OPERATORS,
|
|
95
|
+
...PATTERN_FILTER_OPERATORS,
|
|
96
|
+
...INSENSITIVE_FILTER_OPERATORS,
|
|
97
|
+
...ARRAY_FILTER_OPERATORS,
|
|
98
|
+
...POSTGIS_FILTER_OPERATORS
|
|
99
|
+
];
|
|
100
|
+
export function isFilterOperator(op) {
|
|
101
|
+
return FILTER_OPERATORS.includes(op);
|
|
102
|
+
}
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Filter grammar
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
/** Boolean combinators */
|
|
107
|
+
export const LOGICAL_OPERATORS = ['and', 'or', 'not'];
|
|
108
|
+
export function isLogicalOperator(op) {
|
|
109
|
+
return LOGICAL_OPERATORS.includes(op);
|
|
110
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Constructive query language specification.
|
|
3
|
+
*
|
|
4
|
+
* Defines the shared filter, ordering, and pagination grammar used across the
|
|
5
|
+
* stack: the generated GraphQL SDK/ORM clients, the PostGraphile connection
|
|
6
|
+
* filters, and the SQL query builder all conform to these shapes.
|
|
7
|
+
*
|
|
8
|
+
* The `Filter` grammar is generic over the operand type so each layer can
|
|
9
|
+
* constrain what a field may be compared against (plain JSON values in the
|
|
10
|
+
* SDK, parameterized values / expressions / subqueries in SQL).
|
|
11
|
+
*/
|
|
12
|
+
/** Null check */
|
|
13
|
+
export declare const NULL_FILTER_OPERATORS: readonly ["isNull"];
|
|
14
|
+
export type NullFilterOperator = (typeof NULL_FILTER_OPERATORS)[number];
|
|
15
|
+
/** Binary comparisons */
|
|
16
|
+
export declare const COMPARISON_FILTER_OPERATORS: readonly ["equalTo", "notEqualTo", "lessThan", "lessThanOrEqualTo", "greaterThan", "greaterThanOrEqualTo"];
|
|
17
|
+
export type ComparisonFilterOperator = (typeof COMPARISON_FILTER_OPERATORS)[number];
|
|
18
|
+
/** IS DISTINCT FROM / IS NOT DISTINCT FROM */
|
|
19
|
+
export declare const DISTINCT_FILTER_OPERATORS: readonly ["distinctFrom", "notDistinctFrom"];
|
|
20
|
+
export type DistinctFilterOperator = (typeof DISTINCT_FILTER_OPERATORS)[number];
|
|
21
|
+
/** List membership */
|
|
22
|
+
export declare const LIST_FILTER_OPERATORS: readonly ["in", "notIn"];
|
|
23
|
+
export type ListFilterOperator = (typeof LIST_FILTER_OPERATORS)[number];
|
|
24
|
+
/** LIKE / ILIKE with caller-supplied patterns */
|
|
25
|
+
export declare const LIKE_FILTER_OPERATORS: readonly ["like", "notLike", "likeInsensitive", "notLikeInsensitive"];
|
|
26
|
+
export type LikeFilterOperator = (typeof LIKE_FILTER_OPERATORS)[number];
|
|
27
|
+
/** Pattern sugar — the layer builds the `%` pattern from a plain string */
|
|
28
|
+
export declare const PATTERN_FILTER_OPERATORS: readonly ["includes", "notIncludes", "includesInsensitive", "notIncludesInsensitive", "startsWith", "notStartsWith", "startsWithInsensitive", "notStartsWithInsensitive", "endsWith", "notEndsWith", "endsWithInsensitive", "notEndsWithInsensitive"];
|
|
29
|
+
export type PatternFilterOperator = (typeof PATTERN_FILTER_OPERATORS)[number];
|
|
30
|
+
/** Case-insensitive equality/membership variants */
|
|
31
|
+
export declare const INSENSITIVE_FILTER_OPERATORS: readonly ["equalToInsensitive", "notEqualToInsensitive", "distinctFromInsensitive", "notDistinctFromInsensitive", "inInsensitive", "notInInsensitive"];
|
|
32
|
+
export type InsensitiveFilterOperator = (typeof INSENSITIVE_FILTER_OPERATORS)[number];
|
|
33
|
+
/** Array containment/overlap */
|
|
34
|
+
export declare const ARRAY_FILTER_OPERATORS: readonly ["contains", "containedBy", "overlaps"];
|
|
35
|
+
export type ArrayFilterOperator = (typeof ARRAY_FILTER_OPERATORS)[number];
|
|
36
|
+
/** PostGIS spatial operators */
|
|
37
|
+
export declare const POSTGIS_FILTER_OPERATORS: readonly ["intersects", "intersects3D", "containsProperly", "coveredBy", "covers", "crosses", "disjoint", "orderingEquals", "touches", "within", "bboxIntersects2D", "bboxIntersects3D", "bboxOverlapsOrLeftOf", "bboxOverlapsOrRightOf", "bboxOverlapsOrBelow", "bboxOverlapsOrAbove", "bboxLeftOf", "bboxRightOf", "bboxBelow", "bboxAbove", "bboxContains", "bboxEquals"];
|
|
38
|
+
export type PostgisFilterOperator = (typeof POSTGIS_FILTER_OPERATORS)[number];
|
|
39
|
+
/** All supported filter operators */
|
|
40
|
+
export declare const FILTER_OPERATORS: readonly ["isNull", "equalTo", "notEqualTo", "lessThan", "lessThanOrEqualTo", "greaterThan", "greaterThanOrEqualTo", "distinctFrom", "notDistinctFrom", "in", "notIn", "like", "notLike", "likeInsensitive", "notLikeInsensitive", "includes", "notIncludes", "includesInsensitive", "notIncludesInsensitive", "startsWith", "notStartsWith", "startsWithInsensitive", "notStartsWithInsensitive", "endsWith", "notEndsWith", "endsWithInsensitive", "notEndsWithInsensitive", "equalToInsensitive", "notEqualToInsensitive", "distinctFromInsensitive", "notDistinctFromInsensitive", "inInsensitive", "notInInsensitive", "contains", "containedBy", "overlaps", "intersects", "intersects3D", "containsProperly", "coveredBy", "covers", "crosses", "disjoint", "orderingEquals", "touches", "within", "bboxIntersects2D", "bboxIntersects3D", "bboxOverlapsOrLeftOf", "bboxOverlapsOrRightOf", "bboxOverlapsOrBelow", "bboxOverlapsOrAbove", "bboxLeftOf", "bboxRightOf", "bboxBelow", "bboxAbove", "bboxContains", "bboxEquals"];
|
|
41
|
+
export type FilterOperator = (typeof FILTER_OPERATORS)[number];
|
|
42
|
+
export declare function isFilterOperator(op: string): op is FilterOperator;
|
|
43
|
+
/** Boolean combinators */
|
|
44
|
+
export declare const LOGICAL_OPERATORS: readonly ["and", "or", "not"];
|
|
45
|
+
export type LogicalOperator = (typeof LOGICAL_OPERATORS)[number];
|
|
46
|
+
export declare function isLogicalOperator(op: string): op is LogicalOperator;
|
|
47
|
+
/**
|
|
48
|
+
* Filter on a single field.
|
|
49
|
+
*
|
|
50
|
+
* Generic over the operand type: the SDK uses plain JSON values, the SQL
|
|
51
|
+
* layer additionally allows expressions and subqueries.
|
|
52
|
+
*/
|
|
53
|
+
export type FieldFilter<TOperand = unknown> = {
|
|
54
|
+
[K in FilterOperator]?: TOperand;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Filter on related records (GraphQL/ORM layer semantics).
|
|
58
|
+
*/
|
|
59
|
+
export interface RelationalFilter<TOperand = unknown> {
|
|
60
|
+
/** All related records must match */
|
|
61
|
+
every?: Filter<TOperand>;
|
|
62
|
+
/** At least one related record must match */
|
|
63
|
+
some?: Filter<TOperand>;
|
|
64
|
+
/** No related records match */
|
|
65
|
+
none?: Filter<TOperand>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The main filter type. Sibling keys combine with AND; `or`/`not`
|
|
69
|
+
* introduce disjunction and negation. Nestable arbitrarily.
|
|
70
|
+
*/
|
|
71
|
+
export interface Filter<TOperand = unknown> {
|
|
72
|
+
[field: string]: FieldFilter<TOperand> | RelationalFilter<TOperand> | Filter<TOperand> | Filter<TOperand>[] | undefined;
|
|
73
|
+
and?: Filter<TOperand>[];
|
|
74
|
+
or?: Filter<TOperand>[];
|
|
75
|
+
not?: Filter<TOperand>;
|
|
76
|
+
}
|
|
77
|
+
export type OrderDirection = 'asc' | 'desc';
|
|
78
|
+
/** Single order by specification */
|
|
79
|
+
export interface OrderByItem {
|
|
80
|
+
field: string;
|
|
81
|
+
direction: OrderDirection;
|
|
82
|
+
}
|
|
83
|
+
/** Relay-style pagination info */
|
|
84
|
+
export interface PageInfo {
|
|
85
|
+
hasNextPage: boolean;
|
|
86
|
+
hasPreviousPage: boolean;
|
|
87
|
+
startCursor?: string | null;
|
|
88
|
+
endCursor?: string | null;
|
|
89
|
+
}
|
|
90
|
+
/** Relay-style connection result */
|
|
91
|
+
export interface ConnectionResult<T = unknown> {
|
|
92
|
+
nodes: T[];
|
|
93
|
+
totalCount: number;
|
|
94
|
+
pageInfo: PageInfo;
|
|
95
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The Constructive query language specification.
|
|
4
|
+
*
|
|
5
|
+
* Defines the shared filter, ordering, and pagination grammar used across the
|
|
6
|
+
* stack: the generated GraphQL SDK/ORM clients, the PostGraphile connection
|
|
7
|
+
* filters, and the SQL query builder all conform to these shapes.
|
|
8
|
+
*
|
|
9
|
+
* The `Filter` grammar is generic over the operand type so each layer can
|
|
10
|
+
* constrain what a field may be compared against (plain JSON values in the
|
|
11
|
+
* SDK, parameterized values / expressions / subqueries in SQL).
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.LOGICAL_OPERATORS = exports.FILTER_OPERATORS = exports.POSTGIS_FILTER_OPERATORS = exports.ARRAY_FILTER_OPERATORS = exports.INSENSITIVE_FILTER_OPERATORS = exports.PATTERN_FILTER_OPERATORS = exports.LIKE_FILTER_OPERATORS = exports.LIST_FILTER_OPERATORS = exports.DISTINCT_FILTER_OPERATORS = exports.COMPARISON_FILTER_OPERATORS = exports.NULL_FILTER_OPERATORS = void 0;
|
|
15
|
+
exports.isFilterOperator = isFilterOperator;
|
|
16
|
+
exports.isLogicalOperator = isLogicalOperator;
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Filter operators
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
/** Null check */
|
|
21
|
+
exports.NULL_FILTER_OPERATORS = ['isNull'];
|
|
22
|
+
/** Binary comparisons */
|
|
23
|
+
exports.COMPARISON_FILTER_OPERATORS = [
|
|
24
|
+
'equalTo',
|
|
25
|
+
'notEqualTo',
|
|
26
|
+
'lessThan',
|
|
27
|
+
'lessThanOrEqualTo',
|
|
28
|
+
'greaterThan',
|
|
29
|
+
'greaterThanOrEqualTo'
|
|
30
|
+
];
|
|
31
|
+
/** IS DISTINCT FROM / IS NOT DISTINCT FROM */
|
|
32
|
+
exports.DISTINCT_FILTER_OPERATORS = ['distinctFrom', 'notDistinctFrom'];
|
|
33
|
+
/** List membership */
|
|
34
|
+
exports.LIST_FILTER_OPERATORS = ['in', 'notIn'];
|
|
35
|
+
/** LIKE / ILIKE with caller-supplied patterns */
|
|
36
|
+
exports.LIKE_FILTER_OPERATORS = [
|
|
37
|
+
'like',
|
|
38
|
+
'notLike',
|
|
39
|
+
'likeInsensitive',
|
|
40
|
+
'notLikeInsensitive'
|
|
41
|
+
];
|
|
42
|
+
/** Pattern sugar — the layer builds the `%` pattern from a plain string */
|
|
43
|
+
exports.PATTERN_FILTER_OPERATORS = [
|
|
44
|
+
'includes',
|
|
45
|
+
'notIncludes',
|
|
46
|
+
'includesInsensitive',
|
|
47
|
+
'notIncludesInsensitive',
|
|
48
|
+
'startsWith',
|
|
49
|
+
'notStartsWith',
|
|
50
|
+
'startsWithInsensitive',
|
|
51
|
+
'notStartsWithInsensitive',
|
|
52
|
+
'endsWith',
|
|
53
|
+
'notEndsWith',
|
|
54
|
+
'endsWithInsensitive',
|
|
55
|
+
'notEndsWithInsensitive'
|
|
56
|
+
];
|
|
57
|
+
/** Case-insensitive equality/membership variants */
|
|
58
|
+
exports.INSENSITIVE_FILTER_OPERATORS = [
|
|
59
|
+
'equalToInsensitive',
|
|
60
|
+
'notEqualToInsensitive',
|
|
61
|
+
'distinctFromInsensitive',
|
|
62
|
+
'notDistinctFromInsensitive',
|
|
63
|
+
'inInsensitive',
|
|
64
|
+
'notInInsensitive'
|
|
65
|
+
];
|
|
66
|
+
/** Array containment/overlap */
|
|
67
|
+
exports.ARRAY_FILTER_OPERATORS = ['contains', 'containedBy', 'overlaps'];
|
|
68
|
+
/** PostGIS spatial operators */
|
|
69
|
+
exports.POSTGIS_FILTER_OPERATORS = [
|
|
70
|
+
'intersects',
|
|
71
|
+
'intersects3D',
|
|
72
|
+
'containsProperly',
|
|
73
|
+
'coveredBy',
|
|
74
|
+
'covers',
|
|
75
|
+
'crosses',
|
|
76
|
+
'disjoint',
|
|
77
|
+
'orderingEquals',
|
|
78
|
+
'touches',
|
|
79
|
+
'within',
|
|
80
|
+
'bboxIntersects2D',
|
|
81
|
+
'bboxIntersects3D',
|
|
82
|
+
'bboxOverlapsOrLeftOf',
|
|
83
|
+
'bboxOverlapsOrRightOf',
|
|
84
|
+
'bboxOverlapsOrBelow',
|
|
85
|
+
'bboxOverlapsOrAbove',
|
|
86
|
+
'bboxLeftOf',
|
|
87
|
+
'bboxRightOf',
|
|
88
|
+
'bboxBelow',
|
|
89
|
+
'bboxAbove',
|
|
90
|
+
'bboxContains',
|
|
91
|
+
'bboxEquals'
|
|
92
|
+
];
|
|
93
|
+
/** All supported filter operators */
|
|
94
|
+
exports.FILTER_OPERATORS = [
|
|
95
|
+
...exports.NULL_FILTER_OPERATORS,
|
|
96
|
+
...exports.COMPARISON_FILTER_OPERATORS,
|
|
97
|
+
...exports.DISTINCT_FILTER_OPERATORS,
|
|
98
|
+
...exports.LIST_FILTER_OPERATORS,
|
|
99
|
+
...exports.LIKE_FILTER_OPERATORS,
|
|
100
|
+
...exports.PATTERN_FILTER_OPERATORS,
|
|
101
|
+
...exports.INSENSITIVE_FILTER_OPERATORS,
|
|
102
|
+
...exports.ARRAY_FILTER_OPERATORS,
|
|
103
|
+
...exports.POSTGIS_FILTER_OPERATORS
|
|
104
|
+
];
|
|
105
|
+
function isFilterOperator(op) {
|
|
106
|
+
return exports.FILTER_OPERATORS.includes(op);
|
|
107
|
+
}
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// Filter grammar
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
/** Boolean combinators */
|
|
112
|
+
exports.LOGICAL_OPERATORS = ['and', 'or', 'not'];
|
|
113
|
+
function isLogicalOperator(op) {
|
|
114
|
+
return exports.LOGICAL_OPERATORS.includes(op);
|
|
115
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "query-spec",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "The Constructive query language specification — shared filter, ordering, and pagination types",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"query",
|
|
33
|
+
"filter",
|
|
34
|
+
"spec",
|
|
35
|
+
"sql",
|
|
36
|
+
"graphql",
|
|
37
|
+
"constructive"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"makage": "^0.3.0"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "0e4acd5d0db04587fb0112b047be6f2c8f2493d9"
|
|
43
|
+
}
|