@rebasepro/server-postgres 0.13.1-canary.gc9afce1 → 0.13.1-canary.gca63148
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/PostgresBackendDriver.d.ts +48 -1
- package/dist/{src-CU6WZGYV.js → auth-users-columns-BQttUZE2.js} +587 -33
- package/dist/auth-users-columns-BQttUZE2.js.map +1 -0
- package/dist/{backup-service-CD8o_1Sl.js → backup-service-Bww-Lg0s.js} +2 -2
- package/dist/{backup-service-CD8o_1Sl.js.map → backup-service-Bww-Lg0s.js.map} +1 -1
- package/dist/{ensure-collection-policies-Bck0ky4u.js → ensure-collection-policies-C8H7ig-m.js} +2 -2
- package/dist/{ensure-collection-policies-Bck0ky4u.js.map → ensure-collection-policies-C8H7ig-m.js.map} +1 -1
- package/dist/{ensure-collection-tables-BLIIACla.js → ensure-collection-tables-CyMBc7wo.js} +78 -11
- package/dist/ensure-collection-tables-CyMBc7wo.js.map +1 -0
- package/dist/index.es.js +650 -129
- package/dist/index.es.js.map +1 -1
- package/dist/{policy-CeA1JcxP.js → policy-CPkCqVTz.js} +4 -4
- package/dist/policy-CPkCqVTz.js.map +1 -0
- package/dist/rls-bootstrap-sql-Bpv3nUZo.js +244 -0
- package/dist/rls-bootstrap-sql-Bpv3nUZo.js.map +1 -0
- package/dist/schema/auth-users-columns.d.ts +97 -0
- package/dist/schema/ensure-collection-tables.d.ts +1 -1
- package/dist/schema/rls-bootstrap-sql.d.ts +135 -0
- package/dist/schema/search-column.d.ts +192 -0
- package/dist/security/rls-enforcement.d.ts +53 -2
- package/dist/services/FetchService.d.ts +13 -0
- package/dist/{src-DoU9yPqq.js → src-C_wvdMnl.js} +91 -2
- package/dist/src-C_wvdMnl.js.map +1 -0
- package/dist/utils/drizzle-conditions.d.ts +71 -2
- package/dist/{websocket-B2LsrINK.js → websocket-D0TBU3ia.js} +3 -3
- package/dist/{websocket-B2LsrINK.js.map → websocket-D0TBU3ia.js.map} +1 -1
- package/package.json +8 -7
- package/src/PostgresBackendDriver.ts +165 -3
- package/src/PostgresBootstrapper.ts +41 -2
- package/src/auth/ensure-tables.ts +185 -86
- package/src/cli-helpers.ts +14 -7
- package/src/cli.ts +96 -30
- package/src/data-transformer.ts +18 -0
- package/src/history/ensure-history-table.ts +7 -0
- package/src/schema/auth-users-columns.ts +131 -0
- package/src/schema/ensure-collection-tables.ts +157 -18
- package/src/schema/generate-drizzle-schema-logic.ts +25 -1
- package/src/schema/generate-postgres-ddl-logic.ts +127 -8
- package/src/schema/rls-bootstrap-sql.ts +288 -0
- package/src/schema/search-column.ts +549 -0
- package/src/security/anonymous-grants.test.ts +4 -2
- package/src/security/rls-enforcement.ts +141 -3
- package/src/services/BranchService.ts +5 -0
- package/src/services/FetchService.ts +136 -15
- package/src/services/RelationService.ts +2 -1
- package/src/services/channel-history.ts +8 -0
- package/src/services/channel-presence.ts +6 -0
- package/src/utils/drizzle-conditions.ts +223 -2
- package/dist/ensure-collection-tables-BLIIACla.js.map +0 -1
- package/dist/policy-CeA1JcxP.js.map +0 -1
- package/dist/schema/auth-bootstrap-sql.d.ts +0 -24
- package/dist/src-CU6WZGYV.js.map +0 -1
- package/dist/src-DoU9yPqq.js.map +0 -1
- package/src/schema/auth-bootstrap-sql.ts +0 -47
package/dist/index.es.js
CHANGED
|
@@ -2,11 +2,12 @@ import { createRequire as __createRequire } from "module";
|
|
|
2
2
|
import process from "process";
|
|
3
3
|
__createRequire(import.meta.url);
|
|
4
4
|
import { a as guardPoolAgainstDirtyRelease, i as createReadReplicaConnection, n as createDirectDatabaseConnection, o as pinSearchPath, r as createPostgresDatabaseConnection } from "./connection-BuZ97wsr.js";
|
|
5
|
-
import { A as
|
|
6
|
-
import {
|
|
7
|
-
import { t as ANONYMOUS_USER_ID } from "./policy-
|
|
8
|
-
import { t as createPostgresWebSocket } from "./websocket-
|
|
9
|
-
import { A as parsePgToolMajor, C as checkToolServerCompatibility, D as parseBackupDestination, E as joinStorageKey, M as serverVersionNumToMajor, N as splitGlobalsStatements, O as parseBackupTimestamp, P as withDatabaseName, S as buildRowSecurityPgOptions, T as globalsFileForDump, _ as buildBackupFilename, a as detectToolMajor, b as buildPgRestoreArgs, c as listBackups, d as resolvePgBinary, f as restoreDump, g as selectBackupsToPrune, h as require_source, i as createDump, j as resolveConnectionString, k as parseDbNameFromUrl, l as preflight, m as validateDump, n as applyGlobals, o as ensureDatabaseExists, p as uploadBackup, s as getServerVersionMajor, t as BackupToolError, u as pruneBackups, v as buildPgDumpArgs, w as diagnoseRowSecurityDumpFailure, x as buildPgRestoreListArgs, y as buildPgDumpallGlobalsArgs } from "./backup-service-
|
|
5
|
+
import { A as resolveCollectionRelations, B as legacyForeignKeyName, C as securityRuleToConditions, D as getEnumVarName, E as getColumnName, F as createRelationRef, G as camelCase, H as getPolicyNamesForRule, I as createRelationRefWithData, K as toSnakeCase, L as normalizeToEntityRelation, M as getDeclaredPrimaryKeys, N as isAddressableId, O as getTableName$1, P as parseIdValues, Q as Vector, R as updateDateAutoValues, S as policyToPostgres, T as findRelation, U as isPrototypePollutingKey, W as mergeDeep, X as hasForeignKeyOnTarget, Y as resolveClientListLimit, Z as isManyToMany, _ as resolveStringColumnLength, b as resolveJunctionSpecs, c as buildSearchColumnSpec, g as relationalCollections, h as CollectionRegistry, j as buildCompositeId, k as getTableVarName, l as hiddenColumnsOption, m as buildSdkData, o as SEARCH_UNACCENT_FN, p as visibleColumnProjection, r as authUsersColumnSql, t as AUTH_USERS_COLUMNS, v as getJunctionCollectionConfig, w as findAnonymousGrants, x as getEffectiveSecurityRules, y as getJunctionSecurityRules, z as generateForeignKeyName } from "./auth-users-columns-BQttUZE2.js";
|
|
6
|
+
import { c as isPostgresCollectionConfig, l as isRelationalCollectionConfig, n as REBASE_SCHEMA, o as usesLegacyRlsFunctions } from "./src-C_wvdMnl.js";
|
|
7
|
+
import { t as ANONYMOUS_USER_ID } from "./policy-CPkCqVTz.js";
|
|
8
|
+
import { t as createPostgresWebSocket } from "./websocket-D0TBU3ia.js";
|
|
9
|
+
import { A as parsePgToolMajor, C as checkToolServerCompatibility, D as parseBackupDestination, E as joinStorageKey, M as serverVersionNumToMajor, N as splitGlobalsStatements, O as parseBackupTimestamp, P as withDatabaseName, S as buildRowSecurityPgOptions, T as globalsFileForDump, _ as buildBackupFilename, a as detectToolMajor, b as buildPgRestoreArgs, c as listBackups, d as resolvePgBinary, f as restoreDump, g as selectBackupsToPrune, h as require_source, i as createDump, j as resolveConnectionString, k as parseDbNameFromUrl, l as preflight, m as validateDump, n as applyGlobals, o as ensureDatabaseExists, p as uploadBackup, s as getServerVersionMajor, t as BackupToolError, u as pruneBackups, v as buildPgDumpArgs, w as diagnoseRowSecurityDumpFailure, x as buildPgRestoreListArgs, y as buildPgDumpallGlobalsArgs } from "./backup-service-Bww-Lg0s.js";
|
|
10
|
+
import { t as RLS_BOOTSTRAP_STATEMENTS } from "./rls-bootstrap-sql-Bpv3nUZo.js";
|
|
10
11
|
import { Client, Pool } from "pg";
|
|
11
12
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
12
13
|
import { ApiError, createEmailService, loadCollectionsFromDirectory, logger } from "@rebasepro/server";
|
|
@@ -133,6 +134,142 @@ var buildPropertyCallbacks = (properties) => {
|
|
|
133
134
|
return Object.keys(propertyCallbacks).length > 0 ? propertyCallbacks : void 0;
|
|
134
135
|
};
|
|
135
136
|
//#endregion
|
|
137
|
+
//#region ../common/src/util/internal-tables.ts
|
|
138
|
+
/**
|
|
139
|
+
* The tables Rebase creates for its own bookkeeping, and the SQL that keeps the
|
|
140
|
+
* end-user role away from them.
|
|
141
|
+
*
|
|
142
|
+
* ## Why this exists
|
|
143
|
+
*
|
|
144
|
+
* Authenticated requests run as {@link REBASE_USER_ROLE}, and the boot-time role
|
|
145
|
+
* provisioning grants that role `SELECT, INSERT, UPDATE, DELETE` on every table
|
|
146
|
+
* in the schemas a project uses — including `rebase`, because a project's own
|
|
147
|
+
* collections are allowed to live there (the scaffold puts `users` there). It
|
|
148
|
+
* also sets `ALTER DEFAULT PRIVILEGES`, so a table created *later* by the
|
|
149
|
+
* migrating role inherits the same grant.
|
|
150
|
+
*
|
|
151
|
+
* Every framework-internal table is created later: auth's tables come up during
|
|
152
|
+
* `initializeAuth`, `api_keys` during route mounting, `cron_logs` when the first
|
|
153
|
+
* job registers, `idempotency_keys` on the first request that carries a key. So
|
|
154
|
+
* they all inherited full DML for the end-user role — and none of them enables
|
|
155
|
+
* row-level security, because none of them is a collection with
|
|
156
|
+
* `securityRules`. Measured on a freshly provisioned database, `SET ROLE
|
|
157
|
+
* rebase_user` could read `rebase.refresh_tokens` (session token hashes),
|
|
158
|
+
* `rebase.mfa_factors` (`secret_encrypted`), `rebase.recovery_codes`, and
|
|
159
|
+
* `rebase.api_keys` (including its `admin` flag), and insert into
|
|
160
|
+
* `rebase.app_config`.
|
|
161
|
+
*
|
|
162
|
+
* Nothing routes a user-context query at those tables today, so this was not
|
|
163
|
+
* reachable over the API. That is the wrong thing to depend on: the documented
|
|
164
|
+
* model is that RLS is the authorization boundary, and these tables sat outside
|
|
165
|
+
* it. The boundary is now a privilege boundary instead — the role simply cannot
|
|
166
|
+
* address them.
|
|
167
|
+
*
|
|
168
|
+
* ## Why REVOKE rather than ENABLE ROW LEVEL SECURITY
|
|
169
|
+
*
|
|
170
|
+
* RLS with no policy denies every row, which is the same outcome, but it is the
|
|
171
|
+
* *weaker* statement: it leaves the grant in place, so a later policy — or a
|
|
172
|
+
* `FORCE` flag cleared by some future migration — reopens the table. There is no
|
|
173
|
+
* row of `refresh_tokens` any end user should ever reach, so the honest encoding
|
|
174
|
+
* is "this role has no privilege here at all". It also keeps the owner
|
|
175
|
+
* connection (which auth actually runs on) completely unaffected.
|
|
176
|
+
*
|
|
177
|
+
* ## Keeping it true
|
|
178
|
+
*
|
|
179
|
+
* `packages/rls-check` scans the `rebase` schema — it used to skip it as a
|
|
180
|
+
* "platform" schema — and its `rls-disabled` check fires on exactly the
|
|
181
|
+
* condition this module removes: RLS off *and* a DML grant to a reachable role.
|
|
182
|
+
* So a table added here without a revoke is caught by `pnpm rls:check`, not by
|
|
183
|
+
* someone re-reading this file.
|
|
184
|
+
*/
|
|
185
|
+
/**
|
|
186
|
+
* The Postgres role authenticated requests run as.
|
|
187
|
+
*
|
|
188
|
+
* Defined here rather than in the Postgres driver because both the driver (which
|
|
189
|
+
* provisions the role) and this module (which revokes on its behalf) need it,
|
|
190
|
+
* and a second spelling of a role name is a silent no-op waiting to happen.
|
|
191
|
+
*/
|
|
192
|
+
var REBASE_USER_ROLE = "rebase_user";
|
|
193
|
+
/**
|
|
194
|
+
* Framework-internal table names, unqualified.
|
|
195
|
+
*
|
|
196
|
+
* Deliberately NOT including `users`: the auth user table is also a collection,
|
|
197
|
+
* with `securityRules`, RLS enabled and policies applied. Users read their own
|
|
198
|
+
* row through it — revoking there would break sign-in.
|
|
199
|
+
*
|
|
200
|
+
* `atlas_schema_revisions` is Atlas's migration ledger, which lands in `rebase`
|
|
201
|
+
* because `db migrate apply` passes `--revisions-schema rebase`.
|
|
202
|
+
*/
|
|
203
|
+
var REBASE_INTERNAL_TABLES = [
|
|
204
|
+
"user_identities",
|
|
205
|
+
"refresh_tokens",
|
|
206
|
+
"password_reset_tokens",
|
|
207
|
+
"magic_link_tokens",
|
|
208
|
+
"mfa_factors",
|
|
209
|
+
"mfa_challenges",
|
|
210
|
+
"recovery_codes",
|
|
211
|
+
"app_config",
|
|
212
|
+
"schema_meta",
|
|
213
|
+
"api_keys",
|
|
214
|
+
"cron_logs",
|
|
215
|
+
"cron_claims",
|
|
216
|
+
"idempotency_keys",
|
|
217
|
+
"entity_history",
|
|
218
|
+
"branches",
|
|
219
|
+
"channel_messages",
|
|
220
|
+
"channel_cursors",
|
|
221
|
+
"channel_presence",
|
|
222
|
+
"atlas_schema_revisions"
|
|
223
|
+
];
|
|
224
|
+
/** Postgres identifiers this module is willing to interpolate. */
|
|
225
|
+
var SAFE_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
226
|
+
/**
|
|
227
|
+
* A single statement that takes every privilege on `schema.table` away from the
|
|
228
|
+
* end-user role.
|
|
229
|
+
*
|
|
230
|
+
* Wrapped in a `DO` block guarded on `pg_roles` for two reasons, both of which
|
|
231
|
+
* happen in practice:
|
|
232
|
+
*
|
|
233
|
+
* - the role does not exist when the connection is unprivileged (Rebase then
|
|
234
|
+
* relies on native RLS rather than a role switch), and a bare `REVOKE` on a
|
|
235
|
+
* missing role is an error, not a no-op;
|
|
236
|
+
* - the table may not exist yet — `cron_logs` never appears in a project with
|
|
237
|
+
* no cron jobs — and `to_regclass` returning NULL has to be tolerated too.
|
|
238
|
+
*
|
|
239
|
+
* One command, so it is safe on handles that speak the extended query protocol
|
|
240
|
+
* and reject multi-statement strings.
|
|
241
|
+
*/
|
|
242
|
+
function revokeInternalTableSql(schema, table) {
|
|
243
|
+
if (!SAFE_IDENTIFIER.test(schema)) throw new Error(`Refusing to build SQL with an unsafe schema name: ${JSON.stringify(schema)}`);
|
|
244
|
+
if (!SAFE_IDENTIFIER.test(table)) throw new Error(`Refusing to build SQL with an unsafe table name: ${JSON.stringify(table)}`);
|
|
245
|
+
const qualified = `"${schema}"."${table}"`;
|
|
246
|
+
return `
|
|
247
|
+
DO $rebase_revoke$
|
|
248
|
+
BEGIN
|
|
249
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '${REBASE_USER_ROLE}')
|
|
250
|
+
AND to_regclass('${qualified}') IS NOT NULL THEN
|
|
251
|
+
EXECUTE 'REVOKE ALL ON ${qualified} FROM ${REBASE_USER_ROLE}';
|
|
252
|
+
END IF;
|
|
253
|
+
END
|
|
254
|
+
$rebase_revoke$;
|
|
255
|
+
`.trim();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Revoke on every internal table in `schema`, one statement at a time.
|
|
259
|
+
*
|
|
260
|
+
* Best-effort per table: a connection that does not own one of them (a
|
|
261
|
+
* pre-provisioned database, a platform-managed ledger) cannot revoke on it, and
|
|
262
|
+
* that must not take down a boot. The caller decides how loud to be — `onError`
|
|
263
|
+
* exists so the driver can warn without this module importing a logger.
|
|
264
|
+
*/
|
|
265
|
+
async function revokeInternalTableAccess(execute, schema, options) {
|
|
266
|
+
for (const table of options?.tables ?? REBASE_INTERNAL_TABLES) try {
|
|
267
|
+
await execute(revokeInternalTableSql(schema, table));
|
|
268
|
+
} catch (error) {
|
|
269
|
+
options?.onError?.(table, error);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
//#endregion
|
|
136
273
|
//#region ../common/src/table-classification.ts
|
|
137
274
|
/** Schemas that are always considered Rebase-internal. */
|
|
138
275
|
var REBASE_INTERNAL_SCHEMAS = ["rebase", "auth"];
|
|
@@ -417,6 +554,12 @@ function deriveRowAddress(row, collection, registry) {
|
|
|
417
554
|
//#endregion
|
|
418
555
|
//#region src/utils/drizzle-conditions.ts
|
|
419
556
|
/**
|
|
557
|
+
* Postgres's own default for `pg_trgm.word_similarity_threshold`. Named here
|
|
558
|
+
* because the fuzzy predicate has to know when the index-backed operator agrees
|
|
559
|
+
* with the collection's declared threshold and when it would narrow too far.
|
|
560
|
+
*/
|
|
561
|
+
var PG_TRGM_WORD_SIMILARITY_DEFAULT = .6;
|
|
562
|
+
/**
|
|
420
563
|
* Process-wide default, set once when the driver is constructed.
|
|
421
564
|
*
|
|
422
565
|
* The condition builder is a set of *static* methods reached from a dozen
|
|
@@ -474,7 +617,7 @@ function toMembershipList(value) {
|
|
|
474
617
|
* @example
|
|
475
618
|
* const builder: ConditionBuilderStatic<SQL> = DrizzleConditionBuilder;
|
|
476
619
|
*/
|
|
477
|
-
var DrizzleConditionBuilder = class {
|
|
620
|
+
var DrizzleConditionBuilder = class DrizzleConditionBuilder {
|
|
478
621
|
/**
|
|
479
622
|
* Express "reachable from this parent through this relation" as a plain
|
|
480
623
|
* `WHERE` condition on the target table.
|
|
@@ -1100,10 +1243,28 @@ var DrizzleConditionBuilder = class {
|
|
|
1100
1243
|
return or(...conditions);
|
|
1101
1244
|
}
|
|
1102
1245
|
/**
|
|
1103
|
-
* Build search conditions for text fields
|
|
1246
|
+
* Build search conditions for text fields.
|
|
1247
|
+
*
|
|
1248
|
+
* Two shapes, chosen by whether the collection declared a `search` block:
|
|
1249
|
+
*
|
|
1250
|
+
* - **Declared** — one `@@ websearch_to_tsquery` against the generated
|
|
1251
|
+
* `tsvector` column. Stems, drops stopwords, AND-es the terms, reaches
|
|
1252
|
+
* inside JSONB and arrays, and uses the GIN index.
|
|
1253
|
+
* - **Not declared** — the original `ILIKE '%term%'` OR-ed across top-level
|
|
1254
|
+
* string properties, unchanged.
|
|
1255
|
+
*
|
|
1256
|
+
* The second is the default and stays the default. A collection that has
|
|
1257
|
+
* not opted in compiles to exactly the SQL it compiled to before this
|
|
1258
|
+
* branch existed, which is the only reason it is safe to have added it.
|
|
1259
|
+
*
|
|
1260
|
+
* `collection` is optional so that the callers which genuinely have no
|
|
1261
|
+
* collection in hand — nested paths, derived views — keep working; without
|
|
1262
|
+
* one there is no `search` block to read and the ILIKE path is correct.
|
|
1104
1263
|
*/
|
|
1105
|
-
static buildSearchConditions(searchString, properties, table) {
|
|
1264
|
+
static buildSearchConditions(searchString, properties, table, collection) {
|
|
1106
1265
|
const searchConditions = [];
|
|
1266
|
+
const ftsCondition = collection ? DrizzleConditionBuilder.buildFullTextCondition(searchString, table, collection) : void 0;
|
|
1267
|
+
if (ftsCondition) return [ftsCondition];
|
|
1107
1268
|
for (const [key, prop] of Object.entries(properties)) {
|
|
1108
1269
|
const p = prop;
|
|
1109
1270
|
if (p.type === "string" && !p.enum && p.isId !== "uuid") {
|
|
@@ -1116,6 +1277,117 @@ var DrizzleConditionBuilder = class {
|
|
|
1116
1277
|
return searchConditions;
|
|
1117
1278
|
}
|
|
1118
1279
|
/**
|
|
1280
|
+
* The `@@` predicate for a collection that declared a `search` block, or
|
|
1281
|
+
* undefined for one that did not.
|
|
1282
|
+
*
|
|
1283
|
+
* The query is normalized exactly as the indexed content was — same text
|
|
1284
|
+
* search configuration, same accent folding. Skipping that on the query
|
|
1285
|
+
* side is the subtle way to get a search that matches nothing: the column
|
|
1286
|
+
* would hold `gestion` while the query asked for `gestión`.
|
|
1287
|
+
*
|
|
1288
|
+
* `websearch_to_tsquery` rather than `plainto_tsquery` because it is the
|
|
1289
|
+
* one that behaves the way a search box looks like it should — quoted
|
|
1290
|
+
* phrases, `or`, and a leading `-` to exclude — and because it never throws
|
|
1291
|
+
* on user input, which `to_tsquery` does on so much as a stray parenthesis.
|
|
1292
|
+
*/
|
|
1293
|
+
static buildFullTextCondition(searchString, table, collection) {
|
|
1294
|
+
let spec;
|
|
1295
|
+
try {
|
|
1296
|
+
spec = buildSearchColumnSpec(collection);
|
|
1297
|
+
} catch {
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
if (!spec) return void 0;
|
|
1301
|
+
const column = table[spec.column];
|
|
1302
|
+
if (!column) return;
|
|
1303
|
+
const exact = sql`${column} @@ ${DrizzleConditionBuilder.normalizedTsQuery(searchString, spec)}`;
|
|
1304
|
+
if (!spec.fuzzy) return exact;
|
|
1305
|
+
const fuzzyColumn = table[spec.fuzzy.column];
|
|
1306
|
+
if (!fuzzyColumn) return exact;
|
|
1307
|
+
const needle = spec.unaccent ? sql`${sql.raw(SEARCH_UNACCENT_FN)}(${searchString})` : sql`${searchString}`;
|
|
1308
|
+
const similar = sql`public.word_similarity(${needle}, ${fuzzyColumn}) >= ${spec.fuzzy.threshold}`;
|
|
1309
|
+
return sql`(${exact} OR ${spec.fuzzy.threshold > PG_TRGM_WORD_SIMILARITY_DEFAULT ? sql`(${needle} OPERATOR(public.<%) ${fuzzyColumn} AND ${similar})` : similar})`;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* `websearch_to_tsquery(<config>, <normalized search string>)`.
|
|
1313
|
+
*
|
|
1314
|
+
* Split out because the ranking expression needs the identical query — a
|
|
1315
|
+
* row ranked against a different tsquery than it was matched against is a
|
|
1316
|
+
* ranking of something else.
|
|
1317
|
+
*/
|
|
1318
|
+
static normalizedTsQuery(searchString, spec) {
|
|
1319
|
+
const normalized = spec.unaccent ? sql`${sql.raw(SEARCH_UNACCENT_FN)}(${searchString})` : sql`${searchString}`;
|
|
1320
|
+
return sql`websearch_to_tsquery(${spec.language}, ${normalized})`;
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* A JSONB array of `{ field, snippet }` naming which declared fields matched
|
|
1324
|
+
* and showing the text around each hit — what backs `_matches`.
|
|
1325
|
+
*
|
|
1326
|
+
* A ranked list answers "which rows", never "why this row". For a talent
|
|
1327
|
+
* pool that difference is the product: a candidate surfacing for
|
|
1328
|
+
* "iso 14001" on a *certification* is a different candidate from one whose
|
|
1329
|
+
* bio happens to mention the standard, and the score cannot tell them apart.
|
|
1330
|
+
*
|
|
1331
|
+
* Built as a correlated subquery over a `VALUES` list of the declared
|
|
1332
|
+
* fields, rather than one `CASE` per field, so the shape does not change
|
|
1333
|
+
* with the number of fields and the empty result is a plain `[]`.
|
|
1334
|
+
*
|
|
1335
|
+
* `ts_headline` runs over the same normalized text that was indexed. Over
|
|
1336
|
+
* the *original* text it would find nothing to mark whenever `unaccent` is
|
|
1337
|
+
* on — the query's lexemes are folded and the document's are not — and
|
|
1338
|
+
* would return the text silently unhighlighted. Folded-but-marked beats
|
|
1339
|
+
* pretty-but-inert.
|
|
1340
|
+
*
|
|
1341
|
+
* Undefined when the collection has not opted in, when the column is not on
|
|
1342
|
+
* the table yet, or when the caller did not ask: this costs a `ts_headline`
|
|
1343
|
+
* per field per row and `ts_headline` re-parses the document.
|
|
1344
|
+
*/
|
|
1345
|
+
static buildSearchMatchesExpression(searchString, table, collection) {
|
|
1346
|
+
let spec;
|
|
1347
|
+
try {
|
|
1348
|
+
spec = buildSearchColumnSpec(collection);
|
|
1349
|
+
} catch {
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
if (!spec || spec.fields.length === 0) return void 0;
|
|
1353
|
+
if (!table[spec.column]) return void 0;
|
|
1354
|
+
const query = DrizzleConditionBuilder.normalizedTsQuery(searchString, spec);
|
|
1355
|
+
const config = sql`${spec.language}`;
|
|
1356
|
+
const rows = spec.fields.map((f, i) => sql`(${i}, ${f.path}, ${sql.raw(f.textSql)})`);
|
|
1357
|
+
return sql`(
|
|
1358
|
+
SELECT coalesce(jsonb_agg(s.m ORDER BY f.ord), '[]'::jsonb)
|
|
1359
|
+
FROM (VALUES ${sql.join(rows, sql`, `)}) AS f(ord, path, txt)
|
|
1360
|
+
CROSS JOIN LATERAL (
|
|
1361
|
+
SELECT jsonb_build_object(
|
|
1362
|
+
'field', f.path,
|
|
1363
|
+
'snippet', ts_headline(${config}::regconfig, f.txt, ${query},
|
|
1364
|
+
'StartSel=<mark>,StopSel=</mark>,MaxWords=14,MinWords=1,MaxFragments=1,FragmentDelimiter= … ')
|
|
1365
|
+
) AS m
|
|
1366
|
+
WHERE to_tsvector(${config}::regconfig, f.txt) @@ ${query}
|
|
1367
|
+
) s
|
|
1368
|
+
)`;
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* `ts_rank(<column>, <query>)` for the collection, or undefined when it has
|
|
1372
|
+
* not opted in. This is what backs `orderBy: ["_score", "desc"]`.
|
|
1373
|
+
*/
|
|
1374
|
+
static buildSearchRankExpression(searchString, table, collection) {
|
|
1375
|
+
let spec;
|
|
1376
|
+
try {
|
|
1377
|
+
spec = buildSearchColumnSpec(collection);
|
|
1378
|
+
} catch {
|
|
1379
|
+
return;
|
|
1380
|
+
}
|
|
1381
|
+
if (!spec) return void 0;
|
|
1382
|
+
const column = table[spec.column];
|
|
1383
|
+
if (!column) return void 0;
|
|
1384
|
+
const rank = sql`ts_rank(${column}, ${DrizzleConditionBuilder.normalizedTsQuery(searchString, spec)})`;
|
|
1385
|
+
if (!spec.fuzzy) return rank;
|
|
1386
|
+
const fuzzyColumn = table[spec.fuzzy.column];
|
|
1387
|
+
if (!fuzzyColumn) return rank;
|
|
1388
|
+
return sql`(${rank} + public.word_similarity(${spec.unaccent ? sql`${sql.raw(SEARCH_UNACCENT_FN)}(${searchString})` : sql`${searchString}`}, ${fuzzyColumn}))`;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1119
1391
|
* Build a unique field check condition
|
|
1120
1392
|
*/
|
|
1121
1393
|
static buildUniqueFieldCondition(fieldColumn, value, idColumn, excludeId) {
|
|
@@ -1656,6 +1928,18 @@ function parsePropertyFromServer(value, property, collection, propertyKey) {
|
|
|
1656
1928
|
* from the result (used by `normalizeDbValues` where
|
|
1657
1929
|
* Drizzle's relational API already hydrates them).
|
|
1658
1930
|
*/
|
|
1931
|
+
/**
|
|
1932
|
+
* Keys a query computes and attaches to a row, rather than reads from a column.
|
|
1933
|
+
*
|
|
1934
|
+
* An explicit set rather than a `_` prefix rule: a user's column may perfectly
|
|
1935
|
+
* well be called `_internal`, and passing it through here would put a value on
|
|
1936
|
+
* the row that no property describes and nothing downstream knows how to type.
|
|
1937
|
+
*/
|
|
1938
|
+
var QUERY_METADATA_KEYS = /* @__PURE__ */ new Set([
|
|
1939
|
+
"_score",
|
|
1940
|
+
"_distance",
|
|
1941
|
+
"_matches"
|
|
1942
|
+
]);
|
|
1659
1943
|
function normalizeScalarValues(data, properties, collection, resolvedRelations, options) {
|
|
1660
1944
|
const result = {};
|
|
1661
1945
|
const internalFKColumns = /* @__PURE__ */ new Set();
|
|
@@ -1667,6 +1951,10 @@ function normalizeScalarValues(data, properties, collection, resolvedRelations,
|
|
|
1667
1951
|
result[key] = value === null ? null : typeof value === "number" ? value : String(value);
|
|
1668
1952
|
continue;
|
|
1669
1953
|
}
|
|
1954
|
+
if (QUERY_METADATA_KEYS.has(key)) {
|
|
1955
|
+
result[key] = value;
|
|
1956
|
+
continue;
|
|
1957
|
+
}
|
|
1670
1958
|
const property = properties[key];
|
|
1671
1959
|
if (!property) continue;
|
|
1672
1960
|
if (options.skipRelations && property.type === "relation") continue;
|
|
@@ -1919,7 +2207,7 @@ var RelationService = class {
|
|
|
1919
2207
|
let query = this.db.select().from(targetTable).$dynamic();
|
|
1920
2208
|
const additionalFilters = [];
|
|
1921
2209
|
if (options.searchString) {
|
|
1922
|
-
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, targetCollection.properties, targetTable);
|
|
2210
|
+
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, targetCollection.properties, targetTable, targetCollection);
|
|
1923
2211
|
if (searchConditions.length === 0) return [];
|
|
1924
2212
|
const searchCombined = DrizzleConditionBuilder.combineConditionsWithOr(searchConditions);
|
|
1925
2213
|
if (searchCombined) additionalFilters.push(searchCombined);
|
|
@@ -3025,7 +3313,7 @@ function sanitizeErrorForClient(error, context) {
|
|
|
3025
3313
|
* Service for handling all row read operations.
|
|
3026
3314
|
* Handles fetching, searching, counting, and filtering rows.
|
|
3027
3315
|
*/
|
|
3028
|
-
var FetchService = class {
|
|
3316
|
+
var FetchService = class FetchService {
|
|
3029
3317
|
db;
|
|
3030
3318
|
registry;
|
|
3031
3319
|
relationService;
|
|
@@ -3098,6 +3386,24 @@ var FetchService = class {
|
|
|
3098
3386
|
* and skips rows rather than erroring. The guesses stay, last, for a
|
|
3099
3387
|
* caller that hands over no collection to resolve against.
|
|
3100
3388
|
*/
|
|
3389
|
+
/**
|
|
3390
|
+
* The ORDER BY target, which may be relevance rather than a column.
|
|
3391
|
+
*
|
|
3392
|
+
* `_score` is only meaningful for a collection that declared a `search`
|
|
3393
|
+
* block *and* for a request that carried a search string — ranking rows
|
|
3394
|
+
* against no query ranks them all at zero. Outside those two conditions it
|
|
3395
|
+
* is an unknown field and gets the same 400 as any other typo, which is the
|
|
3396
|
+
* behaviour that matters: a sort that is silently dropped returns 200 with
|
|
3397
|
+
* rows in arbitrary order, and paging over that repeats and skips rows.
|
|
3398
|
+
*/
|
|
3399
|
+
static SCORE_FIELD = "_score";
|
|
3400
|
+
resolveOrderTarget(table, orderBy, collection, searchString) {
|
|
3401
|
+
if (orderBy === FetchService.SCORE_FIELD && collection && searchString) {
|
|
3402
|
+
const rank = DrizzleConditionBuilder.buildSearchRankExpression(searchString, table, collection);
|
|
3403
|
+
if (rank) return rank;
|
|
3404
|
+
}
|
|
3405
|
+
return this.resolveOrderByField(table, orderBy, collection);
|
|
3406
|
+
}
|
|
3101
3407
|
resolveOrderByField(table, orderBy, collection) {
|
|
3102
3408
|
const columnAt = (key) => (key in table ? table[key] : void 0) || void 0;
|
|
3103
3409
|
const direct = columnAt(orderBy);
|
|
@@ -3225,12 +3531,14 @@ var FetchService = class {
|
|
|
3225
3531
|
*/
|
|
3226
3532
|
buildDrizzleQueryOptions(table, idField, idInfo, options, collectionPath, withConfig, scopeCondition) {
|
|
3227
3533
|
const queryOpts = {};
|
|
3534
|
+
const hidden = hiddenColumnsOption(getTableColumns(table), this.registry.getCollectionByPath(collectionPath) ?? void 0);
|
|
3535
|
+
if (hidden) queryOpts.columns = hidden;
|
|
3228
3536
|
if (withConfig) queryOpts.with = withConfig;
|
|
3229
3537
|
const allConditions = [];
|
|
3230
3538
|
if (scopeCondition) allConditions.push(scopeCondition);
|
|
3231
3539
|
if (options.searchString) {
|
|
3232
3540
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
3233
|
-
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table);
|
|
3541
|
+
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table, collection);
|
|
3234
3542
|
if (searchConditions.length === 0) {
|
|
3235
3543
|
queryOpts.where = and(eq(idField, -99999999));
|
|
3236
3544
|
return queryOpts;
|
|
@@ -3253,7 +3561,7 @@ var FetchService = class {
|
|
|
3253
3561
|
const orderExpressions = [];
|
|
3254
3562
|
if (options.orderBy) {
|
|
3255
3563
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
3256
|
-
const orderByField = this.
|
|
3564
|
+
const orderByField = this.resolveOrderTarget(table, options.orderBy, collection, options.searchString);
|
|
3257
3565
|
if (orderByField) orderExpressions.push(options.order === "asc" ? asc(orderByField) : desc(orderByField));
|
|
3258
3566
|
}
|
|
3259
3567
|
orderExpressions.push(desc(idField));
|
|
@@ -3270,6 +3578,7 @@ var FetchService = class {
|
|
|
3270
3578
|
if (!options.startAfter) return [];
|
|
3271
3579
|
const cursor = options.startAfter;
|
|
3272
3580
|
if (options.orderBy) {
|
|
3581
|
+
if (options.orderBy === FetchService.SCORE_FIELD) throw ApiError.badRequest("Cursor pagination (`startAfter`) cannot be combined with `orderBy: \"_score\"`. Relevance is computed per query rather than stored, so it cannot key a cursor. Use `limit`/`offset` for relevance-ordered pages, or order by a column.", "SCORE_CURSOR_UNSUPPORTED", { field: FetchService.SCORE_FIELD });
|
|
3273
3582
|
const collection = collectionPath ? getCollectionByPath(collectionPath, this.registry) : void 0;
|
|
3274
3583
|
const orderByField = this.resolveOrderByField(table, options.orderBy, collection);
|
|
3275
3584
|
if (orderByField) {
|
|
@@ -3340,9 +3649,11 @@ var FetchService = class {
|
|
|
3340
3649
|
const qb = this.getQueryBuilder(tableName);
|
|
3341
3650
|
if (qb) try {
|
|
3342
3651
|
const withConfig = this.buildWithConfig(collection);
|
|
3652
|
+
const hidden = hiddenColumnsOption(getTableColumns(table), collection);
|
|
3343
3653
|
const row = await qb.findFirst({
|
|
3344
3654
|
where: eq(idField, parsedId),
|
|
3345
|
-
with: withConfig
|
|
3655
|
+
with: withConfig,
|
|
3656
|
+
...hidden ? { columns: hidden } : {}
|
|
3346
3657
|
});
|
|
3347
3658
|
if (!row) return void 0;
|
|
3348
3659
|
const flatRow = toFlatRow(row, collection, this.registry);
|
|
@@ -3356,7 +3667,8 @@ var FetchService = class {
|
|
|
3356
3667
|
if (reachedDatabase(e)) throw e;
|
|
3357
3668
|
logger.warn(`[FetchService] db.query.findFirst failed for ${collectionPath}, falling back to db.select`, { error: e });
|
|
3358
3669
|
}
|
|
3359
|
-
const
|
|
3670
|
+
const visibleOne = visibleColumnProjection(getTableColumns(table), collection);
|
|
3671
|
+
const result = await this.db.select(visibleOne).from(table).where(eq(idField, parsedId)).limit(1);
|
|
3360
3672
|
if (result.length === 0) return void 0;
|
|
3361
3673
|
const raw = result[0];
|
|
3362
3674
|
const values = await parseDataFromServer(raw, collection, this.db, this.registry);
|
|
@@ -3412,14 +3724,21 @@ var FetchService = class {
|
|
|
3412
3724
|
}
|
|
3413
3725
|
let vectorMeta;
|
|
3414
3726
|
if (options.vectorSearch) vectorMeta = DrizzleConditionBuilder.buildVectorSearchConditions(table, options.vectorSearch);
|
|
3727
|
+
const visible = visibleColumnProjection(getTableColumns(table), collection);
|
|
3728
|
+
const rankSelect = options.searchString ? DrizzleConditionBuilder.buildSearchRankExpression(options.searchString, table, collection) : void 0;
|
|
3729
|
+
const matchesSelect = options.searchString && options.searchExplain ? DrizzleConditionBuilder.buildSearchMatchesExpression(options.searchString, table, collection) : void 0;
|
|
3415
3730
|
let query = vectorMeta ? this.db.select({
|
|
3416
|
-
table_row: table,
|
|
3731
|
+
table_row: visible ?? table,
|
|
3417
3732
|
_distance: vectorMeta.distanceSelect
|
|
3418
|
-
}).from(table).$dynamic() : this.db.select(
|
|
3733
|
+
}).from(table).$dynamic() : rankSelect ? this.db.select({
|
|
3734
|
+
table_row: visible ?? table,
|
|
3735
|
+
_score: rankSelect,
|
|
3736
|
+
...matchesSelect ? { _matches: matchesSelect } : {}
|
|
3737
|
+
}).from(table).$dynamic() : visible ? this.db.select(visible).from(table).$dynamic() : this.db.select().from(table).$dynamic();
|
|
3419
3738
|
const allConditions = [];
|
|
3420
3739
|
if (scopeCondition) allConditions.push(scopeCondition);
|
|
3421
3740
|
if (options.searchString) {
|
|
3422
|
-
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table);
|
|
3741
|
+
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table, collection);
|
|
3423
3742
|
if (searchConditions.length === 0) return [];
|
|
3424
3743
|
allConditions.push(DrizzleConditionBuilder.combineConditionsWithOr(searchConditions));
|
|
3425
3744
|
}
|
|
@@ -3439,7 +3758,7 @@ var FetchService = class {
|
|
|
3439
3758
|
const orderExpressions = [];
|
|
3440
3759
|
if (vectorMeta) orderExpressions.push(asc(vectorMeta.orderBy));
|
|
3441
3760
|
else if (options.orderBy) {
|
|
3442
|
-
const orderByField = this.
|
|
3761
|
+
const orderByField = this.resolveOrderTarget(table, options.orderBy, collection, options.searchString);
|
|
3443
3762
|
if (orderByField) orderExpressions.push(options.order === "asc" ? asc(orderByField) : desc(orderByField));
|
|
3444
3763
|
}
|
|
3445
3764
|
orderExpressions.push(desc(idField));
|
|
@@ -3459,6 +3778,10 @@ var FetchService = class {
|
|
|
3459
3778
|
const results = vectorMeta ? rawResults.map((r) => ({
|
|
3460
3779
|
...r.table_row,
|
|
3461
3780
|
_distance: typeof r._distance === "number" ? r._distance : parseFloat(String(r._distance))
|
|
3781
|
+
})) : rankSelect ? rawResults.map((r) => ({
|
|
3782
|
+
...r.table_row,
|
|
3783
|
+
_score: typeof r._score === "number" ? r._score : parseFloat(String(r._score)),
|
|
3784
|
+
...matchesSelect ? { _matches: r._matches ?? [] } : {}
|
|
3462
3785
|
})) : rawResults;
|
|
3463
3786
|
return this.processRowResults(results, collection, collectionPath, idInfo, options.databaseId, false, idInfoArray);
|
|
3464
3787
|
}
|
|
@@ -3550,7 +3873,7 @@ var FetchService = class {
|
|
|
3550
3873
|
const allConditions = [];
|
|
3551
3874
|
if (hop) allConditions.push(this.buildRelationScope(hop));
|
|
3552
3875
|
if (options.searchString) {
|
|
3553
|
-
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table);
|
|
3876
|
+
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table, collection);
|
|
3554
3877
|
if (searchConditions.length === 0) return 0;
|
|
3555
3878
|
allConditions.push(DrizzleConditionBuilder.combineConditionsWithOr(searchConditions));
|
|
3556
3879
|
}
|
|
@@ -3695,7 +4018,8 @@ var FetchService = class {
|
|
|
3695
4018
|
if (reachedDatabase(e)) throw e;
|
|
3696
4019
|
logger.warn(`[fetchOneForRest] db.query.findFirst failed for ${collectionPath}, falling back`, { error: e });
|
|
3697
4020
|
}
|
|
3698
|
-
const
|
|
4021
|
+
const visibleOne = visibleColumnProjection(getTableColumns(table), collection);
|
|
4022
|
+
const result = await this.db.select(visibleOne).from(table).where(eq(idField, parsedId)).limit(1);
|
|
3699
4023
|
if (result.length === 0) return null;
|
|
3700
4024
|
const flatEntity = { ...result[0] };
|
|
3701
4025
|
if (!include || include.length === 0) return flatEntity;
|
|
@@ -3733,14 +4057,21 @@ var FetchService = class {
|
|
|
3733
4057
|
const idField = table[requirePrimaryKeys(collection, this.registry)[0].fieldName];
|
|
3734
4058
|
let vectorMeta;
|
|
3735
4059
|
if (options.vectorSearch) vectorMeta = DrizzleConditionBuilder.buildVectorSearchConditions(table, options.vectorSearch);
|
|
4060
|
+
const visible = visibleColumnProjection(getTableColumns(table), collection);
|
|
4061
|
+
const rankSelect = options.searchString ? DrizzleConditionBuilder.buildSearchRankExpression(options.searchString, table, collection) : void 0;
|
|
4062
|
+
const matchesSelect = options.searchString && options.searchExplain ? DrizzleConditionBuilder.buildSearchMatchesExpression(options.searchString, table, collection) : void 0;
|
|
3736
4063
|
let query = vectorMeta ? this.db.select({
|
|
3737
|
-
table_row: table,
|
|
4064
|
+
table_row: visible ?? table,
|
|
3738
4065
|
_distance: vectorMeta.distanceSelect
|
|
3739
|
-
}).from(table).$dynamic() : this.db.select(
|
|
4066
|
+
}).from(table).$dynamic() : rankSelect ? this.db.select({
|
|
4067
|
+
table_row: visible ?? table,
|
|
4068
|
+
_score: rankSelect,
|
|
4069
|
+
...matchesSelect ? { _matches: matchesSelect } : {}
|
|
4070
|
+
}).from(table).$dynamic() : visible ? this.db.select(visible).from(table).$dynamic() : this.db.select().from(table).$dynamic();
|
|
3740
4071
|
const allConditions = [];
|
|
3741
4072
|
if (options.relatedTo) allConditions.push(this.buildRelationScope(options.relatedTo));
|
|
3742
4073
|
if (options.searchString) {
|
|
3743
|
-
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table);
|
|
4074
|
+
const searchConditions = DrizzleConditionBuilder.buildSearchConditions(options.searchString, collection.properties, table, collection);
|
|
3744
4075
|
if (searchConditions.length === 0) return [];
|
|
3745
4076
|
allConditions.push(DrizzleConditionBuilder.combineConditionsWithOr(searchConditions));
|
|
3746
4077
|
}
|
|
@@ -3756,7 +4087,7 @@ var FetchService = class {
|
|
|
3756
4087
|
const orderExpressions = [];
|
|
3757
4088
|
if (vectorMeta) orderExpressions.push(asc(vectorMeta.orderBy));
|
|
3758
4089
|
else if (options.orderBy) {
|
|
3759
|
-
const orderByField = this.
|
|
4090
|
+
const orderByField = this.resolveOrderTarget(table, options.orderBy, collection, options.searchString);
|
|
3760
4091
|
if (orderByField) orderExpressions.push(options.order === "asc" ? asc(orderByField) : desc(orderByField));
|
|
3761
4092
|
}
|
|
3762
4093
|
orderExpressions.push(desc(idField));
|
|
@@ -3769,6 +4100,11 @@ var FetchService = class {
|
|
|
3769
4100
|
...r.table_row,
|
|
3770
4101
|
_distance: typeof r._distance === "number" ? r._distance : parseFloat(String(r._distance))
|
|
3771
4102
|
}));
|
|
4103
|
+
if (rankSelect) return rawResults.map((r) => ({
|
|
4104
|
+
...r.table_row,
|
|
4105
|
+
_score: typeof r._score === "number" ? r._score : parseFloat(String(r._score)),
|
|
4106
|
+
...matchesSelect ? { _matches: r._matches ?? [] } : {}
|
|
4107
|
+
}));
|
|
3772
4108
|
return rawResults;
|
|
3773
4109
|
}
|
|
3774
4110
|
/**
|
|
@@ -4258,6 +4594,7 @@ var BranchService = class {
|
|
|
4258
4594
|
metadata JSONB DEFAULT '{}'
|
|
4259
4595
|
);
|
|
4260
4596
|
`));
|
|
4597
|
+
await this.db.execute(sql.raw(revokeInternalTableSql("rebase", "branches")));
|
|
4261
4598
|
}
|
|
4262
4599
|
/**
|
|
4263
4600
|
* Create a new branch database by templating the source database.
|
|
@@ -4365,43 +4702,49 @@ var BranchService = class {
|
|
|
4365
4702
|
};
|
|
4366
4703
|
//#endregion
|
|
4367
4704
|
//#region src/security/rls-enforcement.ts
|
|
4705
|
+
var quoteIdent$1 = (name) => `"${name.replace(/"/g, "\"\"")}"`;
|
|
4706
|
+
/** DML the user role holds on managed tables (RLS still filters per row). */
|
|
4707
|
+
var USER_TABLE_PRIVILEGES = "SELECT, INSERT, UPDATE, DELETE";
|
|
4368
4708
|
/**
|
|
4369
|
-
*
|
|
4370
|
-
*
|
|
4371
|
-
* Every operation runs in one of two contexts:
|
|
4709
|
+
* Warn when the connection role shares its name with an existing schema.
|
|
4372
4710
|
*
|
|
4373
|
-
*
|
|
4374
|
-
*
|
|
4375
|
-
*
|
|
4376
|
-
*
|
|
4377
|
-
* `securityRules` are the whole authorization model; app-layer callbacks
|
|
4378
|
-
* are validation/side-effects, not a security boundary.
|
|
4711
|
+
* Postgres resolves unqualified names through `search_path`, which defaults to
|
|
4712
|
+
* `"$user", public` — and `$user` is the connection ROLE. When a schema of that
|
|
4713
|
+
* name exists it sits ahead of `public`, so every unqualified statement
|
|
4714
|
+
* silently operates on it instead:
|
|
4379
4715
|
*
|
|
4380
|
-
*
|
|
4381
|
-
* background jobs, and the explicit `rebase.dataAsAdmin` accessor. As table
|
|
4382
|
-
* owner it bypasses RLS. This is the trusted plane, equivalent to
|
|
4383
|
-
* Supabase's `service_role`.
|
|
4716
|
+
* CREATE TABLE posts (...); -- you meant public.posts; you got <role>.posts
|
|
4384
4717
|
*
|
|
4385
|
-
*
|
|
4718
|
+
* Nothing errors. You get a second table of the same name in the wrong schema,
|
|
4719
|
+
* and reads that pin `public` cannot see it — which reads as "missing table" and
|
|
4720
|
+
* sends people to re-run a push that creates a *third* copy. The bootstrapper
|
|
4721
|
+
* has a whole branch dedicated to recognising the symptom after the fact.
|
|
4386
4722
|
*
|
|
4387
|
-
*
|
|
4388
|
-
*
|
|
4389
|
-
*
|
|
4390
|
-
*
|
|
4391
|
-
*
|
|
4392
|
-
*
|
|
4393
|
-
*
|
|
4394
|
-
* RLS binds. Transaction-scoped, so it composes with poolers.
|
|
4723
|
+
* Rebase shipped straight into this: it creates a schema named `rebase` while
|
|
4724
|
+
* every template named the database role `rebase` too. The scaffold uses
|
|
4725
|
+
* `rebase_app` now, and every pool Rebase opens pins `search_path=public`
|
|
4726
|
+
* (`pinSearchPath`), which covers the paths the framework controls. This covers
|
|
4727
|
+
* the ones it does not — `psql`, `pg_dump`, drizzle-kit, a colleague's script,
|
|
4728
|
+
* a hand-written migration — because the hazard is a property of the two NAMES,
|
|
4729
|
+
* not of any one connection.
|
|
4395
4730
|
*
|
|
4396
|
-
*
|
|
4397
|
-
*
|
|
4398
|
-
*
|
|
4731
|
+
* A warning rather than a boot failure: the database works, the framework's own
|
|
4732
|
+
* traffic is pinned, and refusing to start over a naming choice a user may have
|
|
4733
|
+
* inherited would be worse than the risk.
|
|
4399
4734
|
*/
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4735
|
+
async function warnOnRoleSchemaCollision(run) {
|
|
4736
|
+
try {
|
|
4737
|
+
const rows = await run(`
|
|
4738
|
+
SELECT current_user AS role,
|
|
4739
|
+
EXISTS (
|
|
4740
|
+
SELECT 1 FROM pg_namespace n WHERE n.nspname = current_user
|
|
4741
|
+
) AS collides
|
|
4742
|
+
`);
|
|
4743
|
+
if (rows[0]?.collides !== true) return;
|
|
4744
|
+
const role = String(rows[0]?.role ?? "the connection role");
|
|
4745
|
+
logger.warn(`⚠️ The database role "${role}" has the same name as a schema. Postgres resolves unqualified names through \`search_path\`, which defaults to \`"$user", public\` — so "${role}" is searched BEFORE public, and any unqualified \`CREATE TABLE\`/\`SELECT\` from a tool that does not pin the path (psql, pg_dump, drizzle-kit, a hand-written migration) silently lands in "${role}" instead. Rebase's own connections pin \`search_path=public\`, so the server is unaffected. To remove the hazard entirely, connect as a role whose name is not also a schema — the scaffold uses "rebase_app".`);
|
|
4746
|
+
} catch {}
|
|
4747
|
+
}
|
|
4405
4748
|
async function detectConnectionPosture(run) {
|
|
4406
4749
|
const row = (await run(`
|
|
4407
4750
|
SELECT current_user AS role,
|
|
@@ -4477,6 +4820,9 @@ async function ensureAppRole(run, schemas) {
|
|
|
4477
4820
|
await run(`GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA ${s} TO ${REBASE_USER_ROLE}`);
|
|
4478
4821
|
await run(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${s} GRANT ${USER_TABLE_PRIVILEGES} ON TABLES TO ${REBASE_USER_ROLE}`);
|
|
4479
4822
|
await run(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${s} GRANT USAGE, SELECT ON SEQUENCES TO ${REBASE_USER_ROLE}`);
|
|
4823
|
+
await revokeInternalTableAccess(async (text) => {
|
|
4824
|
+
await run(text);
|
|
4825
|
+
}, schema, { onError: (table, error) => logger.warn(`🔐 [rls] Could not revoke "${REBASE_USER_ROLE}" access to "${schema}"."${table}" — it stays reachable by authenticated requests: ` + (error instanceof Error ? error.message : String(error))) });
|
|
4480
4826
|
}
|
|
4481
4827
|
logger.info(`🔐 [rls] User role "${REBASE_USER_ROLE}" provisioned (schemas: ${uniqueSchemas.join(", ")})`);
|
|
4482
4828
|
}
|
|
@@ -4562,6 +4908,40 @@ function warnOnAnonymousGrants(collections) {
|
|
|
4562
4908
|
logger.warn(`Security rules that read as a lockdown but grant access to anonymous requests. Every caller from a client carries a user id ('${ANONYMOUS_USER_ID}' when nobody is signed in), so these clauses are true for everyone:\n\n` + problems.join("\n\n") + "\n");
|
|
4563
4909
|
}
|
|
4564
4910
|
/**
|
|
4911
|
+
* Name the collections whose raw policy SQL still calls the pre-1.0 helpers.
|
|
4912
|
+
*
|
|
4913
|
+
* The compiler rewrites `auth.uid()` to `rebase.uid()` on the way into the
|
|
4914
|
+
* database, so nothing is broken and no policy is wrong — which is exactly why
|
|
4915
|
+
* this has to be said out loud. A silent rewrite that works forever is not a
|
|
4916
|
+
* migration, it is a second supported spelling nobody wrote down, and the next
|
|
4917
|
+
* person to read those rules will copy the old one.
|
|
4918
|
+
*
|
|
4919
|
+
* Only `raw` expressions can carry it. Structured rules (`policy.authUid()`,
|
|
4920
|
+
* `policy.rolesOverlap(...)`) compile from the model and were never affected.
|
|
4921
|
+
*/
|
|
4922
|
+
function warnOnLegacyRlsFunctions(collections) {
|
|
4923
|
+
const sites = [];
|
|
4924
|
+
for (const collection of collections) for (const rule of collection.securityRules ?? []) {
|
|
4925
|
+
const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);
|
|
4926
|
+
if (![usingExpr, withCheckExpr].filter((e) => e !== null).some(containsLegacyRlsCall)) continue;
|
|
4927
|
+
const site = `${collection.slug ?? "(unnamed)"} → "${rule.name ?? "(unnamed rule)"}"`;
|
|
4928
|
+
if (!sites.includes(site)) sites.push(site);
|
|
4929
|
+
}
|
|
4930
|
+
if (sites.length === 0) return;
|
|
4931
|
+
logger.warn(`These security rules call the pre-1.0 RLS helpers (\`auth.uid()\`, \`auth.roles()\`, \`auth.jwt()\`). They still work — the compiler rewrites them — but the functions now live in the \`rebase\` schema, and the \`auth\` one is Supabase's. Update the raw SQL in these rules to \`${REBASE_SCHEMA}.uid()\` and friends, or switch them to the structured helpers (\`policy.authUid()\`, \`policy.rolesOverlap()\`), which never had to be spelled by hand:\n\n` + sites.map((s) => ` • ${s}`).join("\n") + "\n");
|
|
4932
|
+
}
|
|
4933
|
+
/** Whether any `raw` expression in the tree calls a pre-1.0 helper. */
|
|
4934
|
+
function containsLegacyRlsCall(expr) {
|
|
4935
|
+
switch (expr.kind) {
|
|
4936
|
+
case "raw": return usesLegacyRlsFunctions(expr.sql);
|
|
4937
|
+
case "and":
|
|
4938
|
+
case "or": return expr.operands.some(containsLegacyRlsCall);
|
|
4939
|
+
case "not": return containsLegacyRlsCall(expr.operand);
|
|
4940
|
+
case "existsIn": return containsLegacyRlsCall(expr.where);
|
|
4941
|
+
default: return false;
|
|
4942
|
+
}
|
|
4943
|
+
}
|
|
4944
|
+
/**
|
|
4565
4945
|
* Reject `pgRoles` that this server can never satisfy.
|
|
4566
4946
|
*
|
|
4567
4947
|
* `pgRoles` sets the `TO` clause of a generated policy, so a policy naming a
|
|
@@ -4691,6 +5071,25 @@ var PostgresBackendDriver = class PostgresBackendDriver {
|
|
|
4691
5071
|
}
|
|
4692
5072
|
};
|
|
4693
5073
|
}
|
|
5074
|
+
/**
|
|
5075
|
+
* Build the context handed to every collection callback.
|
|
5076
|
+
*
|
|
5077
|
+
* Note `data: this.data` — `this` is whichever driver is running the
|
|
5078
|
+
* operation, so the callback's data plane inherits that driver's privilege.
|
|
5079
|
+
* On a user request `AuthenticatedPostgresBackendDriver.withTransaction`
|
|
5080
|
+
* constructs a fresh base driver bound to the RLS-scoped transaction and
|
|
5081
|
+
* runs the operation on it, so `this.data` speaks through that connection
|
|
5082
|
+
* and policies apply. On server-context work `this` is the base driver on
|
|
5083
|
+
* the owner connection, and they do not. Pinned by the
|
|
5084
|
+
* `"scopes context.data to the caller"` case in the `rls-enforcement` e2e
|
|
5085
|
+
* suite, because it is the kind of property that is easy to break from a
|
|
5086
|
+
* distance and impossible to notice.
|
|
5087
|
+
*
|
|
5088
|
+
* Previously returned through `as unknown as RebaseCallContext`, which
|
|
5089
|
+
* disabled checking for the whole object and let `driver` — documented in
|
|
5090
|
+
* the callbacks guide — sit on the runtime context while absent from the
|
|
5091
|
+
* contract. Both are declared now, so this is a plain typed return.
|
|
5092
|
+
*/
|
|
4694
5093
|
buildCallContext() {
|
|
4695
5094
|
return {
|
|
4696
5095
|
user: this.user,
|
|
@@ -5146,6 +5545,106 @@ var PostgresBackendDriver = class PostgresBackendDriver {
|
|
|
5146
5545
|
return saved;
|
|
5147
5546
|
});
|
|
5148
5547
|
}
|
|
5548
|
+
/**
|
|
5549
|
+
* Update many rows through the same pipeline as {@link save}, in one
|
|
5550
|
+
* transaction.
|
|
5551
|
+
*
|
|
5552
|
+
* Structurally the mirror of {@link saveMany} — same tx-bound sub-driver,
|
|
5553
|
+
* same deferred notifications, same per-row error labelling — but it calls
|
|
5554
|
+
* `save` with an explicit `id` and `status: "existing"`, which is precisely
|
|
5555
|
+
* what `saveMany` cannot do: that one passes `status: "new"` and keeps the
|
|
5556
|
+
* key inside `values`, so it inserts or upserts and can never target a
|
|
5557
|
+
* particular row.
|
|
5558
|
+
*
|
|
5559
|
+
* All-or-nothing, so an id matching no row aborts the batch. A partial
|
|
5560
|
+
* update is the outcome with no good recovery: the caller cannot tell which
|
|
5561
|
+
* half landed without re-reading everything.
|
|
5562
|
+
*/
|
|
5563
|
+
async updateMany({ path, updates, collection }) {
|
|
5564
|
+
return this.db.transaction(async (tx) => {
|
|
5565
|
+
const txDriver = new PostgresBackendDriver(tx, this.realtimeService, this.registry, this.user, this.poolManager, this.historyService);
|
|
5566
|
+
txDriver.dataService = new DataService(tx, this.registry);
|
|
5567
|
+
txDriver.client = this.client;
|
|
5568
|
+
txDriver._deferNotifications = this._deferNotifications;
|
|
5569
|
+
txDriver._pendingNotifications = this._pendingNotifications;
|
|
5570
|
+
const saved = [];
|
|
5571
|
+
for (let i = 0; i < updates.length; i++) {
|
|
5572
|
+
const { id, values } = updates[i];
|
|
5573
|
+
try {
|
|
5574
|
+
if (!await txDriver.fetchOne({
|
|
5575
|
+
path,
|
|
5576
|
+
id: String(id),
|
|
5577
|
+
collection
|
|
5578
|
+
})) throw Object.assign(/* @__PURE__ */ new Error(`No row with id ${JSON.stringify(id)}`), {
|
|
5579
|
+
statusCode: 404,
|
|
5580
|
+
code: "NOT_FOUND"
|
|
5581
|
+
});
|
|
5582
|
+
saved.push(await txDriver.save({
|
|
5583
|
+
path,
|
|
5584
|
+
id: String(id),
|
|
5585
|
+
values,
|
|
5586
|
+
collection,
|
|
5587
|
+
status: "existing"
|
|
5588
|
+
}));
|
|
5589
|
+
} catch (error) {
|
|
5590
|
+
throw Object.assign(new Error(`Update ${i} of ${updates.length} (id ${JSON.stringify(id)}) failed: ${error?.message ?? error}`, { cause: error }), {
|
|
5591
|
+
statusCode: error?.statusCode,
|
|
5592
|
+
code: error?.code,
|
|
5593
|
+
name: error?.name
|
|
5594
|
+
});
|
|
5595
|
+
}
|
|
5596
|
+
}
|
|
5597
|
+
return saved;
|
|
5598
|
+
});
|
|
5599
|
+
}
|
|
5600
|
+
/**
|
|
5601
|
+
* Delete many rows in one transaction, running the full delete pipeline —
|
|
5602
|
+
* `beforeDelete`, the delete, `afterDelete` — for each.
|
|
5603
|
+
*
|
|
5604
|
+
* Looping the single-row {@link delete} rather than emitting one
|
|
5605
|
+
* `DELETE ... WHERE id = ANY($1)` is the deliberate choice: a single
|
|
5606
|
+
* statement would be faster and would skip every callback, so a collection
|
|
5607
|
+
* relying on `beforeDelete` to veto or on `afterDelete` to clean up
|
|
5608
|
+
* dependents would behave differently depending on how many rows the caller
|
|
5609
|
+
* happened to delete at once. Same pipeline, one transaction.
|
|
5610
|
+
*/
|
|
5611
|
+
async deleteMany({ path, ids, collection }) {
|
|
5612
|
+
await this.db.transaction(async (tx) => {
|
|
5613
|
+
const txDriver = new PostgresBackendDriver(tx, this.realtimeService, this.registry, this.user, this.poolManager, this.historyService);
|
|
5614
|
+
txDriver.dataService = new DataService(tx, this.registry);
|
|
5615
|
+
txDriver.client = this.client;
|
|
5616
|
+
txDriver._deferNotifications = this._deferNotifications;
|
|
5617
|
+
txDriver._pendingNotifications = this._pendingNotifications;
|
|
5618
|
+
for (let i = 0; i < ids.length; i++) {
|
|
5619
|
+
const id = ids[i];
|
|
5620
|
+
try {
|
|
5621
|
+
const existing = await txDriver.fetchOne({
|
|
5622
|
+
path,
|
|
5623
|
+
id: String(id),
|
|
5624
|
+
collection
|
|
5625
|
+
});
|
|
5626
|
+
if (!existing) throw Object.assign(/* @__PURE__ */ new Error(`No row with id ${JSON.stringify(id)}`), {
|
|
5627
|
+
statusCode: 404,
|
|
5628
|
+
code: "NOT_FOUND"
|
|
5629
|
+
});
|
|
5630
|
+
await txDriver.delete({
|
|
5631
|
+
row: {
|
|
5632
|
+
id: String(id),
|
|
5633
|
+
path,
|
|
5634
|
+
values: existing
|
|
5635
|
+
},
|
|
5636
|
+
collection
|
|
5637
|
+
});
|
|
5638
|
+
} catch (error) {
|
|
5639
|
+
throw Object.assign(new Error(`Delete ${i} of ${ids.length} (id ${JSON.stringify(id)}) failed: ${error?.message ?? error}`, { cause: error }), {
|
|
5640
|
+
statusCode: error?.statusCode,
|
|
5641
|
+
code: error?.code,
|
|
5642
|
+
name: error?.name
|
|
5643
|
+
});
|
|
5644
|
+
}
|
|
5645
|
+
}
|
|
5646
|
+
});
|
|
5647
|
+
}
|
|
5149
5648
|
async delete({ row, collection }) {
|
|
5150
5649
|
const targetPath = row.path;
|
|
5151
5650
|
const targetRow = { ...row.values ?? {} };
|
|
@@ -6116,6 +6615,7 @@ var generateSchema = async (allCollections, stripPolicies = false) => {
|
|
|
6116
6615
|
const hasUuid = collections.some((c) => c.properties && Object.values(c.properties).some((p) => p.type === "string" && (p.autoValue === "uuid" || p.isId === "uuid")));
|
|
6117
6616
|
const hasVector = collections.some((c) => c.properties && Object.values(c.properties).some((p) => p.type === "vector"));
|
|
6118
6617
|
const hasBinary = collections.some((c) => c.properties && Object.values(c.properties).some((p) => p.type === "binary"));
|
|
6618
|
+
const hasSearch = collections.some((c) => buildSearchColumnSpec(c) !== void 0);
|
|
6119
6619
|
const pgCoreImports = [
|
|
6120
6620
|
"primaryKey",
|
|
6121
6621
|
"pgTable",
|
|
@@ -6140,7 +6640,7 @@ var generateSchema = async (allCollections, stripPolicies = false) => {
|
|
|
6140
6640
|
];
|
|
6141
6641
|
if (hasUuid) pgCoreImports.push("uuid");
|
|
6142
6642
|
if (hasVector) pgCoreImports.push("vector");
|
|
6143
|
-
if (hasBinary) pgCoreImports.push("customType");
|
|
6643
|
+
if (hasBinary || hasSearch) pgCoreImports.push("customType");
|
|
6144
6644
|
const uniqueSchemas = Array.from(new Set(collections.map((c) => isPostgresCollectionConfig(c) ? c.schema : void 0).filter(Boolean)));
|
|
6145
6645
|
if (uniqueSchemas.length > 0) pgCoreImports.push("pgSchema");
|
|
6146
6646
|
schemaContent += `import { ${pgCoreImports.join(", ")} } from 'drizzle-orm/pg-core';\n`;
|
|
@@ -6222,6 +6722,11 @@ var generateSchema = async (allCollections, stripPolicies = false) => {
|
|
|
6222
6722
|
const columnString = getDrizzleColumn(propName, prop, collection, collections);
|
|
6223
6723
|
if (columnString) columns.add(columnString);
|
|
6224
6724
|
});
|
|
6725
|
+
const searchSpec = buildSearchColumnSpec(collection);
|
|
6726
|
+
if (searchSpec) {
|
|
6727
|
+
columns.add(` ${searchSpec.column}: customType({ dataType() { return 'tsvector'; } })("${searchSpec.column}").generatedAlwaysAs(sql\`${searchSpec.expression}\`)`);
|
|
6728
|
+
if (searchSpec.fuzzy) columns.add(` ${searchSpec.fuzzy.column}: text("${searchSpec.fuzzy.column}").generatedAlwaysAs(sql\`${searchSpec.fuzzy.expression}\`)`);
|
|
6729
|
+
}
|
|
6225
6730
|
if (!Array.from(columns).some((col) => col.includes(".primaryKey()"))) columns.add(" id: text(\"id\").primaryKey()");
|
|
6226
6731
|
schemaContent += `${Array.from(columns).join(",\n")}`;
|
|
6227
6732
|
const securityRules = getEffectiveSecurityRules(collection);
|
|
@@ -6925,6 +7430,8 @@ var ChannelHistoryStore = class {
|
|
|
6925
7430
|
last_seq BIGINT NOT NULL
|
|
6926
7431
|
)
|
|
6927
7432
|
`);
|
|
7433
|
+
await this.db.execute(sql.raw(revokeInternalTableSql("rebase", "channel_messages")));
|
|
7434
|
+
await this.db.execute(sql.raw(revokeInternalTableSql("rebase", "channel_cursors")));
|
|
6928
7435
|
this.tablesReady = true;
|
|
6929
7436
|
logger.info(`✅ [ChannelHistory] Retained channels ready (${this.rules.length} rule(s)).`);
|
|
6930
7437
|
}
|
|
@@ -7108,6 +7615,7 @@ var ChannelPresenceStore = class {
|
|
|
7108
7615
|
CREATE INDEX IF NOT EXISTS idx_channel_presence_last_seen
|
|
7109
7616
|
ON rebase.channel_presence (last_seen)
|
|
7110
7617
|
`);
|
|
7618
|
+
await this.db.execute(sql.raw(revokeInternalTableSql("rebase", "channel_presence")));
|
|
7111
7619
|
this.tablesReady = true;
|
|
7112
7620
|
}
|
|
7113
7621
|
/** Record (or refresh) a client's presence. */
|
|
@@ -9070,7 +9578,7 @@ function createBackupCron(config) {
|
|
|
9070
9578
|
enabled: config.enabled ?? true,
|
|
9071
9579
|
timeoutSeconds: 3600,
|
|
9072
9580
|
async handler({ log }) {
|
|
9073
|
-
const { createDump, pruneBackups, uploadBackup, validateDump } = await import("./backup-service-
|
|
9581
|
+
const { createDump, pruneBackups, uploadBackup, validateDump } = await import("./backup-service-Bww-Lg0s.js").then((n) => n.r);
|
|
9074
9582
|
const { destination } = config;
|
|
9075
9583
|
if (destination.kind !== "local" && !config.storage) throw new Error(`Backup destination is ${destination.kind} but no storage controller was provided. Pass the backend's configured StorageController to createBackupCron({ storage }).`);
|
|
9076
9584
|
log(`Starting backup of "${dbName}"…`);
|
|
@@ -9585,7 +10093,7 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
9585
10093
|
if (idProp) {
|
|
9586
10094
|
const isId = "isId" in idProp ? idProp.isId : void 0;
|
|
9587
10095
|
if (isId === "uuid") userIdType = "UUID";
|
|
9588
|
-
else if (isId === "
|
|
10096
|
+
else if (isId === "increment") userIdType = "INTEGER";
|
|
9589
10097
|
}
|
|
9590
10098
|
}
|
|
9591
10099
|
try {
|
|
@@ -9618,43 +10126,42 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
9618
10126
|
const emailLengthConstraint = `"${authIdentifier("email_length_check")}"`;
|
|
9619
10127
|
const emailLowerUniqueIndex = authIdentifier("email_lower_key");
|
|
9620
10128
|
const verificationTokenIndex = authIdentifier("email_verification_token_idx");
|
|
10129
|
+
const usersColumnDdl = AUTH_USERS_COLUMNS.map((spec) => spec.column === "email" ? `${spec.column} ${authUsersColumnSql(spec)} CONSTRAINT ${emailLengthConstraint} CHECK (length(email) <= 320)` : `${spec.column} ${authUsersColumnSql(spec)}`).join(",\n ");
|
|
9621
10130
|
await db.execute(sql`
|
|
9622
10131
|
CREATE TABLE IF NOT EXISTS ${sql.raw(usersTableName)} (
|
|
9623
10132
|
id ${sql.raw(userIdType)} PRIMARY KEY ${sql.raw(idDefault)},
|
|
9624
|
-
|
|
9625
|
-
display_name TEXT,
|
|
9626
|
-
photo_url TEXT,
|
|
9627
|
-
roles TEXT[] DEFAULT '{}' NOT NULL,
|
|
9628
|
-
password_hash TEXT,
|
|
9629
|
-
email_verified BOOLEAN DEFAULT FALSE NOT NULL,
|
|
9630
|
-
email_verification_token TEXT,
|
|
9631
|
-
email_verification_sent_at TIMESTAMP WITH TIME ZONE,
|
|
9632
|
-
is_anonymous BOOLEAN DEFAULT FALSE NOT NULL,
|
|
9633
|
-
metadata JSONB DEFAULT '{}' NOT NULL,
|
|
9634
|
-
tokens_valid_after TIMESTAMP WITH TIME ZONE,
|
|
9635
|
-
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
|
9636
|
-
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
|
|
10133
|
+
${sql.raw(usersColumnDdl)}
|
|
9637
10134
|
)
|
|
9638
10135
|
`);
|
|
9639
|
-
|
|
9640
|
-
CREATE OR REPLACE FUNCTION ${sql.raw(`"${authSchema}"`)}.sync_uid_user_id() RETURNS trigger AS $$
|
|
9641
|
-
BEGIN
|
|
9642
|
-
IF NEW.uid IS NULL AND NEW.user_id IS NOT NULL THEN
|
|
9643
|
-
NEW.uid := NEW.user_id;
|
|
9644
|
-
ELSIF NEW.user_id IS NULL AND NEW.uid IS NOT NULL THEN
|
|
9645
|
-
NEW.user_id := NEW.uid;
|
|
9646
|
-
END IF;
|
|
9647
|
-
RETURN NEW;
|
|
9648
|
-
END $$ LANGUAGE plpgsql
|
|
9649
|
-
`);
|
|
9650
|
-
for (const authTable of [
|
|
10136
|
+
const legacyFkTables = [
|
|
9651
10137
|
"user_identities",
|
|
9652
10138
|
"refresh_tokens",
|
|
9653
10139
|
"password_reset_tokens",
|
|
9654
10140
|
"magic_link_tokens",
|
|
9655
10141
|
"mfa_factors",
|
|
9656
10142
|
"recovery_codes"
|
|
9657
|
-
]
|
|
10143
|
+
];
|
|
10144
|
+
const legacyFkTableList = legacyFkTables.map((t) => `'${t}'`).join(", ");
|
|
10145
|
+
const legacyUserIdPresent = await db.execute(sql`
|
|
10146
|
+
SELECT 1
|
|
10147
|
+
FROM information_schema.columns
|
|
10148
|
+
WHERE table_schema = ${authSchema}
|
|
10149
|
+
AND table_name IN (${sql.raw(legacyFkTableList)})
|
|
10150
|
+
AND column_name = 'user_id'
|
|
10151
|
+
LIMIT 1
|
|
10152
|
+
`);
|
|
10153
|
+
if (legacyUserIdPresent.rows.length > 0) await db.execute(sql`
|
|
10154
|
+
CREATE OR REPLACE FUNCTION ${sql.raw(`"${authSchema}"`)}.sync_uid_user_id() RETURNS trigger AS $$
|
|
10155
|
+
BEGIN
|
|
10156
|
+
IF NEW.uid IS NULL AND NEW.user_id IS NOT NULL THEN
|
|
10157
|
+
NEW.uid := NEW.user_id;
|
|
10158
|
+
ELSIF NEW.user_id IS NULL AND NEW.uid IS NOT NULL THEN
|
|
10159
|
+
NEW.user_id := NEW.uid;
|
|
10160
|
+
END IF;
|
|
10161
|
+
RETURN NEW;
|
|
10162
|
+
END $$ LANGUAGE plpgsql
|
|
10163
|
+
`);
|
|
10164
|
+
for (const authTable of legacyUserIdPresent.rows.length > 0 ? legacyFkTables : []) {
|
|
9658
10165
|
const qualified = `"${authSchema}"."${authTable}"`;
|
|
9659
10166
|
await db.execute(sql`
|
|
9660
10167
|
DO $$
|
|
@@ -9775,54 +10282,50 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
9775
10282
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
9776
10283
|
)
|
|
9777
10284
|
`);
|
|
9778
|
-
await db.execute(sql`CREATE SCHEMA IF NOT EXISTS auth`);
|
|
9779
10285
|
await db.transaction(async (tx) => {
|
|
9780
10286
|
await tx.execute(sql`SELECT pg_advisory_xact_lock(hashtext('rebase_auth_functions_init'))`);
|
|
9781
|
-
await tx.execute(sql
|
|
9782
|
-
CREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$
|
|
9783
|
-
SELECT COALESCE(
|
|
9784
|
-
NULLIF(current_setting('app.uid', true), ''),
|
|
9785
|
-
NULLIF(current_setting('app.user_id', true), '')
|
|
9786
|
-
);
|
|
9787
|
-
$$ LANGUAGE sql STABLE
|
|
9788
|
-
`);
|
|
9789
|
-
await tx.execute(sql`
|
|
9790
|
-
CREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$
|
|
9791
|
-
SELECT COALESCE(
|
|
9792
|
-
NULLIF(current_setting('app.jwt', true), ''),
|
|
9793
|
-
'{}'
|
|
9794
|
-
)::jsonb;
|
|
9795
|
-
$$ LANGUAGE sql STABLE
|
|
9796
|
-
`);
|
|
9797
|
-
await tx.execute(sql`
|
|
9798
|
-
CREATE OR REPLACE FUNCTION auth.roles() RETURNS text AS $$
|
|
9799
|
-
SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');
|
|
9800
|
-
$$ LANGUAGE sql STABLE
|
|
9801
|
-
`);
|
|
10287
|
+
for (const statement of RLS_BOOTSTRAP_STATEMENTS) await tx.execute(sql.raw(statement));
|
|
9802
10288
|
});
|
|
9803
|
-
for (const
|
|
9804
|
-
"
|
|
9805
|
-
|
|
9806
|
-
"roles TEXT[] DEFAULT '{}' NOT NULL",
|
|
9807
|
-
"password_hash TEXT",
|
|
9808
|
-
"email_verified BOOLEAN DEFAULT FALSE NOT NULL",
|
|
9809
|
-
"email_verification_token TEXT",
|
|
9810
|
-
"email_verification_sent_at TIMESTAMP WITH TIME ZONE",
|
|
9811
|
-
"is_anonymous BOOLEAN DEFAULT FALSE NOT NULL",
|
|
9812
|
-
"metadata JSONB DEFAULT '{}' NOT NULL",
|
|
9813
|
-
"tokens_valid_after TIMESTAMP WITH TIME ZONE",
|
|
9814
|
-
"created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL",
|
|
9815
|
-
"updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL"
|
|
9816
|
-
]) await db.execute(sql`
|
|
10289
|
+
for (const spec of AUTH_USERS_COLUMNS) {
|
|
10290
|
+
if (spec.column === "email") continue;
|
|
10291
|
+
await db.execute(sql`
|
|
9817
10292
|
ALTER TABLE ${sql.raw(usersTableName)}
|
|
9818
|
-
ADD COLUMN IF NOT EXISTS ${sql.raw(
|
|
10293
|
+
ADD COLUMN IF NOT EXISTS ${sql.raw(`${spec.column} ${authUsersColumnSql(spec)}`)}
|
|
9819
10294
|
`);
|
|
9820
|
-
|
|
9821
|
-
|
|
10295
|
+
}
|
|
10296
|
+
const usersColumnRows = (await db.execute(sql`
|
|
10297
|
+
SELECT column_name, data_type, is_nullable, column_default
|
|
9822
10298
|
FROM information_schema.columns
|
|
9823
10299
|
WHERE table_schema = ${usersSchema} AND table_name = ${resolvedTable}
|
|
9824
|
-
`);
|
|
9825
|
-
const usersColumnTypes = new Map(
|
|
10300
|
+
`)).rows;
|
|
10301
|
+
const usersColumnTypes = new Map(usersColumnRows.map((row) => [row.column_name, row.data_type]));
|
|
10302
|
+
const usersColumnState = new Map(usersColumnRows.map((row) => [row.column_name, row]));
|
|
10303
|
+
for (const spec of AUTH_USERS_COLUMNS) {
|
|
10304
|
+
const state = usersColumnState.get(spec.column);
|
|
10305
|
+
if (!state) continue;
|
|
10306
|
+
if (spec.default !== void 0 && state.column_default === null) {
|
|
10307
|
+
await db.execute(sql`
|
|
10308
|
+
ALTER TABLE ${sql.raw(usersTableName)}
|
|
10309
|
+
ALTER COLUMN ${sql.raw(`"${spec.column}"`)} SET DEFAULT ${sql.raw(spec.default)}
|
|
10310
|
+
`);
|
|
10311
|
+
logger.info(`🔧 Restored the default on ${usersTableName}.${spec.column}`);
|
|
10312
|
+
}
|
|
10313
|
+
if (!spec.notNull || state.is_nullable !== "YES") continue;
|
|
10314
|
+
if (spec.default !== void 0) await db.execute(sql`
|
|
10315
|
+
UPDATE ${sql.raw(usersTableName)}
|
|
10316
|
+
SET ${sql.raw(`"${spec.column}"`)} = ${sql.raw(spec.default)}
|
|
10317
|
+
WHERE ${sql.raw(`"${spec.column}"`)} IS NULL
|
|
10318
|
+
`);
|
|
10319
|
+
try {
|
|
10320
|
+
await db.execute(sql`
|
|
10321
|
+
ALTER TABLE ${sql.raw(usersTableName)}
|
|
10322
|
+
ALTER COLUMN ${sql.raw(`"${spec.column}"`)} SET NOT NULL
|
|
10323
|
+
`);
|
|
10324
|
+
logger.info(`🔧 Restored NOT NULL on ${usersTableName}.${spec.column}`);
|
|
10325
|
+
} catch (err) {
|
|
10326
|
+
logger.warn(`⚠️ ${usersTableName}.${spec.column} should be NOT NULL but still holds NULLs, so the constraint was not applied. Fill or remove those rows and restart: ` + (err instanceof Error ? err.message : String(err)));
|
|
10327
|
+
}
|
|
10328
|
+
}
|
|
9826
10329
|
for (const column of [
|
|
9827
10330
|
"email",
|
|
9828
10331
|
"display_name",
|
|
@@ -10004,10 +10507,12 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
10004
10507
|
[authSchema, "user_identities"],
|
|
10005
10508
|
[authSchema, "refresh_tokens"],
|
|
10006
10509
|
[authSchema, "password_reset_tokens"],
|
|
10510
|
+
[authSchema, "magic_link_tokens"],
|
|
10007
10511
|
[authSchema, "app_config"],
|
|
10008
10512
|
[authSchema, "mfa_factors"],
|
|
10009
10513
|
[authSchema, "mfa_challenges"],
|
|
10010
|
-
[authSchema, "recovery_codes"]
|
|
10514
|
+
[authSchema, "recovery_codes"],
|
|
10515
|
+
[authSchema, "schema_meta"]
|
|
10011
10516
|
];
|
|
10012
10517
|
for (const [schemaName, tableName] of authTablePairs) if ((await db.execute(sql`
|
|
10013
10518
|
SELECT 1
|
|
@@ -10027,6 +10532,9 @@ async function ensureAuthTablesExist(db, collection) {
|
|
|
10027
10532
|
logger.warn(`⚠️ Could not reconcile FORCE ROW LEVEL SECURITY on auth tables: ${rlsReconcileError instanceof Error ? rlsReconcileError.message : String(rlsReconcileError)}`);
|
|
10028
10533
|
}
|
|
10029
10534
|
await stampAuthSchemaVersion(db, authSchema);
|
|
10535
|
+
await revokeInternalTableAccess(async (text) => {
|
|
10536
|
+
await db.execute(sql.raw(text));
|
|
10537
|
+
}, authSchema, { onError: (table, err) => logger.warn(`🔐 Could not revoke authenticated-role access to "${authSchema}"."${table}": ` + (err instanceof Error ? err.message : String(err))) });
|
|
10030
10538
|
logger.info("✅ Auth tables ready");
|
|
10031
10539
|
} catch (error) {
|
|
10032
10540
|
if (error instanceof AuthSchemaVersionError) throw error;
|
|
@@ -11355,6 +11863,7 @@ async function ensureHistoryTableExists(db) {
|
|
|
11355
11863
|
CREATE INDEX IF NOT EXISTS idx_history_time
|
|
11356
11864
|
ON rebase.entity_history(table_name, entity_id, updated_at DESC)
|
|
11357
11865
|
`);
|
|
11866
|
+
await db.execute(sql.raw(revokeInternalTableSql("rebase", "entity_history")));
|
|
11358
11867
|
logger.info("✅ Entity history table ready");
|
|
11359
11868
|
} catch (error) {
|
|
11360
11869
|
logger.error("❌ Failed to create row history table", { error });
|
|
@@ -12076,12 +12585,12 @@ function createPostgresBootstrapper(pgConfig) {
|
|
|
12076
12585
|
const runSql = async (text) => {
|
|
12077
12586
|
return (await schemaAwareDb.execute(sql.raw(text))).rows ?? [];
|
|
12078
12587
|
};
|
|
12588
|
+
await warnOnRoleSchemaCollision(runSql);
|
|
12079
12589
|
const posture = await detectConnectionPosture(runSql);
|
|
12080
12590
|
if (posture.privileged) {
|
|
12081
12591
|
await ensureAppRole(runSql, [
|
|
12082
12592
|
"public",
|
|
12083
12593
|
"rebase",
|
|
12084
|
-
"auth",
|
|
12085
12594
|
...registry.getCollections().map((c) => c.schema).filter((s) => typeof s === "string")
|
|
12086
12595
|
]);
|
|
12087
12596
|
driver.rlsUserRole = REBASE_USER_ROLE;
|
|
@@ -12091,6 +12600,7 @@ function createPostgresBootstrapper(pgConfig) {
|
|
|
12091
12600
|
} else logger.info(`🔐 RLS enforcement: connection role "${posture.role}" is subject to RLS natively; no role switch needed.`);
|
|
12092
12601
|
await validatePolicyPgRoles(runSql, registry.getCollections(), driver.rlsUserRole ?? posture.role);
|
|
12093
12602
|
warnOnAnonymousGrants(registry.getCollections());
|
|
12603
|
+
warnOnLegacyRlsFunctions(registry.getCollections());
|
|
12094
12604
|
}
|
|
12095
12605
|
if (driver.branchService) try {
|
|
12096
12606
|
await driver.branchService.ensureBranchMetadataTable();
|
|
@@ -12306,7 +12816,7 @@ function createPostgresBootstrapper(pgConfig) {
|
|
|
12306
12816
|
*/
|
|
12307
12817
|
async ensureCollectionSchema(collections, driverResult, log) {
|
|
12308
12818
|
const internals = driverResult.internals;
|
|
12309
|
-
const { ensureCollectionTables } = await import("./ensure-collection-tables-
|
|
12819
|
+
const { ensureCollectionTables } = await import("./ensure-collection-tables-CyMBc7wo.js");
|
|
12310
12820
|
const plan = await ensureCollectionTables({ async query(text) {
|
|
12311
12821
|
const result = await internals.db.execute(sql.raw(text));
|
|
12312
12822
|
return { rows: result.rows ?? (Array.isArray(result) ? result : []) };
|
|
@@ -12331,13 +12841,24 @@ function createPostgresBootstrapper(pgConfig) {
|
|
|
12331
12841
|
*/
|
|
12332
12842
|
async ensureCollectionPolicies(collections, driverResult, log) {
|
|
12333
12843
|
const internals = driverResult.internals;
|
|
12334
|
-
const { ensureCollectionPolicies } = await import("./ensure-collection-policies-
|
|
12844
|
+
const { ensureCollectionPolicies } = await import("./ensure-collection-policies-C8H7ig-m.js");
|
|
12335
12845
|
const outcome = await ensureCollectionPolicies({ async query(text) {
|
|
12336
12846
|
const result = await internals.db.execute(sql.raw(text));
|
|
12337
12847
|
return { rows: result.rows ?? (Array.isArray(result) ? result : []) };
|
|
12338
12848
|
} }, collections, log);
|
|
12339
12849
|
for (const skip of outcome.skipped) logger.warn(`🔐 [rls] Policies not applied to "${skip.table}": ${skip.reason}`);
|
|
12340
12850
|
for (const failure of outcome.failures) logger.warn(`🔐 [rls] Could not fully apply policies to "${failure.table}" — it stays locked (denies) until this is resolved: ${failure.error}`);
|
|
12851
|
+
try {
|
|
12852
|
+
const { dropLegacyAuthSchema } = await import("./rls-bootstrap-sql-Bpv3nUZo.js").then((n) => n.n);
|
|
12853
|
+
await dropLegacyAuthSchema(async (text) => {
|
|
12854
|
+
return (await internals.db.execute(sql.raw(text))).rows ?? [];
|
|
12855
|
+
}, {
|
|
12856
|
+
info: (m) => logger.info(m),
|
|
12857
|
+
warn: (m) => logger.warn(m)
|
|
12858
|
+
});
|
|
12859
|
+
} catch (err) {
|
|
12860
|
+
logger.info("Left the legacy `auth` schema in place: " + (err instanceof Error ? err.message : String(err)));
|
|
12861
|
+
}
|
|
12341
12862
|
return { applied: outcome.policiesApplied };
|
|
12342
12863
|
},
|
|
12343
12864
|
getAdmin(driverResult) {
|
|
@@ -12345,7 +12866,7 @@ function createPostgresBootstrapper(pgConfig) {
|
|
|
12345
12866
|
},
|
|
12346
12867
|
mountRoutes(app, basePath, driverResult) {},
|
|
12347
12868
|
async initializeWebsockets(server, realtimeService, driver, config, adapter) {
|
|
12348
|
-
const { createPostgresWebSocket } = await import("./websocket-
|
|
12869
|
+
const { createPostgresWebSocket } = await import("./websocket-D0TBU3ia.js").then((n) => n.n);
|
|
12349
12870
|
createPostgresWebSocket(server, realtimeService, driver, config, adapter);
|
|
12350
12871
|
}
|
|
12351
12872
|
};
|