Home
> J-Supabase Now Has More Utility Functions

Supabase Tutorial

Coming Soon!

Get updates on future FREE course and blog posts!
Subscribe

J-Supabase Now Has More Utility Functions

4 min read

Jonathan Gamble

jdgamble555 on Sunday, October 30, 2022 (last modified on Sunday, December 17, 2023)

Besides easy subscription functions, I have added more utility functions to my j-supabase package.

Now with Utility Functions#

If you use Postgres, you should be using UUID (Universally Unique Identifiers) for your ID fields. However, if you're like me and you want to use them in your path, things can get ugly.

Do you really want a URL like this:

	https://code.build/p/2f000099-3042-4513-b67d-f6092d240496/j-supabase-now-has-more-utility-functions

Some people would argue that is not much better than this:

	https://code.build/p/6ocrWnu18BM4Ly8UfunJqB/j-supabase-now-has-more-utility-functions

However, the extra characters and dashes can make a huge difference. So, added some quick functions to convert the UUID fields into Base 58 versions.

Install the package of course.

	npm i j-supabase

Import the functions.

	import { decode, encode, range } from 'j-supabase';

All you have to do is use it on your data.

Decode#

You want to decode your data before you use it on your database.

	const pid = decode(id);

const { error, data } = await supabase.from('posts').select('*').eq('id', pid);

Encode#

You want to encode your data from your database results.

	const { error, data } = await supabase.from('posts').select('*');

// map each result
data = data.length ? data.map((p) => ({ ...p, id: encode(p.id) })) : null;

Range Function for Pagination#

Sometimes you want to do Supabase Pagination, and the range() function confuses you. So, I put it in the package for reusability. Simply input the page number, and the page size, and you will get the correct range pagination numbers. It defaults at page 1 and size 3.

	// automatically calculate pagination
const { from, to } = range({ page, size });

const { error, data } = await supabase.from('posts').select('*')
  .order('created_at', { ascending: false })
  .range(from, to);

Note: I wrote my j-supabase package in order to make certain Supabase functions easier, especially Subscriptions.

Please see my previous post.

If anyone has any more ideas, just let me know on github.

J


Related Posts

© 2024 Code.Build