@rfc0/patty 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +214 -2
- package/dist/adapters/access-tokens.js +7 -0
- package/dist/adapters/accounts.js +37 -0
- package/dist/adapters/auth.js +11 -0
- package/dist/adapters/bills.js +32 -0
- package/dist/adapters/budgets.js +29 -0
- package/dist/adapters/categories.js +32 -0
- package/dist/adapters/client-grants.js +7 -0
- package/dist/adapters/credit-cards.js +33 -0
- package/dist/adapters/currencies.js +2 -0
- package/dist/adapters/currency-rates.js +18 -0
- package/dist/adapters/dashboards.js +43 -0
- package/dist/adapters/demo.js +13 -0
- package/dist/adapters/instance-settings.js +7 -0
- package/dist/adapters/invites.js +7 -0
- package/dist/adapters/recurring.js +55 -0
- package/dist/adapters/sessions.js +7 -0
- package/dist/adapters/settings.js +4 -0
- package/dist/adapters/setup-codes.js +7 -0
- package/dist/adapters/tags.js +9 -0
- package/dist/adapters/transactions.js +95 -0
- package/dist/adapters/users.js +7 -0
- package/dist/common/build-version.js +5 -0
- package/dist/common/errors.js +118 -0
- package/dist/common/net.js +71 -0
- package/dist/common/result.js +7 -0
- package/dist/composition/auth.js +106 -0
- package/dist/composition/boot.js +27 -0
- package/dist/composition/cli.js +86 -0
- package/dist/composition/demo/seed.js +92 -0
- package/dist/composition/demo/shell.js +107 -0
- package/dist/composition/export.js +89 -0
- package/dist/composition/http.js +110 -0
- package/dist/composition/import.js +55 -0
- package/dist/composition/lazy-modules.js +2 -0
- package/dist/composition/runtime.js +115 -0
- package/dist/composition/sign-in/interpreter.js +154 -0
- package/dist/composition/sign-in/server.js +36 -0
- package/dist/composition/sign-in/wiring.js +23 -0
- package/dist/composition/stdio.js +69 -0
- package/dist/config.js +136 -0
- package/dist/controllers/account/account.js +88 -0
- package/dist/controllers/account/transaction.js +119 -0
- package/dist/controllers/account/transfer.js +34 -0
- package/dist/controllers/aggregate.js +40 -0
- package/dist/controllers/auth/access-tokens.js +61 -0
- package/dist/controllers/auth/grants.js +136 -0
- package/dist/controllers/auth/key-export.js +20 -0
- package/dist/controllers/auth/owner.js +80 -0
- package/dist/controllers/auth/password.js +35 -0
- package/dist/controllers/auth/session.js +50 -0
- package/dist/controllers/auth/step-up.js +8 -0
- package/dist/controllers/auth/users.js +183 -0
- package/dist/controllers/budgets.js +87 -0
- package/dist/controllers/categories.js +67 -0
- package/dist/controllers/correlations.js +112 -0
- package/dist/controllers/credit-card/bill.js +105 -0
- package/dist/controllers/credit-card/credit-card.js +129 -0
- package/dist/controllers/credit-card/transaction.js +181 -0
- package/dist/controllers/currencies.js +1 -0
- package/dist/controllers/currency-rates.js +40 -0
- package/dist/controllers/dashboards.js +68 -0
- package/dist/controllers/demo/seed.js +31 -0
- package/dist/controllers/demo/tour.js +12 -0
- package/dist/controllers/deps.js +6 -0
- package/dist/controllers/instance.js +6 -0
- package/dist/controllers/merchants.js +12 -0
- package/dist/controllers/recurring.js +70 -0
- package/dist/controllers/resolve.js +37 -0
- package/dist/controllers/settings.js +47 -0
- package/dist/controllers/status.js +19 -0
- package/dist/controllers/summary.js +21 -0
- package/dist/controllers/tags.js +19 -0
- package/dist/controllers/transaction/bulk.js +6 -0
- package/dist/controllers/transaction/core.js +49 -0
- package/dist/controllers/transaction/read.js +28 -0
- package/dist/diplomat/auth/credentials.js +58 -0
- package/dist/diplomat/auth/sign-in-store.js +18 -0
- package/dist/diplomat/cli/commands/auth.js +100 -0
- package/dist/diplomat/cli/commands/db.js +72 -0
- package/dist/diplomat/cli/commands/demo.js +68 -0
- package/dist/diplomat/cli/commands/key.js +119 -0
- package/dist/diplomat/cli/commands/login.js +151 -0
- package/dist/diplomat/cli/commands/setup.js +132 -0
- package/dist/diplomat/cli/commands/users.js +123 -0
- package/dist/diplomat/cli/commands.js +61 -0
- package/dist/diplomat/cli/export-file.js +37 -0
- package/dist/diplomat/cli/import-file.js +44 -0
- package/dist/diplomat/cli/io.js +61 -0
- package/dist/diplomat/cli/key-export-approval.js +37 -0
- package/dist/diplomat/cli/messages.js +96 -0
- package/dist/diplomat/cli/prompt.js +74 -0
- package/dist/diplomat/cli/terminal.js +81 -0
- package/dist/diplomat/client-config/clients.js +150 -0
- package/dist/diplomat/client-config/merge.js +41 -0
- package/dist/diplomat/db/bootstrap.js +98 -0
- package/dist/diplomat/db/database-files.js +147 -0
- package/dist/diplomat/db/driver.js +167 -0
- package/dist/diplomat/db/effects.js +101 -0
- package/dist/diplomat/db/engine.js +9 -0
- package/dist/diplomat/db/export.js +22 -0
- package/dist/diplomat/db/file-lock.js +82 -0
- package/dist/diplomat/db/key-lock.js +14 -0
- package/dist/diplomat/db/locale-cache.js +23 -0
- package/dist/diplomat/db/migration-barrier.js +57 -0
- package/dist/diplomat/db/migration-journal.js +18 -0
- package/dist/diplomat/db/migration-lock.js +15 -0
- package/dist/diplomat/db/migrations.js +11 -0
- package/dist/diplomat/db/present.js +17 -0
- package/dist/diplomat/db/queries/aggregate-whitelist.js +35 -0
- package/dist/diplomat/db/queries/filters.js +59 -0
- package/dist/diplomat/db/queries/fts.js +18 -0
- package/dist/diplomat/db/queries/scope.js +8 -0
- package/dist/diplomat/db/queries/transaction-select.js +26 -0
- package/dist/diplomat/db/queries/tx-predicates.js +13 -0
- package/dist/diplomat/db/repos/access-tokens.js +27 -0
- package/dist/diplomat/db/repos/accounts.js +91 -0
- package/dist/diplomat/db/repos/bills.js +93 -0
- package/dist/diplomat/db/repos/budgets.js +72 -0
- package/dist/diplomat/db/repos/categories.js +53 -0
- package/dist/diplomat/db/repos/client-grants.js +67 -0
- package/dist/diplomat/db/repos/correlations.js +66 -0
- package/dist/diplomat/db/repos/credit-cards.js +67 -0
- package/dist/diplomat/db/repos/currencies.js +8 -0
- package/dist/diplomat/db/repos/currency-rates.js +40 -0
- package/dist/diplomat/db/repos/dashboards.js +123 -0
- package/dist/diplomat/db/repos/health.js +12 -0
- package/dist/diplomat/db/repos/instance-settings.js +14 -0
- package/dist/diplomat/db/repos/instance.js +58 -0
- package/dist/diplomat/db/repos/invites.js +43 -0
- package/dist/diplomat/db/repos/merchants.js +121 -0
- package/dist/diplomat/db/repos/recurring.js +29 -0
- package/dist/diplomat/db/repos/sessions.js +19 -0
- package/dist/diplomat/db/repos/settings.js +41 -0
- package/dist/diplomat/db/repos/setup-codes.js +28 -0
- package/dist/diplomat/db/repos/sub-categories.js +67 -0
- package/dist/diplomat/db/repos/tags.js +51 -0
- package/dist/diplomat/db/repos/transaction-tags.js +57 -0
- package/dist/diplomat/db/repos/transactions.js +163 -0
- package/dist/diplomat/db/repos/users.js +47 -0
- package/dist/diplomat/db/safety-copy.js +87 -0
- package/dist/diplomat/db/schema.js +348 -0
- package/dist/diplomat/db/seed/categories.js +112 -0
- package/dist/diplomat/db/seed/currencies.js +9 -0
- package/dist/diplomat/db/seed/demo/br.js +98 -0
- package/dist/diplomat/db/seed/demo/co.js +89 -0
- package/dist/diplomat/db/seed/demo/index.js +12 -0
- package/dist/diplomat/db/seed/demo/mx.js +90 -0
- package/dist/diplomat/db/seed/demo/shared.js +39 -0
- package/dist/diplomat/db/seed/demo/us.js +90 -0
- package/dist/diplomat/db/seed/merchant-rules.js +188 -0
- package/dist/diplomat/db/sqlite-errors.js +40 -0
- package/dist/diplomat/db/tenancy-integrity.js +45 -0
- package/dist/diplomat/db/version-guard.js +16 -0
- package/dist/diplomat/http/auth-pages.js +41 -0
- package/dist/diplomat/http/auth-paths.js +46 -0
- package/dist/diplomat/http/bearer.js +15 -0
- package/dist/diplomat/http/connect-pages.js +51 -0
- package/dist/diplomat/http/context.js +17 -0
- package/dist/diplomat/http/cookies.js +27 -0
- package/dist/diplomat/http/form-pages.js +38 -0
- package/dist/diplomat/http/hooks/auth.js +106 -0
- package/dist/diplomat/http/hooks/demo-marker.js +59 -0
- package/dist/diplomat/http/openapi.js +73 -0
- package/dist/diplomat/http/principal.js +1 -0
- package/dist/diplomat/http/rate-limiter.js +78 -0
- package/dist/diplomat/http/remedy.js +26 -0
- package/dist/diplomat/http/respond.js +50 -0
- package/dist/diplomat/http/routes/account.js +226 -0
- package/dist/diplomat/http/routes/aggregate.js +28 -0
- package/dist/diplomat/http/routes/auth.js +105 -0
- package/dist/diplomat/http/routes/budget.js +34 -0
- package/dist/diplomat/http/routes/category.js +109 -0
- package/dist/diplomat/http/routes/connect.js +124 -0
- package/dist/diplomat/http/routes/credit-card.js +263 -0
- package/dist/diplomat/http/routes/currency-rates.js +60 -0
- package/dist/diplomat/http/routes/currency.js +16 -0
- package/dist/diplomat/http/routes/dashboard.js +93 -0
- package/dist/diplomat/http/routes/invite.js +63 -0
- package/dist/diplomat/http/routes/recurring.js +65 -0
- package/dist/diplomat/http/routes/settings.js +38 -0
- package/dist/diplomat/http/routes/summary.js +23 -0
- package/dist/diplomat/http/routes/system.js +29 -0
- package/dist/diplomat/http/routes/tag.js +51 -0
- package/dist/diplomat/http/routes/tokens.js +72 -0
- package/dist/diplomat/http/routes/transaction.js +55 -0
- package/dist/diplomat/http/routes/transfer.js +35 -0
- package/dist/diplomat/http/routes/ui.js +110 -0
- package/dist/diplomat/http/routes/users.js +156 -0
- package/dist/diplomat/http/routes/well-known.js +26 -0
- package/dist/diplomat/http/routes.js +85 -0
- package/dist/diplomat/http/session.js +34 -0
- package/dist/diplomat/http/token-pages.js +75 -0
- package/dist/diplomat/http/ui-files.js +21 -0
- package/dist/diplomat/http/user-pages.js +93 -0
- package/dist/diplomat/mcp/annotations.js +8 -0
- package/dist/diplomat/mcp/auth-info.js +20 -0
- package/dist/diplomat/mcp/context.js +14 -0
- package/dist/diplomat/mcp/definition.js +3 -0
- package/dist/diplomat/mcp/gate.js +15 -0
- package/dist/diplomat/mcp/instructions.js +19 -0
- package/dist/diplomat/mcp/prompts.js +125 -0
- package/dist/diplomat/mcp/register.js +12 -0
- package/dist/diplomat/mcp/remedy.js +17 -0
- package/dist/diplomat/mcp/resources.js +93 -0
- package/dist/diplomat/mcp/respond.js +20 -0
- package/dist/diplomat/mcp/route.js +62 -0
- package/dist/diplomat/mcp/scopes.js +53 -0
- package/dist/diplomat/mcp/server.js +26 -0
- package/dist/diplomat/mcp/shared.js +5 -0
- package/dist/diplomat/mcp/stdio-session.js +94 -0
- package/dist/diplomat/mcp/stdio.js +23 -0
- package/dist/diplomat/mcp/tool-name.js +59 -0
- package/dist/diplomat/mcp/tools/account.js +218 -0
- package/dist/diplomat/mcp/tools/aggregate.js +32 -0
- package/dist/diplomat/mcp/tools/budget.js +99 -0
- package/dist/diplomat/mcp/tools/category.js +103 -0
- package/dist/diplomat/mcp/tools/correlations.js +70 -0
- package/dist/diplomat/mcp/tools/credit-card.js +239 -0
- package/dist/diplomat/mcp/tools/currency-rates.js +62 -0
- package/dist/diplomat/mcp/tools/dashboard.js +75 -0
- package/dist/diplomat/mcp/tools/index.js +31 -0
- package/dist/diplomat/mcp/tools/recurring.js +62 -0
- package/dist/diplomat/mcp/tools/summary.js +30 -0
- package/dist/diplomat/mcp/tools/system.js +83 -0
- package/dist/diplomat/mcp/tools/tag.js +48 -0
- package/dist/diplomat/mcp/tools/transaction.js +53 -0
- package/dist/diplomat/mcp/tools/transfer.js +28 -0
- package/dist/diplomat/mcp/tour.js +87 -0
- package/dist/logic/account.js +79 -0
- package/dist/logic/aggregate.js +37 -0
- package/dist/logic/auth/bearer.js +35 -0
- package/dist/logic/auth/grant.js +126 -0
- package/dist/logic/auth/key-export.js +10 -0
- package/dist/logic/auth/keychain.js +3 -0
- package/dist/logic/auth/message.js +22 -0
- package/dist/logic/auth/origin.js +57 -0
- package/dist/logic/auth/owner.js +22 -0
- package/dist/logic/auth/password.js +3 -0
- package/dist/logic/auth/redact.js +25 -0
- package/dist/logic/auth/revoke.js +28 -0
- package/dist/logic/auth/scope.js +17 -0
- package/dist/logic/auth/session.js +30 -0
- package/dist/logic/auth/sign-in-flow.js +305 -0
- package/dist/logic/auth/time.js +2 -0
- package/dist/logic/auth/token.js +74 -0
- package/dist/logic/auth/user-plans.js +84 -0
- package/dist/logic/auth/users.js +78 -0
- package/dist/logic/balance.js +2 -0
- package/dist/logic/bill.js +74 -0
- package/dist/logic/budget.js +78 -0
- package/dist/logic/category.js +37 -0
- package/dist/logic/classify.js +21 -0
- package/dist/logic/correlations.js +147 -0
- package/dist/logic/credit-card.js +2 -0
- package/dist/logic/currency-rate.js +69 -0
- package/dist/logic/dashboard.js +40 -0
- package/dist/logic/demo/database.js +30 -0
- package/dist/logic/demo/plan.js +308 -0
- package/dist/logic/demo/tour.js +33 -0
- package/dist/logic/export-destination.js +12 -0
- package/dist/logic/key-import.js +15 -0
- package/dist/logic/locale.js +5 -0
- package/dist/logic/localize.js +12 -0
- package/dist/logic/merchant.js +31 -0
- package/dist/logic/migration.js +1 -0
- package/dist/logic/money.js +12 -0
- package/dist/logic/period.js +45 -0
- package/dist/logic/persistence.js +48 -0
- package/dist/logic/recurring.js +51 -0
- package/dist/logic/resolve.js +44 -0
- package/dist/logic/summary.js +29 -0
- package/dist/logic/tag.js +13 -0
- package/dist/logic/transaction.js +197 -0
- package/dist/main.js +42 -0
- package/dist/models/account.js +197 -0
- package/dist/models/aggregate.js +61 -0
- package/dist/models/auth/bearer.js +11 -0
- package/dist/models/auth/entity.js +17 -0
- package/dist/models/auth/errors.js +42 -0
- package/dist/models/auth/grant.js +58 -0
- package/dist/models/auth/key-export.js +11 -0
- package/dist/models/auth/message.js +50 -0
- package/dist/models/auth/mode.js +7 -0
- package/dist/models/auth/origin.js +16 -0
- package/dist/models/auth/owner.js +36 -0
- package/dist/models/auth/password.js +24 -0
- package/dist/models/auth/principal.js +26 -0
- package/dist/models/auth/redact.js +4 -0
- package/dist/models/auth/scope.js +9 -0
- package/dist/models/auth/session.js +19 -0
- package/dist/models/auth/sign-in-flow.js +102 -0
- package/dist/models/auth/sign-in.js +41 -0
- package/dist/models/auth/time.js +5 -0
- package/dist/models/auth/token.js +50 -0
- package/dist/models/auth/users.js +52 -0
- package/dist/models/bill.js +55 -0
- package/dist/models/budget.js +106 -0
- package/dist/models/category.js +52 -0
- package/dist/models/client-config.js +18 -0
- package/dist/models/correlations.js +215 -0
- package/dist/models/credit-card.js +156 -0
- package/dist/models/currency-rate.js +83 -0
- package/dist/models/dashboard.js +54 -0
- package/dist/models/database.js +16 -0
- package/dist/models/date.js +8 -0
- package/dist/models/demo/core.js +58 -0
- package/dist/models/demo/tour.js +41 -0
- package/dist/models/encryption-key.js +2 -0
- package/dist/models/error.js +13 -0
- package/dist/models/export.js +9 -0
- package/dist/models/id.js +2 -0
- package/dist/models/instance.js +7 -0
- package/dist/models/key-import.js +6 -0
- package/dist/models/locale.js +25 -0
- package/dist/models/merchant.js +12 -0
- package/dist/models/money.js +14 -0
- package/dist/models/package.js +12 -0
- package/dist/models/page.js +56 -0
- package/dist/models/persistence.js +1 -0
- package/dist/models/prompts.js +5 -0
- package/dist/models/recurring.js +54 -0
- package/dist/models/remedy.js +17 -0
- package/dist/models/resolution.js +1 -0
- package/dist/models/startup.js +7 -0
- package/dist/models/summary.js +24 -0
- package/dist/models/tag.js +14 -0
- package/dist/models/tenancy.js +10 -0
- package/dist/models/transaction-tag.js +6 -0
- package/dist/models/transaction.js +275 -0
- package/dist/wire/db/access-tokens.js +1 -0
- package/dist/wire/db/accounts.js +7 -0
- package/dist/wire/db/aggregate.js +16 -0
- package/dist/wire/db/bills.js +14 -0
- package/dist/wire/db/budgets.js +33 -0
- package/dist/wire/db/categories.js +14 -0
- package/dist/wire/db/client-grants.js +1 -0
- package/dist/wire/db/correlations.js +49 -0
- package/dist/wire/db/credit-cards.js +1 -0
- package/dist/wire/db/currencies.js +1 -0
- package/dist/wire/db/currency-rates.js +1 -0
- package/dist/wire/db/dashboards.js +1 -0
- package/dist/wire/db/instance-settings.js +1 -0
- package/dist/wire/db/invites.js +1 -0
- package/dist/wire/db/merchants.js +18 -0
- package/dist/wire/db/recurring.js +19 -0
- package/dist/wire/db/sessions.js +1 -0
- package/dist/wire/db/settings.js +1 -0
- package/dist/wire/db/setup-codes.js +1 -0
- package/dist/wire/db/tags.js +1 -0
- package/dist/wire/db/tenancy.js +1 -0
- package/dist/wire/db/transactions.js +89 -0
- package/dist/wire/db/users.js +1 -0
- package/dist/wire/in/accounts.js +26 -0
- package/dist/wire/in/aggregate.js +10 -0
- package/dist/wire/in/auth.js +107 -0
- package/dist/wire/in/bills.js +11 -0
- package/dist/wire/in/budgets.js +11 -0
- package/dist/wire/in/categories.js +9 -0
- package/dist/wire/in/cli.js +106 -0
- package/dist/wire/in/coerce.js +6 -0
- package/dist/wire/in/common.js +17 -0
- package/dist/wire/in/credit-cards.js +16 -0
- package/dist/wire/in/currency-rates.js +8 -0
- package/dist/wire/in/period.js +7 -0
- package/dist/wire/in/recurring.js +6 -0
- package/dist/wire/in/settings.js +5 -0
- package/dist/wire/in/summary.js +5 -0
- package/dist/wire/in/tags.js +2 -0
- package/dist/wire/in/transactions.js +31 -0
- package/dist/wire/out/bills.js +9 -0
- package/dist/wire/out/budgets.js +10 -0
- package/dist/wire/out/client-config.js +2 -0
- package/dist/wire/out/common.js +4 -0
- package/dist/wire/out/credit-cards.js +15 -0
- package/dist/wire/out/currency-rates.js +11 -0
- package/dist/wire/out/errors.js +58 -0
- package/dist/wire/out/recurring.js +6 -0
- package/dist/wire/out/status.js +10 -0
- package/dist/wire/out/summary.js +9 -0
- package/dist/wire/out/transactions.js +52 -0
- package/drizzle/0000_futuristic_umar.sql +124 -0
- package/drizzle/0001_fts5_search.sql +23 -0
- package/drizzle/0002_rename_graph_to_history.sql +7 -0
- package/drizzle/0003_lyrical_hairball.sql +2 -0
- package/drizzle/0004_misty_ikaris.sql +7 -0
- package/drizzle/0005_credit-liability-accounts.sql +14 -0
- package/drizzle/0006_smart_aqueduct.sql +1 -0
- package/drizzle/0007_add_dashboards.sql +5 -0
- package/drizzle/0008_yummy_madripoor.sql +1 -0
- package/drizzle/0009_version_groundwork.sql +9 -0
- package/drizzle/0010_category_merchant_association_cascade.sql +16 -0
- package/drizzle/0011_dashboard_id_normalization.sql +16 -0
- package/drizzle/0012_hot_the_spike.sql +12 -0
- package/drizzle/0013_recurring_rules_merchant.sql +1 -0
- package/drizzle/0014_dashboard_source_discriminator.sql +17 -0
- package/drizzle/0015_currencies_decimals.sql +1 -0
- package/drizzle/0016_unusual_zaran.sql +13 -0
- package/drizzle/0017_premium_blur.sql +1 -0
- package/drizzle/0018_auth_identity.sql +39 -0
- package/drizzle/0019_access_tokens.sql +16 -0
- package/drizzle/0020_stdio_sign_in.sql +27 -0
- package/drizzle/0021_tenancy_data.sql +289 -0
- package/drizzle/0022_user_management.sql +17 -0
- package/drizzle/0023_instance_settings_row.sql +2 -0
- package/drizzle/0024_credentials_changed_at.sql +2 -0
- package/drizzle/0025_instance_kind.sql +2 -0
- package/drizzle/0026_demo_seeded_at.sql +2 -0
- package/drizzle/0027_demo_country.sql +2 -0
- package/drizzle/0028_reset_sign_ins.sql +2 -0
- package/drizzle/meta/0000_snapshot.json +894 -0
- package/drizzle/meta/0003_snapshot.json +910 -0
- package/drizzle/meta/0004_snapshot.json +951 -0
- package/drizzle/meta/0005_snapshot.json +991 -0
- package/drizzle/meta/0006_snapshot.json +998 -0
- package/drizzle/meta/0007_snapshot.json +1029 -0
- package/drizzle/meta/0008_snapshot.json +1049 -0
- package/drizzle/meta/0009_snapshot.json +1121 -0
- package/drizzle/meta/0010_snapshot.json +1121 -0
- package/drizzle/meta/0011_snapshot.json +1136 -0
- package/drizzle/meta/0012_snapshot.json +1227 -0
- package/drizzle/meta/0013_snapshot.json +1247 -0
- package/drizzle/meta/0014_snapshot.json +1267 -0
- package/drizzle/meta/0015_snapshot.json +1275 -0
- package/drizzle/meta/0016_snapshot.json +1371 -0
- package/drizzle/meta/0017_snapshot.json +1392 -0
- package/drizzle/meta/0018_snapshot.json +1603 -0
- package/drizzle/meta/0019_snapshot.json +1713 -0
- package/drizzle/meta/0020_snapshot.json +1904 -0
- package/drizzle/meta/0021_snapshot.json +2186 -0
- package/drizzle/meta/0022_snapshot.json +2311 -0
- package/drizzle/meta/0023_snapshot.json +2311 -0
- package/drizzle/meta/0024_snapshot.json +2318 -0
- package/drizzle/meta/0025_snapshot.json +2326 -0
- package/drizzle/meta/0026_snapshot.json +2333 -0
- package/drizzle/meta/0027_snapshot.json +2340 -0
- package/drizzle/meta/0028_snapshot.json +2340 -0
- package/drizzle/meta/_journal.json +209 -0
- package/package.json +106 -4
- package/skills/dashboard-building/SKILL.md +201 -0
- package/skills/patty-tour/SKILL.md +34 -0
- package/ui/app.css +758 -0
- package/ui/app.js +493 -0
- package/ui/connect-result.html +16 -0
- package/ui/connect.html +53 -0
- package/ui/dashboards/accounts.html +3189 -0
- package/ui/dashboards/budget.html +1716 -0
- package/ui/dashboards/cards.html +4032 -0
- package/ui/dashboards/categories.html +4694 -0
- package/ui/dashboards/comparative.html +2464 -0
- package/ui/dashboards/dashboards.html +839 -0
- package/ui/dashboards/overview.html +2405 -0
- package/ui/dashboards/pending.html +2973 -0
- package/ui/dashboards/projections.html +2517 -0
- package/ui/dashboards/tags.html +1365 -0
- package/ui/invite.html +44 -0
- package/ui/login.html +26 -0
- package/ui/money.js +175 -0
- package/ui/setup.html +29 -0
- package/ui/shell.html +96 -0
- package/ui/token-created.html +24 -0
- package/ui/tokens.html +58 -0
- package/ui/user-invited.html +27 -0
- package/ui/users.html +60 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { and, eq, inArray, notInArray } from 'drizzle-orm';
|
|
2
|
+
import { tags, transactions, transactionTags } from '../schema.js';
|
|
3
|
+
import { isForeignKeyConstraintViolation } from '../sqlite-errors.js';
|
|
4
|
+
// Insert first, delete-stale second, so an FK miss on the insert (see the applier note below)
|
|
5
|
+
// fires before anything was removed — a set that can't apply leaves the existing links exactly as
|
|
6
|
+
// they were. A link already present just hits the (transactionId, tagId) primary key and no-ops.
|
|
7
|
+
const replaceLinks = async (tx, transactionId, tagIds) => {
|
|
8
|
+
if (tagIds.length > 0)
|
|
9
|
+
await tx
|
|
10
|
+
.insert(transactionTags)
|
|
11
|
+
.values(tagIds.map((tagId) => ({ transactionId, tagId })))
|
|
12
|
+
.onConflictDoNothing({ target: [transactionTags.transactionId, transactionTags.tagId] });
|
|
13
|
+
const stale = tagIds.length > 0
|
|
14
|
+
? and(eq(transactionTags.transactionId, transactionId), notInArray(transactionTags.tagId, tagIds))
|
|
15
|
+
: eq(transactionTags.transactionId, transactionId);
|
|
16
|
+
await tx.delete(transactionTags).where(stale);
|
|
17
|
+
};
|
|
18
|
+
/** No owner column here, so the applier checks the transaction and every tag are the user's; else it is a no-op. */
|
|
19
|
+
const belongsToUser = async (tx, userId, transactionId, tagIds) => {
|
|
20
|
+
const [transaction] = await tx
|
|
21
|
+
.select({ id: transactions.id })
|
|
22
|
+
.from(transactions)
|
|
23
|
+
.where(and(eq(transactions.id, transactionId), eq(transactions.userId, userId)));
|
|
24
|
+
if (!transaction)
|
|
25
|
+
return false;
|
|
26
|
+
const distinct = [...new Set(tagIds)];
|
|
27
|
+
if (distinct.length === 0)
|
|
28
|
+
return true;
|
|
29
|
+
const owned = await tx
|
|
30
|
+
.select({ id: tags.id })
|
|
31
|
+
.from(tags)
|
|
32
|
+
.where(and(inArray(tags.id, distinct), eq(tags.userId, userId)));
|
|
33
|
+
return owned.length === distinct.length;
|
|
34
|
+
};
|
|
35
|
+
export const transactionTagApplier = (userId) => ({
|
|
36
|
+
// A missing parent (a duplicate-key replay never inserted its id) or someone else's makes the set a no-op.
|
|
37
|
+
insert: async (tx, model) => {
|
|
38
|
+
const { transactionId, tagIds } = model;
|
|
39
|
+
if (!(await belongsToUser(tx, userId, transactionId, tagIds)))
|
|
40
|
+
return { applied: false };
|
|
41
|
+
try {
|
|
42
|
+
await replaceLinks(tx, transactionId, tagIds);
|
|
43
|
+
return { applied: true };
|
|
44
|
+
}
|
|
45
|
+
catch (cause) {
|
|
46
|
+
if (isForeignKeyConstraintViolation(cause))
|
|
47
|
+
return { applied: false };
|
|
48
|
+
throw cause;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
update: async () => {
|
|
52
|
+
throw new Error('transactionTagApplier: update is unsupported — every write is an insert (set-replace)');
|
|
53
|
+
},
|
|
54
|
+
delete: async () => {
|
|
55
|
+
throw new Error('transactionTagApplier: delete is unsupported — links are removed via the transaction cascade');
|
|
56
|
+
},
|
|
57
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import * as transactionAdapters from '../../../adapters/transactions.js';
|
|
4
|
+
import { expectPresent, rowNotFoundAfterWrite } from '../present.js';
|
|
5
|
+
import { SortDirection } from '../../../models/page.js';
|
|
6
|
+
import { TransactionType, } from '../../../models/transaction.js';
|
|
7
|
+
import { AggregateDbRowSchema } from '../../../wire/db/aggregate.js';
|
|
8
|
+
import { CategorySpendRowSchema, CategoryWindowedSpendRowSchema, ORDER_BY_COLUMN, SummaryRowSchema, } from '../../../wire/db/transactions.js';
|
|
9
|
+
import { GROUP_BY_MAP, METRIC_MAP } from '../queries/aggregate-whitelist.js';
|
|
10
|
+
import { buildCommonWhere, buildSearchWhere, toSearchFilters } from '../queries/filters.js';
|
|
11
|
+
import { ownedBy } from '../queries/scope.js';
|
|
12
|
+
import { transactions } from '../schema.js';
|
|
13
|
+
import { fullTxSelect } from '../queries/transaction-select.js';
|
|
14
|
+
import { txPredicates } from '../queries/tx-predicates.js';
|
|
15
|
+
import { ownedTableApplier } from '../effects.js';
|
|
16
|
+
const SearchCountRowSchema = z.object({ count: z.number().int() });
|
|
17
|
+
const fetchTx = async (db, userId, id) => {
|
|
18
|
+
const [row] = await db.all(fullTxSelect(userId, sql `t.id = ${id}`));
|
|
19
|
+
return row ? transactionAdapters.dbToModel(row) : null;
|
|
20
|
+
};
|
|
21
|
+
const orderByClause = (sort, dir) => {
|
|
22
|
+
const column = sql.raw(ORDER_BY_COLUMN[sort]);
|
|
23
|
+
const direction = dir === SortDirection.enum.asc ? sql `ASC` : sql `DESC`;
|
|
24
|
+
return sql `${column} ${direction}, t.id ${direction}`;
|
|
25
|
+
};
|
|
26
|
+
const keysetPredicate = (sort, dir, key) => {
|
|
27
|
+
const column = sql.raw(ORDER_BY_COLUMN[sort]);
|
|
28
|
+
const cmp = dir === SortDirection.enum.asc ? sql `>` : sql `<`;
|
|
29
|
+
return sql `(${column} ${cmp} ${key.value} OR (${column} = ${key.value} AND t.id ${cmp} ${key.id}))`;
|
|
30
|
+
};
|
|
31
|
+
export const correlatedToCard = (cardId, accountId, alias = '') => {
|
|
32
|
+
const col = (name) => sql.raw(alias ? `${alias}.${name}` : name);
|
|
33
|
+
return sql `${col('credit_card_id')} = ${cardId} OR ${col('from_account_id')} = ${accountId} OR ${col('to_account_id')} = ${accountId}`;
|
|
34
|
+
};
|
|
35
|
+
const findAllCorrelatedToCard = async ({ db, userId }, cardId, accountId) => {
|
|
36
|
+
const rows = await db.all(fullTxSelect(userId, correlatedToCard(cardId, accountId, 't')));
|
|
37
|
+
return rows.map(transactionAdapters.dbToModel);
|
|
38
|
+
};
|
|
39
|
+
const search = async ({ db, userId }, input, refs) => {
|
|
40
|
+
const filters = toSearchFilters(input, refs);
|
|
41
|
+
const conditions = buildSearchWhere(filters, userId);
|
|
42
|
+
if (filters.cursorKey)
|
|
43
|
+
conditions.push(keysetPredicate(filters.sort, filters.dir, filters.cursorKey));
|
|
44
|
+
const where = sql.join(conditions, sql ` AND `);
|
|
45
|
+
const rows = await db.all(sql `
|
|
46
|
+
${fullTxSelect(userId, where)}
|
|
47
|
+
ORDER BY ${orderByClause(filters.sort, filters.dir)}
|
|
48
|
+
LIMIT ${filters.limit + 1}
|
|
49
|
+
`);
|
|
50
|
+
const hasMore = rows.length > filters.limit;
|
|
51
|
+
return { items: rows.slice(0, filters.limit).map(transactionAdapters.dbToModel), hasMore };
|
|
52
|
+
};
|
|
53
|
+
const count = async ({ db, userId }, input, refs) => {
|
|
54
|
+
const filters = toSearchFilters(input, refs);
|
|
55
|
+
const where = sql.join(buildSearchWhere(filters, userId), sql ` AND `);
|
|
56
|
+
const [row] = await db.all(sql `SELECT COUNT(*) AS count FROM transactions t WHERE ${where}`);
|
|
57
|
+
return SearchCountRowSchema.parse(row).count;
|
|
58
|
+
};
|
|
59
|
+
const aggregate = async ({ db, userId }, filters) => {
|
|
60
|
+
const g = GROUP_BY_MAP[filters.groupBy];
|
|
61
|
+
const metric = METRIC_MAP[filters.metric];
|
|
62
|
+
const conditions = buildCommonWhere(filters, userId);
|
|
63
|
+
conditions.push(txPredicates.effective());
|
|
64
|
+
conditions.push(txPredicates.nonTransfer());
|
|
65
|
+
const where = sql.join(conditions, sql ` AND `);
|
|
66
|
+
const labelNames = g.labelNames ? sql `, ${g.labelNames} AS label_names` : sql `, NULL AS label_names`;
|
|
67
|
+
const rows = await db.all(sql `
|
|
68
|
+
SELECT ${g.expr} AS key, ${g.label} AS label${labelNames},
|
|
69
|
+
t.currency, ${metric} AS value, COUNT(*) AS count
|
|
70
|
+
FROM transactions t
|
|
71
|
+
${g.join}
|
|
72
|
+
WHERE ${where}
|
|
73
|
+
GROUP BY t.currency, key
|
|
74
|
+
`);
|
|
75
|
+
return rows.map((r) => AggregateDbRowSchema.parse(r));
|
|
76
|
+
};
|
|
77
|
+
const summaryRows = async ({ db, userId }, from, to) => {
|
|
78
|
+
const rows = await db.all(sql `
|
|
79
|
+
SELECT t.currency, t.type, c.name AS category, c.names AS category_names,
|
|
80
|
+
SUM(t.amount) AS total, COUNT(*) AS count
|
|
81
|
+
FROM transactions t
|
|
82
|
+
LEFT JOIN categories c ON c.id = t.category_id
|
|
83
|
+
WHERE ${ownedBy(userId, 't')}
|
|
84
|
+
AND t.date >= ${from} AND t.date < ${to}
|
|
85
|
+
AND ${txPredicates.nonTransfer()}
|
|
86
|
+
AND ${txPredicates.effective()}
|
|
87
|
+
GROUP BY t.currency, t.type, c.id
|
|
88
|
+
`);
|
|
89
|
+
return rows.map((r) => SummaryRowSchema.parse(r));
|
|
90
|
+
};
|
|
91
|
+
const pendingBalanceParts = async ({ db, userId }, accountId, asOf) => {
|
|
92
|
+
const [row] = await db.all(sql `
|
|
93
|
+
SELECT
|
|
94
|
+
COALESCE(SUM(CASE WHEN t.type = ${TransactionType.enum.income} THEN t.amount ELSE 0 END), 0) AS income,
|
|
95
|
+
COALESCE(SUM(CASE WHEN t.type = ${TransactionType.enum.expense} THEN t.amount ELSE 0 END), 0) AS expenses,
|
|
96
|
+
COALESCE(SUM(CASE WHEN t.type = ${TransactionType.enum.transfer} THEN t.amount ELSE 0 END), 0) AS transfers_out,
|
|
97
|
+
COALESCE((
|
|
98
|
+
SELECT SUM(t2.amount) FROM transactions t2
|
|
99
|
+
WHERE ${ownedBy(userId, 't2')}
|
|
100
|
+
AND t2.to_account_id = ${accountId}
|
|
101
|
+
AND t2.type = ${TransactionType.enum.transfer}
|
|
102
|
+
AND ${txPredicates.pending('t2')}
|
|
103
|
+
AND t2.date <= ${asOf}
|
|
104
|
+
), 0) AS transfers_in
|
|
105
|
+
FROM transactions t
|
|
106
|
+
WHERE ${ownedBy(userId, 't')}
|
|
107
|
+
AND t.from_account_id = ${accountId}
|
|
108
|
+
AND ${txPredicates.pending()}
|
|
109
|
+
AND t.date <= ${asOf}
|
|
110
|
+
`);
|
|
111
|
+
return {
|
|
112
|
+
income: row?.income ?? 0,
|
|
113
|
+
expenses: row?.expenses ?? 0,
|
|
114
|
+
transfersIn: row?.transfers_in ?? 0,
|
|
115
|
+
transfersOut: row?.transfers_out ?? 0,
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
const spendByCategory = async ({ db, userId }, from, to) => {
|
|
119
|
+
const rows = await db.all(sql `
|
|
120
|
+
SELECT t.currency, t.category_id, t.subcategory_id,
|
|
121
|
+
COALESCE(SUM(CASE WHEN ${txPredicates.effective()} THEN t.amount END), 0) AS effective,
|
|
122
|
+
COALESCE(SUM(CASE WHEN ${txPredicates.pending()} THEN t.amount END), 0) AS pending
|
|
123
|
+
FROM transactions t
|
|
124
|
+
WHERE ${ownedBy(userId, 't')}
|
|
125
|
+
AND ${txPredicates.expense()}
|
|
126
|
+
AND ${txPredicates.committed()}
|
|
127
|
+
AND t.date >= ${from} AND t.date < ${to}
|
|
128
|
+
AND t.category_id IS NOT NULL
|
|
129
|
+
GROUP BY t.currency, t.category_id, t.subcategory_id
|
|
130
|
+
`);
|
|
131
|
+
return rows.map((r) => CategorySpendRowSchema.parse(r));
|
|
132
|
+
};
|
|
133
|
+
const spendByCategoryWindowed = async ({ db, userId }, baselineFrom, currentFrom, currentTo) => {
|
|
134
|
+
const rows = await db.all(sql `
|
|
135
|
+
SELECT t.currency, t.category_id, t.subcategory_id,
|
|
136
|
+
SUM(CASE WHEN t.date >= ${currentFrom} THEN t.amount ELSE 0 END) AS current,
|
|
137
|
+
SUM(CASE WHEN t.date < ${currentFrom} THEN t.amount ELSE 0 END) AS baseline_total
|
|
138
|
+
FROM transactions t
|
|
139
|
+
WHERE ${ownedBy(userId, 't')}
|
|
140
|
+
AND ${txPredicates.expense()}
|
|
141
|
+
AND ${txPredicates.effective()}
|
|
142
|
+
AND t.date >= ${baselineFrom} AND t.date < ${currentTo}
|
|
143
|
+
AND t.category_id IS NOT NULL
|
|
144
|
+
GROUP BY t.currency, t.category_id, t.subcategory_id
|
|
145
|
+
`);
|
|
146
|
+
return rows.map((r) => CategoryWindowedSpendRowSchema.parse(r));
|
|
147
|
+
};
|
|
148
|
+
export const transactionsRepo = (db, userId) => {
|
|
149
|
+
const scope = { db, userId };
|
|
150
|
+
return {
|
|
151
|
+
getById: (id) => fetchTx(db, userId, id),
|
|
152
|
+
findAllCorrelatedToCard: (cardId, accountId) => findAllCorrelatedToCard(scope, cardId, accountId),
|
|
153
|
+
search: (input, refs) => search(scope, input, refs),
|
|
154
|
+
count: (input, refs) => count(scope, input, refs),
|
|
155
|
+
aggregate: (filters) => aggregate(scope, filters),
|
|
156
|
+
summaryRows: (from, to) => summaryRows(scope, from, to),
|
|
157
|
+
pendingBalanceParts: (accountId, asOf) => pendingBalanceParts(scope, accountId, asOf),
|
|
158
|
+
spendByCategory: (from, to) => spendByCategory(scope, from, to),
|
|
159
|
+
spendByCategoryWindowed: (baselineFrom, currentFrom, currentTo) => spendByCategoryWindowed(scope, baselineFrom, currentFrom, currentTo),
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
const readBackFor = (userId) => async (tx, id) => expectPresent(await fetchTx(tx, userId, id), rowNotFoundAfterWrite('transactions', 'write'));
|
|
163
|
+
export const transactionApplier = (userId) => ownedTableApplier(transactions, userId, { readBack: readBackFor(userId) });
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { asc, eq } from 'drizzle-orm';
|
|
2
|
+
import * as userAdapters from '../../../adapters/users.js';
|
|
3
|
+
import { users } from '../schema.js';
|
|
4
|
+
import { isUniqueConstraintViolation } from '../sqlite-errors.js';
|
|
5
|
+
import { VersionConflict, versionedTableApplier } from '../effects.js';
|
|
6
|
+
export const usersRepo = (db) => ({
|
|
7
|
+
findById: async (id) => {
|
|
8
|
+
const [row] = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
|
9
|
+
return row ? userAdapters.dbToModel(row) : null;
|
|
10
|
+
},
|
|
11
|
+
findByUsername: async (username) => {
|
|
12
|
+
const [row] = await db.select().from(users).where(eq(users.username, username)).limit(1);
|
|
13
|
+
return row ? userAdapters.dbToModel(row) : null;
|
|
14
|
+
},
|
|
15
|
+
listAll: async () => {
|
|
16
|
+
const rows = await db.select().from(users).orderBy(asc(users.createdAt), asc(users.id));
|
|
17
|
+
return rows.map(userAdapters.dbToModel);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
const versionedUserApplier = versionedTableApplier(users);
|
|
21
|
+
/** `users.username` is unique: two admins inviting the same name at once both insert it. The loser's unique
|
|
22
|
+
* violation is
|
|
23
|
+
* rethrown as `VersionConflict`, so the engine re-runs the flow, which then reports `username_taken`. */
|
|
24
|
+
export const userApplier = {
|
|
25
|
+
...versionedUserApplier,
|
|
26
|
+
insert: async (tx, model, idempotencyKey) => {
|
|
27
|
+
try {
|
|
28
|
+
return await versionedUserApplier.insert(tx, model, idempotencyKey);
|
|
29
|
+
}
|
|
30
|
+
catch (cause) {
|
|
31
|
+
if (isUniqueConstraintViolation(cause))
|
|
32
|
+
throw new VersionConflict();
|
|
33
|
+
throw cause;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
// A claim sets the owner's username with an `update`, which can collide on the same unique column.
|
|
37
|
+
update: async (tx, model) => {
|
|
38
|
+
try {
|
|
39
|
+
return await versionedUserApplier.update(tx, model);
|
|
40
|
+
}
|
|
41
|
+
catch (cause) {
|
|
42
|
+
if (isUniqueConstraintViolation(cause))
|
|
43
|
+
throw new VersionConflict();
|
|
44
|
+
throw cause;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { closeSync, existsSync, openSync, renameSync, rmSync } from 'node:fs';
|
|
2
|
+
import { sql } from 'drizzle-orm';
|
|
3
|
+
import { DATA_REWRITING_MIGRATIONS } from './migrations.js';
|
|
4
|
+
import { applyPendingMigrationsHolding } from './migration-barrier.js';
|
|
5
|
+
import { lastAppliedMillis, readJournal } from './migration-journal.js';
|
|
6
|
+
import { withMigrationLock } from './migration-lock.js';
|
|
7
|
+
import { EXCLUSIVE_CREATE_FLAG, PRIVATE_FILE_MODE } from '../../models/export.js';
|
|
8
|
+
import { LOG_PREFIX } from '../../models/package.js';
|
|
9
|
+
const TEMP_SUFFIX = '.tmp';
|
|
10
|
+
/** Where the pre-migration copy of `dbFile` goes (ADR-033 §7) — next to the database, same encrypted
|
|
11
|
+
* bytes under the same key, so it adds no new exposure. */
|
|
12
|
+
export const backupPath = (dbFile, migrationTag) => `${dbFile}.pre-${migrationTag}.bak`;
|
|
13
|
+
const MAIN_SCHEMA = 'main';
|
|
14
|
+
/** Tags of the data-rewriting migrations that are about to run against a database that already holds
|
|
15
|
+
* applied migrations — i.e. against data worth a safety copy. Mirrors drizzle's own "pending" rule
|
|
16
|
+
* (a journal entry newer than the last applied one). Empty for a brand-new database. */
|
|
17
|
+
export const pendingRewritingTags = async (db, migrationsFolder) => {
|
|
18
|
+
const lastApplied = await lastAppliedMillis(db);
|
|
19
|
+
if (lastApplied === undefined)
|
|
20
|
+
return [];
|
|
21
|
+
return readJournal(migrationsFolder)
|
|
22
|
+
.filter(({ when }) => when > lastApplied)
|
|
23
|
+
.map(({ tag }) => tag)
|
|
24
|
+
.filter((tag) => DATA_REWRITING_MIGRATIONS.includes(tag));
|
|
25
|
+
};
|
|
26
|
+
const databaseFile = async (db) => {
|
|
27
|
+
const rows = await db.all(sql `PRAGMA database_list`);
|
|
28
|
+
return rows.find(({ name }) => name === MAIN_SCHEMA)?.file ?? '';
|
|
29
|
+
};
|
|
30
|
+
// SQLite's own snapshot: `VACUUM INTO` copies the committed state in ONE read transaction, so it is consistent
|
|
31
|
+
// even while another process keeps writing, and it carries the same encryption key (a plain file copy after a
|
|
32
|
+
// WAL checkpoint cannot promise either — a running writer can commit, or auto-checkpoint, mid-copy). The target
|
|
33
|
+
// must be empty or absent; creating it ourselves first, 0600 and never over an existing file, means it is never
|
|
34
|
+
// readable by anyone else. The one `VACUUM INTO` in the repo: the safety copy and `db export` both call it.
|
|
35
|
+
export const snapshotTo = async (db, destination) => {
|
|
36
|
+
closeSync(openSync(destination, EXCLUSIVE_CREATE_FLAG, PRIVATE_FILE_MODE));
|
|
37
|
+
await db.run(sql `VACUUM INTO ${destination}`);
|
|
38
|
+
};
|
|
39
|
+
/** `verify` runs in that same transaction, before it commits. */
|
|
40
|
+
/**
|
|
41
|
+
* Applies `migrate` — taking the safety copy first when a data-rewriting migration is pending. Two layers:
|
|
42
|
+
* the migration lock keeps other PATTY STARTERS from interleaving their copies (and "is it still pending?" is
|
|
43
|
+
* re-asked once it is held, so a late starter never backs up an already-migrated file); and the snapshot is taken
|
|
44
|
+
* while this process holds SQLite's WRITE lock, with the migrations applied in that same transaction, so no
|
|
45
|
+
* other writer — including a process already running on an older release — can commit between the snapshot and
|
|
46
|
+
* the migration and be missing from the backup (`applyPendingMigrationsHolding`).
|
|
47
|
+
* `verify` runs in that same transaction, before it commits.
|
|
48
|
+
*/
|
|
49
|
+
export const migrateWithSafetyCopy = async (db, migrationsFolder, migrate, verify) => {
|
|
50
|
+
const dbFile = await databaseFile(db);
|
|
51
|
+
const needsCopy = (await pendingRewritingTags(db, migrationsFolder)).length > 0;
|
|
52
|
+
if (!needsCopy || dbFile === '' || !existsSync(dbFile))
|
|
53
|
+
return migrate();
|
|
54
|
+
return withMigrationLock(dbFile, async () => {
|
|
55
|
+
if ((await pendingRewritingTags(db, migrationsFolder)).length === 0)
|
|
56
|
+
return migrate(); // a starter beat us to it
|
|
57
|
+
await applyPendingMigrationsHolding(db, migrationsFolder, async () => {
|
|
58
|
+
await backupBeforeRewriting(db, migrationsFolder);
|
|
59
|
+
}, verify);
|
|
60
|
+
// Everything is applied; drizzle's own pass is a no-op and stays the final word on its bookkeeping.
|
|
61
|
+
await migrate();
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Before applying a migration flagged as rewriting existing data (`DATA_REWRITING_MIGRATIONS`), snapshot the
|
|
66
|
+
* database to `<db>.pre-<tag>.bak` (a standalone, consistent, still-encrypted file — no `-wal` needed to
|
|
67
|
+
* restore), logging the path to stderr (never the key). A no-op for a brand-new database, an in-memory one, or
|
|
68
|
+
* when nothing flagged is pending — so a second startup never writes a second backup.
|
|
69
|
+
*/
|
|
70
|
+
export const backupBeforeRewriting = async (db, migrationsFolder) => {
|
|
71
|
+
const tags = await pendingRewritingTags(db, migrationsFolder);
|
|
72
|
+
const dbFile = await databaseFile(db);
|
|
73
|
+
if (tags.length === 0 || dbFile === '' || !existsSync(dbFile))
|
|
74
|
+
return [];
|
|
75
|
+
const copied = [];
|
|
76
|
+
for (const tag of tags) {
|
|
77
|
+
const destination = backupPath(dbFile, tag);
|
|
78
|
+
// Snapshot then rename: a crash mid-snapshot never leaves a truncated file under the final name.
|
|
79
|
+
const partial = `${destination}${TEMP_SUFFIX}`;
|
|
80
|
+
rmSync(partial, { force: true });
|
|
81
|
+
await snapshotTo(db, partial);
|
|
82
|
+
renameSync(partial, destination);
|
|
83
|
+
console.error(`${LOG_PREFIX} backed up the database before migration ${tag} → ${destination}`);
|
|
84
|
+
copied.push(destination);
|
|
85
|
+
}
|
|
86
|
+
return copied;
|
|
87
|
+
};
|