Skip to content

@btravstack/prisma


@btravstack/prisma / rls

rls

Type Aliases

ScopedTransaction

ts
type ScopedTransaction = <C, R>(this, fn, options?) => Promise<R>;

Defined in: prisma/src/rls.ts:34

The $transaction a tenantScoped client exposes: the callback form only, with this carrying the extended client through to tx.

Type Parameters

Type Parameter
C
R

Parameters

ParameterType
thisC
fn(tx) => Promise<R>
options?{ isolationLevel?: string; maxWait?: number; timeout?: number; }
options.isolationLevel?string
options.maxWait?number
options.timeout?number

Returns

Promise<R>

Remarks

The generic this is what keeps tx typed. The starter cannot name a generated client, so the receiver is the only place that type can come from. The array form is absent because it cannot be pinned atomically.


ScopedTransactionClient

ts
type ScopedTransactionClient<C> = Omit<C, "$connect" | "$disconnect" | "$extends" | "$on" | "$use">;

Defined in: prisma/src/rls.ts:20

The client a pinned $transaction callback receives — the extended client minus what a transaction cannot do, exactly as Prisma's own deny list has it.

Type Parameters

Type Parameter
C

Remarks

Not @unthrown/prisma's TransactionClient, which denies a longer list; an application applying both extensions imports both names.


TenantScopedOptions

ts
type TenantScopedOptions = object;

Defined in: prisma/src/rls.ts:4

Options of tenantScoped.

Properties

PropertyModifierTypeDescriptionDefined in
setting?readonlystringThe PostgreSQL run-time setting the policy reads. Defaults to app.tenant_id, matching current_setting('app.tenant_id', true).prisma/src/rls.ts:9

Functions

tenantScoped()

ts
function tenantScoped(tenant, options?): (client) => PrismaClientExtends<InternalArgs<{
}, {
}, {
}, {
  $transaction: ScopedTransaction;
}> & DefaultArgs>;

Defined in: prisma/src/rls.ts:58

A Prisma client extension pinning every statement to tenant through a transaction-local set_config, so a row-level-security policy reading current_setting('app.tenant_id', true) sees it.

Parameters

ParameterType
tenantstring
options?TenantScopedOptions

Returns

(client) => PrismaClientExtends<InternalArgs<{ }, { }, { }, { $transaction: ScopedTransaction; }> & DefaultArgs>

Remarks

Apply it last: the transaction callback's tx comes from the client as it stood when this extension was applied, so an extension added after this one is invisible inside a transaction.

$transaction([...]) is refused — the reasoning is in packages/prisma/CLAUDE.md. Use the callback form.

Example

ts
const db = new PrismaClient({ adapter }).$extends(unthrownPrisma).$extends(tenantScoped(tenant));

Released under the MIT License.