bettersight-statement-extra.../pocketbase/pb_migrations/1_create_collections.js

97 lines
3.8 KiB
JavaScript

/// <reference path="../pb_data/types.d.ts" />
/**
* Schema only - no secrets here (see configure-secrets.sh for R2/Google,
* which must never be committed to a migration file).
*
* users: auth collection, self-registration open (createRule: "") -
* found or created defensively rather than assuming PocketBase ships a
* default one. OAuth2 is enabled here; the actual Google provider
* clientId/clientSecret are set by configure-secrets.sh at deploy time,
* never hardcoded into this file.
*
* projects/documents: both scoped to their own owner via
* `owner = @request.auth.id` on every rule - PocketBase enforces this
* per-record itself, no custom backend authorization code needed.
*
* documents.project is REQUIRED (not optional) - every document belongs
* to a project from the moment it's created, because a future per-
* project category-mapping feature needs that scoping from day one
* rather than retrofitted later. ensure_default_project.pb.js (a hook,
* not this migration) guarantees every user has somewhere for that
* relation to point the moment their account exists.
*/
migrate((app) => {
function findOrCreateUsersCollection() {
try {
return app.findCollectionByNameOrId("users")
} catch (e) {
let users = new Collection({
type: "auth",
name: "users",
createRule: "",
listRule: "id = @request.auth.id",
viewRule: "id = @request.auth.id",
updateRule: "id = @request.auth.id",
deleteRule: "id = @request.auth.id",
})
app.save(users)
return users
}
}
let users = findOrCreateUsersCollection()
users.createRule = ""
users.oauth2.enabled = true
app.save(users)
let projects = new Collection({
type: "base",
name: "projects",
listRule: "owner = @request.auth.id",
viewRule: "owner = @request.auth.id",
createRule: "owner = @request.auth.id",
updateRule: "owner = @request.auth.id",
deleteRule: "owner = @request.auth.id",
fields: [
{ type: "text", name: "name", required: true },
{ type: "relation", name: "owner", collectionId: users.id, required: true, maxSelect: 1 },
// Marks the one auto-created "General" project per user (see the
// hook) - lets the frontend find the fallback reliably even if the
// user renames it, and lets the UI refuse to delete it (the
// required documents.project relation must always have somewhere
// to point).
{ type: "bool", name: "isDefault" },
],
})
app.save(projects)
let documents = new Collection({
type: "base",
name: "documents",
listRule: "owner = @request.auth.id",
viewRule: "owner = @request.auth.id",
createRule: "owner = @request.auth.id",
updateRule: "owner = @request.auth.id",
deleteRule: "owner = @request.auth.id",
fields: [
{ type: "relation", name: "owner", collectionId: users.id, required: true, maxSelect: 1 },
{ type: "relation", name: "project", collectionId: projects.id, required: true, maxSelect: 1 },
{ type: "text", name: "docKey", required: true },
{ type: "text", name: "fileName", required: true },
// Stored in R2 once configure-secrets.sh enables S3 storage - see
// that script and the "Prerequisites" note in README.md.
{ type: "file", name: "sourceFile", required: false, maxSelect: 1 },
{ type: "json", name: "result", required: true },
{ type: "text", name: "openingBalanceInput" },
{ type: "text", name: "closingBalanceInput" },
],
})
app.save(documents)
}, (app) => {
// Down migration - removes everything this migration created, in
// reverse dependency order (documents/projects reference users).
try { app.delete(app.findCollectionByNameOrId("documents")) } catch (e) {}
try { app.delete(app.findCollectionByNameOrId("projects")) } catch (e) {}
})