Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions external_plugins/db-admin-generator/.zcode-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "db-admin-generator",
"version": "0.1.0",
"description": "Turn a SQLite database into a working admin panel: reads the schema (tables, views, keys, foreign keys, CHECK options, required and unique columns), lets you pick tables, operations and roles (admin / editor / viewer), and generates a dependency-free Node.js server plus browser UI with sign-in, search, filters, sorting, paging and forms validated against the schema. The generated admin is then started against a temporary copy of the database and self-tested over HTTP (permissions, validation, CRUD, SQL injection). Your database is never modified. Postgres and MySQL are not supported yet.",
"description_i18n": {
"en": "Turn a SQLite database into a working admin panel: reads the schema (tables, views, keys, foreign keys, CHECK options, required and unique columns), lets you pick tables, operations and roles (admin / editor / viewer), and generates a dependency-free Node.js server plus browser UI with sign-in, search, filters, sorting, paging and forms validated against the schema. The generated admin is then started against a temporary copy of the database and self-tested over HTTP (permissions, validation, CRUD, SQL injection). Your database is never modified. Postgres and MySQL are not supported yet.",
"zh-CN": "把 SQLite 数据库变成能用的管理后台:读取表结构(表、视图、主键、外键、CHECK 选项、必填和唯一列),选择要管理的表、操作和角色(admin / editor / viewer),生成没有第三方依赖的 Node.js 服务器和网页界面,带登录、搜索、筛选、排序、分页,以及按数据库结构校验的表单。生成后会用数据库的临时副本实际启动,并通过 HTTP 自测(权限、校验、增删改查、SQL 注入)。不会修改你的数据库。暂不支持 Postgres 和 MySQL。"
},
"author": {
"name": "Penova",
"url": "https://github.com/ppppp2005-p"
},
"license": "MIT",
"keywords": [
"database",
"sqlite",
"admin panel",
"crud",
"back office",
"roles",
"node",
"generator",
"数据库",
"管理后台",
"增删改查",
"权限"
],
"skills": "skills",
"mcpServers": {
"db-admin-generator": {
"type": "stdio",
"command": "node",
"args": [
"${ZCODE_PLUGIN_ROOT}/dist/server.mjs"
],
"cwd": "${ZCODE_PROJECT_DIR}",
"timeoutMs": 900000,
"env": {
"ZCODE_WORKSPACE_ROOT": "${ZCODE_PROJECT_DIR}"
}
}
},
"ui": {
"surfaces": [
{
"id": "editor",
"title": {
"zh-CN": "数据库转管理后台",
"en-US": "DB Admin Generator"
},
"icon": "database",
"server": "db-admin-generator",
"resourceUri": "ui://db-admin-generator/panel.html",
"availability": "session"
}
]
}
}
21 changes: 21 additions & 0 deletions external_plugins/db-admin-generator/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Penova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions external_plugins/db-admin-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# DB Admin Generator

Turn a **SQLite database** into a working **admin panel**: sign-in, roles, a list per table with search and filters, and add/edit/delete forms that follow the database rules. The generated admin has **no third-party dependencies**; it needs only Node.js 22.13+. Before you get it, it is **started and self-tested** against a temporary copy of your database.

## What it does

