@rebasepro/types 0.29.0-canary.g0e3a0e7 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -187,6 +187,27 @@ export interface BaseCollectionConfig<M extends Record<string, unknown> = Record
187
187
  */
188
188
  strictWrites?: boolean;
189
189
  }
190
+ /**
191
+ * The SQL view a collection serves. See {@link PostgresCollectionConfig.view}.
192
+ *
193
+ * @group Models
194
+ */
195
+ export interface CollectionView {
196
+ /** The view's `SELECT`, as SQL. Its columns are the collection's properties. */
197
+ query: string;
198
+ /**
199
+ * Store the rows (`CREATE MATERIALIZED VIEW`) rather than run the query on
200
+ * every read. Refresh them with the collection's `refresh()`.
201
+ */
202
+ materialized?: boolean;
203
+ /**
204
+ * Who may read a materialized view: callers holding one of these roles.
205
+ * Defaults to `["admin"]`, which `dataAsAdmin` and the server's own work
206
+ * hold. Ignored for a plain view, whose rows the base tables' policies
207
+ * decide.
208
+ */
209
+ roles?: string[];
210
+ }
190
211
  /**
191
212
  * A collection backed by PostgreSQL (or any SQL database).
192
213
  * Adds support for SQL-style relations (JOINs) and Row Level Security.
@@ -228,6 +249,28 @@ export interface PostgresCollectionConfig<M extends Record<string, unknown> = Re
228
249
  * If not specified, "public" is used (or the default search path).
229
250
  */
230
251
  schema?: string;
252
+ /**
253
+ * Serve a SQL view instead of a table: the collection is read-only, and
254
+ * `db push` (and a managed boot) creates the view under `table` from
255
+ * `query`, recreating it when the query changes.
256
+ *
257
+ * The properties describe the view's columns, and one of them must be
258
+ * `isId`: a view has no primary key of its own.
259
+ *
260
+ * A plain view runs as the caller (`security_invoker`), so the policies of
261
+ * the tables it reads decide which rows each caller sees; `securityRules`
262
+ * do not apply to it. A materialized view stores its rows and has no
263
+ * row-level security at all, so it is served whole, and only to callers
264
+ * holding one of `roles`.
265
+ *
266
+ * ```ts
267
+ * view: {
268
+ * query: "SELECT lower(artist) AS artist, count(*)::int AS songs FROM songs GROUP BY 1",
269
+ * materialized: true
270
+ * }
271
+ * ```
272
+ */
273
+ view?: CollectionView;
231
274
  /**
232
275
  * For SQL databases, you can define the relations between collections here.
233
276
  * Relations describe JOINs, foreign keys, and junction tables.
@@ -21,6 +21,8 @@
21
21
  * │ "not-in" │ "nin" │ Value not in list │
22
22
  * │ "array-contains" │ "cs" │ Array contains element │
23
23
  * │ "array-contains-any" │ "csa" │ Array contains any of │
24
+ * │ "array-contains-all" │ "csall" │ Array contains all of │
25
+ * │ "array-contained-by" │ "cd" │ Array holds only elements of │
24
26
  * │ "like" │ "like" │ SQL LIKE (case-sensitive) │
25
27
  * │ "ilike" │ "ilike" │ SQL ILIKE (case-insensitive) │
26
28
  * │ "not-like" │ "nlike" │ NOT LIKE (case-sensitive) │
@@ -195,7 +197,7 @@ export declare function sortKeyToString(key: SortKey): string;
195
197
  *
196
198
  * @group Models
197
199
  */
198
- export type WhereFilterOp = "<" | "<=" | "==" | "!=" | ">=" | ">" | "array-contains" | "in" | "not-in" | "array-contains-any" | "like" | "ilike" | "not-like" | "not-ilike" | "is-null" | "is-not-null";
200
+ export type WhereFilterOp = "<" | "<=" | "==" | "!=" | ">=" | ">" | "array-contains" | "in" | "not-in" | "array-contains-any" | "array-contains-all" | "array-contained-by" | "like" | "ilike" | "not-like" | "not-ilike" | "is-null" | "is-not-null";
199
201
  /**
200
202
  * Used to define filters applied in collections.
201
203
  *
@@ -317,7 +319,7 @@ export interface FilterPreset<Key extends string = string> {
317
319
  * in application code. Used by `serializeFilter`/`deserializeFilter`
318
320
  * in `@rebasepro/common`.
319
321
  */
320
- export type RestFilterOp = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "nin" | "cs" | "csa" | "like" | "ilike" | "nlike" | "nilike" | "isnull" | "notnull";
322
+ export type RestFilterOp = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "nin" | "cs" | "csa" | "csall" | "cd" | "like" | "ilike" | "nlike" | "nilike" | "isnull" | "notnull";
321
323
  /** Maps canonical operators to their REST short-code equivalents. */
