Skip to content

add support for pagination #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# @radio4000/sdk

A JavaScript SDK to interact with [Radio4000](https://radio4000.com) via a browser or node.js.
A JavaScript SDK to interact with [Radio4000](https://radio4000.com) via a browser or node.js.

It offers authentication as well as full create, read, update and delete of users, channels and tracks

## Usage
## Usage

### With browser via CDN

Expand All @@ -28,8 +28,8 @@ Here's another, where we sign in (use your own credentials), create a channel an
<script type="module">
import {sdk} from 'https://cdn.jsdelivr.net/npm/@radio4000/sdk'

sdk.auth.signIn({email: '', password: '')}
sdk.auth.signIn({email: '', password: ''})

const {data: channel, error} = await sdk.channels.createChannel({
name: 'My radio',
slug: 'my-radio',
Expand Down Expand Up @@ -83,7 +83,7 @@ npm start

### Environment variables

This SDK connects to the main Radio4000 PostgreSQL database via Supabase.
This SDK connects to the main Radio4000 PostgreSQL database via Supabase.

1. `cp .env.example .env`
2. Fill out the `.env` file
Expand Down
16 changes: 16 additions & 0 deletions src/channels.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {supabase} from './main.js'
import {readUser} from './users.js'
import {getBrowsePageParams} from './utils.js'

/**
* A channel
Expand Down Expand Up @@ -112,6 +113,21 @@ export const readChannels = async (limit = 1000) => {
return supabase.from('channels').select('*').limit(limit).order('created_at', {ascending: true})
}

/**
* Returns a paginated list of channels.
* @returns {Promise<ReturnObj>}
*/

export const browseChannels = async ({page, limit}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we have readChannels(limit) and browseChannels({page, limit}.

How putting them together in readChannels({limit, page}) or readChannels(limit, page)?

Copy link
Contributor Author

@4www 4www Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sounds cool too, passing an object sounds nice to me.

Also I keep thinking about this function here https://github.com/radio4000/components/blob/main/src/components/r4-list.js#L125-L146 where it allows all models to use the same function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example what if we browse "tracks" in a radio channel, and want to order them by title?

  • do we want that?
  • do we prefer having "just a search"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, these params (page, limit) will all be also bound to the URL, I guess this won't be in the sdk though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's change readChannels then.

There must be many smart abstractions we can do. I feel for now we can experiment in r4-app by writing sdk.supabase.from() queries directly? And once we know the patterns move them to the sdk.

const { from, to, limitResults } = getBrowsePageParams({ page, limit })
return supabase
.from('channels')
.select('*')
.limit(limitResults)
.order('created_at', {ascending: true})
.range(from, to)
}

/**
* Find a Firebase channel by "slug" property
* @param {string} slug
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
converts "web component" attributes, to supabase sdk query parameters:
-> page="1" limit="1"
-> from[0] to to[0] limit[0]
*/
export const getBrowsePageParams = ({page, limit}) => {
let from, to, limitResults;
from = (page - 1) * limit
to = from + limit - 1
limitResults = limit - 1
return { from, to, limitResults }
}