@rebasepro/server-postgres 0.13.0 → 0.13.1-canary.g249daa1

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.
Files changed (70) hide show
  1. package/dist/PostgresBackendDriver.d.ts +48 -1
  2. package/dist/{src-DlPBctw_.js → auth-users-columns-Dt9g712t.js} +721 -47
  3. package/dist/auth-users-columns-Dt9g712t.js.map +1 -0
  4. package/dist/{backup-service-CD8o_1Sl.js → backup-service-Bww-Lg0s.js} +2 -2
  5. package/dist/{backup-service-CD8o_1Sl.js.map → backup-service-Bww-Lg0s.js.map} +1 -1
  6. package/dist/cli-helpers.d.ts +57 -1
  7. package/dist/{ensure-collection-policies-ViG8XiPn.js → ensure-collection-policies-CwYUliAa.js} +2 -2
  8. package/dist/{ensure-collection-policies-ViG8XiPn.js.map → ensure-collection-policies-CwYUliAa.js.map} +1 -1
  9. package/dist/{ensure-collection-tables-CBQdOETu.js → ensure-collection-tables-DRxaUG96.js} +86 -15
  10. package/dist/ensure-collection-tables-DRxaUG96.js.map +1 -0
  11. package/dist/index.es.js +765 -223
  12. package/dist/index.es.js.map +1 -1
  13. package/dist/{policy-CeA1JcxP.js → policy-CPkCqVTz.js} +4 -4
  14. package/dist/policy-CPkCqVTz.js.map +1 -0
  15. package/dist/rls-bootstrap-sql-Bpv3nUZo.js +244 -0
  16. package/dist/rls-bootstrap-sql-Bpv3nUZo.js.map +1 -0
  17. package/dist/schema/auth-users-columns.d.ts +97 -0
  18. package/dist/schema/ensure-collection-tables.d.ts +2 -2
  19. package/dist/schema/generate-drizzle-schema-logic.d.ts +1 -1
  20. package/dist/schema/generate-postgres-ddl-logic.d.ts +53 -5
  21. package/dist/schema/generated-schema-staleness.d.ts +39 -0
  22. package/dist/schema/rls-bootstrap-sql.d.ts +135 -0
  23. package/dist/schema/search-column.d.ts +199 -0
  24. package/dist/security/rls-enforcement.d.ts +53 -2
  25. package/dist/services/FetchService.d.ts +25 -7
  26. package/dist/services/dataService.d.ts +3 -0
  27. package/dist/services/realtimeService.d.ts +27 -21
  28. package/dist/{src-DoU9yPqq.js → src-C_wvdMnl.js} +91 -2
  29. package/dist/src-C_wvdMnl.js.map +1 -0
  30. package/dist/utils/drizzle-conditions.d.ts +71 -2
  31. package/dist/{websocket-B2LsrINK.js → websocket-D0TBU3ia.js} +3 -3
  32. package/dist/websocket-D0TBU3ia.js.map +1 -0
  33. package/package.json +9 -8
  34. package/src/PostgresBackendDriver.ts +165 -3
  35. package/src/PostgresBootstrapper.ts +41 -2
  36. package/src/auth/ensure-tables.ts +185 -86
  37. package/src/cli-helpers.ts +129 -10
  38. package/src/cli.ts +232 -30
  39. package/src/collections/validate-relations.ts +124 -17
  40. package/src/data-transformer.ts +31 -3
  41. package/src/history/ensure-history-table.ts +7 -0
  42. package/src/schema/auth-users-columns.ts +131 -0
  43. package/src/schema/doctor.ts +7 -5
  44. package/src/schema/ensure-collection-tables.ts +165 -20
  45. package/src/schema/generate-drizzle-schema-logic.ts +33 -3
  46. package/src/schema/generate-postgres-ddl-logic.ts +266 -15
  47. package/src/schema/generate-postgres-ddl.ts +25 -2
  48. package/src/schema/generated-schema-staleness.ts +169 -0
  49. package/src/schema/introspect-db-logic.ts +1 -1
  50. package/src/schema/non-sql-collections.test.ts +131 -0
  51. package/src/schema/rls-bootstrap-sql.ts +288 -0
  52. package/src/schema/search-column.ts +558 -0
  53. package/src/security/anonymous-grants.test.ts +4 -2
  54. package/src/security/rls-enforcement.ts +141 -3
  55. package/src/services/BranchService.ts +5 -0
  56. package/src/services/FetchService.ts +148 -108
  57. package/src/services/PersistService.ts +14 -1
  58. package/src/services/RelationService.ts +2 -1
  59. package/src/services/channel-history.ts +8 -0
  60. package/src/services/channel-presence.ts +6 -0
  61. package/src/services/dataService.ts +3 -0
  62. package/src/services/realtimeService.ts +46 -37
  63. package/src/utils/drizzle-conditions.ts +223 -2
  64. package/dist/ensure-collection-tables-CBQdOETu.js.map +0 -1
  65. package/dist/policy-CeA1JcxP.js.map +0 -1
  66. package/dist/schema/auth-bootstrap-sql.d.ts +0 -24
  67. package/dist/src-DlPBctw_.js.map +0 -1
  68. package/dist/src-DoU9yPqq.js.map +0 -1
  69. package/dist/websocket-B2LsrINK.js.map +0 -1
  70. package/src/schema/auth-bootstrap-sql.ts +0 -47
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Canonical SQL bootstrap for the RLS helper functions.
3
+ *
4
+ * Generated RLS policies reference `rebase.uid()` / `rebase.roles()` /
5
+ * `rebase.jwt()`, so any SQL stream that can contain policies must be
6
+ * self-contained: it has to (re)create these helpers first. This matters for
7
+ * the migration directory in particular — Atlas replays migrations against a
8
+ * clean dev database where no out-of-band bootstrap has ever run, so a
9
+ * migration carrying policies without this preamble fails with
10
+ * "function rebase.uid() does not exist".
11
+ *
12
+ * Idempotent (`IF NOT EXISTS` / `OR REPLACE`) so it can be prepended to every
13
+ * policies block and re-applied freely. The runtime boot path
14
+ * (`auth/ensure-tables.ts`) creates the same functions under an advisory lock
15
+ * for HMR-safety; keep the definitions in sync.
16
+ *
17
+ * ## Creating the `rebase` schema here is now safe, and required
18
+ *
19
+ * It deliberately did not, once. These functions lived in a schema called
20
+ * `auth`, and the note here read: creating `rebase` would leak it into Atlas's
21
+ * replayed migration state, and — absent from the desired `schema.sql` — Atlas
22
+ * would then plan `DROP SCHEMA "rebase" CASCADE`, taking the auth tables with
23
+ * it. That reasoning still holds; what changed is the second half of it. The
24
+ * DDL generator now emits `CREATE SCHEMA IF NOT EXISTS "rebase"`
25
+ * unconditionally, so the schema is always in the desired state and the diff is
26
+ * empty. (It used to appear only when some collection happened to declare
27
+ * `schema: "rebase"` — true for the scaffold's users collection, and not a
28
+ * property anything guaranteed.) `db push` additionally excludes the whole
29
+ * schema from the declarative apply.
30
+ *
31
+ * See `@rebasepro/types`' `rls-functions` for why the functions moved out of
32
+ * `auth` at all: the short version is that the name was Supabase's, and
33
+ * `CREATE OR REPLACE FUNCTION auth.uid() RETURNS text` cannot be applied over
34
+ * Supabase's `RETURNS uuid` — Postgres refuses, and the refusal used to be
35
+ * swallowed.
36
+ */
37
+ /**
38
+ * The bootstrap as individual statements.
39
+ *
40
+ * Kept as an array because the two consumers need different shapes and only one
41
+ * of them can take a multi-command string: the migration preamble is written to
42
+ * a file and replayed by Atlas, but the boot path runs through drizzle, whose
43
+ * node-postgres handle speaks the extended query protocol and rejects more than
44
+ * one command per call. Splitting a joined string back apart on `$$;` would be
45
+ * a parser for a problem that does not need one.
46
+ */
47
+ export declare const RLS_BOOTSTRAP_STATEMENTS: readonly string[];
48
+ /** The same statements as one script, for migration files and raw clients. */
49
+ export declare const RLS_BOOTSTRAP_SQL: string;
50
+ /**
51
+ * Removes the pre-1.0 `auth` schema, but only when Rebase is what put it there.
52
+ *
53
+ * ## Why this is safe against a Supabase database
54
+ *
55
+ * Two independent guards, and both have to pass:
56
+ *
57
+ * 1. **Each function is identified before it is dropped.** Ours returns `text`
58
+ * and reads the `app.uid` GUC; Supabase's returns `uuid` and reads
59
+ * `request.jwt.claims`. Nothing is dropped on a signature we did not write,
60
+ * so a Supabase database — where our `CREATE OR REPLACE` could never have
61
+ * succeeded in the first place, Postgres refusing to change a return type —
62
+ * matches nothing and this is a no-op.
63
+ * 2. **`DROP SCHEMA … RESTRICT`**, never CASCADE. If anything else at all still
64
+ * lives in `auth` (Supabase's `users` table, its other helpers), the drop
65
+ * fails and the schema stays. CASCADE here would be unrecoverable.
66
+ *
67
+ * ## Why it cannot run too early
68
+ *
69
+ * Postgres records a dependency from every RLS policy to the functions its body
70
+ * calls, so `DROP FUNCTION auth.uid()` fails for as long as a single policy
71
+ * still references it. That is the interlock, and it is load-bearing: the drop
72
+ * can only succeed once every policy has been recompiled to \`rebase.uid()\`.
73
+ * Callers therefore run this *after* applying policies, and treat a failure as
74
+ * "not yet — try again next boot" rather than as an error.
75
+ */
76
+ export declare const DROP_LEGACY_AUTH_SCHEMA_SQL = "\nDO $rebase_drop_legacy$\nDECLARE\n dropped_any boolean := false;\nBEGIN\n -- Each function is matched on its own result type and body, so a schema\n -- that merely shares the name keeps everything it has.\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'uid'\n AND pg_get_function_result(p.oid) = 'text'\n AND p.prosrc LIKE '%app.uid%'\n ) THEN\n DROP FUNCTION auth.uid();\n dropped_any := true;\n END IF;\n\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'jwt'\n AND pg_get_function_result(p.oid) = 'jsonb'\n AND p.prosrc LIKE '%app.jwt%'\n ) THEN\n DROP FUNCTION auth.jwt();\n dropped_any := true;\n END IF;\n\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'roles'\n AND pg_get_function_result(p.oid) = 'text'\n AND p.prosrc LIKE '%app.user_roles%'\n ) THEN\n DROP FUNCTION auth.roles();\n dropped_any := true;\n END IF;\n\n -- RESTRICT: only an empty schema goes. Anything else in there \u2014 including a\n -- Supabase installation left untouched above \u2014 keeps it.\n IF dropped_any THEN\n BEGIN\n EXECUTE 'DROP SCHEMA auth RESTRICT';\n EXCEPTION WHEN OTHERS THEN\n NULL;\n END;\n END IF;\nEND\n$rebase_drop_legacy$;\n";
77
+ /** Somebody's policy that still calls a pre-1.0 helper. */
78
+ export interface LegacyRlsDependent {
79
+ schema: string;
80
+ table: string;
81
+ policy: string;
82
+ }
83
+ /**
84
+ * Policies whose body still calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.
85
+ *
86
+ * Postgres will not drop a function a policy depends on, so this is exactly the
87
+ * set standing between a database and losing the legacy schema. Rebase's own
88
+ * policies leave the list on the next push or boot, when they are recompiled —
89
+ * anything still here afterwards is hand-written, will never be recompiled by
90
+ * anybody, and is the reason the drop keeps being skipped. Silence there would
91
+ * leave an operator staring at a schema the release notes said would go.
92
+ */
93
+ export declare const LEGACY_RLS_DEPENDENTS_SQL = "\n SELECT n.nspname AS schema, c.relname AS \"table\", p.polname AS policy\n FROM pg_policy p\n JOIN pg_class c ON c.oid = p.polrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE pg_get_expr(p.polqual, p.polrelid) ~* '\\mauth\\.(uid|jwt|roles)\\s*\\('\n OR pg_get_expr(p.polwithcheck, p.polrelid) ~* '\\mauth\\.(uid|jwt|roles)\\s*\\('\n ORDER BY 1, 2, 3\n";
94
+ /** Somebody's *function* that still calls a pre-1.0 helper from its own body. */
95
+ export interface LegacyRlsFunctionDependent {
96
+ schema: string;
97
+ function: string;
98
+ }
99
+ /**
100
+ * Functions whose body calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.
101
+ *
102
+ * This is the half `DROP FUNCTION ... RESTRICT` cannot see, and the reason it
103
+ * needs its own query. Postgres records a dependency for a *policy* that calls a
104
+ * function, which is why the drop is safe against the policies above — but a
105
+ * `LANGUAGE sql` function whose body is a **string literal** is not parsed when
106
+ * it is created, so nothing is recorded and `RESTRICT` has nothing to refuse on.
107
+ * The drop succeeds and the caller is left pointing at a function that no longer
108
+ * exists, which fails at *query* time rather than at boot.
109
+ *
110
+ * A downstream project building on these helpers is not hypothetical: the Rebase
111
+ * control plane defines `auth.is_org_member(uuid)` and `auth.is_org_admin(uuid)`
112
+ * in this very schema, each calling `auth.uid()` in a string body, and eleven of
113
+ * its row-level-security policies go through them. Every one of those would have
114
+ * started failing the first time a recompile left no policy referencing
115
+ * `auth.uid()` directly — the drop's own precondition.
116
+ *
117
+ * Matching on the body text is the only option available, and it is deliberately
118
+ * broad: a false positive costs a schema that stays one release longer and says
119
+ * why, while a false negative costs somebody their policies.
120
+ */
121
+ export declare const LEGACY_RLS_FUNCTION_DEPENDENTS_SQL = "\n SELECT n.nspname AS schema, p.proname AS function\n FROM pg_proc p\n JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND NOT (n.nspname = 'auth' AND p.proname IN ('uid', 'jwt', 'roles'))\n AND p.prosrc ~* '\\mauth\\.(uid|jwt|roles)\\s*\\('\n ORDER BY 1, 2\n";
122
+ /**
123
+ * Retire the pre-1.0 `auth` schema, reporting what is holding it back.
124
+ *
125
+ * The shared implementation behind the CLI's post-push step and the runtime's
126
+ * post-policy step. Both used to just fire {@link DROP_LEGACY_AUTH_SCHEMA_SQL}
127
+ * and swallow whatever came back, which is right for the ordinary case — a
128
+ * table not recompiled *yet* — and wrong for the one that never resolves: a
129
+ * hand-written policy nothing will ever rewrite. Then the schema stays forever
130
+ * and nothing ever says why.
131
+ */
132
+ export declare function dropLegacyAuthSchema(run: (sql: string) => Promise<Record<string, unknown>[]>, report: {
133
+ info: (m: string) => void;
134
+ warn: (m: string) => void;
135
+ }): Promise<void>;
@@ -0,0 +1,199 @@
1
+ /**
2
+ * The one place a collection's `search` block becomes SQL.
3
+ *
4
+ * Four things describe a Postgres table in this codebase — the DDL generator,
5
+ * the Drizzle schema generator, the runtime table builder for BaaS mode, and
6
+ * the boot-time schema ensure — and each of them has, at some point, described
7
+ * a column differently from the others. The `varchar(255)` note in
8
+ * `generate-postgres-ddl-logic` is one such scar: the same property produced a
9
+ * capped column down one path and an uncapped one down the other, and nothing
10
+ * failed until a user hit the cap.
11
+ *
12
+ * So the search column is not implemented four times. It is computed once,
13
+ * here, and every generator renders the same {@link SearchColumnSpec}. There is
14
+ * a test asserting exactly that (`search-column-contract.test.ts`); the point of
15
+ * this module is that the test has something to assert *about*.
16
+ *
17
+ * ## Why the expressions look the way they do
18
+ *
19
+ * A `GENERATED ALWAYS AS … STORED` expression must be strictly IMMUTABLE, and
20
+ * Postgres is stricter here than intuition. Verified against PostgreSQL 18:
21
+ *
22
+ * | expression | immutable |
23
+ * |-----------------------------------------|-----------|
24
+ * | `to_tsvector('spanish', col)` | yes |
25
+ * | `to_tsvector(col)` (1-arg) | **no** — depends on `default_text_search_config` |
26
+ * | `array_to_string(col, ' ')` | **no** |
27
+ * | `col::text` on `text[]` | **no** |
28
+ * | `to_jsonb(col)` | **no** |
29
+ * | `unaccent(col)` | **no** — dictionary lookup is STABLE |
30
+ * | `jsonb_to_tsvector('spanish', j, '["string"]')` | yes |
31
+ * | `setweight(...) || setweight(...)` | yes |
32
+ *
33
+ * Three of the four things a real search column needs are therefore unavailable
34
+ * directly, which is why {@link searchHelperFunctions} exists: each wraps a
35
+ * stable built-in in an SQL function declared IMMUTABLE. That declaration is a
36
+ * promise, and it is a true one for these three — array joining, JSON string
37
+ * extraction and accent folding are all deterministic for a given input; the
38
+ * built-ins are marked stable only because they must account for element types
39
+ * and dictionaries in general.
40
+ *
41
+ * The alternative was to skip `unaccent` and text arrays entirely. That is not
42
+ * a real option in an accented language: Postgres stems `auditoría` to
43
+ * `auditor` and `auditoria` to `auditori` — *different lexemes* — so a query
44
+ * typed without accents misses every row that carries them.
45
+ */
46
+ import { CollectionConfig, SearchConfig, SearchWeight } from "@rebasepro/types";
47
+ /**
48
+ * Names of the helper functions. Frozen: they are recorded in the stored
49
+ * generation expression of every search column ever created, so renaming one
50
+ * orphans every table that already has a search column.
51
+ */
52
+ export declare const SEARCH_TEXT_FN = "public.rebase_search_text";
53
+ export declare const SEARCH_UNACCENT_FN = "public.rebase_search_unaccent";
54
+ /** How a declared path reaches text, which decides the SQL that extracts it. */
55
+ type FieldKind = "text" | "text_array" | "jsonb";
56
+ /** One resolved field: where it lives, how to read it, what it is worth. */
57
+ export interface ResolvedSearchField {
58
+ /** The path exactly as the author wrote it, for error messages. */
59
+ path: string;
60
+ /** The physical column the path starts at. */
61
+ column: string;
62
+ /** Dotted remainder addressed inside a JSONB column, if any. */
63
+ jsonPath: string[];
64
+ kind: FieldKind;
65
+ weight: SearchWeight;
66
+ /** The `setweight(to_tsvector(…), 'X')` term this field contributes. */
67
+ sql: string;
68
+ /** The plain-text term this field contributes, for the fuzzy column. */
69
+ textSql: string;
70
+ }
71
+ /** Everything the generators need to render one collection's search column. */
72
+ export interface SearchColumnSpec {
73
+ schema: string;
74
+ table: string;
75
+ /** The generated `tsvector` column. */
76
+ column: string;
77
+ language: string;
78
+ unaccent: boolean;
79
+ fields: ResolvedSearchField[];
80
+ /** Body of `GENERATED ALWAYS AS ( … ) STORED` for the tsvector column. */
81
+ expression: string;
82
+ indexName: string;
83
+ /** Extensions that must exist before the column can be created. */
84
+ extensions: string[];
85
+ fuzzy?: {
86
+ column: string;
87
+ expression: string;
88
+ indexName: string;
89
+ threshold: number;
90
+ };
91
+ }
92
+ /** Raised when a `search` block names something that cannot be searched. */
93
+ export declare class SearchConfigError extends Error {
94
+ constructor(message: string);
95
+ }
96
+ /** The `search` block of a collection, or undefined when it has none. */
97
+ export declare const getSearchConfig: (collection: CollectionConfig) => SearchConfig | undefined;
98
+ /**
99
+ * Refuse a `search` block on a collection this engine does not store.
100
+ *
101
+ * The type only permits one on a `PostgresCollectionConfig`, so TypeScript
102
+ * already stops the ordinary case. This catches the rest — a JS config, a cast,
103
+ * a collection whose `engine` was changed after the block was written — because
104
+ * the alternative is the exact failure the block exists to prevent: a developer
105
+ * who declared what to index, saw no error, and got the substring fallback.
106
+ *
107
+ * Called with *every* collection, before the Postgres ones are filtered out.
108
+ */
109
+ export declare const assertSearchIsPostgresOnly: (collections: CollectionConfig[]) => void;
110
+ /**
111
+ * Build the full spec for a collection, or undefined when it has not opted in.
112
+ *
113
+ * Throws {@link SearchConfigError} on a config that cannot be honoured. Callers
114
+ * at boot surface that as a startup failure — a search block that half-works is
115
+ * worse than one that refuses.
116
+ */
117
+ export declare const buildSearchColumnSpec: (collection: CollectionConfig) => SearchColumnSpec | undefined;
118
+ /**
119
+ * The IMMUTABLE wrappers the generated expressions call.
120
+ *
121
+ * `CREATE OR REPLACE` so a boot against an existing database is a no-op rather
122
+ * than an error, and idempotent for the same reason every other boot-time DDL
123
+ * statement here is.
124
+ *
125
+ * The bodies are stable built-ins wrapped in an immutable promise — see the
126
+ * module comment for why that promise is sound. `STRICT` matters: it makes NULL
127
+ * in mean NULL out without executing the body, which is what the `coalesce` at
128
+ * each call site then absorbs.
129
+ */
130
+ export declare const searchHelperFunctions: (spec: SearchColumnSpec) => string[];
131
+ /**
132
+ * `CREATE EXTENSION` statements the spec's expressions depend on.
133
+ *
134
+ * `WITH SCHEMA public` is load-bearing, not tidiness. An unqualified
135
+ * `CREATE EXTENSION` installs into the first schema on `search_path`, which
136
+ * defaults to `"$user", public` — and the scaffold's database role is named
137
+ * `rebase`, the same as the schema the generator creates one statement earlier.
138
+ * So the moment that schema exists, `CREATE EXTENSION unaccent` puts the
139
+ * dictionary in `rebase`, and every reference to `public.unaccent` below fails
140
+ * with "text search dictionary does not exist". Observed, not theorised.
141
+ */
142
+ export declare const searchExtensionStatements: (spec: SearchColumnSpec) => string[];
143
+ /** The column definition as it appears inside `CREATE TABLE`. */
144
+ export declare const searchColumnDefinition: (spec: SearchColumnSpec) => string;
145
+ /** The fuzzy column definition, when the spec asks for one. */
146
+ export declare const fuzzyColumnDefinition: (spec: SearchColumnSpec) => string | undefined;
147
+ /**
148
+ * Index statements for the spec.
149
+ *
150
+ * `CONCURRENTLY` is deliberately *not* used here. This form is emitted into a
151
+ * SQL file replayed as one unit — a migration, or `search.sql` — where a
152
+ * concurrent build is not allowed. The boot-time ensure path runs statement by
153
+ * statement against tables that are live and populated, and uses the
154
+ * concurrent form instead; see `ensureSearchColumns`.
155
+ */
156
+ export declare const searchIndexStatements: (spec: SearchColumnSpec) => string[];
157
+ /**
158
+ * The index names the spec creates.
159
+ *
160
+ * Needed by name, not just by statement, so Atlas can be told to exclude them
161
+ * from its diff — see `searchExcludePatterns`.
162
+ */
163
+ export declare const searchIndexNames: (spec: SearchColumnSpec) => string[];
164
+ /**
165
+ * The generated column names a collection's search block adds, if any.
166
+ *
167
+ * These are physical columns on the table, so `SELECT *` returns them. They are
168
+ * an index in column form — a list of lexeme positions, or a concatenation of
169
+ * every searchable field on the row — and nothing outside the query planner has
170
+ * any use for them. Left in, every list response carries a second, larger copy
171
+ * of the row's text.
172
+ */
173
+ export declare const searchColumnNames: (collection: CollectionConfig) => string[];
174
+ /**
175
+ * True for a column whose type only ever holds a search index.
176
+ *
177
+ * Independent of any collection config on purpose: an introspected database
178
+ * (BaaS mode) can carry a `tsvector` column this framework never created —
179
+ * Pagila's `film.fulltext` is the canonical one — and it should not be returned
180
+ * to callers either. `isDerivedIndexColumn` already keeps such a column out of
181
+ * the *properties*; this keeps it out of the *rows*.
182
+ */
183
+ export declare const isSearchIndexColumn: (column: {
184
+ getSQLType?: () => string;
185
+ }) => boolean;
186
+ /**
187
+ * A drizzle select projection over `table` with the search columns dropped.
188
+ *
189
+ * Returns undefined when nothing needs dropping, so the common case keeps using
190
+ * a plain `select()` and this stays invisible in the generated SQL.
191
+ */
192
+ export declare const visibleColumnProjection: (tableColumns: Record<string, {
193
+ getSQLType?: () => string;
194
+ }> | undefined, collection?: CollectionConfig) => Record<string, unknown> | undefined;
195
+ /** The same exclusion as a drizzle `db.query` `columns` denylist. */
196
+ export declare const hiddenColumnsOption: (tableColumns: Record<string, {
197
+ getSQLType?: () => string;
198
+ }> | undefined, collection?: CollectionConfig) => Record<string, false> | undefined;
199
+ export {};
@@ -1,5 +1,6 @@
1
1
  import { SQL } from "drizzle-orm";
