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.
Get config
Returns the Basejump configuration for the current project as a JSON value.
Usage
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
}
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
SELECT basejump.is_set('enable_personal_accounts');
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
SELECT basejump.generate_token(32);
Track timestamps
Trigger function to automatically track timestamps on a table. Requires that the table have a created_at
and updated_at
column.
Usage
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();
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
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();