Database utility functions

Database utility functions are a grab-bag of functions that we've found useful for building and scaling apps using Basejump.

Functions that are defined in the basejump schema are available for use within database functions and RLS policies, but not through the API.

FUNCTIONbasejump.get_config

Get config

Returns the Basejump configuration for the current project as a JSON value.

Usage

FUNCTION
basejump.get_config
SELECT basejump.get_config();

Response

{
    "enable_personal_accounts": true,
    "enable_team_accounts": true,
    // your config values can change based
    // on your setup, but more will be
    // returned here
}

FUNCTIONbasejump.is_set

Check specific config

Returns true / false if a specific config value is set.

Required attributes

  • Name
    field_name
    Type
    text
    Description

    The name of the config field to check.

Usage

FUNCTION
basejump.is_set
SELECT basejump.is_set('enable_personal_accounts');

FUNCTIONbasejump.generate_token

Generate secure token

Generates a secure token of a given length.

Required attributes

  • Name
    length
    Type
    integer
    Description

    The length of the token to generate.

Usage

FUNCTION
basejump.generate_token
SELECT basejump.generate_token(32);

TRIGGERbasejump.trigger_set_timestamps

Track timestamps

Trigger function to automatically track timestamps on a table. Requires that the table have a created_at and updated_at column.

Usage

TRIGGER
basejump.trigger_set_timestamps
 CREATE TABLE public.posts (
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    title text NOT NULL,
    body text NOT NULL,
    created_at timestamp with time zone NOT NULL DEFAULT now(),
    updated_at timestamp with time zone NOT NULL DEFAULT now(),
    CONSTRAINT posts_pkey PRIMARY KEY (id)
);

CREATE TRIGGER set_timestamps
    BEFORE UPDATE ON public.posts
    FOR EACH ROW
    EXECUTE PROCEDURE basejump.trigger_set_timestamps();

TRIGGERbasejump.trigger_set_user_tracking

Track user changes

Trigger function to automatically track who performed changes on a table. Requires that the table have a created_by and updated_by column.

Usage

TRIGGER
basejump.trigger_set_user_tracking
CREATE TABLE public.posts (
    id uuid NOT NULL DEFAULT uuid_generate_v4(),
    title text NOT NULL,
    body text NOT NULL,
    created_by uuid NOT NULL,
    updated_by uuid NOT NULL,
    CONSTRAINT posts_pkey PRIMARY KEY (id)
);

CREATE TRIGGER set_timestamps
    BEFORE UPDATE ON public.posts
    FOR EACH ROW
    EXECUTE PROCEDURE basejump.trigger_set_user_tracking();