322
324
  export declare const CANONICAL_TO_REST: Readonly<Record<WhereFilterOp, RestFilterOp>>;
323
325
  /** Maps REST short-code operators to their canonical equivalents. */
@@ -52,6 +52,38 @@ export interface DatabaseOptions extends DeclareOptions {
52
52
  * a stock `postgres:18` does not carry.
53
53
  */
54
54
  extensions?: string[];
55
+ /**
56
+ * How row-level security runs on this database — the request role and
57
+ * everything around it. Every default is what Rebase did before the option
58
+ * existed. Declared here rather than only on the driver because the policy
59
+ * settings shape the SQL `rebase db generate` writes, and the CLI reads
60
+ * this declaration as the server does. The `REBASE_RLS_*` variables win.
61
+ *
62
+ * ```ts
63
+ * export const main = database({ rls: { requestRole: "acme_user" } });
64
+ * ```
65
+ *
66
+ * See `PostgresRlsConfig` in `@rebasepro/server-postgres` for each setting,
67
+ * and the forms only code can give (a function for the role or identity).
68
+ */
69
+ rls?: DatabaseRlsOptions;
70
+ }
71
+ /**
72
+ * The data-only `rls` settings a database declaration takes. The driver's own
73
+ * `rls` block (`PostgresRlsConfig`) accepts these and the function forms.
74
+ */
75
+ export interface DatabaseRlsOptions {
76
+ requestRole?: string | {
77
+ anonymous: string;
78
+ signedIn: string;
79
+ };
80
+ provisionRole?: "auto" | "never";
81
+ grants?: "schema" | "managed" | "none";
82
+ policyTarget?: "public" | "requestRole";
83
+ defaultPolicies?: boolean;
84
+ inheritRoles?: string[];
85
+ identity?: "rebase" | "supabase";
86
+ sharedRole?: "warn" | "refuse" | "ignore";
55
87
  }
56
88
  /** A database handle. Collections point at it via `dataSource`. */
57
89
  export type DatabaseHandle = ResourceHandle;
@@ -84,6 +116,21 @@ export declare function database(key?: string, options?: DatabaseOptions): Datab
84
116
  * not opted in — so this reads as a refusal by default, on purpose.
85
117
  */
86
118
  export declare function declaredDatabaseExtensions(): readonly string[];
119
+ /** The `rls` options a database declaration gave, `{}` when it gave none or was never declared. */
120
+ export declare function declaredDatabaseRls(key?: string): DatabaseRlsOptions;
121
+ /**
122
+ * The policy settings every compile path must agree on — the boot's policy
123
+ * ensure, `rebase db generate`, the drift check — read from the one place all
124
+ * of them can see: the database declaration, with `REBASE_RLS_POLICY_TARGET` /
125
+ * `REBASE_RLS_DEFAULT_POLICIES` over it. Defaults are what Rebase compiled
126
+ * before these existed.
127
+ *
128
+ * Browser-safe: where there is no `process`, only the declaration counts.
129
+ */
130
+ export declare function declaredRlsPolicyShape(dataSource?: string): {
131
+ policyTarget: "public" | "requestRole";
132
+ defaultPolicies: boolean;
133
+ };
87
134
  /** Options a bucket accepts beyond the common ones. */
88
135
  export interface BucketOptions extends DeclareOptions {
89
136
  /**
@@ -54,6 +54,15 @@ export declare const REBASE_SCHEMA = "rebase";
54
54
  * the trusted server plane and `IS NOT NULL` a tautology.
55
55
  */
56
56
  export declare const RLS_UID_SQL = "rebase.uid()";
57
+ /**
58
+ * Is the caller one of this database's request roles? What a permissive
59
+ * policy is ANDed with under `rls.policyTarget: "requestRole"`, so another
60
+ * application's role holding a grant on the table gets nothing from it.
61
+ * `current_user` is passed in — inside the function, which reads the recorded
62
+ * roles with its owner's rights, `current_user` would be the owner — and the
63
+ * sub-select makes it one evaluation per statement, not one per row.
64
+ */
65
+ export declare const RLS_IS_REQUEST_ROLE_SQL = "(SELECT rebase.is_request_role(current_user::text))";
57
66
  /** The request's roles as a comma-separated string, for `string_to_array`. */
58
67
  export declare const RLS_ROLES_SQL = "rebase.roles()";
59
68
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebasepro/types",
3
- "version": "0.29.0-canary.g0e3a0e7",
3
+ "version": "0.29.1",
4
4
  "description": "Rebase type definitions — shared interfaces and controller types",
5
5
  "keywords": [
6
6
  "rebase",