@protobase/cli 0.26.3 → 0.26.4
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/package.js +1 -1
- package/dist/scaffold/data-file.js +12 -3
- package/dist/scaffold/plan.js +13 -4
- package/dist/scaffold/run.js +1 -0
- package/dist/supabase/load-phase.js +19 -12
- package/dist/supabase/report.js +16 -3
- package/package.json +6 -6
- package/skills/protobase/references/reference/cli.md +1 -1
- package/skills/protobase/references/reference/supabase-realtime.md +1 -1
package/dist/package.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { tableKey } from "../db/model.js";
|
|
2
2
|
import { camelCase } from "../infer/names.js";
|
|
3
3
|
import { parseSource } from "./project.js";
|
|
4
|
-
import { fieldsObject, resourceChain } from "./existing-config.js";
|
|
4
|
+
import { fieldsObject, methodCalls, resourceChain } from "./existing-config.js";
|
|
5
5
|
import { renderField } from "../infer/render-field.js";
|
|
6
6
|
//#region src/scaffold/data-file.ts
|
|
7
7
|
const propertyText = ({ proposal, ignored }) => ignored ? `${proposal.name}: null` : `${proposal.name}: ${renderField(proposal)}`;
|
|
@@ -16,7 +16,8 @@ const renderDataFile = (plan) => {
|
|
|
16
16
|
` .primaryKey((r) => ${refs(plan.primaryKey)})`,
|
|
17
17
|
plan.softDelete && ` .softDelete((r) => r.${plan.softDelete})`,
|
|
18
18
|
plan.tenant && ` .tenant((r) => r.${plan.tenant})`,
|
|
19
|
-
plan.search && ` .search((r) => [r.${plan.search}])
|
|
19
|
+
plan.search && ` .search((r) => [r.${plan.search}])`,
|
|
20
|
+
plan.live && " .live()"
|
|
20
21
|
].filter(Boolean);
|
|
21
22
|
return [
|
|
22
23
|
`import { f, resource } from '@protobase/schema'`,
|
|
@@ -42,5 +43,13 @@ const addFieldsToDataFile = (text, decisions) => {
|
|
|
42
43
|
});
|
|
43
44
|
return source.getFullText();
|
|
44
45
|
};
|
|
46
|
+
const makeLiveInDataFile = (text) => {
|
|
47
|
+
const source = parseSource(text);
|
|
48
|
+
const chain = resourceChain(source);
|
|
49
|
+
if (!chain) throw new Error("data.ts has no resource(...) builder chain to make live");
|
|
50
|
+
if (methodCalls(chain.chainTop, "live").length > 0) return text;
|
|
51
|
+
chain.chainTop.replaceWithText(`${chain.chainTop.getText()}\n .live()`);
|
|
52
|
+
return source.getFullText();
|
|
53
|
+
};
|
|
45
54
|
//#endregion
|
|
46
|
-
export { addFieldsToDataFile, renderDataFile };
|
|
55
|
+
export { addFieldsToDataFile, makeLiveInDataFile, renderDataFile };
|
package/dist/scaffold/plan.js
CHANGED
|
@@ -3,7 +3,7 @@ import { keyColumns } from "../db/keys.js";
|
|
|
3
3
|
import { camelCase } from "../infer/names.js";
|
|
4
4
|
import { findField } from "./existing-config.js";
|
|
5
5
|
import { proposeField } from "../infer/proposal.js";
|
|
6
|
-
import { addFieldsToDataFile, renderDataFile } from "./data-file.js";
|
|
6
|
+
import { addFieldsToDataFile, makeLiveInDataFile, renderDataFile } from "./data-file.js";
|
|
7
7
|
import { detectDrift } from "./drift.js";
|
|
8
8
|
import { ensureExports } from "./index-file.js";
|
|
9
9
|
import { assignNames } from "./resource-names.js";
|
|
@@ -50,6 +50,8 @@ const planScaffold = async (input) => {
|
|
|
50
50
|
const drift = [];
|
|
51
51
|
const warnings = [];
|
|
52
52
|
const registrations = [];
|
|
53
|
+
const live = [];
|
|
54
|
+
const isLive = (key) => input.live?.includes(key) ?? false;
|
|
53
55
|
const writeUi = async (dir, resource, identifier, table, current, decisions) => {
|
|
54
56
|
if (ui.length === 0) return;
|
|
55
57
|
const file = path.join(configDir, dir, "ui.ts");
|
|
@@ -75,7 +77,9 @@ const planScaffold = async (input) => {
|
|
|
75
77
|
const clash = decisions.find((d) => current.fields.some((f) => f.name === d.proposal.name));
|
|
76
78
|
if (clash) throw new Error(`${key}: field "${clash.proposal.name}" already exists, edit the name`);
|
|
77
79
|
const file = path.join(configDir, current.dir, "data.ts");
|
|
78
|
-
|
|
80
|
+
const fielded = addFieldsToDataFile(current.text, decisions);
|
|
81
|
+
changes.push(...change(file, current.text, isLive(key) ? makeLiveInDataFile(fielded) : fielded));
|
|
82
|
+
if (isLive(key)) live.push(key);
|
|
79
83
|
drift.push(...detectDrift(current, table, context(table)));
|
|
80
84
|
await writeUi(current.dir, current.name, current.identifier, table, current, decisions);
|
|
81
85
|
continue;
|
|
@@ -88,11 +92,15 @@ const planScaffold = async (input) => {
|
|
|
88
92
|
const file = path.join(configDir, name, "data.ts");
|
|
89
93
|
if (await input.readText(file) !== void 0) throw new Error(`${file} exists but does not declare a resource for ${key}`);
|
|
90
94
|
const decisions = await decideAll(table, table.columns, context(table), input);
|
|
91
|
-
const plan =
|
|
95
|
+
const plan = {
|
|
96
|
+
...planResource(name, table, decisions),
|
|
97
|
+
live: isLive(key)
|
|
98
|
+
};
|
|
92
99
|
changes.push({
|
|
93
100
|
path: file,
|
|
94
101
|
after: renderDataFile(plan)
|
|
95
102
|
});
|
|
103
|
+
if (plan.live) live.push(key);
|
|
96
104
|
registrations.push({
|
|
97
105
|
from: `./${name}/data`,
|
|
98
106
|
names: [camelCase(name)]
|
|
@@ -106,7 +114,8 @@ const planScaffold = async (input) => {
|
|
|
106
114
|
return {
|
|
107
115
|
changes,
|
|
108
116
|
drift,
|
|
109
|
-
warnings
|
|
117
|
+
warnings,
|
|
118
|
+
live
|
|
110
119
|
};
|
|
111
120
|
};
|
|
112
121
|
const registerExisting = async (input, planned) => {
|
package/dist/scaffold/run.js
CHANGED
|
@@ -45,8 +45,9 @@ const doctorOutput = async (url, configDir) => {
|
|
|
45
45
|
/**
|
|
46
46
|
* The import's second run, once the schema migration is applied: loads the data dump, with the replay of Vault's
|
|
47
47
|
* secrets, pgmq's queues and pg_cron's jobs when the dump made one, in one transaction, deletes both, scaffolds the
|
|
48
|
-
* config from the imported tables of the app's schemas, leaving the project's own alone
|
|
49
|
-
* converts as the project's recurring jobs, and writes
|
|
48
|
+
* config from the imported tables of the app's schemas, leaving the project's own alone and making those of the
|
|
49
|
+
* `supabase_realtime` publication live, writes the pg_cron jobs it converts as the project's recurring jobs, and writes
|
|
50
|
+
* the report.
|
|
50
51
|
*/
|
|
51
52
|
const runLoadPhase = async (options, out) => {
|
|
52
53
|
const state = importState(options.projectDir);
|
|
@@ -69,19 +70,25 @@ const runLoadPhase = async (options, out) => {
|
|
|
69
70
|
const schemas = manifest.schemas.filter((schema) => !platformSchemas.includes(schema));
|
|
70
71
|
const sql = connect(options.targetUrl);
|
|
71
72
|
const excludes = await otherTables(sql, schemas, manifest.tables).finally(() => sql.end());
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
const realtime = manifest.realtimeTables.map((table) => `${table.schema}.${table.table}`);
|
|
74
|
+
const live = [];
|
|
75
|
+
for (const schema of schemas) {
|
|
76
|
+
const plan = await runScaffold({
|
|
77
|
+
url: options.targetUrl,
|
|
78
|
+
target: schema,
|
|
79
|
+
configDir: options.configDir,
|
|
80
|
+
yes: true,
|
|
81
|
+
dryRun: false,
|
|
82
|
+
ui: [],
|
|
83
|
+
excludes,
|
|
84
|
+
live: realtime
|
|
85
|
+
}, out);
|
|
86
|
+
live.push(...plan.live);
|
|
87
|
+
}
|
|
81
88
|
const cron = convertedCronJobs(manifest.cronJobs);
|
|
82
89
|
const jobsWired = await writeCronJobs(options.projectDir, cron);
|
|
83
90
|
if (cron.length > 0) out(`wrote ${cron.length} of pg_cron's jobs as recurring jobs in jobs/\n`);
|
|
84
|
-
await writeFile(state.report, importReport(manifest, await doctorOutput(options.targetUrl, options.configDir), jobsWired), { mode: 384 });
|
|
91
|
+
await writeFile(state.report, importReport(manifest, await doctorOutput(options.targetUrl, options.configDir), jobsWired, live), { mode: 384 });
|
|
85
92
|
out(`wrote ${path.relative(options.projectDir, state.report)}: what the import could not carry over\n`);
|
|
86
93
|
};
|
|
87
94
|
//#endregion
|
package/dist/supabase/report.js
CHANGED
|
@@ -22,6 +22,17 @@ const recurringJobs = (manifest, jobsWired) => {
|
|
|
22
22
|
if (convertedCronJobs(manifest.cronJobs).length === 0) return [];
|
|
23
23
|
return [`## Recurring jobs\n\nThe import wrote pg_cron's jobs it could convert in \`jobs/\`, each running its SQL as the app's database role, once per time and without retries, as pg_cron did. ${jobsWired ? "`protobase.config.ts` passes them as its `jobs`." : "`protobase.config.ts` has a `jobs` of its own: add the exports of `jobs/index.ts` to it."} They run once the database has the jobs schema: run \`protobase migration\` and apply it.\n`];
|
|
24
24
|
};
|
|
25
|
+
const tableName = (table) => `${table.schema}.${table.table}`;
|
|
26
|
+
/** The tables of the `supabase_realtime` publication the scaffold gave `.live()`, or nothing when it gave none. */
|
|
27
|
+
const liveTables = (live) => {
|
|
28
|
+
if (live.length === 0) return [];
|
|
29
|
+
return [`## Live tables
|
|
30
|
+
|
|
31
|
+
The \`supabase_realtime\` publication sent these tables' changes to Realtime clients, so the scaffold gave each resource \`.live()\`. \`/realtime/v1\` sends their \`postgres_changes\` once the database has the jobs schema, their capture triggers and the notices table: run \`protobase migration\` and apply it.
|
|
32
|
+
|
|
33
|
+
${live.map((table) => `- ${table}`).join("\n")}
|
|
34
|
+
`];
|
|
35
|
+
};
|
|
25
36
|
/** What the import made again through an extension's own API, or kept since the project's database runs the extension. */
|
|
26
37
|
const carried = (manifest) => [
|
|
27
38
|
...cronCarried(manifest.cronJobs ?? []),
|
|
@@ -36,9 +47,10 @@ const conversions = (manifest) => [...manifest.hypertables.map((table) => `hyper
|
|
|
36
47
|
/**
|
|
37
48
|
* The import's report: what it loaded, and what it could not carry over, for a person or their AI assistant to finish.
|
|
38
49
|
* `doctor` is `protobase doctor`'s output on the scaffolded config, or `undefined` when there was no config to check;
|
|
39
|
-
* `jobsWired` whether protobase.config.ts passes the pg_cron jobs the load wrote as its `jobs
|
|
50
|
+
* `jobsWired` whether protobase.config.ts passes the pg_cron jobs the load wrote as its `jobs`; `live` the tables, as
|
|
51
|
+
* `schema.table`, whose resources the scaffold gave `.live()`.
|
|
40
52
|
*/
|
|
41
|
-
const importReport = (manifest, doctor, jobsWired) => [
|
|
53
|
+
const importReport = (manifest, doctor, jobsWired, live) => [
|
|
42
54
|
"# Supabase import\n",
|
|
43
55
|
`Schemas ${manifest.schemas.join(", ")}, restored by the migration \`${manifest.migration}\`, and their data, loaded in one transaction.\n`,
|
|
44
56
|
`The imported tables take ${megabytes(manifest.bytes)} in the source database${manifest.bytes > starterBytes ? `, more than the ${megabytes(starterBytes)} of the Starter plan: pick a larger plan` : `, within the ${megabytes(starterBytes)} of the Starter plan`}.\n`,
|
|
@@ -47,7 +59,8 @@ const importReport = (manifest, doctor, jobsWired) => [
|
|
|
47
59
|
section("Made again", "A dump cannot carry these. The load made Vault's secrets and pgmq's queues again through each extension's own API, in the same transaction, and scheduled pg_cron's jobs again with `cron.schedule` where the project's database has pg_cron; elsewhere the import made them the app's recurring jobs, on the same schedule. pg_net sends the HTTP triggers' calls from the project's database, Database Webhooks' through the `supabase_functions.http_request` the migration creates.", carried(manifest)),
|
|
48
60
|
...recurringJobs(manifest, jobsWired),
|
|
49
61
|
section("Cron jobs", "These pg_cron jobs did not come over, each for the reason it gives. Make each a Protobase recurring job, or schedule it where the database has pg_cron.", cronLeft(manifest.cronJobs)),
|
|
50
|
-
|
|
62
|
+
...liveTables(live),
|
|
63
|
+
section("Realtime tables", "The `supabase_realtime` publication sends these tables' changes to Realtime clients, but the scaffold made no resource of them, such as for a table without a primary key or one in auth or storage, so `/realtime/v1` refuses them. Give each a resource with `.live()` where the app listens to its changes.", manifest.realtimeTables.map(tableName).filter((table) => !live.includes(table))),
|
|
51
64
|
section("Vault secrets", "The project's database has no Vault, or the importing role could not read the secrets' values, so they stay in Supabase. Enter each again where the app reads it, such as an environment variable.", manifest.vaultReplayed ? [] : manifest.vaultSecrets?.map(secretName)),
|
|
52
65
|
section("pgmq queues", "pgmq owns its queues' tables, so the dump leaves them out. Make each again with pgmq where the project's database has it, partitioned ones with `pgmq.create_partitioned`, and send its messages again.", queuesLeft(manifest)),
|
|
53
66
|
section("Extensions left out", "The project's database does not let the importing role create these, so the migration does not. Replace what the app uses them for, or have the database's administrator make them available.", manifest.extensionsLeftOut.map((extension) => `${extension.name} (schema ${extension.schema})`)),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.4",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@hono/node-server": "^2.1.3",
|
|
37
37
|
"@hono/vite-dev-server": "^0.26.1",
|
|
38
|
-
"@protobase/layout": "0.26.
|
|
39
|
-
"@protobase/query": "0.26.
|
|
40
|
-
"@protobase/schema": "0.26.
|
|
41
|
-
"@protobase/server": "0.26.
|
|
42
|
-
"@protobase/ui": "0.26.
|
|
38
|
+
"@protobase/layout": "0.26.4",
|
|
39
|
+
"@protobase/query": "0.26.4",
|
|
40
|
+
"@protobase/schema": "0.26.4",
|
|
41
|
+
"@protobase/server": "0.26.4",
|
|
42
|
+
"@protobase/ui": "0.26.4",
|
|
43
43
|
"@react-email/render": "^2.1.0",
|
|
44
44
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
45
45
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -471,7 +471,7 @@ Turns a Supabase project's database into the project in the current directory, i
|
|
|
471
471
|
|
|
472
472
|
1. **The first run** takes the Supabase database's connection string, for its `postgres` role (the session pooler's or the direct one; the transaction pooler cannot hold the snapshot the dumps share). It fails unless the database has Supabase's `auth` and `storage` schemas and its `supabase_admin` role, and notes the Supabase CLI's `supabase/config.toml`, `supabase/migrations/` and `supabase/functions/` when the checkout has them. It dumps `auth`, `storage` and every schema the app made, schema and data apart, as of one snapshot, leaving out the platform's own migration tables, and writes the schemas as the project's next migration, `NNN_supabase_schema.sql`. It asks the project's database which of the app's extensions its role may create, by trying each in a transaction it rolls back: Postgres alone allows the trusted ones, Supabase's Postgres image all it allowlists, such as pg_net, pgmq and Vault. The migration creates those first, then, where pg_net is among them and the app has Database Webhooks, the `supabase_functions.http_request` function their triggers call, over pg_net; then the dump without what fails there, as comments: grants and default privileges for `dashboard_user`, `supabase_*`, `authenticator` and `postgres`, statements about the `public` schema, Realtime's publication, database webhooks without pg_net, functions in plv8, plls or plcoffee where the database has no such language, and a timescaledb hypertable's insert-blocking trigger. The project's database needs the roles `anon`, `authenticated` and `service_role` that the dump's grants and policies name; the command makes them when its role may, and otherwise stops, naming them. The data dump goes to `supabase-import/`, which it adds to `.gitignore`, since it holds every row and the users' password hashes. Where the project's database can create Vault, pgmq and pg_cron, `supabase-import/replay.sql` holds what a dump cannot carry: Vault's secrets with their values, read decrypted since only Supabase's key opens their ciphertext, pgmq's queues with their messages, since pgmq owns their tables, and pg_cron's jobs, which live in its own schema.
|
|
473
473
|
2. **Apply the migration** with the project's other migrations, such as `bun run db:migrate`.
|
|
474
|
-
3. **The second run**, without a connection string, loads the data in one transaction, with the app's triggers off so a sign-up trigger on `auth.users` does not run again for users who already have their rows, and fails the transaction unless every table holds the source's number of rows. In the same transaction it makes Vault's secrets and pgmq's queues again through `vault.create_secret`, `pgmq.create` and `pgmq.send`, each message with its headers and the time it becomes visible. Where the project's database has pg_cron, as Supabase's Postgres image does when it serves that database, it schedules pg_cron's jobs again there with `cron.schedule`, run by the importing role, an inactive one inactive. Elsewhere it writes each job of plain SQL as a [recurring job](jobs.md#recurring-jobs)) of its own in `jobs/`, of the same name, schedule and `cron.timezone`, that runs its SQL as the app's database role, tried once each time as pg_cron did, exports it from `jobs/index.ts` and adds `jobs` to `protobase.config.ts` (or writes one); they run once [`migration`](#migration) has added the jobs schema. Recurring jobs wake an app that sleeps, so they run where a host stops an idle app too. A job that calls what the database lacks, such as pg_net's `net.http_*`, `cron` or Vault, an inactive one and a schedule recurring jobs do not take, such as `30 seconds` or `$`, stay in the report, as does a job of another database. It loads only into empty tables, deletes the data dump and the replay, runs [`scaffold`](#scaffold) on the imported tables of the app's schemas (not `auth` or `storage`), and writes `supabase-import/report.md`: the rows loaded, the imported tables' size against the Starter plan's 500 MB, what it made again, and what needs a person: the pg_cron jobs it could not carry over, each with why, Realtime tables, Vault secrets by name and pgmq queues where the database has no such extension (and partitioned queues), the extensions it cannot create, hypertables and plv8, plls or plcoffee functions to convert, triggers that call out over HTTP without pg_net, Edge Functions, and the indexes [`doctor`](#doctor) finds missing.
|
|
474
|
+
3. **The second run**, without a connection string, loads the data in one transaction, with the app's triggers off so a sign-up trigger on `auth.users` does not run again for users who already have their rows, and fails the transaction unless every table holds the source's number of rows. In the same transaction it makes Vault's secrets and pgmq's queues again through `vault.create_secret`, `pgmq.create` and `pgmq.send`, each message with its headers and the time it becomes visible. Where the project's database has pg_cron, as Supabase's Postgres image does when it serves that database, it schedules pg_cron's jobs again there with `cron.schedule`, run by the importing role, an inactive one inactive. Elsewhere it writes each job of plain SQL as a [recurring job](jobs.md#recurring-jobs)) of its own in `jobs/`, of the same name, schedule and `cron.timezone`, that runs its SQL as the app's database role, tried once each time as pg_cron did, exports it from `jobs/index.ts` and adds `jobs` to `protobase.config.ts` (or writes one); they run once [`migration`](#migration) has added the jobs schema. Recurring jobs wake an app that sleeps, so they run where a host stops an idle app too. A job that calls what the database lacks, such as pg_net's `net.http_*`, `cron` or Vault, an inactive one and a schedule recurring jobs do not take, such as `30 seconds` or `$`, stay in the report, as does a job of another database. It loads only into empty tables, deletes the data dump and the replay, runs [`scaffold`](#scaffold) on the imported tables of the app's schemas (not `auth` or `storage`), giving the tables of the `supabase_realtime` publication [`.live()`](live.md)) so [`/realtime/v1`](supabase-realtime.md)) keeps sending their `postgres_changes` once [`migration`](#migration) has written their capture triggers, and writes `supabase-import/report.md`: the rows loaded, the imported tables' size against the Starter plan's 500 MB, what it made again, the tables it made live, and what needs a person: the pg_cron jobs it could not carry over, each with why, Realtime tables it made no resource of, Vault secrets by name and pgmq queues where the database has no such extension (and partitioned queues), the extensions it cannot create, hypertables and plv8, plls or plcoffee functions to convert, triggers that call out over HTTP without pg_net, Edge Functions, and the indexes [`doctor`](#doctor) finds missing.
|
|
475
475
|
|
|
476
476
|
Remove `supabase-import/` to import again. Signing in Supabase's users with Protobase's auth is not part of this command yet.
|
|
477
477
|
|
|
@@ -24,7 +24,7 @@ The server serves the socket when the host gives `createAdmin` its WebSocket upg
|
|
|
24
24
|
|
|
25
25
|
## Which tables
|
|
26
26
|
|
|
27
|
-
A channel may listen to the tables of the resources with [`.live()`](live.md)), by `schema` (`*` for any), `table` (every live table of the schema when left out), `event` (`INSERT`, `UPDATE`, `DELETE` or `*`) and `filter`. A table that is not live is refused when the channel joins, with Supabase's message. A filter is Supabase's: `column=operator.value`, with `eq`, `neq`, `lt`, `lte`, `gt`, `gte`, `in.(a,b)`, `like`, `ilike`, `match`, `imatch`, `is` and `isdistinct`, `not.` before the operator, and several conditions joined by commas, all of which must hold. Postgres compares each value as the column's type. `select` of columns is not supported yet; private channels neither.
|
|
27
|
+
A channel may listen to the tables of the resources with [`.live()`](live.md)), by `schema` (`*` for any), `table` (every live table of the schema when left out), `event` (`INSERT`, `UPDATE`, `DELETE` or `*`) and `filter`. A table that is not live is refused when the channel joins, with Supabase's message; [`import supabase`](cli.md#import-supabase)) makes the tables of the project's `supabase_realtime` publication live. A filter is Supabase's: `column=operator.value`, with `eq`, `neq`, `lt`, `lte`, `gt`, `gte`, `in.(a,b)`, `like`, `ilike`, `match`, `imatch`, `is` and `isdistinct`, `not.` before the operator, and several conditions joined by commas, all of which must hold. Postgres compares each value as the column's type. `select` of columns is not supported yet; private channels neither.
|
|
28
28
|
|
|
29
29
|
## Broadcast and presence
|
|
30
30
|
|