@protobase/server 0.129.1 → 0.130.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/dist/assistant/query-runner/grants-sql.js +32 -0
- package/dist/assistant/query-runner/query-input.js +33 -0
- package/dist/assistant/query-runner/query-module.js +44 -0
- package/dist/assistant/query-runner/query-runner.d.ts +8 -11
- package/dist/assistant/query-runner/query-runner.js +15 -57
- package/dist/assistant/query-runner/read-query.js +54 -0
- package/dist/assistant/query-runner/runner-sql.js +4 -20
- package/dist/assistant/query-runner/steps/001-role.js +19 -0
- package/dist/auth-store.d.ts +2 -2
- package/dist/auth-store.js +3 -3
- package/dist/better-auth/auth-schema.d.ts +13 -4
- package/dist/better-auth/auth-schema.js +40 -32
- package/dist/better-auth/auth-settings.d.ts +59 -11
- package/dist/better-auth/auth-settings.js +57 -13
- package/dist/better-auth/auth-tables.d.ts +6 -3
- package/dist/better-auth/auth-tables.js +12 -7
- package/dist/better-auth/authenticator.js +10 -2
- package/dist/better-auth/create-auth.d.ts +62 -7
- package/dist/better-auth/create-auth.js +55 -14
- package/dist/better-auth/device-sign-in-check.js +13 -0
- package/dist/better-auth/names/declaration.d.ts +18 -5
- package/dist/better-auth/names/declaration.js +3 -1
- package/dist/better-auth/names/modules/auth.js +9 -0
- package/dist/better-auth/names/modules/index.d.ts +34 -4
- package/dist/better-auth/names/modules/organizations.js +10 -3
- package/dist/better-auth/names/modules/personal-access-tokens.js +6 -3
- package/dist/better-auth/names/modules-on.js +25 -2
- package/dist/better-auth/names/resolve-names.d.ts +9 -1
- package/dist/better-auth/names/resolve-names.js +15 -9
- package/dist/better-auth/organizations/accept-invitation.js +5 -4
- package/dist/better-auth/organizations/add-every-user.js +3 -2
- package/dist/better-auth/organizations/host-scope.d.ts +10 -0
- package/dist/better-auth/organizations/host-scope.js +15 -0
- package/dist/better-auth/organizations/host.d.ts +12 -6
- package/dist/better-auth/organizations/host.js +99 -61
- package/dist/better-auth/organizations/kept-organizations.d.ts +21 -0
- package/dist/better-auth/organizations/kept-organizations.js +62 -0
- package/dist/better-auth/organizations/member-endpoints.js +4 -3
- package/dist/better-auth/organizations/options.d.ts +15 -0
- package/dist/better-auth/organizations/options.js +19 -1
- package/dist/better-auth/organizations/people-endpoint.js +3 -2
- package/dist/better-auth/organizations/pinned-membership.js +81 -0
- package/dist/better-auth/organizations/pinned-sessions.js +46 -0
- package/dist/better-auth/organizations/session-organization.js +3 -2
- package/dist/better-auth/organizations/store.js +14 -9
- package/dist/better-auth/personal-access-tokens/pinned-tokens.js +23 -0
- package/dist/better-auth/personal-access-tokens/token-plugin.js +75 -54
- package/dist/better-auth/personal-access-tokens/token-sessions.js +6 -3
- package/dist/better-auth/personal-access-tokens/token-store.js +15 -11
- package/dist/better-auth/store/adoption.js +25 -10
- package/dist/better-auth/store/drift.js +5 -4
- package/dist/better-auth/store/legacy-record.d.ts +10 -0
- package/dist/better-auth/store/legacy-record.js +56 -0
- package/dist/better-auth/store/migration-sql.js +31 -26
- package/dist/better-auth/store/offline/baseline-store.d.ts +11 -7
- package/dist/better-auth/store/offline/baseline-store.js +15 -12
- package/dist/better-auth/store/offline/module-switches.d.ts +27 -0
- package/dist/better-auth/store/offline/module-switches.js +94 -0
- package/dist/better-auth/store/offline/name-changes.d.ts +4 -1
- package/dist/better-auth/store/offline/name-changes.js +5 -2
- package/dist/better-auth/store/offline/offline-steps.d.ts +21 -7
- package/dist/better-auth/store/offline/offline-steps.js +29 -20
- package/dist/better-auth/store/offline/retired-watch.d.ts +5 -0
- package/dist/better-auth/store/offline/retired-watch.js +36 -0
- package/dist/better-auth/store/offline/settings-names.d.ts +3 -2
- package/dist/better-auth/store/offline/settings-names.js +10 -3
- package/dist/better-auth/store/offline/store-model.d.ts +9 -1
- package/dist/better-auth/store/offline/store-model.js +22 -5
- package/dist/better-auth/store/resources-outside.js +1 -1
- package/dist/better-auth/store/step-record.d.ts +13 -0
- package/dist/better-auth/store/step-record.js +59 -0
- package/dist/better-auth/store/steps/index.js +6 -1
- package/dist/better-auth/store/steps/legacy-versions.d.ts +7 -0
- package/dist/better-auth/store/steps/legacy-versions.js +4 -0
- package/dist/better-auth/store/steps/organizations/003-default-organization.js +113 -0
- package/dist/better-auth/store/steps/render.d.ts +3 -0
- package/dist/better-auth/store/steps/render.js +48 -25
- package/dist/better-auth/store/steps/step-lint.d.ts +14 -0
- package/dist/better-auth/store/steps/step-lint.js +31 -10
- package/dist/better-auth/store/steps/step.d.ts +18 -0
- package/dist/better-auth/store/steps/step.js +7 -1
- package/dist/better-auth/store/store-db.d.ts +6 -0
- package/dist/better-auth/store/store-db.js +11 -5
- package/dist/better-auth/store/store-names.d.ts +4 -0
- package/dist/better-auth/token-standing.js +12 -7
- package/dist/better-auth/users.d.ts +7 -2
- package/dist/better-auth/users.js +25 -3
- package/dist/built-in-resources.js +1 -1
- package/dist/calls/calls-module.js +42 -0
- package/dist/calls/calls-sql.js +4 -30
- package/dist/changes/capture-names.d.ts +3 -1
- package/dist/changes/capture-names.js +5 -2
- package/dist/changes/capture-schema-sql.d.ts +10 -0
- package/dist/changes/capture-schema-sql.js +21 -0
- package/dist/changes/capture-schema.d.ts +0 -18
- package/dist/changes/capture-schema.js +1 -74
- package/dist/changes/capture-sql.js +30 -24
- package/dist/changes/changes-input.js +36 -0
- package/dist/changes/changes-module.js +44 -0
- package/dist/changes/read-capture.js +67 -0
- package/dist/changes/steps/001-outbox.js +23 -0
- package/dist/channels/sends-module.js +37 -0
- package/dist/channels/sends-schema-sql.js +5 -5
- package/dist/comments/comments-module.js +30 -0
- package/dist/comments/comments-sql.js +1 -22
- package/dist/computed/buckets-sql.d.ts +5 -1
- package/dist/computed/buckets-sql.js +39 -46
- package/dist/computed/{buckets-schema.d.ts → buckets-targets.d.ts} +2 -3
- package/dist/computed/buckets-targets.js +50 -0
- package/dist/computed/computed-input.js +65 -0
- package/dist/computed/computed-module.js +102 -0
- package/dist/computed/formula-sql.js +14 -1
- package/dist/computed/read-computed.js +100 -0
- package/dist/computed/rollup-sql.js +13 -10
- package/dist/config-exports.d.ts +2 -2
- package/dist/create-admin.js +21 -5
- package/dist/derived/derived-module.js +89 -0
- package/dist/derived/derived-sql.js +1 -47
- package/dist/functions/define-function.d.ts +10 -4
- package/dist/functions/routes.js +5 -3
- package/dist/functions/supabase-routes.js +7 -5
- package/dist/history/history-module.js +34 -0
- package/dist/index.d.ts +19 -28
- package/dist/index.js +11 -21
- package/dist/internal.d.ts +26 -10
- package/dist/internal.js +22 -8
- package/dist/jobs/functions-sql.js +7 -11
- package/dist/jobs/job-data.js +36 -0
- package/dist/jobs/job-types.d.ts +6 -3
- package/dist/jobs/jobs-module.d.ts +5 -0
- package/dist/jobs/jobs-module.js +84 -0
- package/dist/jobs/run-job.d.ts +1 -1
- package/dist/jobs/run-job.js +7 -5
- package/dist/jobs/runtime.d.ts +9 -4
- package/dist/jobs/runtime.js +11 -6
- package/dist/jobs/schema.d.ts +0 -11
- package/dist/jobs/schema.js +1 -59
- package/dist/jobs/steps/001-pg-boss-45.js +483 -0
- package/dist/live/notices-module.js +30 -0
- package/dist/live/notices-sql.js +2 -1
- package/dist/mail/feedback/feedback-schema.js +3 -30
- package/dist/mail/feedback/mail-feedback-module.js +37 -0
- package/dist/mail/unsubscribe/unsubscribe-schema.js +3 -21
- package/dist/mail/unsubscribe/unsubscribes-module.js +31 -0
- package/dist/migration-record/check-record.d.ts +9 -7
- package/dist/migration-record/check-record.js +12 -11
- package/dist/migration-record/module-needs.js +30 -0
- package/dist/migration-record/read-record.js +27 -15
- package/dist/migration-record/record-model.d.ts +7 -2
- package/dist/migration-record/record-model.js +14 -6
- package/dist/migration-record/startup-decision.js +18 -7
- package/dist/migration-sql/additive-statements.d.ts +4 -0
- package/dist/migration-sql/additive-statements.js +105 -0
- package/dist/notifications/devices-module.js +61 -0
- package/dist/notifications/history-module.js +50 -0
- package/dist/notifications/notifications-targets.js +23 -0
- package/dist/notifications/read-table.js +16 -0
- package/dist/notifications/schema-sql.js +1 -22
- package/dist/notifications/schema.js +1 -101
- package/dist/notifications/table-names.js +40 -0
- package/dist/organization-scope/app-scope.js +38 -0
- package/dist/organization-scope/auth-request-organization.js +71 -0
- package/dist/organization-scope/organization-tables.js +20 -0
- package/dist/organization-scope/sign-in-organization.js +21 -0
- package/dist/package.js +1 -1
- package/dist/personal-mail/shared-sender-store.js +4 -2
- package/dist/realtime/relay/realtime-relay-module.js +37 -0
- package/dist/relation-found.js +6 -0
- package/dist/relay/read-relay.js +85 -0
- package/dist/relay/realtime-members-sql.js +51 -28
- package/dist/relay/relay-module.js +53 -0
- package/dist/relay/relay-targets.js +28 -0
- package/dist/relay/session-end.js +1 -3
- package/dist/relay/sessions-sql.js +4 -27
- package/dist/rest/table-routes.js +9 -2
- package/dist/schema-modules/feature-schemas-sql.d.ts +40 -0
- package/dist/schema-modules/feature-schemas-sql.js +51 -0
- package/dist/schema-modules/module-difference.d.ts +31 -0
- package/dist/schema-modules/module-difference.js +98 -0
- package/dist/schema-modules/modules-sql.js +13 -0
- package/dist/schema-modules/modules.d.ts +37 -0
- package/dist/schema-modules/modules.js +56 -0
- package/dist/schema-modules/needed-modules.d.ts +41 -0
- package/dist/schema-modules/needed-modules.js +61 -0
- package/dist/schema-modules/resource-difference.js +83 -0
- package/dist/schema-modules/schema-module.d.ts +112 -0
- package/dist/schema-modules/schema-module.js +8 -0
- package/dist/status/status-module.js +63 -0
- package/dist/status/status-targets.js +21 -0
- package/dist/types.d.ts +6 -1
- package/dist/user-tables/reloading-admin.d.ts +1 -0
- package/dist/user-tables/reloading-admin.js +1 -0
- package/dist/user-tables/schema-sql.js +1 -14
- package/dist/user-tables/schema.js +4 -27
- package/dist/user-tables/user-tables-module.js +38 -0
- package/dist/webhooks/table/delivery-log-module.js +36 -0
- package/dist/webhooks/table/owner-roles.js +3 -2
- package/dist/webhooks/table/schema-sql.js +27 -26
- package/dist/webhooks/table/webhooks-schema-module.js +63 -0
- package/dist/webhooks/table/webhooks-targets.js +26 -0
- package/dist/webhooks/webhooks.js +1 -1
- package/dist/workflows/custom-steps/custom-steps-module.js +51 -0
- package/dist/workflows/custom-steps/custom-steps-sql.js +10 -7
- package/dist/workflows/custom-steps/runners-sql.js +10 -7
- package/dist/workflows/read-catalogue.js +23 -0
- package/dist/workflows/schema-sql.js +12 -9
- package/dist/workflows/status-sql.js +12 -6
- package/dist/workflows/storage/storage-module.js +39 -0
- package/dist/workflows/storage/storage-sql.js +8 -5
- package/dist/workflows/workflows-schema-module.js +49 -0
- package/dist/workflows/workflows-tables-sql.js +32 -0
- package/dist/workflows/workflows-targets.js +16 -0
- package/package.json +6 -6
- package/dist/assistant/query-runner/runner-plan.js +0 -30
- package/dist/better-auth/store/migrations-table.js +0 -27
- package/dist/better-auth/store/modules-off.js +0 -17
- package/dist/calls/calls-sql.d.ts +0 -11
- package/dist/changes/capture-plan.js +0 -79
- package/dist/channels/sends-schema.d.ts +0 -7
- package/dist/channels/sends-schema.js +0 -18
- package/dist/comments/comments-sql.d.ts +0 -14
- package/dist/computed/buckets-plan.d.ts +0 -2
- package/dist/computed/buckets-plan.js +0 -88
- package/dist/computed/buckets-schema.js +0 -39
- package/dist/computed/computed-plan.js +0 -81
- package/dist/computed/computed-schema.d.ts +0 -16
- package/dist/computed/computed-schema.js +0 -63
- package/dist/derived/derived-sql.d.ts +0 -12
- package/dist/mail/feedback/feedback-schema.d.ts +0 -7
- package/dist/mail/unsubscribe/unsubscribe-schema.d.ts +0 -10
- package/dist/notifications/schema.d.ts +0 -14
- package/dist/realtime/relay/relay-schema.d.ts +0 -7
- package/dist/realtime/relay/relay-schema.js +0 -20
- package/dist/relay/realtime-members-schema.js +0 -67
- package/dist/relay/relay-schema.d.ts +0 -14
- package/dist/relay/relay-schema.js +0 -28
- package/dist/status/schema.d.ts +0 -12
- package/dist/status/schema.js +0 -33
- package/dist/user-tables/schema.d.ts +0 -11
- package/dist/webhooks/table/schema.d.ts +0 -16
- package/dist/webhooks/table/schema.js +0 -56
- package/dist/workflows/schema.d.ts +0 -13
- package/dist/workflows/schema.js +0 -88
|
@@ -208,6 +208,23 @@ export declare const builtInModules: readonly [{
|
|
|
208
208
|
};
|
|
209
209
|
};
|
|
210
210
|
};
|
|
211
|
+
readonly auth_steps: {
|
|
212
|
+
readonly scope: "global";
|
|
213
|
+
readonly columns: {
|
|
214
|
+
readonly module: {
|
|
215
|
+
field: "module";
|
|
216
|
+
};
|
|
217
|
+
readonly step: {
|
|
218
|
+
field: "step";
|
|
219
|
+
};
|
|
220
|
+
readonly target: {
|
|
221
|
+
field: "target";
|
|
222
|
+
};
|
|
223
|
+
readonly applied_at: {
|
|
224
|
+
field: "appliedAt";
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
};
|
|
211
228
|
};
|
|
212
229
|
}, {
|
|
213
230
|
readonly id: "two_factor";
|
|
@@ -506,7 +523,10 @@ export declare const builtInModules: readonly [{
|
|
|
506
523
|
};
|
|
507
524
|
};
|
|
508
525
|
};
|
|
509
|
-
readonly indexes:
|
|
526
|
+
readonly indexes: {
|
|
527
|
+
readonly member_organization_id_idx: "organization";
|
|
528
|
+
readonly member_user_id_idx: "global";
|
|
529
|
+
};
|
|
510
530
|
};
|
|
511
531
|
readonly invitation: {
|
|
512
532
|
readonly model: "invitation";
|
|
@@ -543,7 +563,10 @@ export declare const builtInModules: readonly [{
|
|
|
543
563
|
};
|
|
544
564
|
};
|
|
545
565
|
};
|
|
546
|
-
readonly indexes:
|
|
566
|
+
readonly indexes: {
|
|
567
|
+
readonly invitation_organization_id_idx: "organization";
|
|
568
|
+
readonly invitation_email_idx: "global";
|
|
569
|
+
};
|
|
547
570
|
};
|
|
548
571
|
};
|
|
549
572
|
readonly columns: {
|
|
@@ -572,6 +595,11 @@ export declare const builtInModules: readonly [{
|
|
|
572
595
|
};
|
|
573
596
|
};
|
|
574
597
|
};
|
|
598
|
+
readonly indexes: {
|
|
599
|
+
readonly personal_access_token: {
|
|
600
|
+
readonly personal_access_token_organization_id_user_id_idx: "organization";
|
|
601
|
+
};
|
|
602
|
+
};
|
|
575
603
|
}, {
|
|
576
604
|
readonly id: "mcp";
|
|
577
605
|
readonly requires: ["auth"];
|
|
@@ -661,7 +689,7 @@ export declare const builtInModules: readonly [{
|
|
|
661
689
|
readonly tables: {
|
|
662
690
|
readonly personal_access_token: {
|
|
663
691
|
readonly model: "personalAccessToken";
|
|
664
|
-
readonly scope: "
|
|
692
|
+
readonly scope: "organization";
|
|
665
693
|
readonly columns: {
|
|
666
694
|
readonly user_id: {
|
|
667
695
|
field: "userId";
|
|
@@ -713,7 +741,9 @@ export declare const builtInModules: readonly [{
|
|
|
713
741
|
};
|
|
714
742
|
};
|
|
715
743
|
};
|
|
716
|
-
readonly indexes:
|
|
744
|
+
readonly indexes: {
|
|
745
|
+
readonly personal_access_token_user_id_idx: "global";
|
|
746
|
+
};
|
|
717
747
|
};
|
|
718
748
|
};
|
|
719
749
|
}, {
|
|
@@ -32,7 +32,10 @@ const organizations = {
|
|
|
32
32
|
input: false
|
|
33
33
|
})
|
|
34
34
|
},
|
|
35
|
-
indexes:
|
|
35
|
+
indexes: {
|
|
36
|
+
member_organization_id_idx: "organization",
|
|
37
|
+
member_user_id_idx: "global"
|
|
38
|
+
}
|
|
36
39
|
},
|
|
37
40
|
invitation: {
|
|
38
41
|
model: "invitation",
|
|
@@ -52,7 +55,10 @@ const organizations = {
|
|
|
52
55
|
input: true
|
|
53
56
|
})
|
|
54
57
|
},
|
|
55
|
-
indexes:
|
|
58
|
+
indexes: {
|
|
59
|
+
invitation_organization_id_idx: "organization",
|
|
60
|
+
invitation_email_idx: "global"
|
|
61
|
+
}
|
|
56
62
|
}
|
|
57
63
|
},
|
|
58
64
|
columns: {
|
|
@@ -66,7 +72,8 @@ const organizations = {
|
|
|
66
72
|
type: "string",
|
|
67
73
|
required: false
|
|
68
74
|
}) }
|
|
69
|
-
}
|
|
75
|
+
},
|
|
76
|
+
indexes: { personal_access_token: { personal_access_token_organization_id_user_id_idx: "organization" } }
|
|
70
77
|
};
|
|
71
78
|
//#endregion
|
|
72
79
|
export { organizations };
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { own } from "../declaration.js";
|
|
2
2
|
//#region src/better-auth/names/modules/personal-access-tokens.ts
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* The personal access tokens people make for their scripts: one row per token, holding its hash and never the token.
|
|
5
|
+
* Each belongs to the organization it works in, whose column organizations add; the hash stays unique app-wide.
|
|
6
|
+
*/
|
|
4
7
|
const personalAccessTokens = {
|
|
5
8
|
id: "personal_access_tokens",
|
|
6
9
|
requires: ["auth"],
|
|
7
10
|
tables: { personal_access_token: {
|
|
8
11
|
model: "personalAccessToken",
|
|
9
|
-
scope: "
|
|
12
|
+
scope: "organization",
|
|
10
13
|
columns: {
|
|
11
14
|
user_id: own("userId", {
|
|
12
15
|
type: "string",
|
|
@@ -37,7 +40,7 @@ const personalAccessTokens = {
|
|
|
37
40
|
required: false
|
|
38
41
|
})
|
|
39
42
|
},
|
|
40
|
-
indexes:
|
|
43
|
+
indexes: { personal_access_token_user_id_idx: "global" }
|
|
41
44
|
} }
|
|
42
45
|
};
|
|
43
46
|
//#endregion
|
|
@@ -7,7 +7,30 @@ const checkRequiredModules = (on) => {
|
|
|
7
7
|
if (off !== void 0) throw new Error(`The ${module.id} module is on while ${off}, which it requires, is off: turn ${off} on, or ${module.id} off`);
|
|
8
8
|
}
|
|
9
9
|
};
|
|
10
|
-
/** Every module
|
|
10
|
+
/** Every module, in the order they require one another. */
|
|
11
11
|
const allModules = builtInModules.map(({ id }) => id);
|
|
12
|
+
/**
|
|
13
|
+
* The modules the app's auth turns on, the one place the CLI's migrations and the runtime read them from: each
|
|
14
|
+
* feature's module is on while its switch is, `mcp` with the MCP endpoint or the OpenID provider, whose tables are the
|
|
15
|
+
* same, `mail_senders` with personal mail, and `platform_devices` with `signInDevices` or a provider of
|
|
16
|
+
* `signInProviders` that signs in devices: the config alone, as a migration is written without the platform's
|
|
17
|
+
* environment. The others are always on: sign-in itself, two-factor authentication, passkeys, the sign-in policy and
|
|
18
|
+
* staff sign-in have no switch.
|
|
19
|
+
*/
|
|
20
|
+
const modulesOf = (settings) => {
|
|
21
|
+
const on = {
|
|
22
|
+
auth: true,
|
|
23
|
+
two_factor: true,
|
|
24
|
+
passkeys: true,
|
|
25
|
+
sign_in_policy: true,
|
|
26
|
+
staff_sign_in: true,
|
|
27
|
+
platform_devices: settings.signInDevices === true || (settings.signInProviders ?? []).some(({ devices }) => devices === true),
|
|
28
|
+
organizations: Boolean(settings.organizations),
|
|
29
|
+
mcp: Boolean(settings.mcp) || Boolean(settings.openIdProvider),
|
|
30
|
+
personal_access_tokens: settings.personalAccessTokens !== void 0,
|
|
31
|
+
mail_senders: (settings.personalMail ?? []).length > 0
|
|
32
|
+
};
|
|
33
|
+
return allModules.filter((module) => on[module]);
|
|
34
|
+
};
|
|
12
35
|
//#endregion
|
|
13
|
-
export { allModules, checkRequiredModules };
|
|
36
|
+
export { allModules, checkRequiredModules, modulesOf };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TableDeclaration } from "./declaration.js";
|
|
1
|
+
import { IndexScope, TableDeclaration } from "./declaration.js";
|
|
2
2
|
import { ColumnId, IndexId, TableId } from "./identifiers.js";
|
|
3
3
|
import { BuiltInTables, ModuleId } from "./modules/index.js";
|
|
4
4
|
import { DBFieldAttribute } from "@better-auth/core/db";
|
|
@@ -18,6 +18,8 @@ export type ResolvedTable<Column extends string = string> = TableId & {
|
|
|
18
18
|
columns: Record<Column, ResolvedColumn>;
|
|
19
19
|
/** The columns by the Better Auth field each holds. */
|
|
20
20
|
fields: Record<string, ResolvedColumn>;
|
|
21
|
+
/** Its declared indexes, by internal name, with what each serves (`IndexScope`), those of the modules that are on included. */
|
|
22
|
+
indexes: Record<string, IndexScope>;
|
|
21
23
|
/** A declared index of the table, by internal name. */
|
|
22
24
|
index: (internal: string) => IndexId;
|
|
23
25
|
};
|
|
@@ -56,4 +58,10 @@ export type ResolvedNames = {
|
|
|
56
58
|
/** What the names were resolved from, to resolve them again in another schema. */
|
|
57
59
|
input: NamesInput;
|
|
58
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* The store's names: every table of the modules that are on, under the name `tables` gives it or its built-in one, each
|
|
63
|
+
* column likewise. Refuses a module that is on while one it requires is off, a table or column `tables` names that
|
|
64
|
+
* the store has not, a configured name Postgres would cut, and a name two tables, a table and an index, or two columns of one, share.
|
|
65
|
+
*/
|
|
66
|
+
export declare const resolveNames: (input: NamesInput) => ResolvedNames;
|
|
59
67
|
//#endregion
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { indexScopes } from "./declaration.js";
|
|
2
|
+
import { builtInModules } from "./modules/index.js";
|
|
1
3
|
import { checkConfiguredName, generatedName } from "./identifier-length.js";
|
|
2
4
|
import { columnId, indexId, tableId } from "./identifiers.js";
|
|
3
|
-
import { builtInModules } from "./modules/index.js";
|
|
4
5
|
import { allModules, checkRequiredModules } from "./modules-on.js";
|
|
5
6
|
//#region src/better-auth/names/resolve-names.ts
|
|
6
7
|
const declaredTables = (on) => {
|
|
@@ -9,7 +10,8 @@ const declaredTables = (on) => {
|
|
|
9
10
|
module,
|
|
10
11
|
internal,
|
|
11
12
|
table,
|
|
12
|
-
columns: Object.assign({}, table.columns, ...modules.map((other) => other.columns?.[internal] ?? {}))
|
|
13
|
+
columns: Object.assign({}, table.columns, ...modules.map((other) => other.columns?.[internal] ?? {})),
|
|
14
|
+
indexes: Object.assign({}, indexScopes(table), ...modules.map((other) => other.indexes?.[internal] ?? {}))
|
|
13
15
|
})));
|
|
14
16
|
};
|
|
15
17
|
const checkConfig = (tables, declared) => {
|
|
@@ -26,11 +28,15 @@ const checkConfig = (tables, declared) => {
|
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
};
|
|
31
|
+
/** The refusal of a name the store's names give twice, such as a table named as another module's. */
|
|
32
|
+
var NameClash = class extends Error {};
|
|
29
33
|
const refuseTwice = (names, what) => {
|
|
30
34
|
const twice = names.find((name, i) => names.indexOf(name) !== i);
|
|
31
|
-
if (twice !== void 0) throw new
|
|
35
|
+
if (twice !== void 0) throw new NameClash(`auth.tables gives ${what} the name "${twice}" twice: give each its own`);
|
|
32
36
|
};
|
|
33
|
-
|
|
37
|
+
/** The name `tables` gives a table, by internal name: its own `name`, or its built-in one after the prefix. */
|
|
38
|
+
const tableNameOf = (tables, internal) => tables[internal]?.name ?? generatedName(`${tables.prefix ?? ""}${internal}`);
|
|
39
|
+
const resolveTable = (schema, { module, internal, table, columns, indexes }, prefix, config = {}) => {
|
|
34
40
|
const id = tableId(schema, config.name ?? generatedName(`${prefix}${internal}`));
|
|
35
41
|
const resolved = Object.fromEntries(Object.entries(columns).map(([column, { field, attribute }]) => [column, {
|
|
36
42
|
...columnId(id, config.columns?.[column] ?? column),
|
|
@@ -39,7 +45,6 @@ const resolveTable = (schema, { module, internal, table, columns }, prefix, conf
|
|
|
39
45
|
...attribute && { attribute }
|
|
40
46
|
}]));
|
|
41
47
|
refuseTwice(["id", ...Object.values(resolved).map(({ name }) => name)], `two columns of ${internal}`);
|
|
42
|
-
const indexes = table.indexes ?? [];
|
|
43
48
|
return {
|
|
44
49
|
...id,
|
|
45
50
|
internal,
|
|
@@ -48,8 +53,9 @@ const resolveTable = (schema, { module, internal, table, columns }, prefix, conf
|
|
|
48
53
|
scope: table.scope,
|
|
49
54
|
columns: resolved,
|
|
50
55
|
fields: Object.fromEntries(Object.values(resolved).map((column) => [column.field, column])),
|
|
56
|
+
indexes,
|
|
51
57
|
index: (index) => {
|
|
52
|
-
if (!
|
|
58
|
+
if (!(index in indexes) || !index.startsWith(`${internal}_`)) throw new Error(`${index} is not an index ${internal} declares`);
|
|
53
59
|
return indexId(id, generatedName(`${id.name}${index.slice(internal.length)}`));
|
|
54
60
|
}
|
|
55
61
|
};
|
|
@@ -63,10 +69,10 @@ const resolveNames = (input) => {
|
|
|
63
69
|
const { schema, tables = {}, modules = allModules } = input;
|
|
64
70
|
checkRequiredModules(modules);
|
|
65
71
|
const declared = declaredTables(modules);
|
|
66
|
-
checkConfig(tables,
|
|
72
|
+
checkConfig(tables, declaredTables(allModules));
|
|
67
73
|
const configs = tables;
|
|
68
74
|
const each = declared.map((table) => ({
|
|
69
|
-
indexes:
|
|
75
|
+
indexes: Object.keys(table.indexes),
|
|
70
76
|
resolved: resolveTable(schema, table, tables.prefix ?? "", configs[table.internal])
|
|
71
77
|
}));
|
|
72
78
|
const resolved = Object.fromEntries(each.map((table) => [table.resolved.internal, table.resolved]));
|
|
@@ -86,4 +92,4 @@ const resolveNames = (input) => {
|
|
|
86
92
|
};
|
|
87
93
|
};
|
|
88
94
|
//#endregion
|
|
89
|
-
export { resolveNames };
|
|
95
|
+
export { NameClash, resolveNames, tableNameOf };
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { parseAppRoles } from "./app-roles.js";
|
|
2
|
+
import { acrossOrganizations } from "../../organization-scope/sign-in-organization.js";
|
|
2
3
|
import { storeSqlOf } from "../store/store-sql.js";
|
|
3
4
|
import { invalidInvitation, invitationIdOf, verifyInvitationToken } from "../invitations/invitation-token.js";
|
|
4
5
|
import { sql } from "kysely";
|
|
5
6
|
import { APIError } from "better-auth/api";
|
|
6
7
|
//#region src/better-auth/organizations/accept-invitation.ts
|
|
7
|
-
const findInvitation = (context, id) => context.adapter.findOne({
|
|
8
|
+
const findInvitation = (context, id) => acrossOrganizations("an invitation is found by its id for the person it invites, before they are a member", () => context.adapter.findOne({
|
|
8
9
|
model: "invitation",
|
|
9
10
|
where: [{
|
|
10
11
|
field: "id",
|
|
11
12
|
value: id
|
|
12
13
|
}]
|
|
13
|
-
});
|
|
14
|
+
}));
|
|
14
15
|
const pending = (row) => {
|
|
15
16
|
if (row.status !== "pending" || new Date(row.expiresAt).getTime() < Date.now()) throw new APIError("BAD_REQUEST", invalidInvitation);
|
|
16
17
|
return {
|
|
@@ -51,7 +52,7 @@ const acceptInvitation = async (context, invitation, user, membershipLimit) => {
|
|
|
51
52
|
const member = store.table("member");
|
|
52
53
|
const m = member.columns;
|
|
53
54
|
const invitations = store.table("invitation");
|
|
54
|
-
return store.db.transaction().execute(async (trx) => {
|
|
55
|
+
return acrossOrganizations("accepting an invitation makes its person a member of the invitation's organization", () => store.db.transaction().execute(async (trx) => {
|
|
55
56
|
await sql`select from ${store.table("organization")} where id = ${invitation.organizationId} for update`.execute(trx);
|
|
56
57
|
const { rows: [counted] } = await sql`select count(*)::int as members, count(*) filter (where ${m.user_id} = ${user.id}) > 0 as joined
|
|
57
58
|
from ${member} where ${m.organization_id} = ${invitation.organizationId}`.execute(trx);
|
|
@@ -72,7 +73,7 @@ const acceptInvitation = async (context, invitation, user, membershipLimit) => {
|
|
|
72
73
|
...made,
|
|
73
74
|
appRoles: invitation.appRoles
|
|
74
75
|
};
|
|
75
|
-
});
|
|
76
|
+
}));
|
|
76
77
|
};
|
|
77
78
|
//#endregion
|
|
78
79
|
export { acceptInvitation, invitationOfId, invitationOfToken };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { membershipRoles } from "./role-definitions.js";
|
|
2
|
+
import { acrossOrganizations } from "../../organization-scope/sign-in-organization.js";
|
|
2
3
|
import { storeSqlOf } from "../store/store-sql.js";
|
|
3
4
|
import { roleNames } from "../store/active-admin.js";
|
|
4
5
|
import { findOrganizationByKey } from "./host.js";
|
|
@@ -23,7 +24,7 @@ const addEveryUser = async (auth, organizationKey) => {
|
|
|
23
24
|
const m = member.columns;
|
|
24
25
|
const names = roleNames(sql`u.${u.role}`);
|
|
25
26
|
const now = /* @__PURE__ */ new Date();
|
|
26
|
-
return store.db.transaction().execute(async (trx) => {
|
|
27
|
+
return acrossOrganizations("everyone with an account joins the organization, whichever others they are a member of", () => store.db.transaction().execute(async (trx) => {
|
|
27
28
|
await sql`select from ${store.table("organization")} where id = ${organization.id} for update`.execute(trx);
|
|
28
29
|
const { rows } = await sql`with joined as (
|
|
29
30
|
insert into ${member} (id, ${m.organization_id}, ${m.user_id}, ${m.role}, ${m.app_roles}, ${m.created_at})
|
|
@@ -42,7 +43,7 @@ const addEveryUser = async (auth, organizationKey) => {
|
|
|
42
43
|
join ${user} u on u.id = joined.user_id
|
|
43
44
|
order by u.${u.created_at}, u.id`.execute(trx);
|
|
44
45
|
return rows;
|
|
45
|
-
});
|
|
46
|
+
}));
|
|
46
47
|
};
|
|
47
48
|
//#endregion
|
|
48
49
|
export { addEveryUser };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "../names/resolve-names.js";
|
|
2
|
+
import "../store/store-sql.js";
|
|
3
|
+
import { EveryOrganization, Organization } from "@protobase/query";
|
|
4
|
+
//#region src/better-auth/organizations/host-scope.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Where a host function works: one organization, by its id, or every one (`everyOrganization('<reason>')`), the reviewed
|
|
7
|
+
* way out that its caller names, such as `protobase organizations list --all-organizations`.
|
|
8
|
+
*/
|
|
9
|
+
export type HostScope = Organization | EveryOrganization;
|
|
10
|
+
//#endregion
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isEveryOrganization } from "@protobase/query";
|
|
2
|
+
//#region src/better-auth/organizations/host-scope.ts
|
|
3
|
+
/** A table of the store as Kysely's builder names it: `schema.table`, or the table alone where the search path finds it. */
|
|
4
|
+
const builderName = (table) => table.schema === void 0 ? table.name : `${table.schema}.${table.name}`;
|
|
5
|
+
/**
|
|
6
|
+
* A table of the store whose rows belong to one organization, for raw SQL in `scope`: the table itself across every
|
|
7
|
+
* organization, or the organization's rows, a query of Kysely's builder that names it, so a store kept to the
|
|
8
|
+
* organization (`ScopedDb`) checks it too, while one without the scope, such as a pg Pool's, reads it as written.
|
|
9
|
+
*/
|
|
10
|
+
const rowsIn = (store, scope, table) => {
|
|
11
|
+
if (isEveryOrganization(scope)) return table;
|
|
12
|
+
return store.db.selectFrom(builderName(table)).selectAll().where(table.columns.organization_id.name, "=", scope);
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { builderName, rowsIn };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { OrganizationRole, StoredMember, StoredOrganization } from "./store.js";
|
|
2
2
|
import { AdminAuth } from "../create-auth.js";
|
|
3
|
+
import { HostScope } from "./host-scope.js";
|
|
3
4
|
//#region src/better-auth/organizations/host.d.ts
|
|
5
|
+
/** An organization by its id or slug; null when there is none. */
|
|
6
|
+
export declare const organizationByKey: (auth: AdminAuth, key: string) => Promise<StoredOrganization | null>;
|
|
4
7
|
export type NewOrganization = {
|
|
5
8
|
name: string;
|
|
6
9
|
slug: string;
|
|
@@ -30,6 +33,8 @@ export type MemberInput = {
|
|
|
30
33
|
};
|
|
31
34
|
/** Adds someone with an account as a member: organization role `member` unless given, and the app roles given. */
|
|
32
35
|
export declare const addMember: (auth: AdminAuth, input: MemberInput) => Promise<StoredMember>;
|
|
36
|
+
/** Whether someone with `email` has an account and is a member of the organization with id `organizationId`. */
|
|
37
|
+
export declare const isMemberByEmail: (auth: AdminAuth, organizationId: string, email: string) => Promise<boolean>;
|
|
33
38
|
/** Changes a member's organization role, app roles, or both. */
|
|
34
39
|
export declare const setMemberRoles: (auth: AdminAuth, input: MemberInput) => Promise<StoredMember | undefined>;
|
|
35
40
|
/** Removes a member; refuses the last owner. */
|
|
@@ -59,16 +64,17 @@ export type OrganizationsPage = {
|
|
|
59
64
|
/** The organizations a page of `listOrganizations` holds when it is not told. */
|
|
60
65
|
export declare const organizationsPageSize = 100;
|
|
61
66
|
/**
|
|
62
|
-
* A page of the organizations, oldest first, each with its members' emails and roles, oldest member
|
|
63
|
-
* them from `after`, the `next` of the page before, as Postgres pages them, and their members in one
|
|
67
|
+
* A page of the organizations `scope` holds, oldest first, each with its members' emails and roles, oldest member
|
|
68
|
+
* first: `size` of them from `after`, the `next` of the page before, as Postgres pages them, and their members in one
|
|
69
|
+
* join.
|
|
64
70
|
*/
|
|
65
|
-
export declare const listOrganizations: (auth: AdminAuth,
|
|
71
|
+
export declare const listOrganizations: (auth: AdminAuth, scope: HostScope, page?: {
|
|
66
72
|
after?: OrganizationsCursor;
|
|
67
73
|
size?: number;
|
|
68
74
|
}) => Promise<OrganizationsPage>;
|
|
69
75
|
/**
|
|
70
|
-
* Gives an app role to every owner
|
|
71
|
-
*
|
|
76
|
+
* Gives an app role to every owner in `scope` who lacks it, in one statement, for a role the project adds once
|
|
77
|
+
* organizations exist. Returns how many owners got it.
|
|
72
78
|
*/
|
|
73
|
-
export declare const addAppRoleToOwners: (auth: AdminAuth, appRole: string) => Promise<number>;
|
|
79
|
+
export declare const addAppRoleToOwners: (auth: AdminAuth, scope: HostScope, appRole: string) => Promise<number>;
|
|
74
80
|
//#endregion
|