Scaffold Docwell monorepo with PocketBase authentication
Phase 0 foundation for Docwell, an open-source knowledge base: - Database: PocketBase v0.40.6 with initial schema migrations; users is the built-in auth collection, plus a hook for token checks and env-driven app URL/SMTP settings - Server: Go API (health, version, auth endpoints forwarded to PocketBase, admin user list) with an embedded Vue admin panel - Web-App: Go static server and Vue/Tailwind app with Tiptap editor, sign-in, sign-up, password reset and email verification (EN/PL) - Desktop-App: Wails v3 host reusing the web app - Phone-App: Flutter app with sign-in - Shared packages (editor, ui, api-client), OpenAPI spec, Docker compose (separate services and all-in-one), GitHub Actions CI - Docs: feature inventory and architecture/build plan Licensed under AGPL-3.0-or-later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
4903bb12dd
264 files changed
+21422
No files matched your search
@@ -0,0 +1,24 @@
|
||||
# docwell-db: the official PocketBase release plus Docwell migrations.
|
||||
FROM alpine:3.22
|
||||
|
||||
ARG PB_VERSION=0.40.6
|
||||
ARG TARGETARCH=amd64
|
||||
|
||||
RUN apk add --no-cache ca-certificates unzip wget \
|
||||
&& wget -q "https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_${TARGETARCH}.zip" -O /tmp/pb.zip \
|
||||
&& unzip /tmp/pb.zip pocketbase -d /usr/local/bin \
|
||||
&& rm /tmp/pb.zip \
|
||||
&& apk del unzip wget
|
||||
|
||||
COPY pb_migrations /pb_migrations
|
||||
COPY pb_hooks /pb_hooks
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
|
||||
VOLUME /pb_data
|
||||
EXPOSE 8090
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --retries=5 \
|
||||
CMD wget -qO- http://127.0.0.1:8090/api/health >/dev/null || exit 1
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
@@ -0,0 +1,25 @@
|
||||
# docwell-db
|
||||
|
||||
The official [PocketBase](https://pocketbase.io) binary (pinned in `Dockerfile`) with Docwell's schema migrations.
|
||||
|
||||
- `pb_migrations/` holds JS migrations, applied in filename order at startup.
|
||||
- `pb_hooks/` holds optional JS hooks (keep these minimal).
|
||||
- All collection API rules are `null`, so only superusers have access. Never expose this service publicly; only docwell-server talks to it.
|
||||
|
||||
## Run locally
|
||||
|
||||
Download PocketBase v0.40.6 for your OS into `Database/bin/`, then:
|
||||
|
||||
```bash
|
||||
./bin/pocketbase migrate up --dir ./pb_data --migrationsDir ./pb_migrations
|
||||
./bin/pocketbase superuser upsert admin@docwell.test devpassword123 --dir ./pb_data
|
||||
./bin/pocketbase serve --dir ./pb_data --migrationsDir ./pb_migrations --hooksDir ./pb_hooks
|
||||
```
|
||||
|
||||
The PocketBase dashboard is at http://127.0.0.1:8090/_/.
|
||||
|
||||
## New migration
|
||||
|
||||
```bash
|
||||
./bin/pocketbase migrate create "add_comments" --dir ./pb_data --migrationsDir ./pb_migrations
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${PB_ADMIN_EMAIL:?PB_ADMIN_EMAIL is required}"
|
||||
: "${PB_ADMIN_PASSWORD:?PB_ADMIN_PASSWORD is required}"
|
||||
|
||||
pocketbase migrate up --dir /pb_data --migrationsDir /pb_migrations
|
||||
pocketbase superuser upsert "$PB_ADMIN_EMAIL" "$PB_ADMIN_PASSWORD" --dir /pb_data
|
||||
|
||||
exec pocketbase serve \
|
||||
--http 0.0.0.0:8090 \
|
||||
--dir /pb_data \
|
||||
--migrationsDir /pb_migrations \
|
||||
--hooksDir /pb_hooks \
|
||||
--automigrate=false
|
||||
@@ -0,0 +1,39 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
// Token check used by docwell-server: returns the "users" record that owns the
|
||||
// bearer token, or 401. Unlike auth-refresh, it does not issue a new token.
|
||||
routerAdd(
|
||||
"GET",
|
||||
"/api/docwell/auth/verify",
|
||||
(e) => {
|
||||
e.auth.ignoreEmailVisibility(true);
|
||||
return e.json(200, e.auth);
|
||||
},
|
||||
$apis.requireAuth("users"),
|
||||
);
|
||||
|
||||
// Instance settings come from the environment and are applied on every start,
|
||||
// so changing an env var and restarting is enough.
|
||||
onBootstrap((e) => {
|
||||
e.next();
|
||||
|
||||
const env = (key, def) => $os.getenv(key) || def;
|
||||
const settings = e.app.settings();
|
||||
|
||||
settings.meta.appName = env("DOCWELL_APP_NAME", "Docwell");
|
||||
settings.meta.appURL = env("DOCWELL_WEB_URL", "http://localhost:3000");
|
||||
settings.meta.senderName = settings.meta.appName;
|
||||
settings.meta.senderAddress = env("DOCWELL_MAIL_FROM", "no-reply@docwell.localhost");
|
||||
|
||||
const smtpHost = env("DOCWELL_SMTP_HOST", "");
|
||||
settings.smtp.enabled = smtpHost !== "";
|
||||
if (smtpHost) {
|
||||
settings.smtp.host = smtpHost;
|
||||
settings.smtp.port = parseInt(env("DOCWELL_SMTP_PORT", "587"), 10);
|
||||
settings.smtp.username = env("DOCWELL_SMTP_USERNAME", "");
|
||||
settings.smtp.password = env("DOCWELL_SMTP_PASSWORD", "");
|
||||
settings.smtp.tls = env("DOCWELL_SMTP_TLS", "false") === "true";
|
||||
}
|
||||
|
||||
e.app.save(settings);
|
||||
});
|
||||
@@ -0,0 +1,191 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
// Initial Docwell schema. All record API rules stay null (superuser only): PocketBase
|
||||
// is a private service and docwell-server is the only client allowed to use it.
|
||||
|
||||
const timestamps = () => [
|
||||
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
||||
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
||||
];
|
||||
|
||||
const relation = (name, collection, opts = {}) => ({
|
||||
name,
|
||||
type: "relation",
|
||||
collectionId: collection.id,
|
||||
maxSelect: 1,
|
||||
cascadeDelete: false,
|
||||
...opts,
|
||||
});
|
||||
|
||||
migrate(
|
||||
(app) => {
|
||||
// Authentication uses PocketBase's built-in "users" auth collection
|
||||
// (passwords, tokens, verification, password reset, OTP, MFA, OAuth2).
|
||||
// docwell-server forwards the auth calls; signup goes through the server
|
||||
// with the superuser token, so the record API rules stay closed.
|
||||
const users = app.findCollectionByNameOrId("users");
|
||||
users.listRule = null;
|
||||
users.viewRule = null;
|
||||
users.createRule = null;
|
||||
users.updateRule = null;
|
||||
users.deleteRule = null;
|
||||
// Disabled accounts cannot sign in with any method.
|
||||
users.authRule = "status != 'disabled'";
|
||||
users.passwordAuth.enabled = true;
|
||||
users.passwordAuth.identityFields = ["email"];
|
||||
|
||||
users.fields.add(new SelectField({ name: "status", values: ["active", "invited", "disabled"], maxSelect: 1 }));
|
||||
users.fields.add(new BoolField({ name: "is_admin" }));
|
||||
users.fields.add(new TextField({ name: "locale", max: 16 }));
|
||||
users.fields.add(new SelectField({ name: "theme", values: ["system", "light", "dark"], maxSelect: 1 }));
|
||||
users.fields.add(new DateField({ name: "last_seen" }));
|
||||
|
||||
// Email links open docwell-web pages ({APP_URL} = DOCWELL_WEB_URL, set by
|
||||
// pb_hooks/docwell.pb.js), never the private PocketBase dashboard.
|
||||
users.verificationTemplate.subject = "Verify your {APP_NAME} email";
|
||||
users.verificationTemplate.body =
|
||||
"<p>Hello,</p><p>Confirm your email address for {APP_NAME}:</p>" +
|
||||
'<p><a href="{APP_URL}/verify-email?token={TOKEN}">Verify email</a></p>' +
|
||||
"<p>If you did not create an account, you can ignore this email.</p>";
|
||||
users.resetPasswordTemplate.subject = "Reset your {APP_NAME} password";
|
||||
users.resetPasswordTemplate.body =
|
||||
"<p>Hello,</p><p>Someone asked to reset the password of your {APP_NAME} account:</p>" +
|
||||
'<p><a href="{APP_URL}/reset-password?token={TOKEN}">Choose a new password</a></p>' +
|
||||
"<p>If it was not you, you can ignore this email.</p>";
|
||||
users.confirmEmailChangeTemplate.body =
|
||||
"<p>Hello,</p><p>Confirm your new email address for {APP_NAME}:</p>" +
|
||||
'<p><a href="{APP_URL}/confirm-email-change?token={TOKEN}">Confirm new email</a></p>';
|
||||
app.save(users);
|
||||
|
||||
const workspaces = new Collection({
|
||||
type: "base",
|
||||
name: "workspaces",
|
||||
fields: [
|
||||
{ name: "name", type: "text", required: true, max: 120 },
|
||||
{ name: "slug", type: "text", required: true, max: 64, pattern: "^[a-z0-9-]+$" },
|
||||
{ name: "logo", type: "file", maxSelect: 1, maxSize: 2097152 },
|
||||
{ name: "settings", type: "json" },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_workspaces_slug ON workspaces (slug)"],
|
||||
});
|
||||
app.save(workspaces);
|
||||
|
||||
const workspaceMembers = new Collection({
|
||||
type: "base",
|
||||
name: "workspace_members",
|
||||
fields: [
|
||||
relation("workspace", workspaces, { required: true, cascadeDelete: true }),
|
||||
relation("user", users, { required: true, cascadeDelete: true }),
|
||||
{ name: "role", type: "select", values: ["owner", "admin", "member", "guest"], maxSelect: 1, required: true },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_wsm_unique ON workspace_members (workspace, user)"],
|
||||
});
|
||||
app.save(workspaceMembers);
|
||||
|
||||
const spaces = new Collection({
|
||||
type: "base",
|
||||
name: "spaces",
|
||||
fields: [
|
||||
relation("workspace", workspaces, { required: true, cascadeDelete: true }),
|
||||
{ name: "name", type: "text", required: true, max: 120 },
|
||||
{ name: "slug", type: "text", required: true, max: 64, pattern: "^[a-z0-9-]+$" },
|
||||
{ name: "icon", type: "text", max: 32 },
|
||||
{ name: "description", type: "text", max: 1000 },
|
||||
{ name: "visibility", type: "select", values: ["open", "private"], maxSelect: 1, required: true },
|
||||
{ name: "default_role", type: "select", values: ["editor", "commenter", "viewer"], maxSelect: 1 },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_spaces_slug ON spaces (workspace, slug)"],
|
||||
});
|
||||
app.save(spaces);
|
||||
|
||||
const spaceMembers = new Collection({
|
||||
type: "base",
|
||||
name: "space_members",
|
||||
fields: [
|
||||
relation("space", spaces, { required: true, cascadeDelete: true }),
|
||||
{ name: "principal_type", type: "select", values: ["user", "group"], maxSelect: 1, required: true },
|
||||
{ name: "principal_id", type: "text", required: true },
|
||||
{ name: "role", type: "select", values: ["admin", "editor", "commenter", "viewer"], maxSelect: 1, required: true },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_spm_unique ON space_members (space, principal_type, principal_id)"],
|
||||
});
|
||||
app.save(spaceMembers);
|
||||
|
||||
const pages = new Collection({
|
||||
type: "base",
|
||||
name: "pages",
|
||||
fields: [
|
||||
relation("workspace", workspaces, { required: true, cascadeDelete: true }),
|
||||
relation("space", spaces, { required: true, cascadeDelete: true }),
|
||||
{ name: "title", type: "text", max: 500 },
|
||||
{ name: "icon", type: "text", max: 32 },
|
||||
{ name: "cover", type: "file", maxSelect: 1, maxSize: 10485760 },
|
||||
// Fractional index string for ordering siblings without renumbering.
|
||||
{ name: "position", type: "text", required: true, max: 128 },
|
||||
relation("creator", users),
|
||||
relation("last_editor", users),
|
||||
{ name: "content_json", type: "json", maxSize: 20971520 },
|
||||
{ name: "content_text", type: "text", max: 0 },
|
||||
{ name: "word_count", type: "number", onlyInt: true, min: 0 },
|
||||
{ name: "is_template", type: "bool" },
|
||||
{ name: "archived_at", type: "date" },
|
||||
{ name: "deleted_at", type: "date" },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE INDEX idx_pages_workspace ON pages (workspace, updated)"],
|
||||
});
|
||||
app.save(pages);
|
||||
|
||||
// Self-relation needs the collection id, so it is added after the first save.
|
||||
pages.fields.add(new RelationField({ name: "parent", collectionId: pages.id, maxSelect: 1, cascadeDelete: true }));
|
||||
pages.indexes = [
|
||||
"CREATE INDEX idx_pages_workspace ON pages (workspace, updated)",
|
||||
"CREATE INDEX idx_pages_tree ON pages (space, parent, position)",
|
||||
];
|
||||
app.save(pages);
|
||||
|
||||
const pageYdoc = new Collection({
|
||||
type: "base",
|
||||
name: "page_ydoc",
|
||||
fields: [
|
||||
relation("page", pages, { required: true, cascadeDelete: true }),
|
||||
{ name: "state", type: "file", maxSelect: 1, maxSize: 52428800, protected: true },
|
||||
{ name: "state_vector", type: "text", max: 0 },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_page_ydoc_page ON page_ydoc (page)"],
|
||||
});
|
||||
app.save(pageYdoc);
|
||||
|
||||
const pageYupdates = new Collection({
|
||||
type: "base",
|
||||
name: "page_yupdates",
|
||||
fields: [
|
||||
relation("page", pages, { required: true, cascadeDelete: true }),
|
||||
// Base64-encoded Yjs update; compacted into page_ydoc and pruned.
|
||||
{ name: "update", type: "text", required: true, max: 0 },
|
||||
{ name: "client_id", type: "text", max: 32 },
|
||||
...timestamps(),
|
||||
],
|
||||
indexes: ["CREATE INDEX idx_page_yupdates_page ON page_yupdates (page, created)"],
|
||||
});
|
||||
app.save(pageYupdates);
|
||||
},
|
||||
(app) => {
|
||||
for (const name of ["page_yupdates", "page_ydoc", "pages", "space_members", "spaces", "workspace_members", "workspaces"]) {
|
||||
try {
|
||||
app.delete(app.findCollectionByNameOrId(name));
|
||||
} catch (_) {}
|
||||
}
|
||||
const users = app.findCollectionByNameOrId("users");
|
||||
for (const name of ["status", "is_admin", "locale", "theme", "last_seen"]) {
|
||||
users.fields.removeByName(name);
|
||||
}
|
||||
users.authRule = "";
|
||||
app.save(users);
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user