zero-server 0.0.1__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.
- zero_server-0.0.1/LICENSE +202 -0
- zero_server-0.0.1/PKG-INFO +134 -0
- zero_server-0.0.1/README.md +102 -0
- zero_server-0.0.1/pyproject.toml +111 -0
- zero_server-0.0.1/pyproject.toml.orig +77 -0
- zero_server-0.0.1/src/zero_server/__init__.py +36 -0
- zero_server-0.0.1/src/zero_server/codegen.py +133 -0
- zero_server-0.0.1/src/zero_server/integrations/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/integrations/starlette.py +115 -0
- zero_server-0.0.1/src/zero_server/mutators.py +87 -0
- zero_server-0.0.1/src/zero_server/protocol/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/protocol/application_error.py +36 -0
- zero_server-0.0.1/src/zero_server/protocol/ast.py +342 -0
- zero_server-0.0.1/src/zero_server/protocol/custom_queries.py +51 -0
- zero_server-0.0.1/src/zero_server/protocol/error.py +119 -0
- zero_server-0.0.1/src/zero_server/protocol/mutate_server.py +33 -0
- zero_server-0.0.1/src/zero_server/protocol/mutation.py +127 -0
- zero_server-0.0.1/src/zero_server/protocol/mutation_id.py +12 -0
- zero_server-0.0.1/src/zero_server/protocol/push.py +25 -0
- zero_server-0.0.1/src/zero_server/protocol/query_server.py +29 -0
- zero_server-0.0.1/src/zero_server/protocol/wire.py +88 -0
- zero_server-0.0.1/src/zero_server/py.typed +0 -0
- zero_server-0.0.1/src/zero_server/query/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/query/builder.py +338 -0
- zero_server-0.0.1/src/zero_server/query/expression.py +167 -0
- zero_server-0.0.1/src/zero_server/query/registry.py +143 -0
- zero_server-0.0.1/src/zero_server/schema.py +142 -0
- zero_server-0.0.1/src/zero_server/server/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/server/adapters/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/server/adapters/asyncpg.py +43 -0
- zero_server-0.0.1/src/zero_server/server/adapters/psycopg.py +55 -0
- zero_server-0.0.1/src/zero_server/server/adapters/sqlalchemy.py +49 -0
- zero_server-0.0.1/src/zero_server/server/crud.py +134 -0
- zero_server-0.0.1/src/zero_server/server/pg_query_executor.py +70 -0
- zero_server-0.0.1/src/zero_server/server/pg_types.py +69 -0
- zero_server-0.0.1/src/zero_server/server/process_mutations.py +412 -0
- zero_server-0.0.1/src/zero_server/server/process_queries.py +109 -0
- zero_server-0.0.1/src/zero_server/server/push_processor.py +49 -0
- zero_server-0.0.1/src/zero_server/server/server_schema.py +165 -0
- zero_server-0.0.1/src/zero_server/server/transaction.py +70 -0
- zero_server-0.0.1/src/zero_server/server/zql_database.py +129 -0
- zero_server-0.0.1/src/zero_server/shared/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/shared/asserts.py +19 -0
- zero_server-0.0.1/src/zero_server/shared/error.py +97 -0
- zero_server-0.0.1/src/zero_server/shared/js.py +121 -0
- zero_server-0.0.1/src/zero_server/shared/json.py +27 -0
- zero_server-0.0.1/src/zero_server/shared/registry.py +64 -0
- zero_server-0.0.1/src/zero_server/z2s/__init__.py +1 -0
- zero_server-0.0.1/src/zero_server/z2s/compiler.py +375 -0
- zero_server-0.0.1/src/zero_server/z2s/sql.py +130 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zero-server
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Community Python server SDK for Rocicorp Zero. Not affiliated with Rocicorp.
|
|
5
|
+
Keywords: zero,rocicorp,sync,zql
|
|
6
|
+
Author: Agent Labs
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 1 - Planning
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Dist: pydantic>=2.14
|
|
15
|
+
Requires-Dist: typing-extensions>=4.12
|
|
16
|
+
Requires-Dist: asyncpg>=0.32 ; extra == 'asyncpg'
|
|
17
|
+
Requires-Dist: psycopg>=3.3 ; extra == 'psycopg'
|
|
18
|
+
Requires-Dist: psycopg-pool>=3.3 ; extra == 'psycopg'
|
|
19
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.1 ; extra == 'sqlalchemy'
|
|
20
|
+
Requires-Dist: asyncpg>=0.32 ; extra == 'sqlalchemy'
|
|
21
|
+
Requires-Dist: starlette>=1.8 ; extra == 'starlette'
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Project-URL: Homepage, https://github.com/agent-labs-dev/zero-python
|
|
24
|
+
Project-URL: Source, https://github.com/agent-labs-dev/zero-python
|
|
25
|
+
Project-URL: Issues, https://github.com/agent-labs-dev/zero-python/issues
|
|
26
|
+
Project-URL: Changelog, https://github.com/agent-labs-dev/zero-python/releases
|
|
27
|
+
Provides-Extra: asyncpg
|
|
28
|
+
Provides-Extra: psycopg
|
|
29
|
+
Provides-Extra: sqlalchemy
|
|
30
|
+
Provides-Extra: starlette
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# zero-server
|
|
34
|
+
|
|
35
|
+
A community Python server SDK for [Zero](https://zero.rocicorp.dev/). It is not affiliated with or endorsed by Rocicorp.
|
|
36
|
+
|
|
37
|
+
It ports what `@rocicorp/zero/server` gives a TypeScript backend: the query endpoint (`ZERO_QUERY_URL`), the mutate endpoint (`ZERO_MUTATE_URL`), the ZQL query builder, `defineQueries` and `defineMutators`, and `tx.mutate` and `tx.run` on Postgres. The client and `zero-cache` stay in TypeScript, and a Python backend serves the same TypeScript client a TypeScript backend would.
|
|
38
|
+
|
|
39
|
+
Compatibility is pinned to one Zero release at a time. This version is tested against `@rocicorp/zero` **1.10.0** (see `ZERO_VERSION`).
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
pip install "zero-server[sqlalchemy,starlette]" # or [asyncpg], or [psycopg]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The schema stays in your TypeScript `schema.ts`, because the client needs it. Emit it as JSON, and generate Python types from it:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// emit-schema.ts
|
|
51
|
+
import {schema} from './schema.ts';
|
|
52
|
+
process.stdout.write(JSON.stringify(schema));
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
node emit-schema.ts > schema.json
|
|
57
|
+
zero-server codegen schema.json -o zero_schema.py
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`zero_schema.py` has a `TypedDict` per table for a row, an insert, an update and a key, and a builder and `tx.mutate` typed by them. A server then looks like this:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from pathlib import Path
|
|
64
|
+
|
|
65
|
+
from pydantic import BaseModel
|
|
66
|
+
from sqlalchemy.ext.asyncio import create_async_engine
|
|
67
|
+
from starlette.applications import Starlette
|
|
68
|
+
from zero_schema import Project, create_builder, mutate
|
|
69
|
+
|
|
70
|
+
from zero_server import ApplicationError, define_mutator, define_mutators, define_queries, define_query, load_schema
|
|
71
|
+
from zero_server.integrations.starlette import zero_routes
|
|
72
|
+
from zero_server.server.adapters.sqlalchemy import SQLAlchemyDatabase
|
|
73
|
+
|
|
74
|
+
schema = load_schema(Path("schema.json"))
|
|
75
|
+
zql = create_builder(schema)
|
|
76
|
+
db = SQLAlchemyDatabase(create_async_engine("postgresql+asyncpg://..."), schema)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@define_query
|
|
80
|
+
def my_projects(*, args, ctx):
|
|
81
|
+
return zql.project.where("ownerID", ctx["userID"]).related("tasks").order_by("name", "asc")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class NewTask(BaseModel):
|
|
85
|
+
id: str
|
|
86
|
+
projectID: str
|
|
87
|
+
title: str
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@define_mutator(NewTask)
|
|
91
|
+
async def create_task(*, tx, ctx, args: NewTask):
|
|
92
|
+
project: Project | None = await tx.run(zql.project.where("id", args.projectID).one())
|
|
93
|
+
if project is None or project["ownerID"] != ctx["userID"]:
|
|
94
|
+
raise ApplicationError("Not your project")
|
|
95
|
+
await mutate(tx).task.insert({"id": args.id, "projectID": args.projectID, "title": args.title})
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
app = Starlette(
|
|
99
|
+
routes=zero_routes(
|
|
100
|
+
schema=schema,
|
|
101
|
+
database=db,
|
|
102
|
+
queries=define_queries({"projects": {"mine": my_projects}}),
|
|
103
|
+
mutators=define_mutators({"task": {"create": create_task}}),
|
|
104
|
+
authenticate=verify_token, # your function: request -> user ID, or None when logged out
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Point `ZERO_QUERY_URL` and `ZERO_MUTATE_URL` at `/query` and `/mutate`. With FastAPI, `app.router.routes.extend(zero_routes(...))`. With another framework, call `handle_query_request` and `handle_mutate_request` yourself; `zero_server/integrations/starlette.py` is short enough to copy.
|
|
110
|
+
|
|
111
|
+
- **Databases.** `SQLAlchemyDatabase` (SQLAlchemy's asyncio engine on asyncpg), `AsyncpgDatabase` (an asyncpg pool) and `PsycopgDatabase` (a psycopg 3 async pool), under `zero_server.server.adapters`. Another driver plugs into `ZQLDatabase` as a `DBConnection` whose `query` takes `$1` placeholders.
|
|
112
|
+
- **Validation.** `@define_query(Model)` and `@define_mutator(Model)` parse args with a Pydantic model or `TypeAdapter` first; a failure reaches the client as upstream's validation error.
|
|
113
|
+
- **Strict building.** `create_builder(schema, strict=True)` refuses an unknown column when the query is built. Upstream only refuses it when the endpoint maps the query, failing the whole batch, so this is for your tests.
|
|
114
|
+
|
|
115
|
+
## How compatibility is checked
|
|
116
|
+
|
|
117
|
+
The published npm package is the definition of correct. Every test diffs Python against it:
|
|
118
|
+
|
|
119
|
+
- **Wire.** `harness/` runs the real `handleMutateRequest`, `handleQueryRequest`, `PushProcessor` and the query builder under Node. Hypothesis generates pushes (with duplicates, gaps, refusals and cleanup mutations) and builder programs, sends each through both sides against identical Postgres databases, and diffs the responses and every row written.
|
|
120
|
+
- **SQL.** The same generated queries run through upstream's `ZQLDatabase.run` and through `tx.run` against one seeded database, and the result rows are compared, never the SQL text.
|
|
121
|
+
|
|
122
|
+
The wire and SQL tiers run against every adapter.
|
|
123
|
+
- **End to end.** `e2e/run.py` starts Postgres, two `rocicorp/zero` containers at the pinned version, the TypeScript reference app server and the Python one. An unmodified Zero client runs the same script through each, and the run fails if the client ends up with different data, the databases differ, or the reference run strays from `e2e/expected.json`.
|
|
124
|
+
|
|
125
|
+
Run them with `uv run pytest` (needs Node 24 and Docker, or `ZERO_SERVER_PG` pointing at a Postgres) and `uv run --group e2e python e2e/run.py`.
|
|
126
|
+
|
|
127
|
+
## Known differences
|
|
128
|
+
|
|
129
|
+
- **Parse errors.** When a request is malformed, the message after `Failed to parse push body:` describes the bad field in Pydantic's words, not valita's. The rest of the response matches, and zero-cache only logs it.
|
|
130
|
+
- **Non-string values against text columns.** Upstream passes such a value to the JavaScript driver as is, so the driver decides how it binds. zero-server binds `String(value)`, which is what postgres.js does except for an array whose first element is a boolean, which it binds as `false`.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
Apache-2.0. Zero itself is Apache-2.0, and code ported from [rocicorp/mono](https://github.com/rocicorp/mono) keeps its notices.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# zero-server
|
|
2
|
+
|
|
3
|
+
A community Python server SDK for [Zero](https://zero.rocicorp.dev/). It is not affiliated with or endorsed by Rocicorp.
|
|
4
|
+
|
|
5
|
+
It ports what `@rocicorp/zero/server` gives a TypeScript backend: the query endpoint (`ZERO_QUERY_URL`), the mutate endpoint (`ZERO_MUTATE_URL`), the ZQL query builder, `defineQueries` and `defineMutators`, and `tx.mutate` and `tx.run` on Postgres. The client and `zero-cache` stay in TypeScript, and a Python backend serves the same TypeScript client a TypeScript backend would.
|
|
6
|
+
|
|
7
|
+
Compatibility is pinned to one Zero release at a time. This version is tested against `@rocicorp/zero` **1.10.0** (see `ZERO_VERSION`).
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pip install "zero-server[sqlalchemy,starlette]" # or [asyncpg], or [psycopg]
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The schema stays in your TypeScript `schema.ts`, because the client needs it. Emit it as JSON, and generate Python types from it:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// emit-schema.ts
|
|
19
|
+
import {schema} from './schema.ts';
|
|
20
|
+
process.stdout.write(JSON.stringify(schema));
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
node emit-schema.ts > schema.json
|
|
25
|
+
zero-server codegen schema.json -o zero_schema.py
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`zero_schema.py` has a `TypedDict` per table for a row, an insert, an update and a key, and a builder and `tx.mutate` typed by them. A server then looks like this:
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
from pydantic import BaseModel
|
|
34
|
+
from sqlalchemy.ext.asyncio import create_async_engine
|
|
35
|
+
from starlette.applications import Starlette
|
|
36
|
+
from zero_schema import Project, create_builder, mutate
|
|
37
|
+
|
|
38
|
+
from zero_server import ApplicationError, define_mutator, define_mutators, define_queries, define_query, load_schema
|
|
39
|
+
from zero_server.integrations.starlette import zero_routes
|
|
40
|
+
from zero_server.server.adapters.sqlalchemy import SQLAlchemyDatabase
|
|
41
|
+
|
|
42
|
+
schema = load_schema(Path("schema.json"))
|
|
43
|
+
zql = create_builder(schema)
|
|
44
|
+
db = SQLAlchemyDatabase(create_async_engine("postgresql+asyncpg://..."), schema)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@define_query
|
|
48
|
+
def my_projects(*, args, ctx):
|
|
49
|
+
return zql.project.where("ownerID", ctx["userID"]).related("tasks").order_by("name", "asc")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class NewTask(BaseModel):
|
|
53
|
+
id: str
|
|
54
|
+
projectID: str
|
|
55
|
+
title: str
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@define_mutator(NewTask)
|
|
59
|
+
async def create_task(*, tx, ctx, args: NewTask):
|
|
60
|
+
project: Project | None = await tx.run(zql.project.where("id", args.projectID).one())
|
|
61
|
+
if project is None or project["ownerID"] != ctx["userID"]:
|
|
62
|
+
raise ApplicationError("Not your project")
|
|
63
|
+
await mutate(tx).task.insert({"id": args.id, "projectID": args.projectID, "title": args.title})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
app = Starlette(
|
|
67
|
+
routes=zero_routes(
|
|
68
|
+
schema=schema,
|
|
69
|
+
database=db,
|
|
70
|
+
queries=define_queries({"projects": {"mine": my_projects}}),
|
|
71
|
+
mutators=define_mutators({"task": {"create": create_task}}),
|
|
72
|
+
authenticate=verify_token, # your function: request -> user ID, or None when logged out
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Point `ZERO_QUERY_URL` and `ZERO_MUTATE_URL` at `/query` and `/mutate`. With FastAPI, `app.router.routes.extend(zero_routes(...))`. With another framework, call `handle_query_request` and `handle_mutate_request` yourself; `zero_server/integrations/starlette.py` is short enough to copy.
|
|
78
|
+
|
|
79
|
+
- **Databases.** `SQLAlchemyDatabase` (SQLAlchemy's asyncio engine on asyncpg), `AsyncpgDatabase` (an asyncpg pool) and `PsycopgDatabase` (a psycopg 3 async pool), under `zero_server.server.adapters`. Another driver plugs into `ZQLDatabase` as a `DBConnection` whose `query` takes `$1` placeholders.
|
|
80
|
+
- **Validation.** `@define_query(Model)` and `@define_mutator(Model)` parse args with a Pydantic model or `TypeAdapter` first; a failure reaches the client as upstream's validation error.
|
|
81
|
+
- **Strict building.** `create_builder(schema, strict=True)` refuses an unknown column when the query is built. Upstream only refuses it when the endpoint maps the query, failing the whole batch, so this is for your tests.
|
|
82
|
+
|
|
83
|
+
## How compatibility is checked
|
|
84
|
+
|
|
85
|
+
The published npm package is the definition of correct. Every test diffs Python against it:
|
|
86
|
+
|
|
87
|
+
- **Wire.** `harness/` runs the real `handleMutateRequest`, `handleQueryRequest`, `PushProcessor` and the query builder under Node. Hypothesis generates pushes (with duplicates, gaps, refusals and cleanup mutations) and builder programs, sends each through both sides against identical Postgres databases, and diffs the responses and every row written.
|
|
88
|
+
- **SQL.** The same generated queries run through upstream's `ZQLDatabase.run` and through `tx.run` against one seeded database, and the result rows are compared, never the SQL text.
|
|
89
|
+
|
|
90
|
+
The wire and SQL tiers run against every adapter.
|
|
91
|
+
- **End to end.** `e2e/run.py` starts Postgres, two `rocicorp/zero` containers at the pinned version, the TypeScript reference app server and the Python one. An unmodified Zero client runs the same script through each, and the run fails if the client ends up with different data, the databases differ, or the reference run strays from `e2e/expected.json`.
|
|
92
|
+
|
|
93
|
+
Run them with `uv run pytest` (needs Node 24 and Docker, or `ZERO_SERVER_PG` pointing at a Postgres) and `uv run --group e2e python e2e/run.py`.
|
|
94
|
+
|
|
95
|
+
## Known differences
|
|
96
|
+
|
|
97
|
+
- **Parse errors.** When a request is malformed, the message after `Failed to parse push body:` describes the bad field in Pydantic's words, not valita's. The rest of the response matches, and zero-cache only logs it.
|
|
98
|
+
- **Non-string values against text columns.** Upstream passes such a value to the JavaScript driver as is, so the driver decides how it binds. zero-server binds `String(value)`, which is what postgres.js does except for an array whose first element is a boolean, which it binds as `false`.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
Apache-2.0. Zero itself is Apache-2.0, and code ported from [rocicorp/mono](https://github.com/rocicorp/mono) keeps its notices.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "zero-server"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Community Python server SDK for Rocicorp Zero. Not affiliated with Rocicorp."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
keywords = [
|
|
10
|
+
"zero",
|
|
11
|
+
"rocicorp",
|
|
12
|
+
"sync",
|
|
13
|
+
"zql",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 1 - Planning",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Typing :: Typed",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"pydantic>=2.14",
|
|
24
|
+
"typing-extensions>=4.12",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[[project.authors]]
|
|
28
|
+
name = "Agent Labs"
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/agent-labs-dev/zero-python"
|
|
32
|
+
Source = "https://github.com/agent-labs-dev/zero-python"
|
|
33
|
+
Issues = "https://github.com/agent-labs-dev/zero-python/issues"
|
|
34
|
+
Changelog = "https://github.com/agent-labs-dev/zero-python/releases"
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
zero-server = "zero_server.codegen:main"
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
sqlalchemy = [
|
|
41
|
+
"sqlalchemy[asyncio]>=2.1",
|
|
42
|
+
"asyncpg>=0.32",
|
|
43
|
+
]
|
|
44
|
+
asyncpg = ["asyncpg>=0.32"]
|
|
45
|
+
psycopg = [
|
|
46
|
+
"psycopg>=3.3",
|
|
47
|
+
"psycopg-pool>=3.3",
|
|
48
|
+
]
|
|
49
|
+
starlette = ["starlette>=1.8"]
|
|
50
|
+
|
|
51
|
+
[dependency-groups]
|
|
52
|
+
dev = [
|
|
53
|
+
"zero-server[sqlalchemy,asyncpg,psycopg,starlette]",
|
|
54
|
+
"httpx2>=2.13",
|
|
55
|
+
"psycopg[binary]>=3.3",
|
|
56
|
+
"hypothesis>=6.169",
|
|
57
|
+
"pytest>=9.1",
|
|
58
|
+
"pytest-asyncio>=1.4",
|
|
59
|
+
"pytest-cov>=7.1",
|
|
60
|
+
"ruff>=0.17",
|
|
61
|
+
"ty>=0.0.87",
|
|
62
|
+
]
|
|
63
|
+
e2e = [
|
|
64
|
+
"starlette>=1.8",
|
|
65
|
+
"uvicorn>=0.54",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[build-system]
|
|
69
|
+
requires = ["uv_build>=0.11,<0.12"]
|
|
70
|
+
build-backend = "uv_build"
|
|
71
|
+
|
|
72
|
+
[tool.ruff]
|
|
73
|
+
line-length = 120
|
|
74
|
+
target-version = "py312"
|
|
75
|
+
|
|
76
|
+
[tool.ruff.lint]
|
|
77
|
+
select = [
|
|
78
|
+
"E",
|
|
79
|
+
"F",
|
|
80
|
+
"W",
|
|
81
|
+
"I",
|
|
82
|
+
"B",
|
|
83
|
+
"UP",
|
|
84
|
+
"SIM",
|
|
85
|
+
"RUF",
|
|
86
|
+
"ASYNC",
|
|
87
|
+
"PT",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
[tool.pytest.ini_options]
|
|
91
|
+
testpaths = ["tests"]
|
|
92
|
+
asyncio_mode = "auto"
|
|
93
|
+
asyncio_default_fixture_loop_scope = "session"
|
|
94
|
+
asyncio_default_test_loop_scope = "session"
|
|
95
|
+
addopts = "--cov=zero_server --cov-branch --cov-report=term-missing"
|
|
96
|
+
|
|
97
|
+
[tool.coverage.run]
|
|
98
|
+
concurrency = [
|
|
99
|
+
"greenlet",
|
|
100
|
+
"thread",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
[tool.coverage.report]
|
|
104
|
+
fail_under = 100
|
|
105
|
+
skip_covered = true
|
|
106
|
+
exclude_also = [
|
|
107
|
+
"if TYPE_CHECKING:",
|
|
108
|
+
"@overload",
|
|
109
|
+
"raise NotImplementedError",
|
|
110
|
+
'\.\.\.',
|
|
111
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "zero-server"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Community Python server SDK for Rocicorp Zero. Not affiliated with Rocicorp."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
authors = [{ name = "Agent Labs" }]
|
|
10
|
+
keywords = ["zero", "rocicorp", "sync", "zql"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 1 - Planning",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
"Programming Language :: Python :: 3.13",
|
|
16
|
+
"Typing :: Typed",
|
|
17
|
+
]
|
|
18
|
+
dependencies = ["pydantic>=2.14", "typing-extensions>=4.12"]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/agent-labs-dev/zero-python"
|
|
22
|
+
Source = "https://github.com/agent-labs-dev/zero-python"
|
|
23
|
+
Issues = "https://github.com/agent-labs-dev/zero-python/issues"
|
|
24
|
+
Changelog = "https://github.com/agent-labs-dev/zero-python/releases"
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
zero-server = "zero_server.codegen:main"
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
sqlalchemy = ["sqlalchemy[asyncio]>=2.1", "asyncpg>=0.32"]
|
|
31
|
+
asyncpg = ["asyncpg>=0.32"]
|
|
32
|
+
psycopg = ["psycopg>=3.3", "psycopg-pool>=3.3"]
|
|
33
|
+
starlette = ["starlette>=1.8"]
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
dev = [
|
|
37
|
+
"zero-server[sqlalchemy,asyncpg,psycopg,starlette]",
|
|
38
|
+
"httpx2>=2.13",
|
|
39
|
+
# psycopg's prebuilt libpq, so the tests need no system libpq.
|
|
40
|
+
"psycopg[binary]>=3.3",
|
|
41
|
+
"hypothesis>=6.169",
|
|
42
|
+
"pytest>=9.1",
|
|
43
|
+
"pytest-asyncio>=1.4",
|
|
44
|
+
"pytest-cov>=7.1",
|
|
45
|
+
"ruff>=0.17",
|
|
46
|
+
"ty>=0.0.87",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
# The Python app server of the end-to-end tier.
|
|
50
|
+
e2e = ["starlette>=1.8", "uvicorn>=0.54"]
|
|
51
|
+
|
|
52
|
+
[build-system]
|
|
53
|
+
requires = ["uv_build>=0.11,<0.12"]
|
|
54
|
+
build-backend = "uv_build"
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
line-length = 120
|
|
58
|
+
target-version = "py312"
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF", "ASYNC", "PT"]
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
testpaths = ["tests"]
|
|
65
|
+
asyncio_mode = "auto"
|
|
66
|
+
asyncio_default_fixture_loop_scope = "session"
|
|
67
|
+
asyncio_default_test_loop_scope = "session"
|
|
68
|
+
addopts = "--cov=zero_server --cov-branch --cov-report=term-missing"
|
|
69
|
+
|
|
70
|
+
[tool.coverage.run]
|
|
71
|
+
# SQLAlchemy's asyncio layer resumes coroutines inside greenlets.
|
|
72
|
+
concurrency = ["greenlet", "thread"]
|
|
73
|
+
|
|
74
|
+
[tool.coverage.report]
|
|
75
|
+
fail_under = 100
|
|
76
|
+
skip_covered = true
|
|
77
|
+
exclude_also = ["if TYPE_CHECKING:", "@overload", "raise NotImplementedError", "\\.\\.\\."]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Community Python server SDK for Rocicorp Zero. Not affiliated with Rocicorp.
|
|
2
|
+
|
|
3
|
+
The SQLAlchemy transaction provider lives in `zero_server.server.adapters.sqlalchemy` and needs the
|
|
4
|
+
`zero-server[sqlalchemy]` extra.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from zero_server.mutators import define_mutator, define_mutators, get_mutator, must_get_mutator
|
|
8
|
+
from zero_server.protocol.application_error import ApplicationError
|
|
9
|
+
from zero_server.query.builder import Query, create_builder
|
|
10
|
+
from zero_server.query.registry import QueryParseError, define_queries, define_query, get_query, must_get_query
|
|
11
|
+
from zero_server.schema import Schema, load_schema
|
|
12
|
+
from zero_server.server.process_mutations import handle_mutate_request
|
|
13
|
+
from zero_server.server.process_queries import handle_query_request
|
|
14
|
+
from zero_server.server.push_processor import PushProcessor
|
|
15
|
+
|
|
16
|
+
__version__ = "0.0.1"
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"ApplicationError",
|
|
20
|
+
"PushProcessor",
|
|
21
|
+
"Query",
|
|
22
|
+
"QueryParseError",
|
|
23
|
+
"Schema",
|
|
24
|
+
"create_builder",
|
|
25
|
+
"define_mutator",
|
|
26
|
+
"define_mutators",
|
|
27
|
+
"define_queries",
|
|
28
|
+
"define_query",
|
|
29
|
+
"get_mutator",
|
|
30
|
+
"get_query",
|
|
31
|
+
"handle_mutate_request",
|
|
32
|
+
"handle_query_request",
|
|
33
|
+
"load_schema",
|
|
34
|
+
"must_get_mutator",
|
|
35
|
+
"must_get_query",
|
|
36
|
+
]
|