- **Reads the schema** (read-only):
- Tables and views, primary keys (or rowid), foreign keys, and `CHECK (col IN (…))` options.
- Required (NOT NULL without default), unique and `VARCHAR(n)` lengths.
- Booleans, dates and datetimes, email, phone and URL columns, and which tables reference which.
- **Plan**:
- Pick the tables and the operations allowed on each (read / create / update / delete).
- Pick what each role may do: **admin** can do everything, **editor** defaults to read/create/update, **viewer** to read only.
- Pick the column shown when other tables refer to a row.
- Password-, hash- and token-like columns are hidden by default (never sent to the browser). Migration tables are left out by default.
- **Generates** `<db>-admin/`:
- `server.mjs`: `node:http` + `node:sqlite`. It provides:
- Sign-in with salted scrypt passwords and an HMAC-signed HttpOnly SameSite=Strict cookie.
- Role checks on the server, parameterised SQL with identifiers from the schema only, a CSRF header check and a sign-in lockout after 5 failures.
- Friendly messages for UNIQUE / foreign-key / CHECK conflicts, and an `audit.log` of every change.
- `public/`: a browser UI with no inline scripts and a strict CSP:
- A sidebar of tables and a list with search, filters (options, yes/no, references), sorting and paging. References show the referenced row's name.
- A form per row: dropdowns for references and options, date and checkbox inputs, required marks and max length. Server errors appear on the field.
- Read-only mode for viewers and views. Chinese or English.
- `admin.config.json` (editable), `README.md`, `package.json`, `.gitignore`.
- User CLI: `node server.mjs user add <name> <role>`, `user passwd`, `user list`, `user remove`.
- **Self-test**: the admin is started on a free port against a **temporary copy** of the database. It is then checked over HTTP, about 90 checks for the sample shop:
- Sign-in for each role and rejection of wrong passwords.
- Listing every table (row counts match), search, sorting, and SQL-injection attempts.
- Required-field and type errors, then create → read → update → delete.
- Duplicate unique values (409), and deleting a row that is still referenced (409).
- Viewer and editor permissions, and the audit log.

## Usage

Ask the agent in ZCode Desktop:

- "Generate an admin panel for data/shop.db"
- "Make a read-only admin for the customers and orders tables"
- "Re-run the self-test on the generated admin"

Or use the panel: choose the database, **Read schema**, adjust tables, operations and roles, then **Generate & self-test**. Then run it:

```bash
cd shop-admin
node server.mjs # http://127.0.0.1:8787 — the first start creates "admin" and prints its password
```

## Tools

| Tool | Purpose |
|---|---|
| `db_inspect` | Read the schema and propose a plan (writes nothing) |
| `db_generate` | Write the admin folder, then self-test it |
| `db_selftest` | Run the self-test again (for example after editing the generated code) |

## Scope (v0.1)

- **SQLite only.** Postgres and MySQL are not supported yet (a Postgres version needs a database to test against).
- Single-column foreign keys become dropdowns; composite foreign keys are shown as plain values.
- No schema changes or migrations, no file uploads or BLOB editing, no per-row permissions.
- Use a test copy of the database while you try it. The plugin itself never writes to the database; the generated admin does, once you run it.
- Listens on 127.0.0.1 by default. Put it behind HTTPS before sharing it on a network.

## Privacy, network and files

- Runs locally with no network access and no API keys. The self-test talks only to 127.0.0.1.
- Reads the database read-only. Writes `<out>/` and `.db-admin-generator/plans.json`. The self-test's temporary copy, test users and passwords are deleted afterwards.
- Needs Node.js 22.13 or newer for `node:sqlite`, both in ZCode and for running the generated admin.

## License

MIT.
73 changes: 73 additions & 0 deletions external_plugins/db-admin-generator/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 数据库转管理后台

把 **SQLite 数据库**变成能直接用的**管理后台**:有登录和角色,每张表一个列表(支持搜索和筛选),新增、修改、删除表单都遵守数据库的规则。生成的后台**没有第三方依赖**,只需要 Node.js 22.13 以上。交给你之前,插件会先用数据库的**临时副本**把它**真正启动并自测**一遍。

## 功能

