Release 2.7.0: executable handbooks and native user-site workflows

This commit is contained in:
cflmy committed 2026-11-10 17:03:06 +08:00
1 parent aa0c4d0f55
commit d9aef957f7
562 files changed
+21064 -20387

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# web-tenant-saas
从仓库根目录运行:
```sh
marqdo call examples/web-tenant-saas/index.mq.md serve --effects plugin,fs.write,network.write --fs-root examples/web-tenant-saas --json
```
打开 http://127.0.0.1:18092,比较页面里的 Acme 与 Beta 查询链接。缺少 t 参数时返回 400;返回的 JSON 包含 rows 列表。首次空库初始化两篇笔记,重启保留已有记录。
[工程入口](index.mq.md) 串联 [数据库初始化](db/index.mq.md)、[租户选择](../../ext/security/tenant.mq.md) 与 [JSON 查询资源](../../ext/data/api.mq.md)。页面内容位于 [intro.md](intro.md),无原生 HTML 壳。
这是允许匿名切换租户的本机过滤演示。生产访问隔离还需要鉴权与成员关系检查。回归测试 web_handbook 覆盖两种后端的租户过滤、缺少参数与用户记录重启保留。
+47
View File
@@ -0,0 +1,47 @@
---
markup: occlusion
title: Tenant demonstration database
import:
db: ext/data/db.mq.md
fs: lib/fs.mq.md
---
This initializer owns one demonstration table. It keeps existing rows and seeds only an empty table. Pass a disposable absolute SQLite URI for tests; no caller should need to delete a user's data to repeat initialization.
## open
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| url | any | None | None chooses data/tenant-saas.db under the granted site root; otherwise an explicit SQLite URI |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Initialized database resource containing retained user rows or initial demo notes |
Use `url`=None with fs.write and plugin on the site root. The default path is resolved through the native filesystem so its location does not change with the process working directory. An explicit SQLite URI belongs to the caller, who must grant and manage that native database boundary. This initializer never deletes rows or resets ids during a restart.
The schema preserves the title and tenant_id fields used by the scoped query. Check for any existing row before seeding; a nonempty or partially populated database is retained as supplied. Only a completely empty table receives the two notes. This is an idempotent demonstration initializer, not a migration or repair tool.
1. `url` == None
We **[Create the site's selected data directory](fs.make_dirs(path="data"))** and **path = [Resolve the native database filename](fs.resolve(path="data/tenant-saas.db"))** and **url = "sqlite:" + path**.
`fields` =
| name | type | nullable |
|---|---|---|
| id | integer | False |
| title | text | False |
| tenant_id | text | False |
We **store = [Open the selected database resource](db.db(url=`url`))** and **[Ensure the demonstration table exists](> `store`.init name="items" fields=`fields`)** and **existing = [Observe whether any business rows already exist](> `store`.select table="items" limit=1)**.
1. not `existing`
`rows` =
| @ | title | tenant_id |
|---|---|---|
| 1 | Acme note | acme |
| 2 | Beta note | beta |
We **[Insert both initial notes only into the empty table](> `store`.insert table="items" rows=`rows`)**.
Once the selected database is ready, return *store* for route discovery and binding.
+42 -23
View File
@@ -1,35 +1,54 @@
---
markup: occlusion
title: web-tenant-saas demo
description: Dual-tenant isolation via ?t= query + tenant_scope JSON API.
type: web
view: main
route: /
title: Tenant SaaS — explicit database and request resources
import:
component: ext/web/component/index.mq.md
fs: lib/fs.mq.md
web: ext/web/web.mq.md
api: ext/data/api.mq.md
tenant: ext/security/tenant.mq.md
database: db/index.mq.md
---
# main
Read the Markdown leaf for the visible demonstration, the database entry for initialization, and the two resource leaves for query and tenant binding. The old startup DELETE is removed: restarting a demo must preserve existing rows. This loopback site intentionally allows choosing a demo tenant; it does not claim authenticated membership.
`items_schema` =
## main
| name | type | nullable |
|------|------|----------|
| id | integer | false |
| title | text | false |
| tenant_id | text | false |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | list | Native View nodes containing the Markdown explanation and query links |
`api` =
The page is a single content leaf because its internal structure is already simple Markdown. No raw HTML shell or browser script is needed. Site discovery reads it relative to the chosen site root; the returned node data is assembled before any listener starts. The API and database lifecycle remain in serve, so rendering the explanation cannot clear or recreate business data.
We **intro = [Read the terminal content through the granted site root](fs.read_text(path="intro.md"))**.
Return *[Describe the explanation as a Markdown leaf](component.markdown(text=`intro`, slot="tenant-introduction"))*.
## serve
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| root | text | "examples/web-tenant-saas" | Explicit site discovery directory relative to the checkout |
| port | number | 18092 | Local listener port |
| url | any | None | None selects the site-root database; tests pass a disposable SQLite URI |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Blocking listener with explicit tenant-filtered JSON queries |
Use `root`="examples/web-tenant-saas", `port`=18092 and `url`=None with plugin, fs.write and network.write. Set --fs-root to this site's directory so default database files belong to the site. The discovery root is explicit and independent of that filesystem grant. Preparation initializes retained records, discovers the explanation, then binds request tenant selection and the read-only query table before listening.
The route explicitly scopes its query by tenant_id. With the resolver attached, missing tenant context is rejected rather than returning both tenants. Selection via t is intentionally open for this local example; real user access requires a separate authentication and membership policy. Keep both bindings together when editing the flow.
We **store = [Initialize without deleting existing user rows](database.open(url=`url`))** and **app = [Discover the page with its retained database resource](web.prepare(root=`root`, host="127.0.0.1", port=`port`, db=`store`))** and **app = [Resolve the explicit request tenant selector](tenant.attach(app=`app`, mode="path", param="t", default_scope=False))**.
`routes` =
| path | method | table | order | limit | tenant_scope |
|------|--------|-------|-------|-------|--------------|
| api/items | GET | items | id | 50 | true |
|---|---|---|---|---|---|
| api/items | GET | items | id | 50 | True |
**db = > web.db url="sqlite:data/tenant-saas.db"**
> `db`.init name=items fields=`items_schema`
> `db`.exec sql="DELETE FROM items"
> `db`.exec sql="INSERT INTO items(title, tenant_id) VALUES('Acme note','acme')"
> `db`.exec sql="INSERT INTO items(title, tenant_id) VALUES('Beta note','beta')"
**page = > web.page title="Tenant SaaS" intro="<p>Try <a href='/api/items?t=acme'>/api/items?t=acme</a> vs <a href='/api/items?t=beta'>/api/items?t=beta</a>.</p>"**
**app = > web.app page=`page` db=`db` host="127.0.0.1" port=18092 admin=False**
**app = > `app`.tenant mode="path" param="t" default_scope=False**
**app = > `app`.configure json=`api`**
> `app`.listen
We **app = [Bind the explicit tenant-scoped read-only query](api.mount(app=`app`, routes=`routes`))**.
Return *[Listen after data and request policy are ready](web.listen(app=`app`))*.
+11
View File
@@ -0,0 +1,11 @@
# Two-tenant query demonstration
Compare [Acme notes](/api/items?t=acme) with [Beta notes](/api/items?t=beta).
Each response selects only rows belonging to the chosen tenant.
A [request without a tenant](/api/items) is rejected.
This local demonstration permits anonymous tenant selection. It demonstrates
query filtering and does not authenticate a user or prove tenant membership.
Restarting the server retains existing database rows. The initializer adds
the two demonstration notes only when the table is completely empty.