2
2
  import { SecurityRule } from "@rebasepro/types";
3
+ import { REBASE_USER_ROLE } from "@rebasepro/common";
3
4
  /**
4
5
  * Unified RLS enforcement — the "user context vs server context" model.
5
6
  *
@@ -32,8 +33,14 @@ import { SecurityRule } from "@rebasepro/types";
32
33
  * self-creates the `auth` schema and functions) — enforcement is default-on,
33
34
  * not an operator opt-in.
34
35
  */
35
- /** The restricted role every authenticated (user-context) request runs as. */
36
- export declare const REBASE_USER_ROLE = "rebase_user";
36
+ /**
37
+ * The restricted role every authenticated (user-context) request runs as.
38
+ *
39
+ * Re-exported, not re-declared: the same name is needed by
40
+ * `@rebasepro/common`'s internal-table revokes, and two spellings of a role name
41
+ * fail as a silent no-op rather than an error.
42
+ */
43
+ export { REBASE_USER_ROLE };
37
44
  /** Minimal SQL runner so callers can adapt drizzle or pg.Client. */
38
45
  export type RawSqlRunner = (sqlText: string) => Promise<Record<string, unknown>[]>;
39
46
  /** Minimal transaction surface needed by {@link applyAuthContext}. */
@@ -55,6 +62,34 @@ export interface AuthContext {
55
62
  /** Raw roles as carried on the user (strings or `{ id }` objects). */
56
63
  roles: unknown[];
57
64
  }
