Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions drizzle-orm/src/supabase/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { SQL } from '~/sql/sql.ts';
import { entityKind } from '~/entity.ts';

export type FunctionReturns =
| 'void'
| 'record'
| 'trigger'
| 'integer'
| 'bool'
| 'bytea'
| 'date'
| 'double precision'
| 'float4'
| 'float8'
| 'int2'
| 'int4'
| 'int8'
| 'json'
| 'jsonb'
| 'numeric'
| 'text'
| 'time'
| 'timestamp'
| 'timestamptz'
| 'timetz'
| 'uuid'
| 'varchar'
| 'vector';

export type FunctionLanguage = 'plpgsql' | 'sql';
export type FunctionBehavior = 'immutable' | 'stable' | 'volatile';
export type FunctionSecurity = 'definer' | 'invoker';

export interface PgFunctionConfig {
returns?: FunctionReturns;
language?: FunctionLanguage;
behavior?: FunctionBehavior;
security?: FunctionSecurity;
searchPath?: string;
body?: SQL;
}

export class PgFunction implements PgFunctionConfig {
static readonly [entityKind]: string = 'PgFunction';

readonly returns: PgFunctionConfig['returns'];
readonly language: PgFunctionConfig['language'];
readonly behavior: PgFunctionConfig['behavior'];
readonly security: PgFunctionConfig['security'];
readonly searchPath: PgFunctionConfig['searchPath'];
readonly body: PgFunctionConfig['body'];

constructor(
readonly name: string,
config?: PgFunctionConfig
) {
if (config) {
this.returns = config.returns;
this.language = config.language;
this.behavior = config.behavior;
this.security = config.security;
this.searchPath = config.searchPath;
this.body = config.body;
}
}
}

export function pgFunction(name: string, config?: PgFunctionConfig) {
return new PgFunction(name, config);
}
2 changes: 2 additions & 0 deletions drizzle-orm/src/supabase/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './rls.ts';
export * from './functions.ts';
export * from './triggers.ts';
42 changes: 42 additions & 0 deletions drizzle-orm/src/supabase/triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { PgTable } from '~/pg-core/table.ts';
import type { PgFunction } from './functions.ts';
import { entityKind } from '~/entity.ts';

export type TriggerEvent = 'insert' | 'update' | 'delete';
export type TriggerType = 'before' | 'after';
export type TriggerOrientation = 'row' | 'statement';

export interface PgTriggerConfig {
table?: PgTable;
events?: TriggerEvent[];
triggerType?: TriggerType;
orientation?: TriggerOrientation;
function?: PgFunction;
}

export class PgTrigger implements PgTriggerConfig {
static readonly [entityKind]: string = 'PgTrigger';

readonly table: PgTriggerConfig['table'];
readonly events: PgTriggerConfig['events'];
readonly triggerType: PgTriggerConfig['triggerType'];
readonly orientation: PgTriggerConfig['orientation'];
readonly function: PgTriggerConfig['function'];

constructor(
readonly name: string,
config?: PgTriggerConfig
) {
if (config) {
this.table = config.table;
this.events = config.events;
this.triggerType = config.triggerType;
this.orientation = config.orientation;
this.function = config.function;
}
}
}

export function pgTrigger(name: string, config?: PgTriggerConfig) {
return new PgTrigger(name, config);
}