Login
Tutorials

Start learning from the beginning.

J-Supabase Now Has More Utility Functions

J
Jonathan Gamble
Published Updated 4 min read

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:

Text
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:

Text
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.

Text
npm i j-supabase

Import the functions.

Text
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.

JavaScript
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.

JavaScript
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.

JavaScript
// 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

Comments

Share a thought or join the conversation.

Sign In to comment or reply.

Published Updated 9 min read
Published Updated 4 min read
Published Updated 3 min read
Published Updated 7 min read
Newsletter

Get updates on future FREE tutorials, paid courses, and new blog posts!

Table of Contents

Select a title to jump. Use arrows to expand.

Tags

Explore posts by topic.