sway-orm 1.0.0__cp315-cp315-win_amd64.whl
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.
- sway/__init__.py +161 -0
- sway/_msgpack.py +3 -0
- sway/_sway_core.cp315-win_amd64.pyd +0 -0
- sway/cli/__init__.py +7 -0
- sway/cli/app.py +107 -0
- sway/cli/codegen.py +66 -0
- sway/cli/config.py +84 -0
- sway/cli/migrations.py +650 -0
- sway/codegen/__init__.py +15 -0
- sway/codegen/stub_generator.py +1608 -0
- sway/core/__init__.py +106 -0
- sway/core/column_types.py +403 -0
- sway/core/ir.py +434 -0
- sway/core/types.py +73 -0
- sway/core/wrapper.py +344 -0
- sway/db/__init__.py +238 -0
- sway/db/listener.py +96 -0
- sway/db/pool.py +328 -0
- sway/db/registry.py +113 -0
- sway/db/schema.py +58 -0
- sway/db/transaction.py +423 -0
- sway/exceptions.py +94 -0
- sway/migrations/__init__.py +49 -0
- sway/migrations/config.py +187 -0
- sway/migrations/context.py +687 -0
- sway/migrations/executor.py +450 -0
- sway/migrations/extract.py +491 -0
- sway/migrations/generator.py +590 -0
- sway/migrations/replay.py +358 -0
- sway/migrations/squash.py +118 -0
- sway/migrations/tracker.py +160 -0
- sway/migrations/utils.py +167 -0
- sway/models/__init__.py +85 -0
- sway/models/base.py +1524 -0
- sway/models/db_schema.py +787 -0
- sway/models/decorators.py +183 -0
- sway/models/field.py +222 -0
- sway/models/lookups.py +677 -0
- sway/models/metadata.py +293 -0
- sway/models/registry.py +216 -0
- sway/models/serializers.py +180 -0
- sway/models/utils.py +156 -0
- sway/py.typed +0 -0
- sway/queries/__init__.py +84 -0
- sway/queries/_result_set.py +233 -0
- sway/queries/_result_types.py +97 -0
- sway/queries/aggregates.py +105 -0
- sway/queries/base.py +287 -0
- sway/queries/conditions.py +103 -0
- sway/queries/expressions.py +355 -0
- sway/queries/insert.py +258 -0
- sway/queries/joins.py +66 -0
- sway/queries/manager.py +455 -0
- sway/queries/mixins/__init__.py +68 -0
- sway/queries/mixins/aggregation.py +395 -0
- sway/queries/mixins/debug.py +155 -0
- sway/queries/mixins/execution.py +1105 -0
- sway/queries/mixins/filtering.py +142 -0
- sway/queries/mixins/joining.py +192 -0
- sway/queries/mixins/mutation.py +769 -0
- sway/queries/mixins/pagination.py +143 -0
- sway/queries/q.py +253 -0
- sway/queries/raw.py +113 -0
- sway/queries/select.py +769 -0
- sway/queries/typed.py +213 -0
- sway_orm-1.0.0.dist-info/METADATA +126 -0
- sway_orm-1.0.0.dist-info/RECORD +73 -0
- sway_orm-1.0.0.dist-info/WHEEL +4 -0
- sway_orm-1.0.0.dist-info/entry_points.txt +2 -0
- sway_orm-1.0.0.dist-info/licenses/LICENSE.md +22 -0
- sway_orm-1.0.0.dist-info/licenses/vendor/sqlx-postgres/LICENSE-APACHE +203 -0
- sway_orm-1.0.0.dist-info/licenses/vendor/sqlx-postgres/LICENSE-MIT +27 -0
- sway_orm-1.0.0.dist-info/sboms/sway-core-py.cyclonedx.json +7643 -0
sway/__init__.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""Sway ORM - High-performance async Python ORM with Rust core.
|
|
2
|
+
|
|
3
|
+
This is the main entry point for the Sway ORM library. It re-exports
|
|
4
|
+
all public APIs from submodules for convenient access.
|
|
5
|
+
|
|
6
|
+
Public API:
|
|
7
|
+
Models:
|
|
8
|
+
Model: Base class for all ORM models (Pydantic v2 based).
|
|
9
|
+
Field: Field configuration with db_pk, db_index, db_default, etc.
|
|
10
|
+
Index: Composite index decorator for models.
|
|
11
|
+
Check: CHECK constraint decorator for models.
|
|
12
|
+
|
|
13
|
+
Queries:
|
|
14
|
+
Query: SELECT query builder with Django-like filter syntax.
|
|
15
|
+
QueryManager: Model.objects manager for CRUD operations.
|
|
16
|
+
Q: Boolean expressions for complex filters (AND/OR/NOT).
|
|
17
|
+
F: Database-side field references for expressions.
|
|
18
|
+
|
|
19
|
+
Aggregates:
|
|
20
|
+
Count, Sum, Avg, Max, Min: SQL aggregate functions.
|
|
21
|
+
Coalesce, Concat, Greatest, Lower, Upper, Value: scalar expressions
|
|
22
|
+
(annotate(), update(), and Index() expression fields).
|
|
23
|
+
RawSQL: Raw SQL expression wrapper.
|
|
24
|
+
|
|
25
|
+
Database:
|
|
26
|
+
db: Database module with init(), close(), connect(), lifespan().
|
|
27
|
+
AsyncDatabase: Connection pool wrapper.
|
|
28
|
+
PoolSettings: Pool configuration (max_connections, timeouts).
|
|
29
|
+
atomic: Transaction context manager (decorator or async with).
|
|
30
|
+
|
|
31
|
+
Exceptions:
|
|
32
|
+
SwayError: Base exception for all Sway errors.
|
|
33
|
+
NotFoundError: Raised when get() finds no results.
|
|
34
|
+
MultipleObjectsReturned: Raised when get() finds multiple results.
|
|
35
|
+
IntegrityError: Raised on constraint violations.
|
|
36
|
+
FieldError, FieldLookupError, ManagerError: Validation errors.
|
|
37
|
+
|
|
38
|
+
Example:
|
|
39
|
+
from sway import Model, Field, db
|
|
40
|
+
|
|
41
|
+
class User(Model):
|
|
42
|
+
id: int | None = Field(default=None, db_pk=True)
|
|
43
|
+
name: str
|
|
44
|
+
|
|
45
|
+
class Meta:
|
|
46
|
+
is_table = True
|
|
47
|
+
|
|
48
|
+
async with db.connect("postgresql://localhost/app"):
|
|
49
|
+
user = await User.objects.create(name="Alice")
|
|
50
|
+
users = await User.objects.filter(name__startswith="A").all()
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
import warnings
|
|
54
|
+
|
|
55
|
+
from sway import db
|
|
56
|
+
from sway.db import (
|
|
57
|
+
AsyncDatabase,
|
|
58
|
+
PoolSettings,
|
|
59
|
+
TransactionTimeoutError,
|
|
60
|
+
atomic,
|
|
61
|
+
create_tables,
|
|
62
|
+
disconnect_all,
|
|
63
|
+
drop_tables,
|
|
64
|
+
get_connection,
|
|
65
|
+
register_connection,
|
|
66
|
+
)
|
|
67
|
+
from sway.exceptions import (
|
|
68
|
+
CheckViolationError,
|
|
69
|
+
FieldError,
|
|
70
|
+
FieldLookupError,
|
|
71
|
+
FieldLookupValueError,
|
|
72
|
+
ForeignKeyViolationError,
|
|
73
|
+
IntegrityError,
|
|
74
|
+
ManagerError,
|
|
75
|
+
MultipleObjectsReturned,
|
|
76
|
+
NotFoundError,
|
|
77
|
+
NotNullViolationError,
|
|
78
|
+
SwayError,
|
|
79
|
+
UniqueViolationError,
|
|
80
|
+
)
|
|
81
|
+
from sway.models import Check, Field, Hypertable, Index, Model
|
|
82
|
+
from sway.queries import (
|
|
83
|
+
Avg,
|
|
84
|
+
Coalesce,
|
|
85
|
+
Concat,
|
|
86
|
+
Count,
|
|
87
|
+
F,
|
|
88
|
+
Greatest,
|
|
89
|
+
Lower,
|
|
90
|
+
Max,
|
|
91
|
+
Min,
|
|
92
|
+
Q,
|
|
93
|
+
Query,
|
|
94
|
+
QueryManager,
|
|
95
|
+
RawSQL,
|
|
96
|
+
Sum,
|
|
97
|
+
Upper,
|
|
98
|
+
Value,
|
|
99
|
+
execute_raw,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
__version__ = "1.0.0"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def __getattr__(name: str) -> type:
|
|
106
|
+
if name == "SwayModel":
|
|
107
|
+
warnings.warn(
|
|
108
|
+
"SwayModel is deprecated, use Model instead",
|
|
109
|
+
DeprecationWarning,
|
|
110
|
+
stacklevel=2,
|
|
111
|
+
)
|
|
112
|
+
return Model
|
|
113
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
__all__ = [
|
|
117
|
+
"Model",
|
|
118
|
+
"db",
|
|
119
|
+
"AsyncDatabase",
|
|
120
|
+
"PoolSettings",
|
|
121
|
+
"TransactionTimeoutError",
|
|
122
|
+
"register_connection",
|
|
123
|
+
"get_connection",
|
|
124
|
+
"disconnect_all",
|
|
125
|
+
"create_tables",
|
|
126
|
+
"drop_tables",
|
|
127
|
+
"Field",
|
|
128
|
+
"Index",
|
|
129
|
+
"Hypertable",
|
|
130
|
+
"Check",
|
|
131
|
+
"Query",
|
|
132
|
+
"QueryManager",
|
|
133
|
+
"SwayError",
|
|
134
|
+
"FieldError",
|
|
135
|
+
"FieldLookupError",
|
|
136
|
+
"FieldLookupValueError",
|
|
137
|
+
"ManagerError",
|
|
138
|
+
"NotFoundError",
|
|
139
|
+
"MultipleObjectsReturned",
|
|
140
|
+
"IntegrityError",
|
|
141
|
+
"UniqueViolationError",
|
|
142
|
+
"ForeignKeyViolationError",
|
|
143
|
+
"NotNullViolationError",
|
|
144
|
+
"CheckViolationError",
|
|
145
|
+
"atomic",
|
|
146
|
+
"F",
|
|
147
|
+
"Q",
|
|
148
|
+
"Count",
|
|
149
|
+
"Sum",
|
|
150
|
+
"Avg",
|
|
151
|
+
"Max",
|
|
152
|
+
"Min",
|
|
153
|
+
"Concat",
|
|
154
|
+
"Coalesce",
|
|
155
|
+
"Greatest",
|
|
156
|
+
"Lower",
|
|
157
|
+
"Upper",
|
|
158
|
+
"Value",
|
|
159
|
+
"RawSQL",
|
|
160
|
+
"execute_raw",
|
|
161
|
+
]
|
sway/_msgpack.py
ADDED
|
Binary file
|
sway/cli/__init__.py
ADDED
sway/cli/app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""Main CLI application and shared utilities."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
|
|
7
|
+
from sway.migrations.config import SwayConfig, load_config
|
|
8
|
+
|
|
9
|
+
# Main application
|
|
10
|
+
app = typer.Typer(
|
|
11
|
+
name="sway",
|
|
12
|
+
help="Sway ORM - Database migration and management tool",
|
|
13
|
+
no_args_is_help=True,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_config_or_exit() -> SwayConfig:
|
|
18
|
+
"""Load config from sway_config.py or exit with error.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
SwayConfig instance
|
|
22
|
+
"""
|
|
23
|
+
config = load_config()
|
|
24
|
+
if config is None:
|
|
25
|
+
typer.secho(
|
|
26
|
+
"❌ No sway_config.py found",
|
|
27
|
+
fg=typer.colors.RED,
|
|
28
|
+
)
|
|
29
|
+
typer.echo()
|
|
30
|
+
typer.echo("Run 'sway init' to create configuration.")
|
|
31
|
+
raise typer.Exit(1)
|
|
32
|
+
|
|
33
|
+
if not config.models:
|
|
34
|
+
typer.secho(
|
|
35
|
+
"❌ No models configured in sway_config.py",
|
|
36
|
+
fg=typer.colors.RED,
|
|
37
|
+
)
|
|
38
|
+
typer.echo()
|
|
39
|
+
typer.echo("Add models to sway_config.py:")
|
|
40
|
+
typer.secho(' MODELS = ["models", "app.models"]', fg=typer.colors.CYAN)
|
|
41
|
+
raise typer.Exit(1)
|
|
42
|
+
|
|
43
|
+
return config
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def require_databases(config: SwayConfig, db_alias: str = "default") -> None:
|
|
47
|
+
"""Validate that databases are configured and alias exists.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
config: SwayConfig instance
|
|
51
|
+
db_alias: Database alias to check
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
typer.Exit: If validation fails
|
|
55
|
+
"""
|
|
56
|
+
if not config.databases:
|
|
57
|
+
typer.secho(
|
|
58
|
+
"❌ No databases configured in sway_config.py",
|
|
59
|
+
fg=typer.colors.RED,
|
|
60
|
+
)
|
|
61
|
+
typer.echo()
|
|
62
|
+
typer.echo("Add DATABASES to sway_config.py:")
|
|
63
|
+
typer.secho(
|
|
64
|
+
' DATABASES = {"default": "postgresql://localhost/mydb"}',
|
|
65
|
+
fg=typer.colors.CYAN,
|
|
66
|
+
)
|
|
67
|
+
raise typer.Exit(1)
|
|
68
|
+
|
|
69
|
+
if db_alias not in config.databases:
|
|
70
|
+
typer.secho(
|
|
71
|
+
f"❌ Database '{db_alias}' not found in sway_config.py",
|
|
72
|
+
fg=typer.colors.RED,
|
|
73
|
+
)
|
|
74
|
+
typer.echo()
|
|
75
|
+
typer.echo(f"Available databases: {list(config.databases.keys())}")
|
|
76
|
+
raise typer.Exit(1)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
async def init_databases(databases: dict[str, str]) -> None:
|
|
80
|
+
"""Initialize database connections from config.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
databases: Dict mapping alias to connection URL
|
|
84
|
+
"""
|
|
85
|
+
from sway.db import AsyncDatabase
|
|
86
|
+
|
|
87
|
+
for name, url in databases.items():
|
|
88
|
+
db = AsyncDatabase(url, name=name)
|
|
89
|
+
await db.connect()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def ensure_migrations_dir(migrations_dir: str | Path, dry_run: bool = False) -> Path:
|
|
93
|
+
"""Ensure migrations directory exists.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
migrations_dir: Path to migrations directory
|
|
97
|
+
dry_run: If True, don't actually create directory
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Path to migrations directory
|
|
101
|
+
"""
|
|
102
|
+
path = Path(migrations_dir)
|
|
103
|
+
if not path.exists():
|
|
104
|
+
typer.echo(f" 📁 Creating migrations directory: {path.absolute()}")
|
|
105
|
+
if not dry_run:
|
|
106
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
107
|
+
return path
|
sway/cli/codegen.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Code generation commands (generate-stubs)."""
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
|
|
5
|
+
from sway.cli.app import app, load_config_or_exit
|
|
6
|
+
from sway.migrations.config import import_models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@app.command(name="generate-stubs")
|
|
10
|
+
def generate_stubs() -> None:
|
|
11
|
+
"""
|
|
12
|
+
Generate .pyi stub files for all registered table models.
|
|
13
|
+
|
|
14
|
+
Creates type stub files with autocomplete support for QuerySet filter/exclude
|
|
15
|
+
methods with all field lookups (contains, gt, gte, etc.).
|
|
16
|
+
"""
|
|
17
|
+
from sway.codegen import generate_stubs_for_models, write_stubs
|
|
18
|
+
from sway.models.registry import registered_tables
|
|
19
|
+
|
|
20
|
+
# Load config and import models
|
|
21
|
+
config = load_config_or_exit()
|
|
22
|
+
|
|
23
|
+
typer.echo("🔧 Generating type stubs...")
|
|
24
|
+
typer.echo()
|
|
25
|
+
|
|
26
|
+
# Import models first
|
|
27
|
+
imported = import_models(config.models)
|
|
28
|
+
if imported == 0:
|
|
29
|
+
typer.secho("❌ No modules imported", fg=typer.colors.RED)
|
|
30
|
+
raise typer.Exit(1)
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
# Get all registered table models
|
|
34
|
+
models = list(registered_tables().values())
|
|
35
|
+
|
|
36
|
+
if not models:
|
|
37
|
+
typer.secho("⚠️ No table models found", fg=typer.colors.YELLOW)
|
|
38
|
+
typer.echo("Make sure your models have 'class Meta: is_table = True'")
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
typer.echo(f"Found {len(models)} table model(s):")
|
|
42
|
+
for model in models:
|
|
43
|
+
typer.echo(f" - {model.__module__}.{model.__name__}")
|
|
44
|
+
|
|
45
|
+
typer.echo()
|
|
46
|
+
typer.echo("Generating stubs...")
|
|
47
|
+
|
|
48
|
+
# Generate stubs
|
|
49
|
+
stub_mapping = generate_stubs_for_models(models)
|
|
50
|
+
|
|
51
|
+
# Write to disk
|
|
52
|
+
write_stubs(stub_mapping)
|
|
53
|
+
|
|
54
|
+
typer.echo()
|
|
55
|
+
typer.secho(
|
|
56
|
+
f"✅ Generated {len(stub_mapping)} stub file(s)",
|
|
57
|
+
fg=typer.colors.GREEN,
|
|
58
|
+
bold=True,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
except Exception as e:
|
|
62
|
+
typer.secho(f"❌ Error generating stubs: {e}", fg=typer.colors.RED)
|
|
63
|
+
import traceback
|
|
64
|
+
|
|
65
|
+
traceback.print_exc()
|
|
66
|
+
raise typer.Exit(1)
|
sway/cli/config.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Configuration commands (init)."""
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
|
|
5
|
+
from sway.cli.app import app
|
|
6
|
+
from sway.migrations.config import load_config, save_config
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@app.command()
|
|
10
|
+
def init() -> None:
|
|
11
|
+
"""
|
|
12
|
+
Initialize Sway configuration.
|
|
13
|
+
|
|
14
|
+
Interactively asks for configuration values and creates sway_config.py.
|
|
15
|
+
"""
|
|
16
|
+
typer.echo("🔧 Initializing Sway configuration...")
|
|
17
|
+
typer.echo()
|
|
18
|
+
|
|
19
|
+
# Check if config already exists
|
|
20
|
+
existing = load_config()
|
|
21
|
+
if existing:
|
|
22
|
+
typer.secho("Found existing sway_config.py:", fg=typer.colors.YELLOW)
|
|
23
|
+
typer.echo(f" MODELS = {existing.models}")
|
|
24
|
+
typer.echo(f" DIALECT = {existing.dialect}")
|
|
25
|
+
typer.echo(f" MIGRATIONS_DIR = {existing.migrations_dir}")
|
|
26
|
+
typer.echo(f" DATABASES = {existing.databases}")
|
|
27
|
+
typer.echo()
|
|
28
|
+
if not typer.confirm("Overwrite existing configuration?"):
|
|
29
|
+
typer.echo("Aborted.")
|
|
30
|
+
raise typer.Exit(0)
|
|
31
|
+
typer.echo()
|
|
32
|
+
|
|
33
|
+
# Ask for models
|
|
34
|
+
typer.echo("📦 Models configuration")
|
|
35
|
+
typer.echo(" Enter Python modules containing your Model classes.")
|
|
36
|
+
typer.echo(" Examples: 'models', 'app.models', 'myapp.db.models'")
|
|
37
|
+
typer.echo()
|
|
38
|
+
|
|
39
|
+
models_input = typer.prompt(
|
|
40
|
+
" Models module(s)",
|
|
41
|
+
default="models",
|
|
42
|
+
)
|
|
43
|
+
# Parse comma or space separated list
|
|
44
|
+
models = [m.strip() for m in models_input.replace(",", " ").split() if m.strip()]
|
|
45
|
+
|
|
46
|
+
typer.echo()
|
|
47
|
+
|
|
48
|
+
dialect = "postgres"
|
|
49
|
+
|
|
50
|
+
# Ask for database URL
|
|
51
|
+
typer.echo("🔗 Database connection")
|
|
52
|
+
default_url = typer.prompt(
|
|
53
|
+
" Database URL",
|
|
54
|
+
default="postgresql://localhost/mydb",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
typer.echo()
|
|
58
|
+
|
|
59
|
+
# Ask for migrations directory
|
|
60
|
+
migrations_dir = typer.prompt(
|
|
61
|
+
"📁 Migrations directory",
|
|
62
|
+
default="migrations",
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
typer.echo()
|
|
66
|
+
|
|
67
|
+
# Save config
|
|
68
|
+
databases = {"default": default_url}
|
|
69
|
+
save_config(
|
|
70
|
+
models=models,
|
|
71
|
+
dialect=dialect,
|
|
72
|
+
migrations_dir=migrations_dir,
|
|
73
|
+
databases=databases,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
typer.secho(
|
|
77
|
+
"✅ Configuration saved to sway_config.py", fg=typer.colors.GREEN, bold=True
|
|
78
|
+
)
|
|
79
|
+
typer.echo()
|
|
80
|
+
typer.echo("Next steps:")
|
|
81
|
+
typer.echo(" 1. Edit sway_config.py to adjust settings if needed")
|
|
82
|
+
typer.echo(" 2. Create your models in the specified module")
|
|
83
|
+
typer.echo(" 3. Run 'sway makemigrations' to generate migrations")
|
|
84
|
+
typer.echo(" 4. Run 'sway migrate' to apply migrations")
|