strapalchemy 0.1.0__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.
- strapalchemy-0.1.0/.claude/settings.local.json +7 -0
- strapalchemy-0.1.0/.gitignore +178 -0
- strapalchemy-0.1.0/.python-version +1 -0
- strapalchemy-0.1.0/LICENSE +21 -0
- strapalchemy-0.1.0/PKG-INFO +281 -0
- strapalchemy-0.1.0/README.md +242 -0
- strapalchemy-0.1.0/main.py +6 -0
- strapalchemy-0.1.0/pyproject.toml +88 -0
- strapalchemy-0.1.0/src/strapalchemy/__init__.py +67 -0
- strapalchemy-0.1.0/src/strapalchemy/logging/__init__.py +32 -0
- strapalchemy-0.1.0/src/strapalchemy/logging/handlers.py +29 -0
- strapalchemy-0.1.0/src/strapalchemy/logging/logger.py +124 -0
- strapalchemy-0.1.0/src/strapalchemy/models/__init__.py +5 -0
- strapalchemy-0.1.0/src/strapalchemy/models/base.py +5 -0
- strapalchemy-0.1.0/src/strapalchemy/services/__init__.py +21 -0
- strapalchemy-0.1.0/src/strapalchemy/services/field_selector.py +147 -0
- strapalchemy-0.1.0/src/strapalchemy/services/filter_builder.py +424 -0
- strapalchemy-0.1.0/src/strapalchemy/services/operator_handler.py +201 -0
- strapalchemy-0.1.0/src/strapalchemy/services/paginator.py +279 -0
- strapalchemy-0.1.0/src/strapalchemy/services/population_builder.py +238 -0
- strapalchemy-0.1.0/src/strapalchemy/services/query_optimizer.py +250 -0
- strapalchemy-0.1.0/src/strapalchemy/services/search_engine.py +274 -0
- strapalchemy-0.1.0/src/strapalchemy/services/serializer.py +429 -0
- strapalchemy-0.1.0/src/strapalchemy/services/sort_builder.py +234 -0
- strapalchemy-0.1.0/src/strapalchemy/services/type_converter.py +75 -0
- strapalchemy-0.1.0/tests/__init__.py +0 -0
- strapalchemy-0.1.0/tests/conftest.py +166 -0
- strapalchemy-0.1.0/tests/models.py +59 -0
- strapalchemy-0.1.0/tests/services/__init__.py +1 -0
- strapalchemy-0.1.0/tests/services/test_field_selector.py +112 -0
- strapalchemy-0.1.0/tests/services/test_filter_builder.py +327 -0
- strapalchemy-0.1.0/tests/services/test_operator_handler.py +270 -0
- strapalchemy-0.1.0/tests/services/test_paginator.py +135 -0
- strapalchemy-0.1.0/tests/services/test_search_engine.py +125 -0
- strapalchemy-0.1.0/tests/services/test_serializer.py +128 -0
- strapalchemy-0.1.0/tests/services/test_sort_builder.py +131 -0
- strapalchemy-0.1.0/uv.lock +563 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
.cursor
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
data
|
|
30
|
+
.cursorrules
|
|
31
|
+
|
|
32
|
+
# PyInstaller
|
|
33
|
+
# Usually these files are written by a python script from a template
|
|
34
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
35
|
+
*.manifest
|
|
36
|
+
*.spec
|
|
37
|
+
|
|
38
|
+
# Installer logs
|
|
39
|
+
pip-log.txt
|
|
40
|
+
pip-delete-this-directory.txt
|
|
41
|
+
|
|
42
|
+
# Unit test / coverage reports
|
|
43
|
+
htmlcov/
|
|
44
|
+
.tox/
|
|
45
|
+
.nox/
|
|
46
|
+
.coverage
|
|
47
|
+
.coverage.*
|
|
48
|
+
.cache
|
|
49
|
+
nosetests.xml
|
|
50
|
+
coverage.xml
|
|
51
|
+
*.cover
|
|
52
|
+
*.py,cover
|
|
53
|
+
.hypothesis/
|
|
54
|
+
.pytest_cache/
|
|
55
|
+
cover/
|
|
56
|
+
.vscode
|
|
57
|
+
|
|
58
|
+
# Translations
|
|
59
|
+
*.mo
|
|
60
|
+
*.pot
|
|
61
|
+
|
|
62
|
+
# Django stuff:
|
|
63
|
+
*.log
|
|
64
|
+
local_settings.py
|
|
65
|
+
db.sqlite3
|
|
66
|
+
db.sqlite3-journal
|
|
67
|
+
|
|
68
|
+
# Flask stuff:
|
|
69
|
+
instance/
|
|
70
|
+
.webassets-cache
|
|
71
|
+
|
|
72
|
+
# Scrapy stuff:
|
|
73
|
+
.scrapy
|
|
74
|
+
|
|
75
|
+
# Sphinx documentation
|
|
76
|
+
docs/_build/
|
|
77
|
+
|
|
78
|
+
# PyBuilder
|
|
79
|
+
.pybuilder/
|
|
80
|
+
target/
|
|
81
|
+
|
|
82
|
+
# Jupyter Notebook
|
|
83
|
+
.ipynb_checkpoints
|
|
84
|
+
|
|
85
|
+
# IPython
|
|
86
|
+
profile_default/
|
|
87
|
+
ipython_config.py
|
|
88
|
+
|
|
89
|
+
# pyenv
|
|
90
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
91
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
92
|
+
# .python-version
|
|
93
|
+
|
|
94
|
+
# pipenv
|
|
95
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
96
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
97
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
98
|
+
# install all needed dependencies.
|
|
99
|
+
#Pipfile.lock
|
|
100
|
+
|
|
101
|
+
# UV
|
|
102
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
103
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
104
|
+
# commonly ignored for libraries.
|
|
105
|
+
#uv.lock
|
|
106
|
+
|
|
107
|
+
# poetry
|
|
108
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
109
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
110
|
+
# commonly ignored for libraries.
|
|
111
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
112
|
+
#poetry.lock
|
|
113
|
+
|
|
114
|
+
# pdm
|
|
115
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
116
|
+
#pdm.lock
|
|
117
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
118
|
+
# in version control.
|
|
119
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
120
|
+
.pdm.toml
|
|
121
|
+
.pdm-python
|
|
122
|
+
.pdm-build/
|
|
123
|
+
|
|
124
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
125
|
+
__pypackages__/
|
|
126
|
+
|
|
127
|
+
# Celery stuff
|
|
128
|
+
celerybeat-schedule
|
|
129
|
+
celerybeat.pid
|
|
130
|
+
|
|
131
|
+
# SageMath parsed files
|
|
132
|
+
*.sage.py
|
|
133
|
+
|
|
134
|
+
# Environments
|
|
135
|
+
.env
|
|
136
|
+
.venv
|
|
137
|
+
env/
|
|
138
|
+
venv/
|
|
139
|
+
ENV/
|
|
140
|
+
env.bak/
|
|
141
|
+
venv.bak/
|
|
142
|
+
|
|
143
|
+
# Spyder project settings
|
|
144
|
+
.spyderproject
|
|
145
|
+
.spyproject
|
|
146
|
+
|
|
147
|
+
# Rope project settings
|
|
148
|
+
.ropeproject
|
|
149
|
+
|
|
150
|
+
# mkdocs documentation
|
|
151
|
+
/site
|
|
152
|
+
|
|
153
|
+
# mypy
|
|
154
|
+
.mypy_cache/
|
|
155
|
+
.dmypy.json
|
|
156
|
+
dmypy.json
|
|
157
|
+
|
|
158
|
+
# Pyre type checker
|
|
159
|
+
.pyre/
|
|
160
|
+
|
|
161
|
+
# pytype static type analyzer
|
|
162
|
+
.pytype/
|
|
163
|
+
|
|
164
|
+
# Cython debug symbols
|
|
165
|
+
cython_debug/
|
|
166
|
+
|
|
167
|
+
# PyCharm
|
|
168
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
169
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
170
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
171
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
172
|
+
#.idea/
|
|
173
|
+
|
|
174
|
+
# Ruff stuff:
|
|
175
|
+
.ruff_cache/
|
|
176
|
+
|
|
177
|
+
# PyPI configuration file
|
|
178
|
+
.pypirc
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14.0
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 StrapAlchemy Contributors
|
|
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,281 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: strapalchemy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Enhanced SQLAlchemy query builder with advanced filtering, sorting, pagination, and search capabilities.
|
|
5
|
+
Project-URL: Homepage, https://github.com/faisalcayunda/strapalchemy
|
|
6
|
+
Project-URL: Documentation, https://github.com/faisalcayunda/strapalchemy#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/faisalcayunda/strapalchemy
|
|
8
|
+
Project-URL: Issues, https://github.com/faisalcayunda/strapalchemy/issues
|
|
9
|
+
Author-email: Faisal Nugraha Cayunda <faisal.nugraha.c@gmail.com>
|
|
10
|
+
Maintainer-email: Faisal Nugraha Cayunda <faisal.nugraha.c@gmail.com>
|
|
11
|
+
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: database,filtering,orm,pagination,query-builder,search,sqlalchemy,strapi
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Database
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.12
|
|
25
|
+
Requires-Dist: greenlet>=3.3.0
|
|
26
|
+
Requires-Dist: python-dateutil>=2.9.0.post0
|
|
27
|
+
Requires-Dist: rich>=13.0.0
|
|
28
|
+
Requires-Dist: sqlalchemy>=2.0.45
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: aiosqlite>=0.19.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: black>=24.0.0; extra == 'dev'
|
|
32
|
+
Requires-Dist: greenlet>=3.0.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: mypy>=1.8.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
35
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
37
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
# StrapAlchemy
|
|
41
|
+
|
|
42
|
+
> Enhanced SQLAlchemy query builder with advanced filtering, sorting, pagination, and search capabilities.
|
|
43
|
+
|
|
44
|
+
StrapAlchemy is a powerful query builder library for SQLAlchemy that provides Strapi-style query syntax for building complex database queries with ease.
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **Advanced Filtering**: Strapi-style operators (`$eq`, `$in`, `$contains`, `$between`, etc.)
|
|
49
|
+
- **Nested Relationship Filtering**: Filter through related models with dot notation
|
|
50
|
+
- **Flexible Sorting**: Sort by direct fields or relationship fields
|
|
51
|
+
- **Pagination**: Support for both page-based and offset-based pagination
|
|
52
|
+
- **Full-Text Search**: BM25 search with ParadeDB integration and ILIKE fallback
|
|
53
|
+
- **Field Selection**: Select specific fields to optimize query performance
|
|
54
|
+
- **Relationship Population**: Eager load relationships to prevent N+1 queries
|
|
55
|
+
- **Query Optimization**: Built-in caching and optimization for better performance
|
|
56
|
+
- **Model Serialization**: Convert SQLAlchemy models to dictionaries easily
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install strapalchemy
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from sqlalchemy import select
|
|
68
|
+
from strapalchemy import FilterBuilder, SortBuilder, Paginator, SearchEngine
|
|
69
|
+
from strapalchemy.models import Base
|
|
70
|
+
from sqlalchemy import Column, Integer, String
|
|
71
|
+
|
|
72
|
+
# Define your model
|
|
73
|
+
class User(Base):
|
|
74
|
+
__tablename__ = "users"
|
|
75
|
+
id = Column(Integer, primary_key=True)
|
|
76
|
+
name = Column(String)
|
|
77
|
+
email = Column(String)
|
|
78
|
+
status = Column(String)
|
|
79
|
+
|
|
80
|
+
# Build your query
|
|
81
|
+
query = select(User)
|
|
82
|
+
|
|
83
|
+
# Apply filters
|
|
84
|
+
filter_builder = FilterBuilder(User)
|
|
85
|
+
query = await filter_builder.apply_filters(query, {
|
|
86
|
+
"name": {"$contains": "John"},
|
|
87
|
+
"status": {"$eq": "active"}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
# Apply sorting
|
|
91
|
+
sort_builder = SortBuilder(User)
|
|
92
|
+
query = await sort_builder.apply_sorting(query, ["name:asc", "created_at:desc"])
|
|
93
|
+
|
|
94
|
+
# Apply search
|
|
95
|
+
search_engine = SearchEngine()
|
|
96
|
+
query = await search_engine.apply_search(query, User, "search term")
|
|
97
|
+
|
|
98
|
+
# Apply pagination
|
|
99
|
+
paginator = Paginator(session, User)
|
|
100
|
+
query, meta = await paginator.apply_pagination(query, {"page": 1, "page_size": 20})
|
|
101
|
+
|
|
102
|
+
# Execute
|
|
103
|
+
result = await session.execute(query)
|
|
104
|
+
users = result.scalars().all()
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Filtering
|
|
108
|
+
|
|
109
|
+
StrapAlchemy supports Strapi-style filtering operators:
|
|
110
|
+
|
|
111
|
+
| Operator | Description | Example |
|
|
112
|
+
|----------|-------------|---------|
|
|
113
|
+
| `$eq` | Equal | `{"status": {"$eq": "active"}}` |
|
|
114
|
+
| `$ne` | Not equal | `{"status": {"$ne": "deleted"}}` |
|
|
115
|
+
| `$lt` | Less than | `{"age": {"$lt": 18}}` |
|
|
116
|
+
| `$lte` | Less than or equal | `{"age": {"$lte": 18}}` |
|
|
117
|
+
| `$gt` | Greater than | `{"age": {"$gt": 18}}` |
|
|
118
|
+
| `$gte` | Greater than or equal | `{"age": {"$gte": 18}}` |
|
|
119
|
+
| `$in` | In list | `{"status": {"$in": ["active", "pending"]}}` |
|
|
120
|
+
| `$notIn` | Not in list | `{"status": {"$notIn": ["deleted"]}}` |
|
|
121
|
+
| `$contains` | Contains | `{"name": {"$contains": "John"}}` |
|
|
122
|
+
| `$containsi` | Contains (case insensitive) | `{"name": {"$containsi": "john"}}` |
|
|
123
|
+
| `$startsWith` | Starts with | `{"email": {"$startsWith": "admin"}}` |
|
|
124
|
+
| `$endsWith` | Ends with | `{"email": {"$endsWith": "@example.com"}}` |
|
|
125
|
+
| `$null` | Is null | `{"deleted_at": {"$null": true}}` |
|
|
126
|
+
| `$notNull` | Is not null | `{"email": {"$notNull": true}}` |
|
|
127
|
+
| `$between` | Between | `{"created_at": {"$between": ["2024-01-01", "2024-12-31"]}}` |
|
|
128
|
+
| `$or` | Logical OR | `{"$or": [{"status": {"$eq": "active"}}, {"status": {"$eq": "pending"}}]}` |
|
|
129
|
+
| `$and` | Logical AND | `{"$and": [{"status": {"$eq": "active"}}, {"verified": {"$eq": true}}]}` |
|
|
130
|
+
|
|
131
|
+
### Nested Relationship Filtering
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
# Filter by relationship fields
|
|
135
|
+
query = await filter_builder.apply_filters(query, {
|
|
136
|
+
"organization": {"slug": {"$eq": "acme"}}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
# Or use dot notation
|
|
140
|
+
query = await filter_builder.apply_filters(query, {
|
|
141
|
+
"organization.slug": {"$eq": "acme"}
|
|
142
|
+
})
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Sorting
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
# Sort by single field
|
|
149
|
+
query = await sort_builder.apply_sorting(query, "name:asc")
|
|
150
|
+
|
|
151
|
+
# Sort by multiple fields
|
|
152
|
+
query = await sort_builder.apply_sorting(query, ["name:asc", "created_at:desc"])
|
|
153
|
+
|
|
154
|
+
# Sort by relationship field
|
|
155
|
+
query = await sort_builder.apply_sorting(query, ["organization.name:asc"])
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Pagination
|
|
159
|
+
|
|
160
|
+
### Page-based Pagination
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
query, meta = await paginator.apply_pagination(query, {
|
|
164
|
+
"page": 1,
|
|
165
|
+
"page_size": 20
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
# meta contains:
|
|
169
|
+
# {
|
|
170
|
+
# "page": 1,
|
|
171
|
+
# "page_size": 20,
|
|
172
|
+
# "page_count": 5,
|
|
173
|
+
# "total": 100,
|
|
174
|
+
# "has_next": True,
|
|
175
|
+
# "has_previous": False
|
|
176
|
+
# }
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Offset-based Pagination
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
query, meta = await paginator.apply_pagination(query, {
|
|
183
|
+
"start": 0,
|
|
184
|
+
"limit": 20
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Field Selection
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from strapalchemy import FieldSelector
|
|
192
|
+
|
|
193
|
+
field_selector = FieldSelector(User)
|
|
194
|
+
query = await field_selector.apply_field_selection(query, ["id", "name", "email"])
|
|
195
|
+
|
|
196
|
+
# Select relationship fields
|
|
197
|
+
query = await field_selector.apply_field_selection(query, ["id", "name", "organization.slug"])
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Model Serialization
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from strapalchemy import ModelSerializer
|
|
204
|
+
|
|
205
|
+
# Serialize a single model
|
|
206
|
+
data = ModelSerializer.serialize(user, fields=["id", "name", "email"])
|
|
207
|
+
|
|
208
|
+
# Serialize a list
|
|
209
|
+
data = ModelSerializer.serialize(users, fields=["id", "name"])
|
|
210
|
+
|
|
211
|
+
# Serialize with relationships
|
|
212
|
+
data = ModelSerializer.serialize(user, populate="organization")
|
|
213
|
+
|
|
214
|
+
# Serialize with nested relationships
|
|
215
|
+
data = ModelSerializer.serialize(user, populate=["organization", "user.role"])
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Search
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from strapalchemy import SearchEngine
|
|
222
|
+
|
|
223
|
+
search_engine = SearchEngine()
|
|
224
|
+
|
|
225
|
+
# Add searchable fields to your model
|
|
226
|
+
class User(Base):
|
|
227
|
+
__tablename__ = "users"
|
|
228
|
+
__searchable__ = {
|
|
229
|
+
"text_fields": ["name", "email", "bio"]
|
|
230
|
+
}
|
|
231
|
+
id = Column(Integer, primary_key=True)
|
|
232
|
+
name = Column(String)
|
|
233
|
+
email = Column(String)
|
|
234
|
+
bio = Column(String)
|
|
235
|
+
|
|
236
|
+
# Apply search
|
|
237
|
+
query = await search_engine.apply_search(query, User, "John Doe")
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Advanced Usage
|
|
241
|
+
|
|
242
|
+
### Combining Multiple Builders
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
async def get_users(filters=None, sort=None, search=None, page=None):
|
|
246
|
+
query = select(User)
|
|
247
|
+
|
|
248
|
+
if filters:
|
|
249
|
+
query = await filter_builder.apply_filters(query, filters)
|
|
250
|
+
|
|
251
|
+
if sort:
|
|
252
|
+
query = await sort_builder.apply_sorting(query, sort)
|
|
253
|
+
|
|
254
|
+
if search:
|
|
255
|
+
query = await search_engine.apply_search(query, User, search)
|
|
256
|
+
|
|
257
|
+
if page:
|
|
258
|
+
query, meta = await paginator.apply_pagination(query, page)
|
|
259
|
+
|
|
260
|
+
result = await session.execute(query)
|
|
261
|
+
return result.scalars().all(), meta
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Requirements
|
|
265
|
+
|
|
266
|
+
- Python >= 3.12
|
|
267
|
+
- SQLAlchemy >= 2.0.45
|
|
268
|
+
- python-dateutil >= 2.9.0
|
|
269
|
+
- rich >= 13.0.0
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT License - see LICENSE file for details.
|
|
274
|
+
|
|
275
|
+
## Contributing
|
|
276
|
+
|
|
277
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
278
|
+
|
|
279
|
+
## Support
|
|
280
|
+
|
|
281
|
+
For issues and questions, please use the GitHub issue tracker.
|