- **读取表结构**(只读):
- 表和视图、主键(或 rowid)、外键、`CHECK (col IN (…))` 的选项。
- 必填列(NOT NULL 且没有默认值)、唯一列、`VARCHAR(n)` 的长度。
- 布尔、日期和时间、邮箱、电话、链接列,以及表之间的引用关系。
- **规划**:
- 勾选要管理的表,以及每张表允许的操作(查 / 增 / 改 / 删)。
- 设置每个角色能做什么:**admin** 拥有全部权限,**editor** 默认可以查、增、改,**viewer** 只能查看。
- 选择其他表引用这一行时显示哪一列。
- 名字像密码、哈希、令牌的列默认隐藏,永远不会发到浏览器;迁移记录表默认不纳入。
- **生成** `<数据库名>-admin/`:
- `server.mjs`:基于 `node:http` + `node:sqlite`,提供:
- 登录:密码用 scrypt 加盐哈希,会话 Cookie 带 HMAC 签名,并设置 HttpOnly 和 SameSite=Strict。
- 安全:权限在服务器端检查;SQL 全部参数化,表名和列名只来自表结构;有防 CSRF 的请求头检查;连续输错 5 次密码会锁定。
- 唯一、外键、CHECK 冲突给出易懂的提示;每次修改都写入 `audit.log`。
- `public/`:网页界面,没有内联脚本,使用严格 CSP:
- 侧边栏列出各表;列表支持搜索、筛选(选项、是/否、引用)、排序和分页;引用列显示被引用记录的名称。
- 每行一个表单:引用和选项用下拉框,日期用日期选择器,布尔用复选框;标出必填项和长度上限;服务器返回的错误显示在对应字段下面。
- viewer 角色和视图是只读的;界面可选中文或英文。
- `admin.config.json`(可以手动修改)、`README.md`、`package.json`、`.gitignore`。
- 用户命令:`node server.mjs user add <名字> <角色>`、`user passwd`、`user list`、`user remove`。
- **自测**:用数据库的**临时副本**,在空闲端口上启动后台,通过 HTTP 检查(示例店铺数据库约 90 项):
- 每种角色都能登录,错误密码被拒绝。
- 每张表都能列出(行数一致),搜索、排序正常,SQL 注入尝试无效。
- 必填项和类型错误会被拦下;新增 → 读取 → 修改 → 删除都能完成。
- 唯一值重复返回 409;删除仍被引用的记录也返回 409。
- viewer 和 editor 的权限限制生效,修改写入了审计日志。

## 使用

在 ZCode Desktop 里对助手说:

- “给 data/shop.db 生成一个管理后台”
- “做一个只读后台,只看客户表和订单表”
- “重新自测生成的管理后台”

也可以用面板:选择数据库,点 **读取结构**,调整表、操作和角色,然后点 **生成管理后台并自测**。生成后这样运行:

```bash
cd shop-admin
node server.mjs # http://127.0.0.1:8787,第一次启动会创建 admin 用户并打印密码
```

## 工具

| 工具 | 作用 |
|---|---|
| `db_inspect` | 读取表结构并给出建议方案(不写任何文件) |
| `db_generate` | 生成管理后台目录,然后自测 |
| `db_selftest` | 重新自测,例如手动改过生成的代码之后 |

## 范围(v0.1)

- **只支持 SQLite。** 暂不支持 Postgres 和 MySQL(Postgres 版本需要有数据库可以实际测试)。
- 单列外键会显示为下拉框;复合外键按普通值显示。
- 不改表结构,不做迁移,不支持文件上传和 BLOB 编辑,也没有行级权限。
- 试用时请用数据库的测试副本。插件本身从不写数据库;但生成的后台运行之后会写。
- 默认只监听 127.0.0.1。要在网络上共享,请先放到 HTTPS 后面。

## 隐私、网络与文件

- 全部在本地运行,不联网,不需要 API Key。自测只访问 127.0.0.1。
- 以只读方式读取数据库,写入 `<输出目录>/` 和 `.db-admin-generator/plans.json`。自测用的临时副本、测试用户和密码用完即删。
- ZCode 里运行插件和运行生成的后台,都需要 Node.js 22.13 或更新版本(`node:sqlite`)。

## 许可证

MIT。
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: db-admin-generator
description: 给 SQLite 数据库生成无依赖的管理后台(Node.js + 网页界面:登录、admin/editor/viewer 角色、搜索、筛选、表单校验、增删改查),并用数据库的临时副本自动启动自测。用户提到“管理后台 / 后台管理系统 / admin panel / 给数据库做个界面 / CRUD”时使用。
---

