@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
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/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { cloudEditorVersionSchema } from '#cloud/contract'
|
|
2
|
+
import { deletedPrefixSchema } from '#cloud/server/config/schema'
|
|
3
|
+
import { staticEntitlementsTOMLSchema } from '#cloud/server/policy/schema'
|
|
4
|
+
import * as v from 'valibot'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The deployment file, `openpencil-cloud.toml`: what an operator writes, with secrets named by the
|
|
8
|
+
* environment variables that hold them. `../deployment.ts` reads it into the server's configuration.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** PostHog's EU Cloud ingestion host, where OpenPencil's own project is. */
|
|
12
|
+
const POSTHOG_DEFAULT_HOST = 'https://eu.i.posthog.com'
|
|
13
|
+
|
|
14
|
+
const DEFAULT_SECRET_NAMES = {
|
|
15
|
+
databaseURL: 'DATABASE_URL',
|
|
16
|
+
authSecret: 'BETTER_AUTH_SECRET',
|
|
17
|
+
googleClientId: 'GOOGLE_CLIENT_ID',
|
|
18
|
+
googleClientSecret: 'GOOGLE_CLIENT_SECRET',
|
|
19
|
+
applePrivateKey: 'APPLE_PRIVATE_KEY',
|
|
20
|
+
s3AccessKeyId: 'S3_ACCESS_KEY_ID',
|
|
21
|
+
s3SecretAccessKey: 'S3_SECRET_ACCESS_KEY',
|
|
22
|
+
s3SessionToken: 'S3_SESSION_TOKEN',
|
|
23
|
+
smtpUser: 'OPENPENCIL_CLOUD_SMTP_USER',
|
|
24
|
+
smtpPassword: 'OPENPENCIL_CLOUD_SMTP_PASSWORD',
|
|
25
|
+
turnstileSecretKey: 'TURNSTILE_SECRET_KEY',
|
|
26
|
+
servicesSigningKey: 'OPENPENCIL_CLOUD_SERVICES_SIGNING_KEY',
|
|
27
|
+
internalServiceToken: 'OPENPENCIL_CLOUD_SERVICE_TOKEN',
|
|
28
|
+
posthogProjectAPIKey: 'POSTHOG_PROJECT_API_KEY'
|
|
29
|
+
} as const
|
|
30
|
+
|
|
31
|
+
const urlSchema = v.pipe(v.string(), v.url())
|
|
32
|
+
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
33
|
+
const positiveIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
34
|
+
const secretReferenceSchema = v.strictObject({ from_env: textSchema })
|
|
35
|
+
const configurableTextSchema = v.union([textSchema, secretReferenceSchema])
|
|
36
|
+
const defaultReference = (name: string) => v.optional(secretReferenceSchema, { from_env: name })
|
|
37
|
+
|
|
38
|
+
const providerSchema = v.strictObject({
|
|
39
|
+
google: v.optional(
|
|
40
|
+
v.strictObject({
|
|
41
|
+
client_id: v.optional(configurableTextSchema, {
|
|
42
|
+
from_env: DEFAULT_SECRET_NAMES.googleClientId
|
|
43
|
+
}),
|
|
44
|
+
client_secret: defaultReference(DEFAULT_SECRET_NAMES.googleClientSecret)
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
apple: v.optional(
|
|
48
|
+
v.strictObject({
|
|
49
|
+
client_id: textSchema,
|
|
50
|
+
team_id: textSchema,
|
|
51
|
+
key_id: textSchema,
|
|
52
|
+
private_key: defaultReference(DEFAULT_SECRET_NAMES.applePrivateKey),
|
|
53
|
+
app_bundle_identifier: v.optional(textSchema)
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const emailSchema = v.optional(
|
|
59
|
+
v.variant('transport', [
|
|
60
|
+
v.strictObject({ transport: v.literal('none') }),
|
|
61
|
+
v.strictObject({
|
|
62
|
+
transport: v.literal('smtp'),
|
|
63
|
+
from: textSchema,
|
|
64
|
+
smtp: v.strictObject({
|
|
65
|
+
host: textSchema,
|
|
66
|
+
port: positiveIntegerSchema,
|
|
67
|
+
secure: v.optional(v.boolean()),
|
|
68
|
+
user: v.optional(secretReferenceSchema, {
|
|
69
|
+
from_env: DEFAULT_SECRET_NAMES.smtpUser
|
|
70
|
+
}),
|
|
71
|
+
password: v.optional(secretReferenceSchema, {
|
|
72
|
+
from_env: DEFAULT_SECRET_NAMES.smtpPassword
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
}),
|
|
76
|
+
v.strictObject({ transport: v.literal('cloudflare'), from: textSchema })
|
|
77
|
+
]),
|
|
78
|
+
{ transport: 'none' }
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Every table is strict: a key the schema doesn't know, such as one renamed since, stops startup
|
|
83
|
+
* instead of leaving its setting at a default the operator never chose.
|
|
84
|
+
*/
|
|
85
|
+
export const cloudDeploymentConfigSchema = v.strictObject({
|
|
86
|
+
schema_version: v.literal(2),
|
|
87
|
+
deployment: v.strictObject({
|
|
88
|
+
mode: v.picklist(['official', 'self-hosted']),
|
|
89
|
+
public_url: urlSchema,
|
|
90
|
+
indexing: v.optional(v.picklist(['allow', 'deny']), 'deny'),
|
|
91
|
+
trusted_origins: v.optional(v.array(urlSchema), []),
|
|
92
|
+
// The server's editor works with files on this device without signing in to the server.
|
|
93
|
+
local_editor: v.optional(v.boolean(), true),
|
|
94
|
+
listing: v.optional(v.strictObject({ minimum_editor_version: cloudEditorVersionSchema })),
|
|
95
|
+
// How a self-hosted server names itself on its public pages.
|
|
96
|
+
branding: v.optional(
|
|
97
|
+
v.strictObject({
|
|
98
|
+
name: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
|
|
99
|
+
logo_url: v.optional(urlSchema),
|
|
100
|
+
help_url: v.optional(urlSchema)
|
|
101
|
+
})
|
|
102
|
+
),
|
|
103
|
+
// Older editors ask to be updated, and the server refuses their requests.
|
|
104
|
+
minimum_editor_version: v.optional(cloudEditorVersionSchema)
|
|
105
|
+
}),
|
|
106
|
+
database: v.optional(
|
|
107
|
+
v.strictObject({ url: defaultReference(DEFAULT_SECRET_NAMES.databaseURL) }),
|
|
108
|
+
{
|
|
109
|
+
url: { from_env: DEFAULT_SECRET_NAMES.databaseURL }
|
|
110
|
+
}
|
|
111
|
+
),
|
|
112
|
+
authentication: v.strictObject({
|
|
113
|
+
secret: defaultReference(DEFAULT_SECRET_NAMES.authSecret),
|
|
114
|
+
enrollment: v.optional(
|
|
115
|
+
v.strictObject({
|
|
116
|
+
mode: v.optional(v.picklist(['open', 'approval', 'closed']), 'open'),
|
|
117
|
+
notification_emails: v.optional(v.array(v.pipe(v.string(), v.email())), [])
|
|
118
|
+
}),
|
|
119
|
+
{ mode: 'open', notification_emails: [] }
|
|
120
|
+
),
|
|
121
|
+
email_password: v.optional(
|
|
122
|
+
v.strictObject({
|
|
123
|
+
enabled: v.optional(v.boolean(), false),
|
|
124
|
+
sign_up: v.optional(v.boolean(), true),
|
|
125
|
+
minimum_password_length: v.optional(
|
|
126
|
+
v.pipe(v.number(), v.integer(), v.minValue(8), v.maxValue(128)),
|
|
127
|
+
8
|
|
128
|
+
),
|
|
129
|
+
maximum_password_length: v.optional(
|
|
130
|
+
v.pipe(v.number(), v.integer(), v.minValue(15), v.maxValue(128)),
|
|
131
|
+
128
|
|
132
|
+
),
|
|
133
|
+
verification_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
134
|
+
password_reset_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
135
|
+
compromised_password_check: v.optional(v.boolean(), false)
|
|
136
|
+
}),
|
|
137
|
+
{
|
|
138
|
+
enabled: false,
|
|
139
|
+
sign_up: true,
|
|
140
|
+
minimum_password_length: 8,
|
|
141
|
+
maximum_password_length: 128,
|
|
142
|
+
verification_link_lifetime_minutes: 60,
|
|
143
|
+
password_reset_link_lifetime_minutes: 60,
|
|
144
|
+
compromised_password_check: false
|
|
145
|
+
}
|
|
146
|
+
),
|
|
147
|
+
mfa: v.optional(
|
|
148
|
+
v.strictObject({
|
|
149
|
+
required_for_admins: v.optional(v.boolean(), false),
|
|
150
|
+
totp: v.optional(v.boolean(), false),
|
|
151
|
+
recovery_codes: v.optional(v.boolean(), false),
|
|
152
|
+
trusted_device_lifetime_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 14)
|
|
153
|
+
}),
|
|
154
|
+
{
|
|
155
|
+
required_for_admins: false,
|
|
156
|
+
totp: false,
|
|
157
|
+
recovery_codes: false,
|
|
158
|
+
trusted_device_lifetime_days: 14
|
|
159
|
+
}
|
|
160
|
+
),
|
|
161
|
+
// Passkeys belong to public_url's host and origin.
|
|
162
|
+
passkeys: v.optional(
|
|
163
|
+
v.strictObject({
|
|
164
|
+
enabled: v.optional(v.boolean(), false),
|
|
165
|
+
rp_name: v.optional(textSchema, 'OpenPencil Cloud')
|
|
166
|
+
}),
|
|
167
|
+
{ enabled: false, rp_name: 'OpenPencil Cloud' }
|
|
168
|
+
),
|
|
169
|
+
captcha: v.optional(
|
|
170
|
+
v.strictObject({
|
|
171
|
+
provider: v.literal('cloudflare-turnstile'),
|
|
172
|
+
site_key: textSchema,
|
|
173
|
+
secret_key: defaultReference(DEFAULT_SECRET_NAMES.turnstileSecretKey),
|
|
174
|
+
allowed_hostnames: v.optional(v.array(textSchema), [])
|
|
175
|
+
})
|
|
176
|
+
),
|
|
177
|
+
trusted_proxies: v.optional(
|
|
178
|
+
v.strictObject({
|
|
179
|
+
headers: v.optional(v.array(textSchema), []),
|
|
180
|
+
addresses: v.optional(v.array(textSchema), [])
|
|
181
|
+
}),
|
|
182
|
+
{ headers: [], addresses: [] }
|
|
183
|
+
),
|
|
184
|
+
providers: v.optional(providerSchema, {})
|
|
185
|
+
}),
|
|
186
|
+
object_storage: v.strictObject({
|
|
187
|
+
endpoint: urlSchema,
|
|
188
|
+
region: textSchema,
|
|
189
|
+
bucket: textSchema,
|
|
190
|
+
credentials: v.optional(
|
|
191
|
+
v.strictObject({
|
|
192
|
+
access_key_id: defaultReference(DEFAULT_SECRET_NAMES.s3AccessKeyId),
|
|
193
|
+
secret_access_key: defaultReference(DEFAULT_SECRET_NAMES.s3SecretAccessKey),
|
|
194
|
+
session_token: v.optional(secretReferenceSchema, {
|
|
195
|
+
from_env: DEFAULT_SECRET_NAMES.s3SessionToken
|
|
196
|
+
})
|
|
197
|
+
}),
|
|
198
|
+
{
|
|
199
|
+
access_key_id: { from_env: DEFAULT_SECRET_NAMES.s3AccessKeyId },
|
|
200
|
+
secret_access_key: { from_env: DEFAULT_SECRET_NAMES.s3SecretAccessKey }
|
|
201
|
+
}
|
|
202
|
+
),
|
|
203
|
+
force_path_style: v.optional(v.boolean(), true),
|
|
204
|
+
checksum_verification: v.optional(v.picklist(['native', 'metadata']), 'native'),
|
|
205
|
+
server_side_encryption: v.optional(v.picklist(['AES256', 'aws:kms'])),
|
|
206
|
+
kms_key_id: v.optional(textSchema),
|
|
207
|
+
/**
|
|
208
|
+
* Keeps each file cleanup deletes under this prefix, such as `deleted/`, so a database restored
|
|
209
|
+
* to an earlier time still finds it; a lifecycle rule on the prefix expires them.
|
|
210
|
+
*/
|
|
211
|
+
deleted_prefix: v.optional(deletedPrefixSchema),
|
|
212
|
+
maximum_upload_bytes: v.optional(positiveIntegerSchema)
|
|
213
|
+
}),
|
|
214
|
+
collaboration: v.optional(
|
|
215
|
+
v.strictObject({
|
|
216
|
+
relay_url: v.optional(urlSchema),
|
|
217
|
+
maximum_message_bytes: v.optional(positiveIntegerSchema),
|
|
218
|
+
maximum_connections_per_room: v.optional(positiveIntegerSchema)
|
|
219
|
+
}),
|
|
220
|
+
{}
|
|
221
|
+
),
|
|
222
|
+
documents: v.optional(
|
|
223
|
+
v.strictObject({
|
|
224
|
+
retention_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 30),
|
|
225
|
+
purge_grace_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 7)
|
|
226
|
+
}),
|
|
227
|
+
{ retention_days: 30, purge_grace_days: 7 }
|
|
228
|
+
),
|
|
229
|
+
email: emailSchema,
|
|
230
|
+
workers: v.optional(
|
|
231
|
+
v.strictObject({
|
|
232
|
+
email: v.optional(
|
|
233
|
+
v.strictObject({
|
|
234
|
+
batch_size: v.optional(positiveIntegerSchema, 50),
|
|
235
|
+
interval_ms: v.optional(positiveIntegerSchema, 30_000),
|
|
236
|
+
lease_ms: v.optional(positiveIntegerSchema, 300_000),
|
|
237
|
+
maximum_attempts: v.optional(positiveIntegerSchema, 5)
|
|
238
|
+
}),
|
|
239
|
+
{ batch_size: 50, interval_ms: 30_000, lease_ms: 300_000, maximum_attempts: 5 }
|
|
240
|
+
),
|
|
241
|
+
cleanup: v.optional(
|
|
242
|
+
v.strictObject({
|
|
243
|
+
enabled: v.optional(v.boolean(), true),
|
|
244
|
+
batch_size: v.optional(positiveIntegerSchema, 100),
|
|
245
|
+
interval_ms: v.optional(positiveIntegerSchema, 60_000),
|
|
246
|
+
lease_ms: v.optional(positiveIntegerSchema, 300_000)
|
|
247
|
+
}),
|
|
248
|
+
{ enabled: true, batch_size: 100, interval_ms: 60_000, lease_ms: 300_000 }
|
|
249
|
+
)
|
|
250
|
+
})
|
|
251
|
+
),
|
|
252
|
+
entitlements: v.optional(
|
|
253
|
+
v.variant('source', [
|
|
254
|
+
v.strictObject({ source: v.literal('static'), ...staticEntitlementsTOMLSchema.entries }),
|
|
255
|
+
// The values every account takes until billing or an operator stores its own: the free plan.
|
|
256
|
+
v.strictObject({ source: v.literal('database'), ...staticEntitlementsTOMLSchema.entries })
|
|
257
|
+
])
|
|
258
|
+
),
|
|
259
|
+
services: v.optional(
|
|
260
|
+
v.strictObject({
|
|
261
|
+
endpoint: urlSchema,
|
|
262
|
+
signing_key: defaultReference(DEFAULT_SECRET_NAMES.servicesSigningKey),
|
|
263
|
+
token_lifetime_minutes: v.optional(
|
|
264
|
+
v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(60)),
|
|
265
|
+
10
|
|
266
|
+
)
|
|
267
|
+
})
|
|
268
|
+
),
|
|
269
|
+
billing: v.optional(v.strictObject({ endpoint: urlSchema })),
|
|
270
|
+
internal_api: v.optional(
|
|
271
|
+
v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) })
|
|
272
|
+
),
|
|
273
|
+
/**
|
|
274
|
+
* Optional: the operator's terms and privacy policy, linked at sign-up and in the portal, and
|
|
275
|
+
* with `require_acceptance`, accepted once per `terms_version` before an account uses the server.
|
|
276
|
+
*/
|
|
277
|
+
legal: v.optional(
|
|
278
|
+
v.strictObject({
|
|
279
|
+
terms_url: v.optional(urlSchema),
|
|
280
|
+
privacy_url: v.optional(urlSchema),
|
|
281
|
+
terms_version: v.optional(textSchema),
|
|
282
|
+
require_acceptance: v.optional(v.boolean(), false),
|
|
283
|
+
abuse_email: v.optional(v.pipe(v.string(), v.email())),
|
|
284
|
+
minimum_age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
285
|
+
})
|
|
286
|
+
),
|
|
287
|
+
/**
|
|
288
|
+
* Optional: a workspace whose documents anyone may open read-only, without signing in or a
|
|
289
|
+
* link, as the deployment's community gallery. Everything saved there is public.
|
|
290
|
+
*/
|
|
291
|
+
community: v.optional(v.strictObject({ workspace: v.pipe(v.string(), v.uuid()) })),
|
|
292
|
+
/** Optional: reports server errors to PostHog error tracking. Leave it out to send nothing. */
|
|
293
|
+
error_tracking: v.optional(
|
|
294
|
+
v.strictObject({
|
|
295
|
+
provider: v.literal('posthog'),
|
|
296
|
+
host: v.optional(urlSchema, POSTHOG_DEFAULT_HOST),
|
|
297
|
+
project_api_key: v.optional(configurableTextSchema, {
|
|
298
|
+
from_env: DEFAULT_SECRET_NAMES.posthogProjectAPIKey
|
|
299
|
+
})
|
|
300
|
+
})
|
|
301
|
+
)
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
export type CloudDeploymentConfig = v.InferOutput<typeof cloudDeploymentConfigSchema>
|
|
305
|
+
/** Deployment configuration as an operator writes it, before defaults are applied. */
|
|
306
|
+
export type CloudDeploymentInput = v.InferInput<typeof cloudDeploymentConfigSchema>
|
|
307
|
+
export type CloudSecretReference = v.InferOutput<typeof secretReferenceSchema>
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { sql, type Expression } from 'kysely'
|
|
1
|
+
import { sql, type Expression, type Transaction } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import type { CloudDatabase } from './tables'
|
|
2
4
|
|
|
3
5
|
export const CURRENT_TIMESTAMP = sql<Date>`current_timestamp`
|
|
4
6
|
export const EMPTY_JSON_OBJECT = sql`'{}'::jsonb`
|
|
@@ -11,3 +13,15 @@ export function checkColumnIn(column: string, values: readonly string[]): Expres
|
|
|
11
13
|
}
|
|
12
14
|
return sql<boolean>`${sql.ref(column)} in (${sql.join(values.map((value) => sql.lit(value)))})`
|
|
13
15
|
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Holds a lock named `key` until the transaction ends, for a check and a write that must not
|
|
19
|
+
* interleave with another transaction's, such as reserving storage from an account's allowance,
|
|
20
|
+
* which spans rows no single one of which could be locked for it.
|
|
21
|
+
*/
|
|
22
|
+
export async function lockForTransaction(
|
|
23
|
+
transaction: Transaction<CloudDatabase>,
|
|
24
|
+
key: string
|
|
25
|
+
): Promise<void> {
|
|
26
|
+
await sql`select pg_advisory_xact_lock(hashtextextended(${key}, 0))`.execute(transaction)
|
|
27
|
+
}
|
package/src/server/db/index.ts
CHANGED
package/src/server/db/migrate.ts
CHANGED
|
@@ -1,69 +1,50 @@
|
|
|
1
1
|
import type { Kysely } from 'kysely'
|
|
2
2
|
import { Migrator, type Migration, type MigrationProvider } from 'kysely/migration'
|
|
3
3
|
|
|
4
|
-
import * as
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
7
|
-
import * as
|
|
8
|
-
import * as
|
|
9
|
-
import * as
|
|
10
|
-
import * as
|
|
11
|
-
import * as
|
|
12
|
-
import * as
|
|
13
|
-
import * as
|
|
14
|
-
import * as
|
|
15
|
-
import * as
|
|
16
|
-
import * as
|
|
17
|
-
import * as
|
|
18
|
-
import * as cloudRateLimit from './migrations/015_cloud_rate_limit'
|
|
19
|
-
import * as authSchema from './migrations/016_auth_schema'
|
|
20
|
-
import * as authenticationEmailKinds from './migrations/017_authentication_email_kinds'
|
|
21
|
-
import * as authenticationMFA from './migrations/018_authentication_mfa'
|
|
22
|
-
import * as betterAuthAccountIssuer from './migrations/019_better_auth_account_issuer'
|
|
23
|
-
import * as workspaceInvitations from './migrations/020_workspace_invitations'
|
|
24
|
-
import * as documentVersions from './migrations/021_document_versions'
|
|
25
|
-
import * as entitlementExternalVersion from './migrations/022_entitlement_external_version'
|
|
26
|
-
import * as documentImages from './migrations/023_document_images'
|
|
27
|
-
import * as accountActivity from './migrations/024_account_activity'
|
|
28
|
-
import * as tableNames from './migrations/025_table_names'
|
|
29
|
-
import * as userPicture from './migrations/026_user_picture'
|
|
30
|
-
import * as documentKind from './migrations/027_document_kind'
|
|
31
|
-
import * as collaborationUpdates from './migrations/028_collaboration_updates'
|
|
4
|
+
import * as accounts from './migrations/001_accounts'
|
|
5
|
+
import * as workspaces from './migrations/002_workspaces'
|
|
6
|
+
import * as storage from './migrations/003_storage'
|
|
7
|
+
import * as documents from './migrations/004_documents'
|
|
8
|
+
import * as sharing from './migrations/005_sharing'
|
|
9
|
+
import * as operations from './migrations/006_operations'
|
|
10
|
+
import * as trash from './migrations/007_trash'
|
|
11
|
+
import * as terms from './migrations/008_terms'
|
|
12
|
+
import * as accessTokens from './migrations/009_access_tokens'
|
|
13
|
+
import * as storageLimitNotices from './migrations/010_storage_limit_notices'
|
|
14
|
+
import * as accountIds from './migrations/011_account_ids'
|
|
15
|
+
import * as emailLocales from './migrations/012_email_locales'
|
|
16
|
+
import * as documentBlockEmails from './migrations/013_document_block_emails'
|
|
17
|
+
import * as reportReplies from './migrations/014_report_replies'
|
|
32
18
|
import type { CloudDatabase } from './tables'
|
|
33
19
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
'
|
|
44
|
-
'
|
|
45
|
-
'
|
|
46
|
-
'
|
|
47
|
-
'
|
|
48
|
-
'
|
|
49
|
-
'
|
|
50
|
-
'
|
|
51
|
-
'
|
|
52
|
-
'
|
|
53
|
-
'
|
|
54
|
-
'
|
|
55
|
-
'
|
|
56
|
-
'
|
|
57
|
-
'023_document_images': documentImages,
|
|
58
|
-
'024_account_activity': accountActivity,
|
|
59
|
-
'025_table_names': tableNames,
|
|
60
|
-
'026_user_picture': userPicture,
|
|
61
|
-
'027_document_kind': documentKind,
|
|
62
|
-
'028_collaboration_updates': collaborationUpdates
|
|
20
|
+
/**
|
|
21
|
+
* A migration runs before the code it ships with, while the previous release still serves, so it
|
|
22
|
+
* only adds (expand): no table or column dropped, renamed, retyped, or newly required. One that
|
|
23
|
+
* must (contract) names each table or `table.column` it changes in `contracts`, and ships a
|
|
24
|
+
* release after the last code that used them (`tests/integration/server/db/expand.test.ts`).
|
|
25
|
+
*/
|
|
26
|
+
export type CloudMigration = Migration & { contracts?: readonly string[] }
|
|
27
|
+
|
|
28
|
+
const migrations: Record<string, CloudMigration> = {
|
|
29
|
+
'001_accounts': accounts,
|
|
30
|
+
'002_workspaces': workspaces,
|
|
31
|
+
'003_storage': storage,
|
|
32
|
+
'004_documents': documents,
|
|
33
|
+
'005_sharing': sharing,
|
|
34
|
+
'006_operations': operations,
|
|
35
|
+
'007_trash': trash,
|
|
36
|
+
'008_terms': terms,
|
|
37
|
+
'009_access_tokens': accessTokens,
|
|
38
|
+
'010_storage_limit_notices': storageLimitNotices,
|
|
39
|
+
'011_account_ids': accountIds,
|
|
40
|
+
'012_email_locales': emailLocales,
|
|
41
|
+
'013_document_block_emails': documentBlockEmails,
|
|
42
|
+
'014_report_replies': reportReplies
|
|
63
43
|
}
|
|
64
44
|
|
|
65
|
-
|
|
66
|
-
|
|
45
|
+
/** Cloud's migrations, in order. */
|
|
46
|
+
export class CloudMigrationProvider implements MigrationProvider {
|
|
47
|
+
async getMigrations(): Promise<Record<string, CloudMigration>> {
|
|
67
48
|
return migrations
|
|
68
49
|
}
|
|
69
50
|
}
|
|
@@ -73,46 +54,11 @@ export type CloudAuthMigration = {
|
|
|
73
54
|
schemaVersion: string
|
|
74
55
|
}
|
|
75
56
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
'email_verified',
|
|
82
|
-
'created_at',
|
|
83
|
-
'updated_at',
|
|
84
|
-
'role',
|
|
85
|
-
'banned',
|
|
86
|
-
'two_factor_enabled'
|
|
87
|
-
],
|
|
88
|
-
session: ['id', 'expires_at', 'token', 'user_id', 'impersonated_by'],
|
|
89
|
-
account: ['id', 'account_id', 'provider_id', 'user_id'],
|
|
90
|
-
verification: ['id', 'identifier', 'value', 'expires_at'],
|
|
91
|
-
device_code: ['id', 'device_code', 'user_code', 'status', 'client_id'],
|
|
92
|
-
rate_limit: ['id', 'key', 'count', 'last_request'],
|
|
93
|
-
two_factor: [
|
|
94
|
-
'id',
|
|
95
|
-
'secret',
|
|
96
|
-
'backup_codes',
|
|
97
|
-
'user_id',
|
|
98
|
-
'verified',
|
|
99
|
-
'failed_verification_count',
|
|
100
|
-
'locked_until'
|
|
101
|
-
],
|
|
102
|
-
passkey: ['id', 'public_key', 'user_id', 'credential_id', 'counter', 'device_type', 'backed_up']
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async function existingAuthSchema(
|
|
106
|
-
database: Kysely<CloudDatabase>
|
|
107
|
-
): Promise<Map<string, Set<string>>> {
|
|
108
|
-
const tables = await database.introspection.getTables()
|
|
109
|
-
return new Map(
|
|
110
|
-
tables
|
|
111
|
-
.filter((table) => Object.hasOwn(REQUIRED_AUTH_SCHEMA, table.name))
|
|
112
|
-
.map((table) => [table.name, new Set(table.columns.map((column) => column.name))])
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Better Auth's tables come from its own migration API, once, after Cloud's: the version they
|
|
59
|
+
* were built for is recorded, and a different one stops startup until someone migrates it
|
|
60
|
+
* deliberately, since Better Auth cannot say what an upgrade between its versions changes.
|
|
61
|
+
*/
|
|
116
62
|
async function migrateAuthSchema(
|
|
117
63
|
database: Kysely<CloudDatabase>,
|
|
118
64
|
migration: CloudAuthMigration
|
|
@@ -130,23 +76,7 @@ async function migrateAuthSchema(
|
|
|
130
76
|
}
|
|
131
77
|
return
|
|
132
78
|
}
|
|
133
|
-
|
|
134
|
-
const existing = await existingAuthSchema(database)
|
|
135
|
-
if (existing.size === 0) {
|
|
136
|
-
await migration.run()
|
|
137
|
-
} else {
|
|
138
|
-
const missing = Object.entries(REQUIRED_AUTH_SCHEMA).flatMap(([table, requiredColumns]) => {
|
|
139
|
-
const columns = existing.get(table)
|
|
140
|
-
if (!columns) return [table]
|
|
141
|
-
return requiredColumns
|
|
142
|
-
.filter((column) => !columns.has(column))
|
|
143
|
-
.map((column) => `${table}.${column}`)
|
|
144
|
-
})
|
|
145
|
-
if (missing.length > 0) {
|
|
146
|
-
throw new Error(`Existing Better Auth schema is incomplete: ${missing.join(', ')}`)
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
79
|
+
await migration.run()
|
|
150
80
|
await database
|
|
151
81
|
.insertInto('authSchemaVersion')
|
|
152
82
|
.values({ id: 'better-auth', version: migration.schemaVersion, updatedAt: new Date() })
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addChecks, addIndexes, dropTables, now, required } from './schema'
|
|
4
|
+
|
|
5
|
+
/** What Cloud keeps about an account beside Better Auth's tables: its plan, sign-ins, and picture. */
|
|
6
|
+
const TABLES = [
|
|
7
|
+
'auth_schema_version',
|
|
8
|
+
'account_entitlement',
|
|
9
|
+
'mfa_assurance',
|
|
10
|
+
'sign_in',
|
|
11
|
+
'user_picture',
|
|
12
|
+
'api_rate_limit'
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
16
|
+
// The Better Auth schema version this database was built for.
|
|
17
|
+
await database.schema
|
|
18
|
+
.createTable('auth_schema_version')
|
|
19
|
+
.addColumn('id', 'text', (column) => column.primaryKey())
|
|
20
|
+
.addColumn('version', 'text', required)
|
|
21
|
+
.addColumn('updated_at', 'timestamptz', now)
|
|
22
|
+
.execute()
|
|
23
|
+
// An account's plan, as billing or an operator set it; its workspaces share it.
|
|
24
|
+
await database.schema
|
|
25
|
+
.createTable('account_entitlement')
|
|
26
|
+
.addColumn('user_id', 'text', (column) => column.primaryKey())
|
|
27
|
+
.addColumn('values', 'jsonb', required)
|
|
28
|
+
.addColumn('source', 'text', required)
|
|
29
|
+
.addColumn('revision', 'integer', (column) => column.notNull().defaultTo(1))
|
|
30
|
+
.addColumn('external_version', 'bigint')
|
|
31
|
+
.addColumn('updated_at', 'timestamptz', now)
|
|
32
|
+
.execute()
|
|
33
|
+
await database.schema
|
|
34
|
+
.createTable('mfa_assurance')
|
|
35
|
+
.addColumn('session_id', 'uuid', (column) => column.primaryKey())
|
|
36
|
+
.addColumn('user_id', 'uuid', required)
|
|
37
|
+
.addColumn('method', 'text', required)
|
|
38
|
+
.addColumn('verified_at', 'timestamptz', required)
|
|
39
|
+
.execute()
|
|
40
|
+
await database.schema
|
|
41
|
+
.createTable('sign_in')
|
|
42
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
43
|
+
.addColumn('user_id', 'uuid', required)
|
|
44
|
+
.addColumn('session_id', 'uuid')
|
|
45
|
+
.addColumn('method', 'text', required)
|
|
46
|
+
.addColumn('ip_address', 'text')
|
|
47
|
+
.addColumn('user_agent', 'text')
|
|
48
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
49
|
+
.execute()
|
|
50
|
+
await database.schema
|
|
51
|
+
.createTable('user_picture')
|
|
52
|
+
.addColumn('user_id', 'uuid', (column) => column.primaryKey())
|
|
53
|
+
.addColumn('bytes', 'bytea', required)
|
|
54
|
+
.addColumn('format', 'text', required)
|
|
55
|
+
.addColumn('checksum', 'text', required)
|
|
56
|
+
.addColumn('source', 'text', required)
|
|
57
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
58
|
+
.execute()
|
|
59
|
+
// OpenPencil's own rate limits; Better Auth keeps its own in `rate_limit`.
|
|
60
|
+
await database.schema
|
|
61
|
+
.createTable('api_rate_limit')
|
|
62
|
+
.addColumn('key_hash', 'text', (column) => column.primaryKey())
|
|
63
|
+
.addColumn('window_started_at', 'timestamptz', required)
|
|
64
|
+
.addColumn('request_count', 'integer', (column) => column.notNull().defaultTo(1))
|
|
65
|
+
.addColumn('updated_at', 'timestamptz', now)
|
|
66
|
+
.execute()
|
|
67
|
+
await addChecks(database, [
|
|
68
|
+
['user_picture', 'format', ['png', 'jpeg']],
|
|
69
|
+
['user_picture', 'source', ['upload', 'provider']]
|
|
70
|
+
])
|
|
71
|
+
await addIndexes(database, [
|
|
72
|
+
['sign_in', ['user_id', 'created_at']],
|
|
73
|
+
['sign_in', ['session_id']]
|
|
74
|
+
])
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
78
|
+
await dropTables(database, TABLES)
|
|
79
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addChecks, addIndexes, dropTables, now, required, ROLES, withInvitation } from './schema'
|
|
4
|
+
|
|
5
|
+
/** Workspaces, their members, and the people invited to join. */
|
|
6
|
+
const TABLES = ['workspace', 'workspace_member', 'workspace_invitation']
|
|
7
|
+
|
|
8
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
9
|
+
// A workspace takes its limits from its owner's plan.
|
|
10
|
+
await database.schema
|
|
11
|
+
.createTable('workspace')
|
|
12
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
13
|
+
.addColumn('name', 'text', required)
|
|
14
|
+
.addColumn('slug', 'text', (column) => column.notNull().unique())
|
|
15
|
+
.addColumn('created_by', 'text', required)
|
|
16
|
+
.addColumn('owner_id', 'text', required)
|
|
17
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
18
|
+
.addColumn('updated_at', 'timestamptz', now)
|
|
19
|
+
.execute()
|
|
20
|
+
await database.schema
|
|
21
|
+
.createTable('workspace_member')
|
|
22
|
+
.addColumn('workspace_id', 'uuid', (column) =>
|
|
23
|
+
column.notNull().references('workspace.id').onDelete('cascade')
|
|
24
|
+
)
|
|
25
|
+
.addColumn('user_id', 'text', required)
|
|
26
|
+
.addColumn('role', 'text', required)
|
|
27
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
28
|
+
.addPrimaryKeyConstraint('workspace_member_pkey', ['workspace_id', 'user_id'])
|
|
29
|
+
.execute()
|
|
30
|
+
await withInvitation(database.schema.createTable('workspace_invitation'), 'role')
|
|
31
|
+
.addColumn('workspace_id', 'uuid', (column) =>
|
|
32
|
+
column.notNull().references('workspace.id').onDelete('cascade')
|
|
33
|
+
)
|
|
34
|
+
.execute()
|
|
35
|
+
await addChecks(database, [
|
|
36
|
+
['workspace_member', 'role', ROLES],
|
|
37
|
+
['workspace_invitation', 'role', ROLES]
|
|
38
|
+
])
|
|
39
|
+
await addIndexes(database, [
|
|
40
|
+
['workspace', ['owner_id']],
|
|
41
|
+
['workspace_member', ['user_id']],
|
|
42
|
+
['workspace_invitation', ['workspace_id', 'revoked_at']]
|
|
43
|
+
])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
47
|
+
await dropTables(database, TABLES)
|
|
48
|
+
}
|