datatables-backend 0.1.0rc2__tar.gz
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.
- datatables_backend-0.1.0rc2/.gitignore +14 -0
- datatables_backend-0.1.0rc2/LICENSE +21 -0
- datatables_backend-0.1.0rc2/PKG-INFO +214 -0
- datatables_backend-0.1.0rc2/README.md +173 -0
- datatables_backend-0.1.0rc2/pyproject.toml +134 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/__init__.py +44 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/_plan.py +1077 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/cache.py +49 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/columns.py +53 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/fastapi.py +77 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/opensearch.py +623 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/protocol.py +567 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/py.typed +0 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/rows.py +32 -0
- datatables_backend-0.1.0rc2/src/datatables_backend/sqlalchemy.py +657 -0
- datatables_backend-0.1.0rc2/tests/__init__.py +0 -0
- datatables_backend-0.1.0rc2/tests/conftest.py +265 -0
- datatables_backend-0.1.0rc2/tests/test_fastapi.py +241 -0
- datatables_backend-0.1.0rc2/tests/test_opensearch.py +1650 -0
- datatables_backend-0.1.0rc2/tests/test_opensearch_client.py +107 -0
- datatables_backend-0.1.0rc2/tests/test_opensearch_live.py +791 -0
- datatables_backend-0.1.0rc2/tests/test_protocol.py +689 -0
- datatables_backend-0.1.0rc2/tests/test_readme.py +125 -0
- datatables_backend-0.1.0rc2/tests/test_sql.py +2793 -0
- datatables_backend-0.1.0rc2/tests/test_sql_search.py +396 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 R. Hannes Niedner
|
|
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.
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: datatables-backend
|
|
3
|
+
Version: 0.1.0rc2
|
|
4
|
+
Summary: Server-side processing for DataTables from SQL (SQLAlchemy 2, async and sync) and OpenSearch.
|
|
5
|
+
Project-URL: Homepage, https://github.com/hniedner/datatables-backend
|
|
6
|
+
Project-URL: Documentation, https://github.com/hniedner/datatables-backend#readme
|
|
7
|
+
Project-URL: Issues, https://github.com/hniedner/datatables-backend/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/hniedner/datatables-backend/blob/main/CHANGELOG.md
|
|
9
|
+
Author: R. Hannes Niedner
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Classifier: Topic :: Database :: Front-Ends
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.12
|
|
26
|
+
Requires-Dist: pydantic>=2.10
|
|
27
|
+
Requires-Dist: pydantic>=2.12; python_version >= '3.14'
|
|
28
|
+
Requires-Dist: pydantic>=2.14; python_version >= '3.15'
|
|
29
|
+
Provides-Extra: fastapi
|
|
30
|
+
Requires-Dist: fastapi>=0.133; extra == 'fastapi'
|
|
31
|
+
Requires-Dist: python-multipart>=0.0.9; extra == 'fastapi'
|
|
32
|
+
Provides-Extra: opensearch
|
|
33
|
+
Requires-Dist: opensearch-py>=2.6; extra == 'opensearch'
|
|
34
|
+
Provides-Extra: opensearch-async
|
|
35
|
+
Requires-Dist: opensearch-py[async]>=2.6; extra == 'opensearch-async'
|
|
36
|
+
Provides-Extra: sqlalchemy
|
|
37
|
+
Requires-Dist: sqlalchemy>=2.0.31; extra == 'sqlalchemy'
|
|
38
|
+
Provides-Extra: sqlalchemy-async
|
|
39
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0.31; extra == 'sqlalchemy-async'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# datatables-backend
|
|
43
|
+
|
|
44
|
+
[](https://github.com/hniedner/datatables-backend/actions/workflows/ci.yml)
|
|
45
|
+
[](https://github.com/hniedner/datatables-backend/security/code-scanning)
|
|
46
|
+
[](https://scorecard.dev/viewer/?uri=github.com/hniedner/datatables-backend)
|
|
47
|
+
[](https://codecov.io/gh/hniedner/datatables-backend)
|
|
48
|
+
[](https://github.com/hniedner/datatables-backend/blob/main/pyproject.toml)
|
|
49
|
+
[](https://github.com/hniedner/datatables-backend/blob/main/LICENSE)
|
|
50
|
+
|
|
51
|
+
Server-side processing for [DataTables](https://datatables.net): paging, sorting, the global and column searches,
|
|
52
|
+
and [SearchBuilder](https://datatables.net/extensions/searchbuilder/) criteria. Data can come from:
|
|
53
|
+
|
|
54
|
+
- **SQL**: SQLAlchemy 2, async or sync.
|
|
55
|
+
- **OpenSearch**: async or sync client.
|
|
56
|
+
|
|
57
|
+
The library is read-only and framework-agnostic.
|
|
58
|
+
|
|
59
|
+
> Status: release candidate (0.1.0rc2). Until 1.0, a minor release (0.x) may break the Python API; each such change is
|
|
60
|
+
> listed in [CHANGELOG.md][changelog]. The wire format and the error codes change only in a release whose notes say so.
|
|
61
|
+
> See [docs/PRD.md][prd] for the scope and design.
|
|
62
|
+
|
|
63
|
+
If you need a SQL backend with editing (DataTables Editor), use SpryMedia's own
|
|
64
|
+
[`datatables_server`](https://datatables.net/manual/python). This library adds:
|
|
65
|
+
|
|
66
|
+
- async SQLAlchemy;
|
|
67
|
+
- a search-engine backend;
|
|
68
|
+
- the same server-declared columns and safety rules on every source.
|
|
69
|
+
|
|
70
|
+
datatables-backend is not affiliated with or endorsed by DataTables or SpryMedia.
|
|
71
|
+
|
|
72
|
+
## Install
|
|
73
|
+
|
|
74
|
+
pip installs no pre-release unless asked, so while 0.1.0 is a release candidate add `--pre` to each command.
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
pip install "datatables-backend[sqlalchemy]" # SQL, sync
|
|
78
|
+
pip install "datatables-backend[sqlalchemy-async]" # SQL, async (adds greenlet): also your async driver, e.g. asyncpg
|
|
79
|
+
pip install "datatables-backend[opensearch]" # OpenSearch, sync (installs opensearch-py)
|
|
80
|
+
pip install "datatables-backend[opensearch-async]" # OpenSearch, async (adds aiohttp)
|
|
81
|
+
pip install "datatables-backend[fastapi]" # the FastAPI dependency
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## SQL
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from datatables_backend import Column, parse_request
|
|
88
|
+
from datatables_backend.sqlalchemy import SQLTable
|
|
89
|
+
|
|
90
|
+
users = SQLTable(
|
|
91
|
+
users_table, # a Table, join or subquery
|
|
92
|
+
[Column('id'), Column('email', field='email_address'), Column('joined')],
|
|
93
|
+
base_filter=users_table.c.active.is_(True), # rows the table shows at all
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
params = parse_request(request_json) # or the flat form/query parameters
|
|
97
|
+
async with AsyncSession(engine) as session:
|
|
98
|
+
response = await users.fetch(params, session) # fetch_sync() for a Session or Connection
|
|
99
|
+
return response.to_dict()
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## OpenSearch
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from datatables_backend import Column
|
|
106
|
+
from datatables_backend.opensearch import IndexColumn, SearchTable
|
|
107
|
+
|
|
108
|
+
reports = SearchTable(
|
|
109
|
+
'reports',
|
|
110
|
+
[
|
|
111
|
+
IndexColumn('id', text='keyword'),
|
|
112
|
+
IndexColumn('title', text='text+keyword'), # the dynamic-mapping default
|
|
113
|
+
IndexColumn('body', text='text'), # analysed only: no sorting
|
|
114
|
+
Column('pages', type='number'),
|
|
115
|
+
Column('created', type='date'),
|
|
116
|
+
Column('published', type='boolean'), # a column is a string unless declared: the mapping is not read
|
|
117
|
+
],
|
|
118
|
+
unique_key='id', # required: an index has no primary key to break ties with
|
|
119
|
+
)
|
|
120
|
+
response = await reports.fetch(params, AsyncOpenSearch(hosts=['http://localhost:9200']))
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**The unique key.** `unique_key` names a declared column (a tuple of names for a key of several) whose values are
|
|
124
|
+
unique for every document; OpenSearch needs it, since an index has no primary key to break ties with, and on SQL it
|
|
125
|
+
replaces the primary key. Its rules, the document `_id` and paging past the window are in [the OpenSearch
|
|
126
|
+
rules][rules-opensearch].
|
|
127
|
+
|
|
128
|
+
## FastAPI
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from datatables_backend.fastapi import DataTablesParams
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@app.post('/users/table')
|
|
135
|
+
async def users_table(params: DataTablesParams, session: SessionDep) -> dict[str, Any]:
|
|
136
|
+
return (await users.fetch(params, session)).to_dict()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
On the page, DataTables' default POST (a form) is read as it comes, so the client needs no `data` or `contentType`:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
new DataTable('#users', {
|
|
143
|
+
serverSide: true,
|
|
144
|
+
processing: true,
|
|
145
|
+
ajax: { url: '/users/table', type: 'POST' },
|
|
146
|
+
columns: [{ data: 'id' }, { data: 'email' }, { data: 'joined' }],
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Options (`TotalCache`, `RowHooks`, page limits, a table per request for a per-user filter): [docs/usage.md][usage].
|
|
151
|
+
|
|
152
|
+
## Tested with
|
|
153
|
+
|
|
154
|
+
CI runs the tests on Python 3.12 to 3.15 and on SQLite (sync, and async with aiosqlite); PostgreSQL 18 (psycopg 3, sync
|
|
155
|
+
and async; psycopg2 with asyncpg); MySQL 8.4 (pymysql, aiomysql); SQL Server 2022 (pyodbc, aioodbc, ODBC Driver 18);
|
|
156
|
+
and OpenSearch 3.10.0 and 2.19.7. It also runs the oldest supported releases: SQLAlchemy 2.0.31, Pydantic 2.10,
|
|
157
|
+
opensearch-py 2.6 and FastAPI 0.133.
|
|
158
|
+
|
|
159
|
+
## What the server decides
|
|
160
|
+
|
|
161
|
+
The library does not change how a database compares values. Each rule below is stated in full in
|
|
162
|
+
[docs/rules.md][rules], and what each engine does on its own in [docs/databases.md][databases].
|
|
163
|
+
|
|
164
|
+
- [Columns and ordering][rules-columns]: only declared columns are used, the server decides what sorts, NULL is
|
|
165
|
+
lowest, and a descending order is the exact reverse (one exception below).
|
|
166
|
+
- [Types and matching][rules-types]: the declared type builds the filter, a date is matched by day, a day and a number
|
|
167
|
+
have one spelling, and text matches in any case.
|
|
168
|
+
- [Errors][rules-errors]: a user's mistake comes back as a response with one of eight stable codes (the list is
|
|
169
|
+
closed) and a text that says how to fix it; any other failure raises.
|
|
170
|
+
- [Requests][rules-requests]: bounded in length, lists, tree depth and conditions; a request the library cannot read
|
|
171
|
+
raises `MalformedRequest`, answered 422 by the FastAPI dependency.
|
|
172
|
+
- [OpenSearch][rules-opensearch] and [SQL][rules-sql]: the unique key, the document id, last pages, analysed text.
|
|
173
|
+
|
|
174
|
+
**Exceptions to "no surprise", each with its reason in the rules:**
|
|
175
|
+
|
|
176
|
+
- On OpenSearch, an order on a field of several values: descending is not the exact reverse of ascending.
|
|
177
|
+
- An OpenSearch column declared `text='text'` has no whole string to sort by, so it is unorderable.
|
|
178
|
+
- On PostgreSQL a column that can hold NULL needs its index declared `NULLS FIRST` to serve a sort.
|
|
179
|
+
- A column that carries a time zone is matched by its UTC day; SQLite and MySQL `DATETIME` store no offset.
|
|
180
|
+
- SQLite, and OpenSearch before 3.1, fold ASCII letters only.
|
|
181
|
+
- A SQL column with a type of its own, or none, is compared as sent, not folded.
|
|
182
|
+
- On OpenSearch a page in the middle of more than `max_result_window` rows is refused (`page_out_of_range`), where the
|
|
183
|
+
first and last pages work and SQL serves every page ([paging past the window][rules-opensearch]).
|
|
184
|
+
- An OpenSearch column of analysed text only answers the word search, `contains`, `null`, `!null` and empty lists,
|
|
185
|
+
where SQL answers every condition ([the rule][rules-opensearch]).
|
|
186
|
+
- OpenSearch's global search skips number and date columns, and every backend's skips boolean columns
|
|
187
|
+
([the rule][rules-types]).
|
|
188
|
+
- On OpenSearch a stored `""` is not empty, where on SQL it is ([the rule][rules-opensearch]).
|
|
189
|
+
- On OpenSearch a number outside its field's mapped range fails the request, a stated limit of this library
|
|
190
|
+
([the entry][db-number-range], [issue #47][issue-47]).
|
|
191
|
+
|
|
192
|
+
[prd]: https://github.com/hniedner/datatables-backend/blob/main/docs/PRD.md
|
|
193
|
+
[changelog]: https://github.com/hniedner/datatables-backend/blob/main/CHANGELOG.md
|
|
194
|
+
[db-number-range]: https://github.com/hniedner/datatables-backend/blob/main/docs/databases.md#a-number-outside-the-range-of-its-field-fails-the-request
|
|
195
|
+
[issue-47]: https://github.com/hniedner/datatables-backend/issues/47
|
|
196
|
+
[usage]: https://github.com/hniedner/datatables-backend/blob/main/docs/usage.md
|
|
197
|
+
[rules]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md
|
|
198
|
+
[databases]: https://github.com/hniedner/datatables-backend/blob/main/docs/databases.md
|
|
199
|
+
[rules-columns]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#columns-and-ordering
|
|
200
|
+
[rules-types]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#types-and-matching
|
|
201
|
+
[rules-errors]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#errors
|
|
202
|
+
[rules-requests]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#requests
|
|
203
|
+
[rules-opensearch]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#opensearch
|
|
204
|
+
[rules-sql]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#sql
|
|
205
|
+
|
|
206
|
+
## Development
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
uv sync
|
|
210
|
+
uv run pytest
|
|
211
|
+
uv run ruff check . && uv run basedpyright
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Licence: MIT ([LICENSE](https://github.com/hniedner/datatables-backend/blob/main/LICENSE)).
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# datatables-backend
|
|
2
|
+
|
|
3
|
+
[](https://github.com/hniedner/datatables-backend/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/hniedner/datatables-backend/security/code-scanning)
|
|
5
|
+
[](https://scorecard.dev/viewer/?uri=github.com/hniedner/datatables-backend)
|
|
6
|
+
[](https://codecov.io/gh/hniedner/datatables-backend)
|
|
7
|
+
[](https://github.com/hniedner/datatables-backend/blob/main/pyproject.toml)
|
|
8
|
+
[](https://github.com/hniedner/datatables-backend/blob/main/LICENSE)
|
|
9
|
+
|
|
10
|
+
Server-side processing for [DataTables](https://datatables.net): paging, sorting, the global and column searches,
|
|
11
|
+
and [SearchBuilder](https://datatables.net/extensions/searchbuilder/) criteria. Data can come from:
|
|
12
|
+
|
|
13
|
+
- **SQL**: SQLAlchemy 2, async or sync.
|
|
14
|
+
- **OpenSearch**: async or sync client.
|
|
15
|
+
|
|
16
|
+
The library is read-only and framework-agnostic.
|
|
17
|
+
|
|
18
|
+
> Status: release candidate (0.1.0rc2). Until 1.0, a minor release (0.x) may break the Python API; each such change is
|
|
19
|
+
> listed in [CHANGELOG.md][changelog]. The wire format and the error codes change only in a release whose notes say so.
|
|
20
|
+
> See [docs/PRD.md][prd] for the scope and design.
|
|
21
|
+
|
|
22
|
+
If you need a SQL backend with editing (DataTables Editor), use SpryMedia's own
|
|
23
|
+
[`datatables_server`](https://datatables.net/manual/python). This library adds:
|
|
24
|
+
|
|
25
|
+
- async SQLAlchemy;
|
|
26
|
+
- a search-engine backend;
|
|
27
|
+
- the same server-declared columns and safety rules on every source.
|
|
28
|
+
|
|
29
|
+
datatables-backend is not affiliated with or endorsed by DataTables or SpryMedia.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
pip installs no pre-release unless asked, so while 0.1.0 is a release candidate add `--pre` to each command.
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
pip install "datatables-backend[sqlalchemy]" # SQL, sync
|
|
37
|
+
pip install "datatables-backend[sqlalchemy-async]" # SQL, async (adds greenlet): also your async driver, e.g. asyncpg
|
|
38
|
+
pip install "datatables-backend[opensearch]" # OpenSearch, sync (installs opensearch-py)
|
|
39
|
+
pip install "datatables-backend[opensearch-async]" # OpenSearch, async (adds aiohttp)
|
|
40
|
+
pip install "datatables-backend[fastapi]" # the FastAPI dependency
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## SQL
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from datatables_backend import Column, parse_request
|
|
47
|
+
from datatables_backend.sqlalchemy import SQLTable
|
|
48
|
+
|
|
49
|
+
users = SQLTable(
|
|
50
|
+
users_table, # a Table, join or subquery
|
|
51
|
+
[Column('id'), Column('email', field='email_address'), Column('joined')],
|
|
52
|
+
base_filter=users_table.c.active.is_(True), # rows the table shows at all
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
params = parse_request(request_json) # or the flat form/query parameters
|
|
56
|
+
async with AsyncSession(engine) as session:
|
|
57
|
+
response = await users.fetch(params, session) # fetch_sync() for a Session or Connection
|
|
58
|
+
return response.to_dict()
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## OpenSearch
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from datatables_backend import Column
|
|
65
|
+
from datatables_backend.opensearch import IndexColumn, SearchTable
|
|
66
|
+
|
|
67
|
+
reports = SearchTable(
|
|
68
|
+
'reports',
|
|
69
|
+
[
|
|
70
|
+
IndexColumn('id', text='keyword'),
|
|
71
|
+
IndexColumn('title', text='text+keyword'), # the dynamic-mapping default
|
|
72
|
+
IndexColumn('body', text='text'), # analysed only: no sorting
|
|
73
|
+
Column('pages', type='number'),
|
|
74
|
+
Column('created', type='date'),
|
|
75
|
+
Column('published', type='boolean'), # a column is a string unless declared: the mapping is not read
|
|
76
|
+
],
|
|
77
|
+
unique_key='id', # required: an index has no primary key to break ties with
|
|
78
|
+
)
|
|
79
|
+
response = await reports.fetch(params, AsyncOpenSearch(hosts=['http://localhost:9200']))
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**The unique key.** `unique_key` names a declared column (a tuple of names for a key of several) whose values are
|
|
83
|
+
unique for every document; OpenSearch needs it, since an index has no primary key to break ties with, and on SQL it
|
|
84
|
+
replaces the primary key. Its rules, the document `_id` and paging past the window are in [the OpenSearch
|
|
85
|
+
rules][rules-opensearch].
|
|
86
|
+
|
|
87
|
+
## FastAPI
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from datatables_backend.fastapi import DataTablesParams
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@app.post('/users/table')
|
|
94
|
+
async def users_table(params: DataTablesParams, session: SessionDep) -> dict[str, Any]:
|
|
95
|
+
return (await users.fetch(params, session)).to_dict()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
On the page, DataTables' default POST (a form) is read as it comes, so the client needs no `data` or `contentType`:
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
new DataTable('#users', {
|
|
102
|
+
serverSide: true,
|
|
103
|
+
processing: true,
|
|
104
|
+
ajax: { url: '/users/table', type: 'POST' },
|
|
105
|
+
columns: [{ data: 'id' }, { data: 'email' }, { data: 'joined' }],
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Options (`TotalCache`, `RowHooks`, page limits, a table per request for a per-user filter): [docs/usage.md][usage].
|
|
110
|
+
|
|
111
|
+
## Tested with
|
|
112
|
+
|
|
113
|
+
CI runs the tests on Python 3.12 to 3.15 and on SQLite (sync, and async with aiosqlite); PostgreSQL 18 (psycopg 3, sync
|
|
114
|
+
and async; psycopg2 with asyncpg); MySQL 8.4 (pymysql, aiomysql); SQL Server 2022 (pyodbc, aioodbc, ODBC Driver 18);
|
|
115
|
+
and OpenSearch 3.10.0 and 2.19.7. It also runs the oldest supported releases: SQLAlchemy 2.0.31, Pydantic 2.10,
|
|
116
|
+
opensearch-py 2.6 and FastAPI 0.133.
|
|
117
|
+
|
|
118
|
+
## What the server decides
|
|
119
|
+
|
|
120
|
+
The library does not change how a database compares values. Each rule below is stated in full in
|
|
121
|
+
[docs/rules.md][rules], and what each engine does on its own in [docs/databases.md][databases].
|
|
122
|
+
|
|
123
|
+
- [Columns and ordering][rules-columns]: only declared columns are used, the server decides what sorts, NULL is
|
|
124
|
+
lowest, and a descending order is the exact reverse (one exception below).
|
|
125
|
+
- [Types and matching][rules-types]: the declared type builds the filter, a date is matched by day, a day and a number
|
|
126
|
+
have one spelling, and text matches in any case.
|
|
127
|
+
- [Errors][rules-errors]: a user's mistake comes back as a response with one of eight stable codes (the list is
|
|
128
|
+
closed) and a text that says how to fix it; any other failure raises.
|
|
129
|
+
- [Requests][rules-requests]: bounded in length, lists, tree depth and conditions; a request the library cannot read
|
|
130
|
+
raises `MalformedRequest`, answered 422 by the FastAPI dependency.
|
|
131
|
+
- [OpenSearch][rules-opensearch] and [SQL][rules-sql]: the unique key, the document id, last pages, analysed text.
|
|
132
|
+
|
|
133
|
+
**Exceptions to "no surprise", each with its reason in the rules:**
|
|
134
|
+
|
|
135
|
+
- On OpenSearch, an order on a field of several values: descending is not the exact reverse of ascending.
|
|
136
|
+
- An OpenSearch column declared `text='text'` has no whole string to sort by, so it is unorderable.
|
|
137
|
+
- On PostgreSQL a column that can hold NULL needs its index declared `NULLS FIRST` to serve a sort.
|
|
138
|
+
- A column that carries a time zone is matched by its UTC day; SQLite and MySQL `DATETIME` store no offset.
|
|
139
|
+
- SQLite, and OpenSearch before 3.1, fold ASCII letters only.
|
|
140
|
+
- A SQL column with a type of its own, or none, is compared as sent, not folded.
|
|
141
|
+
- On OpenSearch a page in the middle of more than `max_result_window` rows is refused (`page_out_of_range`), where the
|
|
142
|
+
first and last pages work and SQL serves every page ([paging past the window][rules-opensearch]).
|
|
143
|
+
- An OpenSearch column of analysed text only answers the word search, `contains`, `null`, `!null` and empty lists,
|
|
144
|
+
where SQL answers every condition ([the rule][rules-opensearch]).
|
|
145
|
+
- OpenSearch's global search skips number and date columns, and every backend's skips boolean columns
|
|
146
|
+
([the rule][rules-types]).
|
|
147
|
+
- On OpenSearch a stored `""` is not empty, where on SQL it is ([the rule][rules-opensearch]).
|
|
148
|
+
- On OpenSearch a number outside its field's mapped range fails the request, a stated limit of this library
|
|
149
|
+
([the entry][db-number-range], [issue #47][issue-47]).
|
|
150
|
+
|
|
151
|
+
[prd]: https://github.com/hniedner/datatables-backend/blob/main/docs/PRD.md
|
|
152
|
+
[changelog]: https://github.com/hniedner/datatables-backend/blob/main/CHANGELOG.md
|
|
153
|
+
[db-number-range]: https://github.com/hniedner/datatables-backend/blob/main/docs/databases.md#a-number-outside-the-range-of-its-field-fails-the-request
|
|
154
|
+
[issue-47]: https://github.com/hniedner/datatables-backend/issues/47
|
|
155
|
+
[usage]: https://github.com/hniedner/datatables-backend/blob/main/docs/usage.md
|
|
156
|
+
[rules]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md
|
|
157
|
+
[databases]: https://github.com/hniedner/datatables-backend/blob/main/docs/databases.md
|
|
158
|
+
[rules-columns]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#columns-and-ordering
|
|
159
|
+
[rules-types]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#types-and-matching
|
|
160
|
+
[rules-errors]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#errors
|
|
161
|
+
[rules-requests]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#requests
|
|
162
|
+
[rules-opensearch]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#opensearch
|
|
163
|
+
[rules-sql]: https://github.com/hniedner/datatables-backend/blob/main/docs/rules.md#sql
|
|
164
|
+
|
|
165
|
+
## Development
|
|
166
|
+
|
|
167
|
+
```sh
|
|
168
|
+
uv sync
|
|
169
|
+
uv run pytest
|
|
170
|
+
uv run ruff check . && uv run basedpyright
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Licence: MIT ([LICENSE](https://github.com/hniedner/datatables-backend/blob/main/LICENSE)).
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "datatables-backend"
|
|
7
|
+
description = "Server-side processing for DataTables from SQL (SQLAlchemy 2, async and sync) and OpenSearch."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
authors = [{ name = "R. Hannes Niedner" }]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dynamic = ["version"]
|
|
14
|
+
dependencies = [
|
|
15
|
+
"pydantic>=2.10",
|
|
16
|
+
"pydantic>=2.12; python_version >= '3.14'",
|
|
17
|
+
"pydantic>=2.14; python_version >= '3.15'",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
21
|
+
"Framework :: AsyncIO",
|
|
22
|
+
"Framework :: FastAPI",
|
|
23
|
+
"Intended Audience :: Developers",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
29
|
+
"Programming Language :: Python :: 3.15",
|
|
30
|
+
"Topic :: Database :: Front-Ends",
|
|
31
|
+
"Topic :: Internet :: WWW/HTTP :: Indexing/Search",
|
|
32
|
+
"Typing :: Typed",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
sqlalchemy = ["sqlalchemy>=2.0.31"]
|
|
37
|
+
# Async SQL also needs an async driver of the database's own (aiosqlite, asyncpg, aiomysql, aioodbc).
|
|
38
|
+
sqlalchemy-async = ["sqlalchemy[asyncio]>=2.0.31"]
|
|
39
|
+
opensearch = ["opensearch-py>=2.6"]
|
|
40
|
+
opensearch-async = ["opensearch-py[async]>=2.6"]
|
|
41
|
+
fastapi = ["fastapi>=0.133", "python-multipart>=0.0.9"]
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://github.com/hniedner/datatables-backend"
|
|
45
|
+
Documentation = "https://github.com/hniedner/datatables-backend#readme"
|
|
46
|
+
Issues = "https://github.com/hniedner/datatables-backend/issues"
|
|
47
|
+
Changelog = "https://github.com/hniedner/datatables-backend/blob/main/CHANGELOG.md"
|
|
48
|
+
|
|
49
|
+
[dependency-groups]
|
|
50
|
+
dev = [
|
|
51
|
+
"actionlint-py>=1.7.12",
|
|
52
|
+
"aiosqlite>=0.20",
|
|
53
|
+
"basedpyright>=1.29",
|
|
54
|
+
"diff-cover>=11",
|
|
55
|
+
"fastapi>=0.133",
|
|
56
|
+
"httpx2>=2.16",
|
|
57
|
+
"opensearch-py[async]>=2.6",
|
|
58
|
+
"pre-commit>=4.7",
|
|
59
|
+
"pytest>=8.3",
|
|
60
|
+
"pytest-asyncio>=0.24",
|
|
61
|
+
"pytest-cov>=5.0",
|
|
62
|
+
"python-multipart>=0.0.9",
|
|
63
|
+
"pyyaml>=6.0",
|
|
64
|
+
"ruff>=0.8",
|
|
65
|
+
"sqlalchemy[asyncio]>=2.0.31",
|
|
66
|
+
"starlette>=1.2",
|
|
67
|
+
"zizmor>=1.32",
|
|
68
|
+
]
|
|
69
|
+
# The drivers for the container tests (tests/conftest.py, DTB_SQL_URL): one implementation per database for sync and
|
|
70
|
+
# async alike. SQL Server also needs Microsoft's ODBC Driver 18 on the machine.
|
|
71
|
+
containers = [
|
|
72
|
+
"aiomysql>=0.2",
|
|
73
|
+
"aioodbc>=0.5",
|
|
74
|
+
"asyncpg>=0.30",
|
|
75
|
+
"psycopg[binary]>=3.2",
|
|
76
|
+
"psycopg2-binary>=2.9",
|
|
77
|
+
"pymysql>=1.1",
|
|
78
|
+
"pyodbc>=5.2",
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
[tool.hatch.version]
|
|
82
|
+
path = "src/datatables_backend/__init__.py"
|
|
83
|
+
|
|
84
|
+
# Without a list the sdist takes every file .gitignore does not exclude, tracked or not. scripts/check_dist.py checks
|
|
85
|
+
# the built sdist.
|
|
86
|
+
[tool.hatch.build.targets.sdist]
|
|
87
|
+
include = ["/src", "/tests", "/README.md", "/LICENSE", "/pyproject.toml"]
|
|
88
|
+
|
|
89
|
+
[tool.hatch.build.targets.wheel]
|
|
90
|
+
packages = ["src/datatables_backend"]
|
|
91
|
+
|
|
92
|
+
[tool.pytest.ini_options]
|
|
93
|
+
testpaths = ["tests", "scripts"]
|
|
94
|
+
asyncio_mode = "auto"
|
|
95
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
96
|
+
addopts = "-ra --strict-markers"
|
|
97
|
+
|
|
98
|
+
[tool.ruff]
|
|
99
|
+
line-length = 120
|
|
100
|
+
target-version = "py312"
|
|
101
|
+
src = ["src", "tests", "scripts"]
|
|
102
|
+
|
|
103
|
+
[tool.ruff.format]
|
|
104
|
+
quote-style = "single"
|
|
105
|
+
|
|
106
|
+
[tool.ruff.lint]
|
|
107
|
+
select = ["E", "W", "F", "I", "N", "UP", "RUF", "C4", "B", "A", "SIM", "ARG", "PL", "PERF", "S", "FURB", "T20"]
|
|
108
|
+
ignore = ["PLR0911", "PLR0912", "PLR0913", "PLR2004", "N815", "N818"]
|
|
109
|
+
|
|
110
|
+
[tool.ruff.lint.per-file-ignores]
|
|
111
|
+
"tests/**" = ["S101", "PLR2004", "ARG", "E501", "PLC0415"]
|
|
112
|
+
"scripts/trace_check.py" = ["T201"]
|
|
113
|
+
"scripts/private_word_scan.py" = ["S603", "S607", "T201"]
|
|
114
|
+
"scripts/check_dist.py" = ["S603", "S607", "T201"]
|
|
115
|
+
"scripts/test_*.py" = ["S101", "S603"]
|
|
116
|
+
"scripts/test_private_word_scan.py" = ["S607"]
|
|
117
|
+
"scripts/test_check_dist.py" = ["S607"]
|
|
118
|
+
|
|
119
|
+
[tool.ruff.lint.isort]
|
|
120
|
+
force-single-line = true
|
|
121
|
+
force-sort-within-sections = true
|
|
122
|
+
known-first-party = ["datatables_backend"]
|
|
123
|
+
|
|
124
|
+
[tool.basedpyright]
|
|
125
|
+
include = ["src", "tests", "scripts"]
|
|
126
|
+
pythonVersion = "3.12"
|
|
127
|
+
typeCheckingMode = "standard"
|
|
128
|
+
failOnWarnings = true
|
|
129
|
+
|
|
130
|
+
[tool.coverage.run]
|
|
131
|
+
# The word-scan tests run the script as a subprocess, as its hooks do; this measures those runs too.
|
|
132
|
+
patch = ["subprocess"]
|
|
133
|
+
# The changed-line gate measures the scripts, not their tests.
|
|
134
|
+
omit = ["*/scripts/test_*.py"] # relative to each measured process's cwd, so a glob, not a path
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Server-side processing for DataTables from SQL (SQLAlchemy 2) and OpenSearch.
|
|
2
|
+
|
|
3
|
+
The core needs only Pydantic. The backends are imported from their own modules, so each needs only its own extra:
|
|
4
|
+
``datatables_backend.sqlalchemy`` (``[sqlalchemy]``), ``datatables_backend.opensearch`` (no client library is
|
|
5
|
+
imported; ``[opensearch]`` installs one) and ``datatables_backend.fastapi`` (``[fastapi]``).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = '0.1.0rc2'
|
|
9
|
+
|
|
10
|
+
from .cache import TotalCache
|
|
11
|
+
from .columns import Column
|
|
12
|
+
from .columns import ColumnType
|
|
13
|
+
from .protocol import DTColumn
|
|
14
|
+
from .protocol import DTOrder
|
|
15
|
+
from .protocol import DTParams
|
|
16
|
+
from .protocol import DTResponse
|
|
17
|
+
from .protocol import InvalidFilter
|
|
18
|
+
from .protocol import MalformedRequest
|
|
19
|
+
from .protocol import PartialResultError
|
|
20
|
+
from .protocol import SchemaError
|
|
21
|
+
from .protocol import Search
|
|
22
|
+
from .protocol import SearchCriterion
|
|
23
|
+
from .protocol import SearchGroup
|
|
24
|
+
from .protocol import parse_request
|
|
25
|
+
from .rows import RowHooks
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
'Column',
|
|
29
|
+
'ColumnType',
|
|
30
|
+
'DTColumn',
|
|
31
|
+
'DTOrder',
|
|
32
|
+
'DTParams',
|
|
33
|
+
'DTResponse',
|
|
34
|
+
'InvalidFilter',
|
|
35
|
+
'MalformedRequest',
|
|
36
|
+
'PartialResultError',
|
|
37
|
+
'RowHooks',
|
|
38
|
+
'SchemaError',
|
|
39
|
+
'Search',
|
|
40
|
+
'SearchCriterion',
|
|
41
|
+
'SearchGroup',
|
|
42
|
+
'TotalCache',
|
|
43
|
+
'parse_request',
|
|
44
|
+
]
|