# 数据库转管理后台

1. 找到 SQLite 文件(.db / .sqlite / .sqlite3)。如果用户说的是 Postgres 或 MySQL,告诉他这一版只支持 SQLite;可以先导出一份 SQLite 测试库。
2. 用 `db_inspect({db})` 读取表结构,向用户简要说明有哪些表和视图、行数、引用关系,以及默认隐藏的敏感列(密码、令牌等)。
3. 和用户确认:要管理哪些表、每张表允许哪些操作、editor 和 viewer 能做什么、界面语言和标题。然后调用 `db_generate({db, only?, tables?, roles?, lang?, title?})`。
4. 查看自测结果。如果有失败项,说明是哪一项、可能的原因(例如数据库里的触发器或 CHECK 约束),必要时调整方案后重新生成。
5. 告诉用户怎么运行:`cd <out> && node server.mjs`(需要 Node 22.13+),第一次启动会在终端打印 admin 的密码;用 `node server.mjs user add <名字> <角色>` 添加其他人。默认只能本机访问;要给别人用,需要先放到 HTTPS 后面。
6. 用户手动改过生成的代码后,可以用 `db_selftest({out})` 再验证一遍。
50 changes: 50 additions & 0 deletions marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,56 @@
"把上一个 Hi3D 任务生成的模型按角色拆分成部件"
]
}
},
{
"name": "db-admin-generator",
"componentTypes": [
"ui"
],
"source": "./external_plugins/db-admin-generator",
"displayName": "DB Admin Generator",
"displayName_i18n": {
"en": "DB Admin Generator",
"zh-CN": "数据库转管理后台"
},
"description": "Turn a SQLite database into a working admin panel: reads the schema (tables, views, keys, foreign keys, CHECK options, required and unique columns), lets you pick tables, operations and roles (admin / editor / viewer), and generates a dependency-free Node.js server plus browser UI with sign-in, search, filters, sorting, paging and forms validated against the schema. The generated admin is then started against a temporary copy of the database and self-tested over HTTP (permissions, validation, CRUD, SQL injection). Your database is never modified. Postgres and MySQL are not supported yet.",
"description_i18n": {
"en": "Turn a SQLite database into a working admin panel: reads the schema (tables, views, keys, foreign keys, CHECK options, required and unique columns), lets you pick tables, operations and roles (admin / editor / viewer), and generates a dependency-free Node.js server plus browser UI with sign-in, search, filters, sorting, paging and forms validated against the schema. The generated admin is then started against a temporary copy of the database and self-tested over HTTP (permissions, validation, CRUD, SQL injection). Your database is never modified. Postgres and MySQL are not supported yet.",
"zh-CN": "把 SQLite 数据库变成能用的管理后台:读取表结构(表、视图、主键、外键、CHECK 选项、必填和唯一列),选择要管理的表、操作和角色(admin / editor / viewer),生成没有第三方依赖的 Node.js 服务器和网页界面,带登录、搜索、筛选、排序、分页,以及按数据库结构校验的表单。生成后会用数据库的临时副本实际启动,并通过 HTTP 自测(权限、校验、增删改查、SQL 注入)。不会修改你的数据库。暂不支持 Postgres 和 MySQL。"
},
"version": "0.1.0",
"author": {
"name": "Penova",
"url": "https://github.com/ppppp2005-p"
},
"license": "MIT",
"category": "developer-tools",
"keywords": [
"database",
"sqlite",
"admin panel",
"crud",
"back office",
"roles",
"node",
"generator",
"数据库",
"管理后台",
"增删改查",
"权限"
],
"examplePrompts": [
"Generate an admin panel for data/shop.db",
"Make a read-only admin for the customers and orders tables",
"Re-run the self-test on the generated admin"
],
"examplePrompts_i18n": {
"zh-CN": [
"给 data/shop.db 生成一个管理后台",
"做一个只读后台,只看客户表和订单表",
"重新自测生成的管理后台"
]
}
}
]
}
Loading