Skip to content
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

get users by range #541

Merged
merged 1 commit into from
Feb 26, 2025
Merged
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
19 changes: 17 additions & 2 deletions packages/site/src/lib/supabase/cached-query-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function cached_query_data_store<T extends { id?: string, user_id?: strin
const month_year = new Date().toLocaleDateString('default', { month: '2-digit', year: 'numeric' }).replace('/', '.')
const cache_key = `${key}_${mode}_${month_year}`
let timestamp_from_which_to_fetch_data = '1971-01-01T00:00:00Z'
let range_start = 0
const limit = 1000

async function get_data_from_cache_then_db(refresh = false) {
if (log)
Expand All @@ -44,7 +46,13 @@ export function cached_query_data_store<T extends { id?: string, user_id?: strin
timestamp_from_which_to_fetch_data = cached_or_materialized[cached_or_materialized.length - 1][order_field] as string
const query = materialized_query
.order(order_field, { ascending: true })
.gt(order_field, timestamp_from_which_to_fetch_data)

if (order_field === 'id') {
query.range(range_start, range_start + limit)
range_start += limit
} else {
query.gt(order_field, timestamp_from_which_to_fetch_data)
}
// if (!cached_data?.length) {
// query.is('deleted', null)
// }
Expand Down Expand Up @@ -77,7 +85,14 @@ export function cached_query_data_store<T extends { id?: string, user_id?: strin
const query = live_query
.limit(1000)
.order(order_field, { ascending: true })
.gt(order_field, timestamp_from_which_to_fetch_data)

if (order_field === 'id') {
query.range(range_start, range_start + limit)
range_start += limit
} else {
query.gt(order_field, timestamp_from_which_to_fetch_data)
}

// if (!cached_or_materialized?.length) {
// query.is('deleted', null)
// }
Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/routes/admin/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
await data.public_dictionaries.reset()
await data.other_dictionaries.reset()
await data.dictionary_roles.reset()
await data.users.refresh()
await data.users.reset()
location.reload()
}}>Reset cache (after public/private toggle, remove editor)</Button>
</nav>
Expand Down
1 change: 1 addition & 0 deletions packages/site/src/routes/admin/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const load = (async ({ parent }) => {
live_query: supabase.rpc('users_for_admin_table')
.select(),
key: 'users',
order_field: 'id',
})

const dictionary_roles = cached_query_data_store<Tables<'dictionary_roles'>>({
Expand Down
8 changes: 8 additions & 0 deletions supabase/migrations/20250225151251_able-to-delete-users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE user_data
DROP CONSTRAINT user_data_id_fkey;

ALTER TABLE user_data
ADD CONSTRAINT user_data_id_fkey
FOREIGN KEY (id)
REFERENCES auth.users(id)
ON DELETE CASCADE;
2 changes: 1 addition & 1 deletion supabase/summarized-migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ FROM auth.users;
REVOKE ALL ON public.profiles_view FROM anon, authenticated, public;

CREATE TABLE user_data (
id uuid references auth.users not null primary key,
id uuid references auth.users not null primary key ON DELETE CASCADE,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
welcome_email_sent timestamp with time zone,
unsubscribed_from_emails timestamp with time zone,
Expand Down
Loading