@protobase/cli 0.125.1 → 0.126.1
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/build/bundle-relay.js +30 -10
- package/dist/build/config-bundle.js +2 -2
- package/dist/build/deploy-bundle.js +6 -2
- package/dist/bundle/manifest.js +4 -4
- package/dist/cli/package.js +1 -1
- package/dist/dev/dev-relay.js +7 -3
- package/dist/dev/entry.js +13 -4
- package/dist/migration/load-project.js +3 -1
- package/dist/migration/parts.js +10 -5
- package/dist/relay/register.js +14 -5
- package/dist/serve/relay-check.js +19 -0
- package/dist/serve/startup-checks.js +9 -3
- package/package.json +8 -8
- package/skills/protobase/references/features/history-comments-and-live-updates.md +1 -1
- package/skills/protobase/references/reference/cli.md +6 -6
- package/skills/protobase/references/reference/realtime-relay.md +18 -18
|
@@ -1,20 +1,40 @@
|
|
|
1
1
|
import { projectResources } from "../config/project-parts.js";
|
|
2
2
|
import { relayAppPart } from "../relay/app-entry.js";
|
|
3
|
-
import { configExports } from "@protobase/server";
|
|
3
|
+
import { appRelayChannels, configExports } from "@protobase/server";
|
|
4
4
|
//#region src/build/bundle-relay.ts
|
|
5
5
|
/**
|
|
6
|
-
* What the manifest says of the
|
|
7
|
-
* the
|
|
8
|
-
* `
|
|
6
|
+
* What the manifest says of the app's relay: the app's part of its entry in the relay's apps file, from `realtime.relay`
|
|
7
|
+
* and the app's views, or else from the channels the config folder's conversation pages derive (`appRelayChannels`),
|
|
8
|
+
* as `protobase relay app` prints it, without the address, id or keys a build does not hold. `undefined` for an app
|
|
9
|
+
* that cannot use the relay, so its presence means the app uses it, or the platform can move it there with its switch.
|
|
10
|
+
* Only `realtime.relay` loads the app's resources again; deriving reads the folder the view check loaded.
|
|
9
11
|
*/
|
|
10
|
-
const
|
|
12
|
+
const bundleRealtime = async (config, folder) => {
|
|
11
13
|
const relay = config.realtime?.relay;
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
if (relay) {
|
|
15
|
+
const exported = configExports(await projectResources(config));
|
|
16
|
+
const app = {
|
|
17
|
+
views: exported.views.map((view) => view.toModel()),
|
|
18
|
+
models: exported.resources.map((resource) => resource.toModel()),
|
|
19
|
+
signIn: true
|
|
20
|
+
};
|
|
21
|
+
return relayAppPart({
|
|
22
|
+
...relay,
|
|
23
|
+
channels: appRelayChannels(relay, app)
|
|
24
|
+
}, {
|
|
25
|
+
apiPath: config.apiPath,
|
|
26
|
+
views: app.views
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const channels = appRelayChannels(void 0, {
|
|
30
|
+
...folder,
|
|
31
|
+
signIn: Boolean(config.auth)
|
|
32
|
+
});
|
|
33
|
+
if (!channels) return;
|
|
34
|
+
return relayAppPart({ channels }, {
|
|
15
35
|
apiPath: config.apiPath,
|
|
16
|
-
views
|
|
36
|
+
views: folder.views
|
|
17
37
|
});
|
|
18
38
|
};
|
|
19
39
|
//#endregion
|
|
20
|
-
export {
|
|
40
|
+
export { bundleRealtime };
|
|
@@ -7,7 +7,7 @@ import { layoutCheck } from "./layout-check.js";
|
|
|
7
7
|
import { extensionImports } from "../project/extensions.js";
|
|
8
8
|
import { viteBundle } from "./vite-bundle.js";
|
|
9
9
|
import { cliProtobaseIds, provideCliModules } from "../project/cli-modules.js";
|
|
10
|
-
import {
|
|
10
|
+
import { bundleRealtime } from "./bundle-relay.js";
|
|
11
11
|
import { collectComponents } from "./component-names.js";
|
|
12
12
|
import { bundleEntryCode } from "./entry-code.js";
|
|
13
13
|
import { hostPackages } from "./packages/host-packages.js";
|
|
@@ -102,7 +102,7 @@ const buildConfigBundle = async ({ projectDir, outFile, bun, extensions = [], co
|
|
|
102
102
|
native: [...native.values()],
|
|
103
103
|
mobile: config.mobile,
|
|
104
104
|
notifications: Boolean(config.notifications),
|
|
105
|
-
|
|
105
|
+
realtime: (folder) => bundleRealtime(config, folder)
|
|
106
106
|
};
|
|
107
107
|
};
|
|
108
108
|
//#endregion
|
|
@@ -53,7 +53,7 @@ const buildDeployBundle = async ({ projectDir, outDir, bun, extensions = [], sou
|
|
|
53
53
|
await removeOwnFolder(outDir, bundleNodeModules, "nodeModules");
|
|
54
54
|
await removeOwnFolder(outDir, bundleMigrations, "migrations");
|
|
55
55
|
const components = /* @__PURE__ */ new Set();
|
|
56
|
-
const { native, mobile, notifications,
|
|
56
|
+
const { native, mobile, notifications, realtime } = await buildConfigBundle({
|
|
57
57
|
projectDir,
|
|
58
58
|
outFile: path.join(outDir, bundleLayout.server),
|
|
59
59
|
bun,
|
|
@@ -62,6 +62,10 @@ const buildDeployBundle = async ({ projectDir, outDir, bun, extensions = [], sou
|
|
|
62
62
|
});
|
|
63
63
|
const mobileSection = bundleMobile(mobile, notifications);
|
|
64
64
|
const { pages, views, models } = await checkViews(projectDir);
|
|
65
|
+
const realtimePart = await realtime({
|
|
66
|
+
views,
|
|
67
|
+
models
|
|
68
|
+
});
|
|
65
69
|
checkPageScopes(pages, models, {
|
|
66
70
|
strict,
|
|
67
71
|
onWarning,
|
|
@@ -104,7 +108,7 @@ const buildDeployBundle = async ({ projectDir, outDir, bun, extensions = [], sou
|
|
|
104
108
|
...mobileSection && { mobile: mobileSection },
|
|
105
109
|
...packages.length > 0 ? { nodeModules: bundleNodeModules } : {},
|
|
106
110
|
...migrations ? { migrations } : {},
|
|
107
|
-
...
|
|
111
|
+
...realtimePart && { realtime: realtimePart }
|
|
108
112
|
};
|
|
109
113
|
await writeFile(path.join(outDir, manifestFile), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
110
114
|
return manifest;
|
package/dist/bundle/manifest.js
CHANGED
|
@@ -21,12 +21,12 @@ const isPlugin = (value) => isObject(value) && typeof value.name === "string" &&
|
|
|
21
21
|
const isShell = (value) => isObject(value) && typeof value.protobase === "string" && Number.isInteger(value.pluginApi) && isStringList(value.paths) && (value.builtAt === void 0 || isTime(value.builtAt));
|
|
22
22
|
const isTime = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
23
23
|
const isVersions = (value) => isObject(value) && Object.values(value).every((item) => typeof item === "string");
|
|
24
|
-
const
|
|
24
|
+
const isRealtime = (value) => isObject(value) && (value.client_events === void 0 || isObject(value.client_events) && isObject(value.client_events.channels)) && (value.presence === void 0 || isObject(value.presence) && typeof value.presence.path === "string" && Number.isInteger(value.presence.grace_seconds));
|
|
25
25
|
const isIcons = (value) => isObject(value) && typeof value.favicon === "boolean" && isStringList(value.png) && value.png.every((icon) => icon in pngIcons);
|
|
26
26
|
const parseManifest = (value, file) => {
|
|
27
27
|
const manifest = value;
|
|
28
28
|
if (manifest?.version !== 3) throw new Error(`${file} is not a version 3 bundle manifest; rebuild it with \`protobase build\``);
|
|
29
|
-
const { builtAt, server, public: publicDir, api, spa, plugins, theme, shell, icons, native, mobile, nodeModules, migrations,
|
|
29
|
+
const { builtAt, server, public: publicDir, api, spa, plugins, theme, shell, icons, native, mobile, nodeModules, migrations, realtime } = manifest;
|
|
30
30
|
if (typeof server !== "string" || typeof publicDir !== "string" || typeof spa !== "string" || typeof theme !== "string" || !isStringList(api)) throw new Error(`${file} needs server, public, spa and theme paths and an api list`);
|
|
31
31
|
if (!Array.isArray(plugins) || !plugins.every(isPlugin)) throw new Error(`${file} needs a plugins list, each with a name, path and api`);
|
|
32
32
|
if (!isShell(shell)) throw new Error(`${file} needs a shell with its protobase version, pluginApi and paths`);
|
|
@@ -36,7 +36,7 @@ const parseManifest = (value, file) => {
|
|
|
36
36
|
if (mobile !== void 0 && !(isObject(mobile) && isVersions(mobile.plugins) && (mobile.appId === void 0 || typeof mobile.appId === "string") && (mobile.push === void 0 || typeof mobile.push === "boolean"))) throw new Error(`${file} has a mobile that is not { appId, plugins, push } with an app id, a map of package names to versions and true or false`);
|
|
37
37
|
if (nodeModules !== void 0 && typeof nodeModules !== "string") throw new Error(`${file} has a nodeModules that is no path`);
|
|
38
38
|
if (migrations !== void 0 && typeof migrations !== "string") throw new Error(`${file} has a migrations that is no path`);
|
|
39
|
-
if (
|
|
39
|
+
if (realtime !== void 0 && !isRealtime(realtime)) throw new Error(`${file} has a realtime that is not { client_events, presence } with its channels and a presence path and grace`);
|
|
40
40
|
return {
|
|
41
41
|
version: 3,
|
|
42
42
|
...builtAt !== void 0 && { builtAt },
|
|
@@ -52,7 +52,7 @@ const parseManifest = (value, file) => {
|
|
|
52
52
|
...mobile && { mobile },
|
|
53
53
|
...nodeModules === void 0 ? {} : { nodeModules },
|
|
54
54
|
...migrations === void 0 ? {} : { migrations },
|
|
55
|
-
...
|
|
55
|
+
...realtime && { realtime }
|
|
56
56
|
};
|
|
57
57
|
};
|
|
58
58
|
const readManifest = async (bundleDir) => {
|
package/dist/cli/package.js
CHANGED
package/dist/dev/dev-relay.js
CHANGED
|
@@ -2,7 +2,7 @@ import { relayAppEntry } from "../relay/app-entry.js";
|
|
|
2
2
|
import { relayBinary } from "./relay-binary.js";
|
|
3
3
|
import { freePort, relayUrl, startRelay } from "./relay-process.js";
|
|
4
4
|
import { randomBytes } from "node:crypto";
|
|
5
|
-
import { checkRelayOptions } from "@protobase/server";
|
|
5
|
+
import { checkRelayOptions, defaultRelayChannels } from "@protobase/server";
|
|
6
6
|
//#region src/dev/dev-relay.ts
|
|
7
7
|
const store = globalThis;
|
|
8
8
|
const devLimits = {
|
|
@@ -23,10 +23,10 @@ const relayEnded = (code) => {
|
|
|
23
23
|
/**
|
|
24
24
|
* The config with a relay of its own when `realtime.relay` names none: without a `url`, protobase dev runs the relay
|
|
25
25
|
* from its npm package for the app at `devUrl`, with keys it makes unless the config sets them, the presence key too, and gives the app its
|
|
26
|
-
* address and keys; its apps file declares the events of `views`' typing conversation pages too. It starts on the first load whose config has `realtime.relay`, and again when an edit changes the
|
|
26
|
+
* address and keys; its apps file declares the events of `views`' typing conversation pages too, and its channels default to those of the conversation pages with members (`defaultRelayChannels`). It starts on the first load whose config has `realtime.relay`, and again when an edit changes the
|
|
27
27
|
* app's entry in its apps file; a config without `realtime.relay`, or one with a `url`, stops it.
|
|
28
28
|
*/
|
|
29
|
-
const withDevRelay = async (config, projectDir, devUrl, views) => {
|
|
29
|
+
const withDevRelay = async (config, projectDir, devUrl, { views, models }) => {
|
|
30
30
|
const relay = config.realtime?.relay;
|
|
31
31
|
if (!relay || relay.url) {
|
|
32
32
|
await stopDevRelay();
|
|
@@ -35,6 +35,10 @@ const withDevRelay = async (config, projectDir, devUrl, views) => {
|
|
|
35
35
|
const keys = devKeys();
|
|
36
36
|
const options = checkRelayOptions({
|
|
37
37
|
...relay,
|
|
38
|
+
channels: relay.channels ?? defaultRelayChannels({
|
|
39
|
+
views,
|
|
40
|
+
models
|
|
41
|
+
}),
|
|
38
42
|
url: devUrl,
|
|
39
43
|
apiKey: relay.apiKey || keys.apiKey,
|
|
40
44
|
ticketKey: relay.ticketKey || keys.ticketKey,
|
package/dist/dev/entry.js
CHANGED
|
@@ -4,19 +4,24 @@ import { projectIcons } from "../build/app-icons.js";
|
|
|
4
4
|
import { projectErrorReporter } from "../project/error-reporter.js";
|
|
5
5
|
import { servedAdmin } from "../project/served-admin.js";
|
|
6
6
|
import { devJobs } from "./dev-jobs.js";
|
|
7
|
+
import { checkProjectRelay } from "../serve/relay-check.js";
|
|
7
8
|
import { withDevRelay } from "./dev-relay.js";
|
|
8
9
|
import { devRealtimePrune } from "./dev-prune.js";
|
|
9
10
|
import { devDatabase, loadDevConfig } from "./project.js";
|
|
10
11
|
import { withDevServiceWorker } from "./service-worker-route.js";
|
|
11
12
|
import { setMissing, supabaseEnv } from "./supabase-env.js";
|
|
12
|
-
import { apiKeySettings, assistantSettings, authSchemaDrift, authStoreTables, checkAi, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay,
|
|
13
|
+
import { apiKeySettings, assistantSettings, authSchemaDrift, authStoreTables, checkAi, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkResourcesOutsideAuthStore, checkStatusSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, hasUsers, relayed, sqlSends } from "@protobase/server";
|
|
13
14
|
import { upgradeWebSocket } from "@hono/node-server";
|
|
14
15
|
import { Hono } from "hono";
|
|
15
16
|
//#region src/dev/entry.ts
|
|
16
17
|
const projectDir = process.env.PROTOBASE_PROJECT;
|
|
17
18
|
const loaded = await loadDevConfig(projectDir, JSON.parse(process.env.PROTOBASE_EXTENSIONS ?? "[]"));
|
|
18
|
-
const
|
|
19
|
-
const
|
|
19
|
+
const relayExports = loaded.realtime?.relay ? configExports(await projectResources(loaded)) : void 0;
|
|
20
|
+
const relayApp = {
|
|
21
|
+
views: relayExports?.views.map((view) => view.toModel()) ?? [],
|
|
22
|
+
models: relayExports?.resources.map((resource) => resource.toModel()) ?? []
|
|
23
|
+
};
|
|
24
|
+
const config = await withDevRelay(loaded, projectDir, process.env.PROTOBASE_DEV_URL, relayApp);
|
|
20
25
|
const database = devDatabase(projectDir);
|
|
21
26
|
const { db } = database;
|
|
22
27
|
const project = await buildProject(config, database);
|
|
@@ -46,7 +51,11 @@ if (project.webhooks) await checkWebhooksSchema(db, project.webhooks);
|
|
|
46
51
|
if (project.workflows) await checkWorkflowsSchema(db, project.workflows);
|
|
47
52
|
if (relayed(project.realtime)) await checkRealtimeRelay(db);
|
|
48
53
|
if (sqlSends(project.realtime)) await checkChannelSends(db);
|
|
49
|
-
|
|
54
|
+
await checkProjectRelay(db, {
|
|
55
|
+
...project,
|
|
56
|
+
models: resources.map((resource) => resource.toModel()),
|
|
57
|
+
views: views.map((view) => view.toModel())
|
|
58
|
+
}, process.env);
|
|
50
59
|
if (project.notifications) await checkNotificationsSchema(db, project.notifications, project.auth);
|
|
51
60
|
if (project.status) await checkStatusSchema(db, project.status, project.auth);
|
|
52
61
|
if (project.userTables) await checkUserTablesSchema(db);
|
|
@@ -35,8 +35,10 @@ const migrationProject = async (config, url) => {
|
|
|
35
35
|
projectNotifications(config),
|
|
36
36
|
projectAuth(config, database)
|
|
37
37
|
]);
|
|
38
|
+
const exported = configExports(resources);
|
|
38
39
|
return {
|
|
39
|
-
models:
|
|
40
|
+
models: exported.resources.map((source) => source.toModel()),
|
|
41
|
+
views: exported.views.map((source) => source.toModel()),
|
|
40
42
|
db: database.db,
|
|
41
43
|
...auth && { auth },
|
|
42
44
|
...config.events && { events: config.events },
|
package/dist/migration/parts.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { callsMigration, capturedBy, changesMigration, channelSendsMigration, commentsMigration, computedMigration, derivedMigration, hasBuckets, jobsSchemaMigration, mailFeedbackMigration, mailFeedbackResource, notificationsMigration, ownedDefinitionsMigration, queryRunnerMigration, realtimeRelayMigration, relayMigration, relayed, sqlSends, statusMigration, unsubscribesMigration, userTablesMigration, webhooksMigration, workflowsMigration } from "@protobase/server";
|
|
1
|
+
import { appRelayChannels, callsMigration, capturedBy, changesMigration, channelSendsMigration, commentsMigration, computedMigration, derivedMigration, hasBuckets, jobsSchemaMigration, mailFeedbackMigration, mailFeedbackResource, notificationsMigration, ownedDefinitionsMigration, queryRunnerMigration, realtimeRelayMigration, relayMigration, relayed, sqlSends, statusMigration, unsubscribesMigration, userTablesMigration, webhooksMigration, workflowsMigration } from "@protobase/server";
|
|
2
2
|
//#region src/migration/parts.ts
|
|
3
3
|
/**
|
|
4
4
|
* Every feature the project's config enables but the auth store, which the snapshot holds, in the order their
|
|
@@ -9,8 +9,13 @@ const migrationParts = (project) => {
|
|
|
9
9
|
const aiFields = project.models.some((model) => Object.values(model.fields).some((field) => field.ai));
|
|
10
10
|
const feedback = project.mailFeedback ? [mailFeedbackResource.toModel()] : [];
|
|
11
11
|
const captured = [...project.models, ...feedback];
|
|
12
|
+
const relayChannels = appRelayChannels(project.realtime?.relay, {
|
|
13
|
+
views: project.views,
|
|
14
|
+
models: project.models,
|
|
15
|
+
signIn: Boolean(project.auth)
|
|
16
|
+
});
|
|
12
17
|
return [
|
|
13
|
-
...project.jobs.length > 0 || captured.some(capturedBy(captured)) || comments || aiFields || project.models.some(hasBuckets) || project.webhooks || project.workflows || project.notifications ||
|
|
18
|
+
...project.jobs.length > 0 || captured.some(capturedBy(captured)) || comments || aiFields || project.models.some(hasBuckets) || project.webhooks || project.workflows || project.notifications || relayChannels ? [{
|
|
14
19
|
name: "jobs",
|
|
15
20
|
title: "The jobs schema this version of Protobase needs",
|
|
16
21
|
sql: () => jobsSchemaMigration(project.db)
|
|
@@ -90,10 +95,10 @@ const migrationParts = (project) => {
|
|
|
90
95
|
title: "The delete of the definitions of user tables that resources in code own now",
|
|
91
96
|
sql: () => ownedDefinitionsMigration(project.db, project.models)
|
|
92
97
|
}] : [],
|
|
93
|
-
...
|
|
98
|
+
...relayChannels ? [{
|
|
94
99
|
name: "relay",
|
|
95
|
-
title: "The trigger that closes a deleted sign-in session's sockets on the realtime relay",
|
|
96
|
-
sql: ({ authWritten }) => relayMigration(project.db, project.auth, authWritten)
|
|
100
|
+
title: "The trigger that closes a deleted sign-in session's sockets on the realtime relay, and realtime.members with the triggers that keep it from the channel records that list their members",
|
|
101
|
+
sql: ({ authWritten }) => relayMigration(project.db, project.auth, authWritten, relayChannels, project.models)
|
|
97
102
|
}] : [],
|
|
98
103
|
...sqlSends(project.realtime) ? [{
|
|
99
104
|
name: "channel_sends",
|
package/dist/relay/register.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//#region src/relay/register.ts
|
|
2
2
|
const registerRelay = (program) => {
|
|
3
|
-
program.command("relay").description("The realtime relay the app hands its connections to (realtime.relay)").command("app").description("Print the app's entry for the relay's apps file, from realtime.relay or
|
|
4
|
-
const [{ configExports, relaySettings }, { loadSourceConfig }, { projectResources }, { loadNearestEnvFile }, { relayAppEntry }] = await Promise.all([
|
|
3
|
+
program.command("relay").description("The realtime relay the app hands its connections to (realtime.relay)").command("app").description("Print the app's entry for the relay's apps file, from realtime.relay or the conversation pages' members, PROTOBASE_REALTIME_*, url and the views").action(async () => {
|
|
4
|
+
const [{ appRelayChannels, configExports, relaySettings }, { loadSourceConfig }, { projectResources }, { loadNearestEnvFile }, { relayAppEntry }] = await Promise.all([
|
|
5
5
|
import("@protobase/server"),
|
|
6
6
|
import("../config/source-config.js"),
|
|
7
7
|
import("../config/project-parts.js"),
|
|
@@ -10,10 +10,19 @@ const registerRelay = (program) => {
|
|
|
10
10
|
]);
|
|
11
11
|
loadNearestEnvFile(process.cwd());
|
|
12
12
|
const config = await loadSourceConfig(process.cwd(), { command: "tool" });
|
|
13
|
-
if (!config.realtime?.relay) throw new Error("The config has no realtime.relay");
|
|
14
13
|
const url = config.url ?? process.env.PROTOBASE_URL;
|
|
15
|
-
const
|
|
16
|
-
|
|
14
|
+
const exported = configExports(await projectResources(config));
|
|
15
|
+
const views = exported.views.map((view) => view.toModel());
|
|
16
|
+
const channels = appRelayChannels(config.realtime?.relay, {
|
|
17
|
+
views,
|
|
18
|
+
models: exported.resources.map((resource) => resource.toModel()),
|
|
19
|
+
signIn: Boolean(config.auth)
|
|
20
|
+
});
|
|
21
|
+
if (!channels) throw new Error("The app cannot use the relay: its config has no realtime.relay, and no live conversation resource whose page has members, with sign-in, to derive its channels from");
|
|
22
|
+
process.stdout.write(`${JSON.stringify(relayAppEntry(relaySettings({
|
|
23
|
+
...config.realtime?.relay,
|
|
24
|
+
channels
|
|
25
|
+
}, process.env), {
|
|
17
26
|
url,
|
|
18
27
|
apiPath: config.apiPath,
|
|
19
28
|
views
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { appRelayChannels, checkRelaySchema, readRelaySwitch } from "@protobase/server";
|
|
2
|
+
//#region src/serve/relay-check.ts
|
|
3
|
+
/**
|
|
4
|
+
* Refuses to start while the relay is on, by the config's `realtime.relay` or the platform's switch, and the app's
|
|
5
|
+
* database lacks what its channels need of it. A switch with no channels to derive is left to the server, which
|
|
6
|
+
* stops naming it.
|
|
7
|
+
*/
|
|
8
|
+
const checkProjectRelay = async (db, { realtime, auth, models, views }, env) => {
|
|
9
|
+
if (!realtime?.relay && !readRelaySwitch(env)) return;
|
|
10
|
+
const channels = appRelayChannels(realtime?.relay, {
|
|
11
|
+
views,
|
|
12
|
+
models,
|
|
13
|
+
signIn: Boolean(auth)
|
|
14
|
+
});
|
|
15
|
+
if (!channels) return;
|
|
16
|
+
await checkRelaySchema(db, auth, channels, models);
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { checkProjectRelay };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { checkProjectRelay } from "./relay-check.js";
|
|
2
|
+
import { assistantSettings, authStoreTables, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkMailFeedbackSchema, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkResourcesOutsideAuthStore, checkStatusSchema, checkUnsubscribesSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, mailFeedbackResource, relayed, sqlSends } from "@protobase/server";
|
|
2
3
|
//#region src/serve/startup-checks.ts
|
|
3
4
|
/**
|
|
4
5
|
* The server's startup checks of the database: each throws, with what to write as a migration, while a schema the
|
|
@@ -6,7 +7,8 @@ import { assistantSettings, authStoreTables, checkAuthSchema, checkCalls, checkC
|
|
|
6
7
|
* `protobase doctor` runs them too, so a change that would not start fails wherever doctor runs.
|
|
7
8
|
*/
|
|
8
9
|
const checkStartup = async (project, db) => {
|
|
9
|
-
const
|
|
10
|
+
const exported = configExports(project.config);
|
|
11
|
+
const models = exported.resources.map((resource) => resource.toModel());
|
|
10
12
|
const captured = project.mailFeedback ? [...models, mailFeedbackResource.toModel()] : models;
|
|
11
13
|
if (project.auth) await checkAuthSchema(project.auth);
|
|
12
14
|
if (project.auth) await checkResourcesOutsideAuthStore(db, models, project.auth);
|
|
@@ -24,7 +26,11 @@ const checkStartup = async (project, db) => {
|
|
|
24
26
|
if (project.workflows) await checkWorkflowsSchema(db, project.workflows);
|
|
25
27
|
if (relayed(project.realtime)) await checkRealtimeRelay(db);
|
|
26
28
|
if (sqlSends(project.realtime)) await checkChannelSends(db);
|
|
27
|
-
|
|
29
|
+
await checkProjectRelay(db, {
|
|
30
|
+
...project,
|
|
31
|
+
models,
|
|
32
|
+
views: exported.views.map((view) => view.toModel())
|
|
33
|
+
}, process.env);
|
|
28
34
|
if (project.notifications) await checkNotificationsSchema(db, project.notifications, project.auth);
|
|
29
35
|
if (project.status) await checkStatusSchema(db, project.status, project.auth);
|
|
30
36
|
if (project.unsubscribe) await checkUnsubscribesSchema(db);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.126.1",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@better-auth/core": "^1.7.12",
|
|
45
45
|
"@hono/node-server": "^2.1.3",
|
|
46
|
-
"@protobase/config": "0.
|
|
47
|
-
"@protobase/layout": "0.
|
|
48
|
-
"@protobase/query": "0.
|
|
49
|
-
"@protobase/schema": "0.
|
|
50
|
-
"@protobase/server": "0.
|
|
51
|
-
"@protobase/ui": "0.
|
|
52
|
-
"@protobase/workflows": "0.
|
|
46
|
+
"@protobase/config": "0.126.1",
|
|
47
|
+
"@protobase/layout": "0.126.1",
|
|
48
|
+
"@protobase/query": "0.126.1",
|
|
49
|
+
"@protobase/schema": "0.126.1",
|
|
50
|
+
"@protobase/server": "0.126.1",
|
|
51
|
+
"@protobase/ui": "0.126.1",
|
|
52
|
+
"@protobase/workflows": "0.126.1",
|
|
53
53
|
"@react-email/render": "^2.1.0",
|
|
54
54
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
55
55
|
"@sentry/vite-plugin": "^11.10.0",
|
|
@@ -17,7 +17,7 @@ The changes of each record, comments on it, and pages that refresh as records ch
|
|
|
17
17
|
- [Change stream](../reference/live.md#the-stream): Apps follow record changes over a stream that catches up after a reconnect.
|
|
18
18
|
- [Polling, then a socket](../reference/live.md#polling-then-a-socket): Apps and Node scripts poll for changes first and switch to a WebSocket after 30 seconds, or at once, falling back to polling while the socket is down.
|
|
19
19
|
- [Presence](../reference/channels.md#presence) (beta): Apps see who else has a record open and what each of them tracks, such as online or away, for whoever may read the record.
|
|
20
|
-
- [Realtime relay](../reference/realtime-relay.md) (in development): Apps hand their realtime connections to a relay, so they and their database can sleep while people stay connected: the app issues connect tickets for signed-in people and visitors, keeps the relay's channel members in step with its members records, tells each channel's members of every change, and closes a session's sockets when it signs out or expires. `protobase dev` runs a relay for the app by itself. An app on a platform that runs the relay picks up its address and keys from
|
|
20
|
+
- [Realtime relay](../reference/realtime-relay.md) (in development): Apps hand their realtime connections to a relay, so they and their database can sleep while people stay connected: the app issues connect tickets for signed-in people and visitors, keeps the relay's channel members in step with its members records, tells each channel's members of every change, and closes a session's sockets when it signs out or expires. `protobase dev` runs a relay for the app by itself. An app on a platform that runs the relay, Protobase Realtime, picks up its address and keys from PROTOBASE_REALTIME_URL, PROTOBASE_REALTIME_APP, PROTOBASE_REALTIME_API_KEY, PROTOBASE_REALTIME_TICKET_KEY and PROTOBASE_REALTIME_PRESENCE_KEY; a value in the config wins. `protobase build` writes the app's events and presence for the relay's apps file into the bundle's manifest, and the platform's PROTOBASE_REALTIME_ENABLED switch, true or false, says when it moves the app, an app without realtime.relay too: its conversation pages with members become its channels, whose members Protobase keeps in an indexed table of its own.
|
|
21
21
|
- [Relay presence](../reference/realtime-relay.md#presence) (in development): The relay reports who comes online, goes away and goes offline; the app keeps it in a resource of its own, raises that resource's events, so workflows can act when someone goes offline, and tells the people who share a conversation with them, whose sidebar and members page show them online, away or offline.
|
|
22
22
|
- [Relay in pages](../reference/realtime-relay.md#pages) (in development): Pages follow the relay on one socket: open views stay current from its notices and read again once after a reconnect, conversations show who types, sidebar groups say "typing…" and count unread messages without reading per message, also of a conversation the person was just added to, all by configuration; `client.relay` gives the same from code.
|
|
23
23
|
- [Relay client events](../reference/realtime-relay.md#client-events) (in development): Each channel resource declares in the config the events its clients may send each other, such as typing, with a schema, a size and a rate.
|
|
@@ -290,7 +290,7 @@ By default the bundle runs on Node and on Bun. `--bun` makes it a bundle for Bun
|
|
|
290
290
|
"mobile": { "appId": "com.example.shop", "plugins": { "@capacitor/camera": "^8.0.1" }, "push": true },
|
|
291
291
|
"nodeModules": "node_modules",
|
|
292
292
|
"migrations": "migrations",
|
|
293
|
-
"
|
|
293
|
+
"realtime": {
|
|
294
294
|
"client_events": { "channels": { "conversations": { "events": { "typing": { "schema": { "type": "object" }, "max_bytes": 256, "per_second": 2, "burst": 4 } } } } },
|
|
295
295
|
"presence": { "path": "/api/relay/presence", "grace_seconds": 30 }
|
|
296
296
|
}
|
|
@@ -306,7 +306,7 @@ By default the bundle runs on Node and on Bun. `--bun` makes it a bundle for Bun
|
|
|
306
306
|
- `theme` is the project's [theme](theme.md), which the app links at `/protobase.theme.css`. It is always there, empty when the project has none.
|
|
307
307
|
- `nodeModules` is present only when the config module imports [native packages](#native-packages). It is always the `node_modules` folder beside `server`, where Bun and Node resolve the module's imports from, and holds plain packages: JavaScript, `package.json` files and native binaries for linux x64 and arm64 (glibc), which the runtime loads with `dlopen`. A host keeps it next to the server module and must not mount it `noexec` (an add-on there then fails with `failed to map segment from shared object`). Its binaries link nothing but glibc, `libstdc++.so.6`, `libgcc_s.so.1` and libraries in the bundle itself, so the run image must have those three. Without `nodeModules`, the server module is the only file the runtime reads.
|
|
308
308
|
- `migrations` is present only when the project has a `db/migrations` folder: its `.sql` files, which the runtime's `migrate` applies (see [Deploy](../guides/deploy.md)). A host never serves it.
|
|
309
|
-
- `
|
|
309
|
+
- `realtime` is present only when the config has [`realtime.relay`](realtime-relay.md), or the app has sign-in and a live resource whose conversation page has `members`, which [the platform's switch](realtime-relay.md#from-the-platform) can move to the relay: the app's part of its entry in the apps file of Protobase Realtime, the WebSocket relay, as [`relay app`](#relay-app) prints it, without the address, id or any key: `client_events`, when a channel resource has events, and with presence `presence`, the path of the app's presence endpoint and the reports' `grace_seconds`. A host that runs Realtime completes it with the app's id, keys, origin and presence key, and the presence endpoint at the app's address.
|
|
310
310
|
- `api` lists the path prefixes the server answers. A path is under a prefix when it equals it or continues it with `/` (`/api` covers `/api/meta` and `/api/v1:batchWrite`, not `/apiary`). It is always `/api` and the two OAuth discovery documents [MCP clients](auth.md#oauth-for-mcp-clients) read, which answer 404 without MCP: a bundle assumes the API lives under `/api`, with the resources at the default `apiPath` `/api/v1`, `/api/meta`, the docs, the API functions and the built-in assistant beside them and Better Auth at `/api/auth`. A bundle built before them sends them to the UI: rebuild it. The serve runtime refuses a config that sets another `apiPath`.
|
|
311
311
|
- Every other path is the UI's. A `GET` or `HEAD` for a file in `public` gets that file; a folder gets its `index.html`. Otherwise a request whose `Accept` header contains `text/html` (a navigation) gets `spa` with status 200, so deep links such as `/orders/42` load the app; anything else gets 404. Other methods get 405.
|
|
312
312
|
- Files under an `assets/` folder, `public/assets/` and each plugin's, are named by their content hash and never change: serve them with `Cache-Control: public, max-age=31536000, immutable`, and everything else, `index.html`, each `plugin.json` and the theme, with `no-cache`.
|
|
@@ -487,8 +487,8 @@ Serves the API of a bundle from `protobase build` and owns the process around it
|
|
|
487
487
|
| `PROTOBASE_RUNS_ON_NOTE` | `true` ends the sign-in page on the [Runs on Protobase note](app-identity.md#runs-on-protobase-note), for a hosting platform; default `false`, and any value but `true` or `false` stops startup |
|
|
488
488
|
| `PROTOBASE_RUNTIME_URL` | the endpoint behind the top bar's [runtime update button](versioning.md#runtime-updates), such as a hosting platform's; without it there is no button |
|
|
489
489
|
| `PROTOBASE_SENTRY_DSN` | the DSN of a Sentry-compatible error tracker the app's server and admin app send their errors to, after the config's `errorReporting` ([Error reporting](api.md#error-reporting)); without it nothing is sent |
|
|
490
|
-
| `
|
|
491
|
-
| `
|
|
490
|
+
| `PROTOBASE_REALTIME_URL`, `PROTOBASE_REALTIME_APP`, `PROTOBASE_REALTIME_API_KEY`, `PROTOBASE_REALTIME_TICKET_KEY`, `PROTOBASE_REALTIME_PRESENCE_KEY` | the address, the app's id, its API and ticket keys and its presence key of [Protobase Realtime](realtime-relay.md#from-the-platform), the WebSocket relay, each where the config's `realtime.relay` leaves it out; without `realtime.relay` or `PROTOBASE_REALTIME_ENABLED` the relay stays off |
|
|
491
|
+
| `PROTOBASE_REALTIME_ENABLED` | `true` when the platform moves the app to [Realtime](realtime-relay.md#from-the-platform), with the channels of `realtime.relay` or else of its live resources whose conversation page has `members`; `true` for an app with neither stops the server. Unset or empty is `false`; any other value stops the server |
|
|
492
492
|
| `PROTOBASE_MAP_STYLE_URL` | the URL of the MapLibre style the record page's [maps](ui-config.md#maps) are drawn with, after the config's `maps`; without it there are no maps |
|
|
493
493
|
| `PROTOBASE_SOURCES` | other Protobase APIs whose resources the app draws as its own, a JSON list of `{ "url", "label", "resources", "roles"? }` after the config's `remoteResources` ([Remote resources](remote-resources.md#declaring-a-source)) |
|
|
494
494
|
| `PROTOBASE_FILES_<NAME>`, `PROTOBASE_FILES_RETENTION`, `PROTOBASE_FILES_SECRET` | [file providers](files.md#providers) and their settings, over `files` in the config; a bucket's credentials in `PROTOBASE_FILES_<NAME>_ACCESS_KEY_ID` and `_SECRET_ACCESS_KEY` |
|
|
@@ -654,7 +654,7 @@ Writes what the config and this version of Protobase need beyond the project's `
|
|
|
654
654
|
|
|
655
655
|
The snapshot describes the schema the project's migrations make: the sign-in store as the runtime reads it, each module that is on with its tables, columns and indexes under the names the config gives them, its last step, and whether every table whose rows belong to an organization has the column that names it, with `modelHash`, the SHA-256 of exactly that; what the migrations decided, the file it ends at with its id, its `format` and the version of Protobase that wrote it, the modules kept or archived, whether organizations were ever on and the default organization; and `physical`, every schema, table, column and index name the store has. The sign-in store's part of the file comes from the snapshot alone, without a database: the [steps](auth.md#migrations) after each module's last one, those a newer Protobase ships included, each version followed by its row in `auth_migrations`, which the server's startup check reads. With `DATABASE_URL` set, the checks of those steps run first against the database's rows, in a read-only transaction, and refuse as the step would, such as on two accounts with one provider and account id. A name the store has that the config changes, such as a table renamed in [`auth.tables`](auth.md#table-and-column-names), is refused with what changed, since Protobase does not write renames: write it by hand with `--custom` and `--record-config`. <gap>`protobase migration` cannot write the sign-in store of a project with auth migrations from before `protobase.snapshot.json` because `protobase migration baseline`, which reads the database once to start the snapshot, is not implemented.</gap>
|
|
656
656
|
|
|
657
|
-
The rest of the file is what the other features the config enables still need of the database, compared with it, each under its own comment and named in the file name as in brackets: the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks`, `workflows`, `notifications` or `mailFeedback`, or a resource has events, history, comments or an [AI field](ai.md) or is a [time rollup](data-config.md#keeping-it-up-to-date) (`jobs`), the table of the bounces and complaints of [`mailFeedback`](email.md#bounces-and-complaints) (`mail_feedback`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`changes`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas), the triggers that keep the rollups, and the tables, indexes and refresh functions of [time rollups](data-config.md#time-rollups) (`computed`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`query`), the [comments table](comments.md#the-table) (`comments`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`webhooks`), the [workflows, versions, runs and steps](workflows.md#turning-them-on) of `workflows` (`workflows`), the [devices and history tables](notifications.md#table-names) of `notifications` (`notifications`), the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`unsubscribes`), the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) (`realtime_relay`), the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) (`channel_sends`), the trigger of [`realtime.relay`](realtime-relay.md#sessions) that ends a deleted session's sockets (`relay`), and, with [`userTables`](user-tables.md#the-switch), the schemas of user tables (`user_tables`) and the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`owned_definitions`). Those are compared only with `DATABASE_URL` set, against the database with the project's migrations applied, and the command says so without it. <gap>`protobase migration` cannot write the jobs, change capture, rollup and other feature schemas from the snapshot because recording their models in it is not implemented.</gap> It reads the config merged with those of `--extend` as `dev` merges them, and the `.env` nearest the current directory.
|
|
657
|
+
The rest of the file is what the other features the config enables still need of the database, compared with it, each under its own comment and named in the file name as in brackets: the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks`, `workflows`, `notifications` or `mailFeedback`, or a resource has events, history, comments or an [AI field](ai.md) or is a [time rollup](data-config.md#keeping-it-up-to-date) (`jobs`), the table of the bounces and complaints of [`mailFeedback`](email.md#bounces-and-complaints) (`mail_feedback`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`changes`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas), the triggers that keep the rollups, and the tables, indexes and refresh functions of [time rollups](data-config.md#time-rollups) (`computed`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`query`), the [comments table](comments.md#the-table) (`comments`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`webhooks`), the [workflows, versions, runs and steps](workflows.md#turning-them-on) of `workflows` (`workflows`), the [devices and history tables](notifications.md#table-names) of `notifications` (`notifications`), the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`unsubscribes`), the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) (`realtime_relay`), the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) (`channel_sends`), the trigger of [`realtime.relay`](realtime-relay.md#sessions) that ends a deleted session's sockets and `realtime.members` with the triggers that keep it from channel records that list their members, also for an app with sign-in and a live resource whose conversation page has `members` (`relay`), and, with [`userTables`](user-tables.md#the-switch), the schemas of user tables (`user_tables`) and the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`owned_definitions`). Those are compared only with `DATABASE_URL` set, against the database with the project's migrations applied, and the command says so without it. <gap>`protobase migration` cannot write the jobs, change capture, rollup and other feature schemas from the snapshot because recording their models in it is not implemented.</gap> It reads the config merged with those of `--extend` as `dev` merges them, and the `.env` nearest the current directory.
|
|
658
658
|
|
|
659
659
|
- `--custom <name>` writes an empty numbered file, `NNN_<name>.sql`, with Protobase's lines and `-- protobase:custom`, for statements you write by hand, and makes the snapshot end at it. It counts as not additive; once it only adds, put `-- protobase:additive` among its `-- protobase:` lines and `check` lints it with the lint rules Protobase's own steps pass, such as no dropped table or column, no new required column and no unique constraint without a check of the rows. With `--record-config`, the snapshot takes the sign-in store's names as the config has them now, each module at the step it was: the file is where you make the store match, such as a rename.
|
|
660
660
|
- `check`, for CI, writes nothing and exits 1 with a line for each problem: a config or Protobase version that needs steps the migrations lack, a name the config changed, two files that share a number when Protobase wrote one of them, a snapshot that does not end at the newest file Protobase wrote or whose model does not match its `modelHash`, and a hand-written file marked additive that is not.
|
|
@@ -684,7 +684,7 @@ Recounts a [time rollup](data-config.md#keeping-it-up-to-date)'s buckets from th
|
|
|
684
684
|
protobase relay app
|
|
685
685
|
```
|
|
686
686
|
|
|
687
|
-
Prints the app's entry for the apps file of
|
|
687
|
+
Prints the app's entry for the apps file of [Realtime](realtime-relay.md), as JSON: its `id`, `api_keys` and `ticket_keys` from `realtime.relay`, or else `PROTOBASE_REALTIME_APP`, `PROTOBASE_REALTIME_API_KEY` and `PROTOBASE_REALTIME_TICKET_KEY`, and in `origins` the origin of the app's `url` (or `PROTOBASE_URL`); in `client_events` each channel resource's [client events](realtime-relay.md#client-events), those of its conversation pages with `members` when it declares no `channels`, with `typing` on those whose conversation page types; with [presence](realtime-relay.md#presence), `presence` with the app's presence endpoint at that `url`, the presence key (or `PROTOBASE_REALTIME_PRESENCE_KEY`) and a `grace_seconds` of 30. Its `client_events` and presence path and grace are the bundle manifest's [`realtime`](#the-manifest). Add the relay's limits and options to it there. It reads `protobase.config.ts` with `command: 'tool'` and the `.env` nearest the current directory.
|
|
688
688
|
|
|
689
689
|
## `auth migration`
|
|
690
690
|
|
|
@@ -39,33 +39,33 @@ export default defineConfig(({ env }) => ({
|
|
|
39
39
|
| `url` | The relay's address, `https://` or `http://`. The app sends its commands there, and clients connect to its `/socket`. |
|
|
40
40
|
| `app` | The app's id on the relay: 1 to 64 of `a-z`, `0-9`, `_` and `-`. |
|
|
41
41
|
| `apiKey`, `ticketKey` | One of the app's API keys and one of its ticket keys on the relay, each at least 32 characters. |
|
|
42
|
-
| `channels` | The channel resources, by name. Each record of one is a channel, `<resource>/<key>`, named as its [record channel](channels.md) is; the resource needs a key of one field. |
|
|
43
|
-
| `channels.<resource>.members` | Where the channel's members come from: a resource whose records each put one person in one channel, with its relation field to the channel resource (`channel`) and its field of the person's user id (`user`)
|
|
42
|
+
| `channels` | The channel resources, by name. Each record of one is a channel, `<resource>/<key>`, named as its [record channel](channels.md) is; the resource needs a key of one field. Without `channels`, each resource whose [conversation page](ui-config.md#conversation-pages) has `members` is one, with that field as its members, when its changes are captured, such as with [`.live()`](live.md), and its key is one field; views of one resource whose pages list members in different fields stop the server. |
|
|
43
|
+
| `channels.<resource>.members` | Where the channel's members come from: a resource whose records each put one person in one channel, with its relation field to the channel resource (`channel`) and its field of the person's user id (`user`); index both columns, as tickets read by the user and notices by the channel. Or `{ field }`, a JSON field of user ids on the channel record itself, as a conversation page's `members`, neither sensitive nor in `.history({ ignore })`, so its every change reaches the relay: Protobase keeps who is in each channel in its `realtime.members` table, so the app adds no index. Without `members`, the channel is open: a channel of a public record, which tickets list when asked (see [Tickets](#tickets)). |
|
|
44
44
|
| `channels.<resource>.mentions` | A field of the records noticed on the channel that lists the user ids they mention, an array or one id, which [notices](#notices) carry. |
|
|
45
45
|
| `channels.<resource>.events` | The [events](#client-events) the channel's members may send each other, by name, each with its payload's `schema`, its `maxBytes` and the rate one person may send it at, `perSecond` in bursts of `burst`. |
|
|
46
|
-
| `presence` | Turns on [presence](#presence): the app's own resource it is kept in, its fields of the person's user id (`user`), the status (`status`) and when it began (`at`), and the bearer token of the relay's reports (`key`), at least 32 characters, which defaults to [`
|
|
46
|
+
| `presence` | Turns on [presence](#presence): the app's own resource it is kept in, its fields of the person's user id (`user`), the status (`status`) and when it began (`at`), and the bearer token of the relay's reports (`key`), at least 32 characters, which defaults to [`PROTOBASE_REALTIME_PRESENCE_KEY`](#from-the-platform). |
|
|
47
47
|
|
|
48
|
-
The relay needs the app's own [sign-in](auth.md), whose sessions its tickets name, with its store in the app's own database, as the `auth` config puts it: an auth store in another database is refused. A members resource's changes must be captured, so declare [`.live()`](live.md) on it; the relay hears of the changes of every resource whose changes are captured, such as a live one, that is a channel resource or has a relation field to one. [`protobase migration`](cli.md#migration) writes the trigger that ends a deleted session's sockets, in the `relay` schema, after the auth store's steps when the same file writes them, so a new app's first migration sets up both; the server checks it at start.
|
|
48
|
+
The relay needs the app's own [sign-in](auth.md), whose sessions its tickets name, with its store in the app's own database, as the `auth` config puts it: an auth store in another database is refused. A members resource's changes must be captured, so declare [`.live()`](live.md) on it, and so must a channel resource's whose records list their members; the relay hears of the changes of every resource whose changes are captured, such as a live one, that is a channel resource or has a relation field to one. [`protobase migration`](cli.md#migration) writes the trigger that ends a deleted session's sockets, in the `relay` schema, after the auth store's steps when the same file writes them, so a new app's first migration sets up both. For channel records that list their members it also writes `realtime.members`, keyed by channel and indexed by person, the triggers on their tables that keep it as records are written, deleted, soft-deleted or truncated, and a fill from the records already there. It writes these for an app with `realtime.relay`, and for one with sign-in and a live resource whose conversation page has `members`, which the platform can move to the relay; the server checks them at start while it uses the relay. A trigger left disabled stops the server at start, and the next migration writes it again. A partitioned channel table is kept too: Postgres puts the row trigger on each partition, the migration puts a truncate trigger on each, and a truncated partition writes the resource's members again from the table, so its siblings keep theirs; a partition attached later stops the server at start until the next migration gives it its trigger. A partition, or a table in an inheritance tree, is refused as a channel table. Writes made while the triggers do not fire, such as a restore with `session_replication_role = replica`, are not kept in `realtime.members` until the next migration that writes it again.
|
|
49
49
|
|
|
50
50
|
[`protobase relay app`](cli.md#relay-app) prints the app's entry for the relay's apps file, from this config, these variables and the app's `url`.
|
|
51
51
|
|
|
52
52
|
### From the platform
|
|
53
53
|
|
|
54
|
-
An app on a platform that runs the relay picks up its address and keys from the environment, so its config names only its channels:
|
|
54
|
+
An app on a platform that runs the relay, Protobase Realtime, picks up its address and keys from the environment, so its config names only its channels:
|
|
55
55
|
|
|
56
56
|
| Variable | Default of |
|
|
57
57
|
| --- | --- |
|
|
58
|
-
| `
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
62
|
-
| `
|
|
58
|
+
| `PROTOBASE_REALTIME_URL` | `url` |
|
|
59
|
+
| `PROTOBASE_REALTIME_APP` | `app` |
|
|
60
|
+
| `PROTOBASE_REALTIME_API_KEY` | `apiKey` |
|
|
61
|
+
| `PROTOBASE_REALTIME_TICKET_KEY` | `ticketKey` |
|
|
62
|
+
| `PROTOBASE_REALTIME_PRESENCE_KEY` | `presence.key` |
|
|
63
63
|
|
|
64
|
-
Each is a default on its own: a value in the config wins, and a key that neither sets stops the server at start, naming the key and its variable.
|
|
64
|
+
Each is a default on its own: a value in the config wins, and a key that neither sets stops the server at start, naming the key and its variable. Setting these turns nothing on: the relay stays off without `realtime.relay` in the config or the switch below.
|
|
65
65
|
|
|
66
|
-
`
|
|
66
|
+
`PROTOBASE_REALTIME_ENABLED` is the platform's switch: `true` when its apps file carries the app's entry, `false`, or unset or empty, otherwise; any other value stops the server at start, naming it. A config with `realtime.relay` uses the relay whatever it says. `true` moves an app without `realtime.relay` too, with the channels its conversation pages with `members` give it and typing where they type, so the app changes neither its config nor its tables; `true` for an app with no such page on a live resource, or without sign-in, stops the server at start, naming the switch. <gap>A conversation page whose resource has no `.live()` cannot move to the relay with the switch because hearing who joins or leaves without capturing the resource's changes is not implemented.</gap> The app moves once it runs a version whose `protobase migration` wrote `realtime.members`; until then `true` stops the server at start, naming the migration to write. While the switch is off, sign-outs still queue their `relay.close-sessions` jobs, which then finish at once.
|
|
67
67
|
|
|
68
|
-
[`protobase build`](cli.md#the-manifest) writes the app's part of its apps file entry into the bundle's manifest, as `relay
|
|
68
|
+
[`protobase build`](cli.md#the-manifest) writes the app's part of its apps file entry into the bundle's manifest, as `realtime`, for an app with `realtime.relay` or one the switch can move: its `client_events` and, with presence, the presence endpoint's path and `grace_seconds`, without the address, id or any key. A platform reads it from each build and completes the entry with the app's id, keys, origin and presence key, and the endpoint at the app's address.
|
|
69
69
|
|
|
70
70
|
```ts
|
|
71
71
|
export default defineConfig({
|
|
@@ -89,7 +89,7 @@ It starts the relay on the first load of a config with `realtime.relay`, an edit
|
|
|
89
89
|
|
|
90
90
|
A client connects to the relay with a ticket from `POST /api/relay/ticket`, with `{ "channels": [...] }`, optional, as JSON. The answer is `{ ticket, url, channels, expiresAt }`: the relay's socket address, a ticket that connects once, within a minute, and the channels it lists, which the client joins. Every connection, a reconnect too, takes a new ticket.
|
|
91
91
|
|
|
92
|
-
- **Signed in:** the ticket names the person and the sign-in session they called with, so a device that connects makes no new session, and lists every channel they are a member of, as the members resources hold it, and `people`, which every signed-in person has and [presence](#presence) is published on. A caller without a session of the app, such as an API key or an outside app acting for the user, is refused with `403`; a session that ended with `401`.
|
|
92
|
+
- **Signed in:** the ticket names the person and the sign-in session they called with, so a device that connects makes no new session, and lists every channel they are a member of, as the members resources and `realtime.members` hold it, and `people`, which every signed-in person has and [presence](#presence) is published on. A caller without a session of the app, such as an API key or an outside app acting for the user, is refused with `403`; a session that ended with `401`.
|
|
93
93
|
- **Visitors:** with an `anonymous` role, a caller without a token gets a ticket of an anonymous session of its own, which receives and never sends.
|
|
94
94
|
- **Open channels:** both list the channels they ask for in `channels` of resources without `members` whose records are public: the `anonymous` role may read them, as `GET /api/v1/<resource>/<key>` decides it for a visitor. Someone's own access to a record never makes its channel open, so no one's access to one can end while their socket stays open. A channel there is none of, one whose record is not public and one of a resource with members are refused alike with `404`; a record only some people may read takes `members`. A ticket lists at most 200 channels (`400`, or `422` when the memberships are more).
|
|
95
95
|
|
|
@@ -101,9 +101,9 @@ The [dispatch](events.md) that turns each transaction's changes into events also
|
|
|
101
101
|
{ "kind": "create", "resource": "messages", "key": "42", "channel": "conversations/7", "mentions": ["u2"] }
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
`kind` is `create`, `update`, `delete` or `undelete`; `mentions` is there with `mentions` set. A notice tells clients what changed, so they read only the records they show. On a channel with members it reaches its members as the database holds them at dispatch, and a person who joined or left with that change; on an open channel, everyone joined to it, while the anonymous role reads its record as it stands at dispatch. The relay sends a notice once, however often the job sends it again.
|
|
104
|
+
`kind` is `create`, `update`, `delete` or `undelete`; `mentions` is there with `mentions` set. A notice tells clients what changed, so they read only the records they show. On a channel with members it reaches its members as the database holds them at dispatch, in the members resource or `realtime.members`, and a person who joined or left with that change; on an open channel, everyone joined to it, while the anonymous role reads its record as it stands at dispatch. The relay sends a notice once, however often the job sends it again.
|
|
105
105
|
|
|
106
|
-
A members record that is created or undeleted adds its person to the channel on the sockets they have open, before its notice, and one that is deleted or soft-deleted takes them out after it: they can join, or are closed out, without connecting again. The person added, and no one else, also hears a `joined` event on `people` that names the channel, `{ "channel": "conversations/7" }`, so their pages join it. Commands go in commit order, one job at a time; while the relay is down, they wait and try again, once a minute at most. A command the relay refuses, such as one over its size limit, goes to the app's error reporter and is passed over.
|
|
106
|
+
A members record that is created or undeleted adds its person to the channel on the sockets they have open, before its notice, and one that is deleted or soft-deleted takes them out after it; a channel record that lists its members does the same for each person its change adds to or drops from the list: they can join, or are closed out, without connecting again. The person added, and no one else, also hears a `joined` event on `people` that names the channel, `{ "channel": "conversations/7" }`, so their pages join it. Commands go in commit order, one job at a time; while the relay is down, they wait and try again, once a minute at most. A command the relay refuses, such as one over its size limit, goes to the app's error reporter and is passed over.
|
|
107
107
|
|
|
108
108
|
Once an open channel's record stops being public, its notices stop, and a new ticket refuses it. <gap>Sockets that joined an open channel stay joined after its record stops being public, hearing nothing of it, until they reconnect, because closing a channel on every socket is not implemented.</gap>
|
|
109
109
|
|
|
@@ -127,11 +127,11 @@ The resource is keyed by its user field, its status field is text or an enum of
|
|
|
127
127
|
|
|
128
128
|
Only the relay writes the records, so give no role `presences.create`, `presences.update` or `presences.delete`: whoever may write one can set anyone's presence and raise its events. A role that reads the resource reads the presence of everyone its rows allow, which can reach further than `people` does, so grant `presences.read` no wider than that.
|
|
129
129
|
|
|
130
|
-
The relay posts its reports to `POST /api/relay/presence` with the presence key as its bearer token, never a sign-in; the [apps file entry](cli.md#relay-app) names the address. A report holds at most 500 changes, each a person's status and when it began. The app writes them in one statement, which replaces a record's status only with one that began later, and answers `204`: a report the relay sends again, or one that arrives late, changes nothing. A report with a missing or wrong key is refused with `401` before its body is read. To change the key, change it in the config, or `
|
|
130
|
+
The relay posts its reports to `POST /api/relay/presence` with the presence key as its bearer token, never a sign-in; the [apps file entry](cli.md#relay-app) names the address. A report holds at most 500 changes, each a person's status and when it began. The app writes them in one statement, which replaces a record's status only with one that began later, and answers `204`: a report the relay sends again, or one that arrives late, changes nothing. A report with a missing or wrong key is refused with `401` before its body is read. To change the key, change it in the config, or `PROTOBASE_REALTIME_PRESENCE_KEY`, and in the apps file; the relay sends a refused report again, waiting up to a minute between tries, until the app takes it, so no change is lost. When the relay restarted, which a report says with its start time, everyone it has not reported since and who is not offline yet is set offline as of that start, once in each server process for each start; the relay sends no report until someone's status changes, so this waits for the first one.
|
|
131
131
|
|
|
132
132
|
Each write goes through the resource's table, so its changes raise the resource's [events](events.md), [webhooks](webhooks.md) and [workflows](workflows.md) as any other record's do, such as a workflow on `presences.wentOffline`. An offline report raises its event once.
|
|
133
133
|
|
|
134
|
-
Each change of a person's presence is also published on `people` as a `presence_changed` event, the same bytes to everyone who shares a channel with members with them, themselves included, as the members tables hold it at dispatch, read for the whole batch in one query:
|
|
134
|
+
Each change of a person's presence is also published on `people` as a `presence_changed` event, the same bytes to everyone who shares a channel with members with them, themselves included, as the members tables and `realtime.members` hold it at dispatch, read for the whole batch in one query:
|
|
135
135
|
|
|
136
136
|
```json
|
|
137
137
|
{ "user": "u1", "status": "offline", "at": "2026-11-21T10:00:00.000Z" }
|