@palettelab/cli 0.3.22 → 0.3.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +274 -0
- package/backend-sdk/palette_sdk/__init__.py +5 -1
- package/backend-sdk/palette_sdk/lifecycle.py +58 -0
- package/backend-sdk/palette_sdk/plugin_context.py +26 -0
- package/backend-sdk/palette_sdk/repository.py +119 -0
- package/backend-sdk/pyproject.toml +1 -1
- package/lib/dev-simulator.js +2 -0
- package/package.json +1 -1
- package/template-fallback/package.json +1 -1
- package/template-fallback/templates/dashboard/package.json +1 -1
- package/template-fallback/templates/database/backend/api/main.py +2 -11
- package/template-fallback/templates/database/package.json +1 -1
- package/template-fallback/templates/external-service/package.json +1 -1
- package/template-fallback/templates/frontend-only/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,280 @@ npx @palettelab/cli <command>
|
|
|
21
21
|
pltt <command>
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
## Quick Start: Build And Test A Palette App
|
|
25
|
+
|
|
26
|
+
Use this flow when a developer wants to create an app, run it locally, then test
|
|
27
|
+
it inside the real Palette OS without Docker.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx --yes @palettelab/cli@latest init simple-todo --template database
|
|
31
|
+
cd simple-todo
|
|
32
|
+
npm install
|
|
33
|
+
npx --yes @palettelab/cli@latest dev
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`pltt dev` runs the local SDK simulator. It is the fastest loop for frontend,
|
|
37
|
+
backend, manifest, SDK hooks, and local database checks. It does not require
|
|
38
|
+
Docker and does not publish anything to the platform.
|
|
39
|
+
|
|
40
|
+
When the app is ready to test with real Palette OS services, configure a hosted
|
|
41
|
+
sandbox environment and run:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npx --yes @palettelab/cli@latest login \
|
|
45
|
+
--env staging \
|
|
46
|
+
--url https://YOUR-PALETTE-STAGING-URL \
|
|
47
|
+
--token pltt_xxxxx
|
|
48
|
+
|
|
49
|
+
npx --yes @palettelab/cli@latest dev --sandbox --env staging
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The hosted sandbox flow packages the app, uploads it to the configured Palette
|
|
53
|
+
environment, creates a preview publish, and prints a real OS preview URL. Use
|
|
54
|
+
that URL to test routing, OS shell behavior, login context, organization
|
|
55
|
+
context, Data Room APIs, storage, install/review behavior, logs, permissions,
|
|
56
|
+
and platform APIs.
|
|
57
|
+
|
|
58
|
+
## Staging URL
|
|
59
|
+
|
|
60
|
+
The staging URL is the base URL of the Palette platform backend/API
|
|
61
|
+
environment. The CLI appends the API paths itself, for example:
|
|
62
|
+
|
|
63
|
+
- `/api/v1/appstore/sign-upload`
|
|
64
|
+
- `/api/v1/appstore/publish`
|
|
65
|
+
- `/api/v1/developer/publish-tokens`
|
|
66
|
+
- `/api/superadmin/publish-tokens`
|
|
67
|
+
|
|
68
|
+
Use the same public origin only if that origin proxies API routes to the
|
|
69
|
+
backend. For example, `https://apps.pltt.xyz` is valid only when these paths are
|
|
70
|
+
served by the backend, not by the frontend catch-all route:
|
|
71
|
+
|
|
72
|
+
```text
|
|
73
|
+
/api/v1/*
|
|
74
|
+
/api/superadmin/*
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Validate a staging URL before giving it to developers:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
curl https://YOUR-PALETTE-STAGING-URL/api/v1/health
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A valid staging URL returns a backend health JSON response. If it returns the
|
|
84
|
+
Palette frontend HTML, it is a frontend-only URL and the CLI cannot publish to
|
|
85
|
+
it. In that case either use the real backend domain, for example
|
|
86
|
+
`https://api.your-domain.example`, or update the web server/reverse proxy so
|
|
87
|
+
`/api/v1/*` and `/api/superadmin/*` go to the backend.
|
|
88
|
+
|
|
89
|
+
## Publish Token
|
|
90
|
+
|
|
91
|
+
Every developer needs a publish token before they can use hosted sandbox or
|
|
92
|
+
publish commands. Tokens start with `pltt_`.
|
|
93
|
+
|
|
94
|
+
Developers can create their own token after logging in to Palette:
|
|
95
|
+
|
|
96
|
+
1. Open Palette OS in the browser.
|
|
97
|
+
2. Open Settings.
|
|
98
|
+
3. Go to Developer.
|
|
99
|
+
4. Create a developer publish token.
|
|
100
|
+
5. Copy the token immediately. The raw token is shown only once.
|
|
101
|
+
|
|
102
|
+
Superadmins can also allocate tokens from the superadmin publish-token section.
|
|
103
|
+
Use superadmin allocation for service accounts, CI, or developers who should
|
|
104
|
+
not create their own token.
|
|
105
|
+
|
|
106
|
+
Do not commit tokens to a repository. Store them through `pltt login` or an
|
|
107
|
+
environment variable.
|
|
108
|
+
|
|
109
|
+
## Configure A Hosted Sandbox
|
|
110
|
+
|
|
111
|
+
The recommended setup is `pltt login`, which writes
|
|
112
|
+
`~/.palette/config.json` with file mode `0600`:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npx --yes @palettelab/cli@latest login \
|
|
116
|
+
--env staging \
|
|
117
|
+
--url https://YOUR-PALETTE-STAGING-URL \
|
|
118
|
+
--token pltt_xxxxx
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
You can verify the saved environment with:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
cat ~/.palette/config.json
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
You can also use environment variables instead of storing the token:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
export PALETTE_STAGING_URL=https://YOUR-PALETTE-STAGING-URL
|
|
131
|
+
export PALETTE_STAGING_TOKEN=pltt_xxxxx
|
|
132
|
+
npx --yes @palettelab/cli@latest dev --sandbox --env staging
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Environment variable precedence:
|
|
136
|
+
|
|
137
|
+
1. `PALETTE_<ENV>_URL` and `PALETTE_<ENV>_TOKEN`
|
|
138
|
+
2. `PALETTE_PUBLISH_TOKEN` as a token fallback
|
|
139
|
+
3. `~/.palette/config.json`
|
|
140
|
+
4. `./palette.config.json` for repo-local overrides
|
|
141
|
+
|
|
142
|
+
## Test Inside The Real Palette OS Without Docker
|
|
143
|
+
|
|
144
|
+
Use hosted sandbox for real OS testing:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
npx --yes @palettelab/cli@latest dev --sandbox --env staging
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This is the correct flow for internal teams that need real platform features
|
|
151
|
+
without pushing the app to production approval and without running Docker on the
|
|
152
|
+
developer machine.
|
|
153
|
+
|
|
154
|
+
Expected behavior:
|
|
155
|
+
|
|
156
|
+
1. The CLI runs local contract checks.
|
|
157
|
+
2. The CLI bundles frontend and backend artifacts.
|
|
158
|
+
3. The CLI uploads the package to the staging Palette environment.
|
|
159
|
+
4. The platform creates a preview/review publish.
|
|
160
|
+
5. The CLI prints the preview URL, status command, and logs command.
|
|
161
|
+
6. The developer opens the preview URL inside the real Palette OS.
|
|
162
|
+
|
|
163
|
+
For developer sandboxes where manual review should not block testing, the
|
|
164
|
+
backend environment should run with:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
APPSTORE_AUTO_APPROVE_SANDBOX_PREVIEWS=true
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
With that backend setting enabled, passing sandbox preview publishes are marked
|
|
171
|
+
active automatically, so developers can test OS shell, auth, Data Room,
|
|
172
|
+
organization, install, permission, log, and platform API behavior immediately.
|
|
173
|
+
|
|
174
|
+
Useful follow-up commands:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
npx --yes @palettelab/cli@latest status --env staging
|
|
178
|
+
npx --yes @palettelab/cli@latest logs simple-todo --env staging --follow
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Local-Only Versus OS Testing
|
|
182
|
+
|
|
183
|
+
Use the right command for the kind of test you need:
|
|
184
|
+
|
|
185
|
+
| Goal | Command | Docker | Uses real OS services |
|
|
186
|
+
|---|---|---:|---:|
|
|
187
|
+
| Fast SDK/frontend/backend loop | `pltt dev` | No | No |
|
|
188
|
+
| Real OS preview in hosted cloud sandbox | `pltt dev --sandbox --env staging` | No | Yes |
|
|
189
|
+
| Alias for hosted cloud sandbox | `pltt dev --cloud --env staging` | No | Yes |
|
|
190
|
+
| Internal full local platform parity | `pltt dev --platform` | Yes | Local platform container |
|
|
191
|
+
| Production/review publish | `pltt publish --env production` | No | Yes |
|
|
192
|
+
|
|
193
|
+
For normal app developers, Docker is not required. Docker is only needed for
|
|
194
|
+
internal platform parity checks with `pltt dev --platform`.
|
|
195
|
+
|
|
196
|
+
## App-Owned Data, Migrations, And Python Backends
|
|
197
|
+
|
|
198
|
+
Palette apps can own their own Python backend, database tables, migrations, and
|
|
199
|
+
organization-scoped data. The generated database template uses this structure:
|
|
200
|
+
|
|
201
|
+
```text
|
|
202
|
+
my-app/
|
|
203
|
+
palette-plugin.json
|
|
204
|
+
frontend/src/index.tsx
|
|
205
|
+
backend/api/main.py
|
|
206
|
+
backend/api/models.py
|
|
207
|
+
backend/migrations/env.py
|
|
208
|
+
backend/migrations/versions/001_init.py
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Declare database ownership in `palette-plugin.json`:
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"capabilities": { "database": true },
|
|
216
|
+
"database": {
|
|
217
|
+
"schema": "app_my_app",
|
|
218
|
+
"migrations": "./backend/migrations"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Define org-scoped models with the backend SDK:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
from sqlalchemy import String
|
|
227
|
+
from sqlalchemy.orm import Mapped, mapped_column
|
|
228
|
+
from palette_sdk import OrgScopedTable
|
|
229
|
+
|
|
230
|
+
class Invoice(OrgScopedTable):
|
|
231
|
+
__tablename__ = "invoices"
|
|
232
|
+
|
|
233
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
234
|
+
customer_name: Mapped[str] = mapped_column(String(255))
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Use the migration helper in Alembic migrations:
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from palette_sdk.db import ensure_org_rls
|
|
241
|
+
|
|
242
|
+
def upgrade():
|
|
243
|
+
op.create_table(...)
|
|
244
|
+
ensure_org_rls(op, "invoices")
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Use `ctx.repo(Model)` for org-safe CRUD:
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
from fastapi import Depends
|
|
251
|
+
from palette_sdk import PluginContext, get_plugin_context
|
|
252
|
+
from models import Invoice
|
|
253
|
+
|
|
254
|
+
@router.get("/invoices")
|
|
255
|
+
async def list_invoices(ctx: PluginContext = Depends(get_plugin_context)):
|
|
256
|
+
rows = await ctx.repo(Invoice).list(order_by="-id")
|
|
257
|
+
return [{"id": r.id, "customer": r.customer_name} for r in rows]
|
|
258
|
+
|
|
259
|
+
@router.post("/invoices")
|
|
260
|
+
async def create_invoice(body: InvoiceIn, ctx: PluginContext = Depends(get_plugin_context)):
|
|
261
|
+
invoice = await ctx.repo(Invoice).create(**body.model_dump())
|
|
262
|
+
return {"id": invoice.id}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Backend SDK features for app-owned data:
|
|
266
|
+
|
|
267
|
+
- `PluginContext` exposes `user_id`, `organization_id`, `plugin_id`, `permissions`, `config`, `storage`, and `ctx.db`.
|
|
268
|
+
- `ctx.repo(Model)` gives org-safe CRUD helpers for app tables.
|
|
269
|
+
- `ctx.has_permission("...")` checks declared permissions.
|
|
270
|
+
- `ctx.config_value("key")` and `ctx.require_config("key")` read app install/config values.
|
|
271
|
+
- `ctx.secret("KEY")` reads app secrets from config or environment variables.
|
|
272
|
+
- `LifecycleHooks` lets apps define install/update/enable/disable/uninstall hooks.
|
|
273
|
+
- `OrgScopedTable` and `PluginBase` keep app data inside the plugin schema model set.
|
|
274
|
+
|
|
275
|
+
Lifecycle example:
|
|
276
|
+
|
|
277
|
+
```python
|
|
278
|
+
from palette_sdk import LifecycleHooks, PluginContext
|
|
279
|
+
|
|
280
|
+
lifecycle = LifecycleHooks()
|
|
281
|
+
|
|
282
|
+
@lifecycle.on_install
|
|
283
|
+
async def seed_defaults(ctx: PluginContext):
|
|
284
|
+
await ctx.repo(DefaultSetting).create(name="currency", value="USD")
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Run local checks before publishing:
|
|
288
|
+
|
|
289
|
+
```bash
|
|
290
|
+
npx --yes @palettelab/cli@latest test
|
|
291
|
+
npx --yes @palettelab/cli@latest package
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
The CLI validates manifest shape, SDK compatibility, frontend bundling, backend
|
|
295
|
+
imports, backend route permission gates, declared permissions, migration safety,
|
|
296
|
+
package dependency policy, and backend package size.
|
|
297
|
+
|
|
24
298
|
## Commands
|
|
25
299
|
|
|
26
300
|
### `pltt init <name>`
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from palette_sdk.plugin_router import PluginRouter
|
|
4
4
|
from palette_sdk.plugin_context import PluginContext, get_plugin_context
|
|
5
|
+
from palette_sdk.repository import OrgRepository
|
|
6
|
+
from palette_sdk.lifecycle import LifecycleHooks
|
|
5
7
|
from palette_sdk.tool_definition import ToolDefinition
|
|
6
8
|
from palette_sdk.manifest import PluginManifest, load_manifest
|
|
7
9
|
from palette_sdk.schemas import (
|
|
@@ -24,6 +26,8 @@ __all__ = [
|
|
|
24
26
|
"PluginRouter",
|
|
25
27
|
"PluginContext",
|
|
26
28
|
"get_plugin_context",
|
|
29
|
+
"OrgRepository",
|
|
30
|
+
"LifecycleHooks",
|
|
27
31
|
"ToolDefinition",
|
|
28
32
|
"PluginManifest",
|
|
29
33
|
"load_manifest",
|
|
@@ -45,4 +49,4 @@ __all__ = [
|
|
|
45
49
|
"route_permission_issues",
|
|
46
50
|
]
|
|
47
51
|
|
|
48
|
-
__version__ = "0.1.
|
|
52
|
+
__version__ = "0.1.4"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Lifecycle hook registry for install/update/uninstall behavior."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable, Callable
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
from palette_sdk.plugin_context import PluginContext
|
|
10
|
+
|
|
11
|
+
LifecycleEvent = Literal["install", "update", "enable", "disable", "uninstall"]
|
|
12
|
+
LifecycleHandler = Callable[[PluginContext], Awaitable[None]]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class LifecycleHooks:
|
|
17
|
+
"""Register optional app lifecycle hooks.
|
|
18
|
+
|
|
19
|
+
App backends can export a `lifecycle` object from `backend/api/main.py`.
|
|
20
|
+
The platform can call these hooks during install/update/uninstall, and tests
|
|
21
|
+
can call `run()` directly.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
_handlers: dict[LifecycleEvent, list[LifecycleHandler]] = field(
|
|
25
|
+
default_factory=lambda: {
|
|
26
|
+
"install": [],
|
|
27
|
+
"update": [],
|
|
28
|
+
"enable": [],
|
|
29
|
+
"disable": [],
|
|
30
|
+
"uninstall": [],
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def on(self, event: LifecycleEvent):
|
|
35
|
+
def decorator(fn: LifecycleHandler) -> LifecycleHandler:
|
|
36
|
+
self._handlers[event].append(fn)
|
|
37
|
+
return fn
|
|
38
|
+
|
|
39
|
+
return decorator
|
|
40
|
+
|
|
41
|
+
def on_install(self, fn: LifecycleHandler) -> LifecycleHandler:
|
|
42
|
+
return self.on("install")(fn)
|
|
43
|
+
|
|
44
|
+
def on_update(self, fn: LifecycleHandler) -> LifecycleHandler:
|
|
45
|
+
return self.on("update")(fn)
|
|
46
|
+
|
|
47
|
+
def on_enable(self, fn: LifecycleHandler) -> LifecycleHandler:
|
|
48
|
+
return self.on("enable")(fn)
|
|
49
|
+
|
|
50
|
+
def on_disable(self, fn: LifecycleHandler) -> LifecycleHandler:
|
|
51
|
+
return self.on("disable")(fn)
|
|
52
|
+
|
|
53
|
+
def on_uninstall(self, fn: LifecycleHandler) -> LifecycleHandler:
|
|
54
|
+
return self.on("uninstall")(fn)
|
|
55
|
+
|
|
56
|
+
async def run(self, event: LifecycleEvent, ctx: PluginContext) -> None:
|
|
57
|
+
for handler in self._handlers[event]:
|
|
58
|
+
await handler(ctx)
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass, field
|
|
6
|
+
import logging
|
|
7
|
+
import os
|
|
6
8
|
from typing import Any
|
|
7
9
|
|
|
8
10
|
from fastapi import Depends, Request
|
|
@@ -35,6 +37,29 @@ class PluginContext:
|
|
|
35
37
|
permissions: list[str] = field(default_factory=list)
|
|
36
38
|
storage: Any = None # Platform storage service injected at runtime
|
|
37
39
|
config: dict[str, Any] = field(default_factory=dict)
|
|
40
|
+
logger: logging.Logger = field(default_factory=lambda: logging.getLogger("palette_sdk.plugin"))
|
|
41
|
+
|
|
42
|
+
def has_permission(self, permission: str) -> bool:
|
|
43
|
+
return permission in self.permissions
|
|
44
|
+
|
|
45
|
+
def config_value(self, key: str, default: Any = None) -> Any:
|
|
46
|
+
return self.config.get(key, default)
|
|
47
|
+
|
|
48
|
+
def require_config(self, key: str) -> Any:
|
|
49
|
+
if key not in self.config or self.config[key] in (None, ""):
|
|
50
|
+
raise KeyError(f"missing plugin config value: {key}")
|
|
51
|
+
return self.config[key]
|
|
52
|
+
|
|
53
|
+
def secret(self, key: str, default: str | None = None) -> str | None:
|
|
54
|
+
secrets = self.config.get("secrets")
|
|
55
|
+
if isinstance(secrets, dict) and key in secrets:
|
|
56
|
+
return secrets[key]
|
|
57
|
+
return os.environ.get(key, default)
|
|
58
|
+
|
|
59
|
+
def repo(self, model: type[Any]):
|
|
60
|
+
from palette_sdk.repository import OrgRepository
|
|
61
|
+
|
|
62
|
+
return OrgRepository(self.db, model, self.organization_id)
|
|
38
63
|
|
|
39
64
|
|
|
40
65
|
async def get_plugin_context(request: Request) -> PluginContext:
|
|
@@ -60,4 +85,5 @@ async def get_plugin_context(request: Request) -> PluginContext:
|
|
|
60
85
|
permissions=getattr(state, "plugin_permissions", []),
|
|
61
86
|
storage=getattr(state, "storage", None),
|
|
62
87
|
config=getattr(state, "plugin_config", {}),
|
|
88
|
+
logger=getattr(state, "plugin_logger", logging.getLogger(f"palette_sdk.plugin.{getattr(state, 'plugin_id', 'unknown')}")),
|
|
63
89
|
)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Org-safe repository helpers for plugin-owned data."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping, Sequence
|
|
6
|
+
from typing import Any, Generic, TypeVar
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import asc, desc, func, select
|
|
9
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
10
|
+
|
|
11
|
+
T = TypeVar("T")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class OrgRepository(Generic[T]):
|
|
15
|
+
"""Small CRUD helper for plugin models.
|
|
16
|
+
|
|
17
|
+
The platform still enforces plugin schema isolation and database RLS in
|
|
18
|
+
production. This helper adds the same organization guard in app code, which
|
|
19
|
+
keeps local SQLite/dev tests honest and reduces repeated query boilerplate.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, db: AsyncSession, model: type[T], organization_id: int):
|
|
23
|
+
self.db = db
|
|
24
|
+
self.model = model
|
|
25
|
+
self.organization_id = organization_id
|
|
26
|
+
|
|
27
|
+
def _org_filter(self):
|
|
28
|
+
if hasattr(self.model, "organization_id"):
|
|
29
|
+
return self.model.organization_id == self.organization_id
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
def _base_select(self):
|
|
33
|
+
stmt = select(self.model)
|
|
34
|
+
org_filter = self._org_filter()
|
|
35
|
+
if org_filter is not None:
|
|
36
|
+
stmt = stmt.where(org_filter)
|
|
37
|
+
return stmt
|
|
38
|
+
|
|
39
|
+
def _apply_filters(self, stmt, filters: Mapping[str, Any] | None):
|
|
40
|
+
for key, value in (filters or {}).items():
|
|
41
|
+
if not hasattr(self.model, key):
|
|
42
|
+
raise ValueError(f"{self.model.__name__} has no column {key!r}")
|
|
43
|
+
stmt = stmt.where(getattr(self.model, key) == value)
|
|
44
|
+
return stmt
|
|
45
|
+
|
|
46
|
+
def _apply_order(self, stmt, order_by: str | Sequence[str] | None):
|
|
47
|
+
if order_by is None:
|
|
48
|
+
return stmt
|
|
49
|
+
fields = [order_by] if isinstance(order_by, str) else list(order_by)
|
|
50
|
+
clauses = []
|
|
51
|
+
for field in fields:
|
|
52
|
+
direction = desc if field.startswith("-") else asc
|
|
53
|
+
key = field[1:] if field.startswith("-") else field
|
|
54
|
+
if not hasattr(self.model, key):
|
|
55
|
+
raise ValueError(f"{self.model.__name__} has no column {key!r}")
|
|
56
|
+
clauses.append(direction(getattr(self.model, key)))
|
|
57
|
+
return stmt.order_by(*clauses)
|
|
58
|
+
|
|
59
|
+
async def list(
|
|
60
|
+
self,
|
|
61
|
+
*,
|
|
62
|
+
filters: Mapping[str, Any] | None = None,
|
|
63
|
+
order_by: str | Sequence[str] | None = None,
|
|
64
|
+
limit: int = 100,
|
|
65
|
+
offset: int = 0,
|
|
66
|
+
) -> list[T]:
|
|
67
|
+
stmt = self._apply_filters(self._base_select(), filters)
|
|
68
|
+
stmt = self._apply_order(stmt, order_by)
|
|
69
|
+
stmt = stmt.limit(max(1, min(limit, 500))).offset(max(0, offset))
|
|
70
|
+
return list((await self.db.execute(stmt)).scalars().all())
|
|
71
|
+
|
|
72
|
+
async def count(self, *, filters: Mapping[str, Any] | None = None) -> int:
|
|
73
|
+
stmt = select(func.count()).select_from(self.model)
|
|
74
|
+
org_filter = self._org_filter()
|
|
75
|
+
if org_filter is not None:
|
|
76
|
+
stmt = stmt.where(org_filter)
|
|
77
|
+
stmt = self._apply_filters(stmt, filters)
|
|
78
|
+
return int((await self.db.execute(stmt)).scalar_one())
|
|
79
|
+
|
|
80
|
+
async def get(self, id: Any) -> T | None:
|
|
81
|
+
stmt = self._base_select().where(self.model.id == id)
|
|
82
|
+
return (await self.db.execute(stmt)).scalar_one_or_none()
|
|
83
|
+
|
|
84
|
+
async def require(self, id: Any) -> T:
|
|
85
|
+
row = await self.get(id)
|
|
86
|
+
if row is None:
|
|
87
|
+
raise LookupError(f"{self.model.__name__} {id!r} not found")
|
|
88
|
+
return row
|
|
89
|
+
|
|
90
|
+
async def create(self, **values: Any) -> T:
|
|
91
|
+
if hasattr(self.model, "organization_id"):
|
|
92
|
+
values["organization_id"] = self.organization_id
|
|
93
|
+
row = self.model(**values)
|
|
94
|
+
self.db.add(row)
|
|
95
|
+
await self.db.commit()
|
|
96
|
+
await self.db.refresh(row)
|
|
97
|
+
return row
|
|
98
|
+
|
|
99
|
+
async def update(self, id: Any, **values: Any) -> T | None:
|
|
100
|
+
row = await self.get(id)
|
|
101
|
+
if row is None:
|
|
102
|
+
return None
|
|
103
|
+
values.pop("id", None)
|
|
104
|
+
values.pop("organization_id", None)
|
|
105
|
+
for key, value in values.items():
|
|
106
|
+
if not hasattr(row, key):
|
|
107
|
+
raise ValueError(f"{self.model.__name__} has no attribute {key!r}")
|
|
108
|
+
setattr(row, key, value)
|
|
109
|
+
await self.db.commit()
|
|
110
|
+
await self.db.refresh(row)
|
|
111
|
+
return row
|
|
112
|
+
|
|
113
|
+
async def delete(self, id: Any) -> bool:
|
|
114
|
+
row = await self.get(id)
|
|
115
|
+
if row is None:
|
|
116
|
+
return False
|
|
117
|
+
await self.db.delete(row)
|
|
118
|
+
await self.db.commit()
|
|
119
|
+
return True
|
package/lib/dev-simulator.js
CHANGED
|
@@ -250,9 +250,11 @@ const platform = {
|
|
|
250
250
|
org_role: "owner",
|
|
251
251
|
},
|
|
252
252
|
organizationId: 1,
|
|
253
|
+
pluginId: ${JSON.stringify(manifest.id)},
|
|
253
254
|
orgRole: "owner",
|
|
254
255
|
orgs: [{ id: 1, name: "Local Dev Org", slug: "local-dev", theme_id: "default", logo_url: null }],
|
|
255
256
|
agents: [],
|
|
257
|
+
permissions: ${JSON.stringify(manifest.permissions || [])},
|
|
256
258
|
apiFetch,
|
|
257
259
|
navigate: (path) => window.history.pushState(null, "", path),
|
|
258
260
|
showToast: (message, type = "info") => {
|
package/package.json
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
from fastapi import Depends, HTTPException
|
|
4
4
|
from pydantic import BaseModel
|
|
5
|
-
from sqlalchemy import select
|
|
6
|
-
|
|
7
5
|
from palette_sdk import PluginRouter, PluginContext, get_plugin_context, require_permission
|
|
8
6
|
from models import Note
|
|
9
7
|
|
|
@@ -16,11 +14,7 @@ class NoteIn(BaseModel):
|
|
|
16
14
|
|
|
17
15
|
@router.get("/notes", dependencies=[require_permission("resources:read")])
|
|
18
16
|
async def list_notes(ctx: PluginContext = Depends(get_plugin_context)) -> list[dict]:
|
|
19
|
-
rows = (
|
|
20
|
-
await ctx.db.execute(
|
|
21
|
-
select(Note).order_by(Note.id.desc())
|
|
22
|
-
)
|
|
23
|
-
).scalars().all()
|
|
17
|
+
rows = await ctx.repo(Note).list(order_by="-id")
|
|
24
18
|
return [{"id": r.id, "body": r.body} for r in rows]
|
|
25
19
|
|
|
26
20
|
|
|
@@ -31,8 +25,5 @@ async def create_note(
|
|
|
31
25
|
) -> dict:
|
|
32
26
|
if not body.body.strip():
|
|
33
27
|
raise HTTPException(status_code=400, detail="body required")
|
|
34
|
-
note = Note(
|
|
35
|
-
ctx.db.add(note)
|
|
36
|
-
await ctx.db.commit()
|
|
37
|
-
await ctx.db.refresh(note)
|
|
28
|
+
note = await ctx.repo(Note).create(body=body.body)
|
|
38
29
|
return {"id": note.id, "body": note.body}
|