65
+ /**
66
+ * Warn when the connection role shares its name with an existing schema.
67
+ *
68
+ * Postgres resolves unqualified names through `search_path`, which defaults to
69
+ * `"$user", public` — and `$user` is the connection ROLE. When a schema of that
70
+ * name exists it sits ahead of `public`, so every unqualified statement
71
+ * silently operates on it instead:
72
+ *
73
+ * CREATE TABLE posts (...); -- you meant public.posts; you got <role>.posts
74
+ *
75
+ * Nothing errors. You get a second table of the same name in the wrong schema,
76
+ * and reads that pin `public` cannot see it — which reads as "missing table" and
77
+ * sends people to re-run a push that creates a *third* copy. The bootstrapper
78
+ * has a whole branch dedicated to recognising the symptom after the fact.
79
+ *
80
+ * Rebase shipped straight into this: it creates a schema named `rebase` while
81
+ * every template named the database role `rebase` too. The scaffold uses
82
+ * `rebase_app` now, and every pool Rebase opens pins `search_path=public`
83
+ * (`pinSearchPath`), which covers the paths the framework controls. This covers
84
+ * the ones it does not — `psql`, `pg_dump`, drizzle-kit, a colleague's script,
85
+ * a hand-written migration — because the hazard is a property of the two NAMES,
86
+ * not of any one connection.
87
+ *
88
+ * A warning rather than a boot failure: the database works, the framework's own
89
+ * traffic is pinned, and refusing to start over a naming choice a user may have
90
+ * inherited would be worse than the risk.
91
+ */
92
+ export declare function warnOnRoleSchemaCollision(run: RawSqlRunner): Promise<void>;
58
93
  export declare function detectConnectionPosture(run: RawSqlRunner): Promise<ConnectionPosture>;
