Skip to content

feat(ui-plugins): add DB Admin Generator (db-admin-generator) - #249

Open
ppppp2005-p wants to merge 12 commits into
zai-org:mainfrom
ppppp2005-p:feat/db-admin-generator
Open

ppppp2005-p wants to merge 12 commits into
zai-org:mainfrom
ppppp2005-p:feat/db-admin-generator

Conversation

@ppppp2005-p

Copy link
Copy Markdown

Plugin type (required) / 插件类型(必填)

  • UI Plugin / 是 UI 插件 — provides MCP Apps interactive pages / 提供 MCP Apps 交互页面
  • Standard plugin / 普通插件(非 UI)
  • Not applicable / 不适用

Affected plugin name(s) / 涉及的插件名称:db-admin-generator (UI Plugin, new)

What / 改动内容

New community UI plugin DB Admin Generator / 数据库转管理后台: a SQLite database becomes a dependency-free admin panel (Node.js server plus browser UI), which is started and self-tested against a temporary copy of the database before it's handed over.

Tools:

  • db_inspect (read-only), db_generate, db_selftest.
  • App-only: db_state, db_files.

How it works:

  • Schema (src/runtime/schema.mjs, shared by the plugin and the generated server):
    • Reads sqlite_master, PRAGMA table_xinfo, foreign_key_list, and index_list / index_info (single-column unique indexes).
    • Parses CHECK (col IN (...)) options, VARCHAR(n) lengths and AUTOINCREMENT / INTEGER PRIMARY KEY.
    • Input kinds: integer, number, text, longtext, date, datetime, boolean, select, ref, email, tel, url.
    • Rowid tables (no PK) and views (read-only) are handled. validateRow() coerces and validates input (zh/en messages); on insert, empty NOT NULL columns that have defaults fall back to the SQLite default.
  • Plan (src/generate.ts):
    • Defaults: all tables with all four operations, views read-only, migration-like tables off, password/hash/token-like columns hidden.
    • Roles: admin = all, editor = read/create/update, viewer = read.
    • Patchable per table (ops, label, list, search, hidden, labelColumn) and per role. Saved in .db-admin-generator/plans.json.
  • Generated admin (src/runtime/*, copied to dist/runtime):
    • server.mjs uses only node:http, node:sqlite and node:crypto.
    • Security:
      • Passwords are scrypt hashes in users.json (0600). Sessions are HMAC-signed cookies (HttpOnly, SameSite=Strict, Secure behind HTTPS), and non-GET requests must send X-Admin: 1.
      • Sign-in is locked for 10 minutes after 5 failures per IP. Effective permission = table ops ∩ role ops, checked on the server.
      • Identifiers come from the schema whitelist and values are bound. Headers: CSP default-src 'self', X-Frame-Options: DENY and nosniff.
    • Behaviour:
      • SQLite constraint errors are mapped to field errors or 409s, and every change goes to audit.log.
      • A user CLI is included, and the first start creates admin with a random password printed once.
    • public/app.js (vanilla) provides sign-in, a table sidebar, a list with search, filters, sort, paging and reference labels, and a modal form with column defaults prefilled and server field errors shown. Viewers and views are read-only.
  • Self-test (src/selftest.ts):
    • Copies the DB to a temp dir, adds a user per role through the generated CLI, and spawns the server (PORT=0, ELECTRON_RUN_AS_NODE=1).
    • Over HTTP it checks:
      • Auth: required sign-in, wrong passwords, the CSRF header, and sign-in for each role.
      • Per table: row counts, search, sort, SQL injection in sort/search, unknown filter columns, and FK option endpoints.
      • Required-field errors, then create, read, update, invalid number, duplicate unique (409), and role denials.
      • Delete, delete of a still-referenced row (409), and the audit log.
    • Temp files are deleted afterwards.

Panel:

  • DB picker and Read schema.
  • Title, language, output folder and port.
  • Table grid: label, R/C/U/D, label column, hidden columns.
  • Role checkboxes.
  • Generate & self-test, and Run self-test.
  • Result with run instructions and ✓/✗ check list.
  • Collapsible schema view.

Network / API / side effects: none. No API keys. The self-test uses 127.0.0.1 only. Writes <out>/ and .db-admin-generator/plans.json. The source database is opened read-only and never modified (asserted by hash in the tests). No hooks. SQLite only; Postgres/MySQL are out of scope for v0.1. Needs Node ≥ 22.13 for node:sqlite.

Testing:

  • vitest (7 tests) on a fictional bike-rental DB (samples/db/make-db.mjs: FKs incl. ON DELETE CASCADE, UNIQUE, CHECK IN, BOOLEAN, DATE/DATETIME, a view, a table without PK) plus an accounts DB with secrets and a migrations table:
    • Kind inference and validation.
    • Default hiding of secret columns.
    • Generation: all ~90 self-test checks pass and the DB hash is unchanged.
    • A tampered server (permission check removed) is caught by db_selftest.
    • The user CLI and error messages.
  • Chrome e2e:
    • The panel flow, ending in a passing self-test.
    • The real generated admin under three roles:
      • editor: wrong password; search, filters and sort; required and 409 duplicate errors; create and edit; no delete button.
      • viewer: read-only form.
      • admin: delete; deleting a referenced category is refused; the view is read-only.

A ZCode Desktop demo recording is attached in the comments. / ZCode 桌面版演示录屏见评论。

Self-check / 自查清单

  • Plugin name is unique kebab-case; new external plugin directory is external_plugins/<name>/
  • Usage category is set (productivity)
  • Plugin type and affected names are declared above
  • version 0.1.0 in both plugin.json and marketplace.json
  • README.md (EN) and README_CN.md (中文) both provided and equivalent
  • description_i18n has both en and zh-CN
  • No secrets, machine-specific paths, or private dependencies
  • Model/API/network dependencies documented
  • python3 scripts/validate.py and python3 scripts/build_dist.py pass locally
  • Exercised in ZCode Desktop (ZCode 4.0.0, macOS arm64: installed from a local marketplace, asked it to inspect db/harbor-bikes.db and generate a Chinese admin; the agent ran db_inspect → db_generate (5 tables + 1 read-only view, built-in self-test 93/93 passed); a second short task ran db_selftest on the generated admin with the panel shown: 93/93 passed; demo video in the comments)

@ppppp2005-p

Copy link
Copy Markdown
Author

ZCode desktop demo (ZCode 4.0.0, macOS arm64)

db-admin-generator-demo.mp4

How this was tested:

  1. Installed db-admin-generator from a local marketplace built from this PR's branch.
  2. Main run (screen recording not usable because the ZCode window was minimized during it, so it is described here instead): asked ZCode to inspect db/harbor-bikes.db (SQLite), generate a Chinese admin titled "Harbor 单车租赁后台" and run the self-test. The agent called db_inspect → db_generate. The plugin wrote harbor-bikes-admin/ (server.mjs, public/, admin.config.json, README.md) for 5 tables (bikes, categories, customers, rentals, audit_log) plus the read-only view open_rentals. The built-in self-test passed 93/93 against a temporary copy of the database.
  3. Recorded follow-up (the video above, a separate short task): asked ZCode to run only db_selftest on the already generated harbor-bikes-admin. The panel shows the run commands and the full checklist (CLI user creation, sign-in per role, permissions, 409 on duplicate codes and on deleting a category still used by bikes, SQL-injection-safe search/sort, required-field errors, and so on). Result: 93/93 passed in 0.4 s, run against a temporary copy, so the original database was not changed. The last card is the final state.

演示说明:本地 marketplace 安装 → 正式运行 db_inspect → db_generate(5 张表 + 1 个视图,自检 93/93;该次录屏因窗口被最小化无效)→ 另开一个短任务录屏,只调 db_selftest,面板显示完整检查清单,93/93 通过。

@panda920

Copy link
Copy Markdown
Collaborator

✅ Internal validation passed. A ZCode maintainer will review the change next; after acceptance, release checks run separately and this PR is closed automatically.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants