Repository navigation
Expand file tree
/
Copy pathworkspace-query.js
More file actions
50 lines (44 loc) · 1.56 KB
/
Copy pathworkspace-query.js
File metadata and controls
50 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Build scoped Supabase reads and collect paginated record sets. */
(() => {
"use strict";
function create({ getClient }) {
function client() {
const resolved = getClient();
if (!resolved)
throw new Error("Workspace data is unavailable until sign-in.");
return resolved;
}
function runWorkspaceRead(workspaceId, read) {
const currentClient = client();
if (read.rpc) return currentClient.rpc(read.rpc);
let query = currentClient
.from(read.table)
.select("*")
.eq("user_id", workspaceId);
for (const [column, ascending] of read.order || [])
query = query.order(column, { ascending });
if (read.limit) query = query.limit(read.limit);
return query;
}
function loadWorkspaceId() {
return client().rpc("pd_workspace_id");
}
async function loadAllPages(table, pageSize = 500) {
if (!Number.isSafeInteger(pageSize) || pageSize < 1)
throw new RangeError("Page size must be a positive integer.");
const currentClient = client();
const rows = [];
for (let offset = 0; ; offset += pageSize) {
const { data, error } = await currentClient
.from(table)
.select("*")
.range(offset, offset + pageSize - 1);
if (error) throw error;
rows.push(...(data || []));
if (!data || data.length < pageSize) return rows;
}
}
return Object.freeze({ runWorkspaceRead, loadWorkspaceId, loadAllPages });
}
window.PropertyDeskWorkspaceQuery = Object.freeze({ create });
})();