59
94
  /**
60
95
  * Human-actionable instructions for when the connection cannot provision the
@@ -123,6 +158,22 @@ export declare function warnOnAnonymousGrants(collections: {
123
158
  slug?: string;
124
159
  securityRules?: readonly SecurityRule[];
125
160
  }[]): void;
161
+ /**
162
+ * Name the collections whose raw policy SQL still calls the pre-1.0 helpers.
163
+ *
164
+ * The compiler rewrites `auth.uid()` to `rebase.uid()` on the way into the
165
+ * database, so nothing is broken and no policy is wrong — which is exactly why
166
+ * this has to be said out loud. A silent rewrite that works forever is not a
167
+ * migration, it is a second supported spelling nobody wrote down, and the next
168
+ * person to read those rules will copy the old one.
169
+ *
170
+ * Only `raw` expressions can carry it. Structured rules (`policy.authUid()`,
171
+ * `policy.rolesOverlap(...)`) compile from the model and were never affected.
172
+ */
173
+ export declare function warnOnLegacyRlsFunctions(collections: {
174
+ slug?: string;
175
+ securityRules?: readonly SecurityRule[];
176
+ }[]): void;
126
177
  /**
127
178
  * Reject `pgRoles` that this server can never satisfy.
128
179
  *
@@ -64,6 +64,18 @@ export declare class FetchService {
64
64
  * and skips rows rather than erroring. The guesses stay, last, for a
65
65
  * caller that hands over no collection to resolve against.
66
66
  */
67
+ /**
68
+ * The ORDER BY target, which may be relevance rather than a column.
69
+ *
70
+ * `_score` is only meaningful for a collection that declared a `search`
71
+ * block *and* for a request that carried a search string — ranking rows
72
+ * against no query ranks them all at zero. Outside those two conditions it
73
+ * is an unknown field and gets the same 400 as any other typo, which is the
74
+ * behaviour that matters: a sort that is silently dropped returns 200 with
75
+ * rows in arbitrary order, and paging over that repeats and skips rows.
76
+ */
77
+ static readonly SCORE_FIELD = "_score";
78
+ private resolveOrderTarget;
67
79
  private resolveOrderByField;
68
80
  /**
69
81
  * Build the `with` config for Drizzle's relational query API.
@@ -132,6 +144,7 @@ export declare class FetchService {
132
144
  offset?: number;
133
145
  startAfter?: Record<string, unknown>;
134
146
  searchString?: string;
147
+ searchExplain?: boolean;
135
148
  databaseId?: string;
136
149
  vectorSearch?: VectorSearchParams;
137
150
  logical?: LogicalCondition;
@@ -153,6 +166,16 @@ export declare class FetchService {
153
166
  */
154
167
  fetchCollection<M extends Record<string, unknown>>(collectionPath: string, options?: {
155
168
  filter?: FilterValues<Extract<keyof M, string>>;
169
+ /**
170
+ * An `or(...)`/`and(...)` group, applied alongside `filter`.
171
+ *
172
+ * `fetchRowsWithConditions` below has always applied this; it was
173
+ * simply absent from this signature, so the only callers that could
174
+ * pass one were the ones that went around this method. Realtime
175
+ * came through here, which is why a subscription filtered by a
176
+ * logical group was pushed every row in the table.
177
+ */
178
+ logical?: LogicalCondition;
156
179
  orderBy?: string;
157
180
  order?: "desc" | "asc";
158
181
  limit?: number;
@@ -171,6 +194,8 @@ export declare class FetchService {
171
194
  order?: "desc" | "asc";
172
195
  limit?: number;
173
196
  databaseId?: string;
197
+ /** Ask each row which declared search field matched. */
198
+ searchExplain?: boolean;
174
199
  }): Promise<Record<string, unknown>[]>;
175
200
  /**
176
201
  * Count rows in a collection
@@ -226,13 +251,6 @@ export declare class FetchService {
226
251
  * Note: Primary path now uses inline `getQueryBuilder()` checks.
227
252
  */
228
253
  private hasDrizzleQueryAPI;
229
- /**
230
- * Attempt to use Drizzle's relational query API (db.query.<table>.findMany)
231
- * for efficient JOIN-based relation loading.
232
- * Returns null if the API is not available or the query fails.
233
- * Note: Primary path now uses `buildWithConfig` + `buildDrizzleQueryOptions`.
234
- */
235
- private fetchWithDrizzleQuery;
236
254
  /**
237
255
  * Fallback path used when db.query is unavailable.
238
256
  * The primary path uses db.query.findMany with `with` config, which
@@ -36,6 +36,8 @@ export declare class DataService implements DataRepository {
36
36
  */
37
37
  fetchCollection<M extends Record<string, unknown>>(collectionPath: string, options?: {
38
38
  filter?: FilterValues<Extract<keyof M, string>>;
39
+ /** An `or(...)`/`and(...)` group, applied alongside `filter`. */
40
+ logical?: LogicalCondition;
39
41
  orderBy?: string;
40
42
  order?: "desc" | "asc";
41
43
  limit?: number;
@@ -54,6 +56,7 @@ export declare class DataService implements DataRepository {
54
56
  order?: "desc" | "asc";
55
57
  limit?: number;
56
58
  databaseId?: string;
59
+ searchExplain?: boolean;
57
60
  }): Promise<Record<string, unknown>[]>;
58
61
  /**
59
62
  * Count rows in a collection
@@ -1,6 +1,6 @@
1
1
  import { WebSocket } from "ws";
2
2
  import { EventEmitter } from "events";
3
- import { DataDriver, WebSocketMessage } from "@rebasepro/types";
3
+ import { DataDriver, WebSocketMessage, LogicalCondition } from "@rebasepro/types";
4
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
5
  import { RealtimeProvider, CollectionSubscriptionConfig, SingleSubscriptionConfig } from "../interfaces";
6
6
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
@@ -14,6 +14,29 @@ export interface SubscriptionAuthContext {
14
14
  uid: string;
15
15
  roles: string[];
16
16
  }
17
+ /**
18
+ * The narrowing a collection subscription was created with, kept so that every
19
+ * refetch answers the same query the initial fetch did.
20
+ *
21
+ * Named once because it used to be written out inline in five places, and a
22
+ * field missing from one of them is accepted over the wire and then silently
23
+ * ignored: `offset` was declared on the incoming props and never stored, so a
24
+ * live list on page three served page one, and `logical` was never stored
25
+ * either, so an `or(...)` subscription was pushed every row in the table.
26
+ */
27
+ type StoredCollectionRequest = {
28
+ filter?: Record<string, unknown>;
29
+ logical?: LogicalCondition;
30
+ orderBy?: string;
31
+ order?: "desc" | "asc";
32
+ limit?: number;
33
+ offset?: number;
34
+ startAfter?: Record<string, unknown>;
35
+ databaseId?: string;
36
+ searchString?: string;
37
+ /** Ask each row which declared search field matched — populates `_matches`. */
38
+ searchExplain?: boolean;
39
+ };
17
40
  /**
18
41
  * PostgreSQL-specific realtime service.
19
42
  * Handles WebSocket connections and subscriptions for real-time row updates.
@@ -128,16 +151,7 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
128
151
  type: "collection" | "single";
129
152
  path: string;
130
153
  id?: string | number;
131
- collectionRequest?: {
132
- filter?: Record<string, unknown>;
133
- orderBy?: string;
134
- order?: "desc" | "asc";
135
- limit?: number;
136
- offset?: number;
137
- startAfter?: Record<string, unknown>;
138
- databaseId?: string;
139
- searchString?: string;
140
- };
154
+ collectionRequest?: StoredCollectionRequest;
141
155
  authContext?: SubscriptionAuthContext;
142
156
  }>;
143
157
  registerDataDriverSubscription(subscriptionId: string, subscription: {
@@ -145,16 +159,7 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
145
159
  type: "collection" | "single";
146
160
  path: string;
147
161
  id?: string | number;
148
- collectionRequest?: {
149
- filter?: Record<string, unknown>;
150
- orderBy?: string;
151
- order?: "desc" | "asc";
152
- limit?: number;
153
- offset?: number;
154
- startAfter?: Record<string, unknown>;
155
- databaseId?: string;
156
- searchString?: string;
157
- };
162
+ collectionRequest?: StoredCollectionRequest;
158
163
  authContext?: SubscriptionAuthContext;
159
164
  }): void;
160
165
  addSubscriptionCallback(subscriptionId: string, callback: (data: Record<string, unknown>[] | Record<string, unknown> | null) => void): void;
@@ -497,3 +502,4 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
497
502
  * This allows code to use PostgresRealtimeProvider alongside future MongoRealtimeProvider, etc.
498
503
  */
499
504
  export declare const PostgresRealtimeProvider: typeof RealtimeService;
505
+ export {};