|
1 | | -// data.ts |
2 | | -import PocketBase from 'pocketbase'; |
| 1 | + // data.ts |
| 2 | + import PocketBase from 'pocketbase'; |
| 3 | + import dotenv from "dotenv"; |
3 | 4 |
|
4 | | -const pb = new PocketBase('http://127.0.0.1:8090'); |
5 | | -pb.autoCancellation(false); |
| 5 | + |
6 | 6 |
|
7 | | -let isAuthenticated = false; |
| 7 | + const ADMIN_EMAIL: string = import.meta.env.VITE_EMAIL; |
| 8 | + const ADMIN_PASSWORD: string = import.meta.env.VITE_PASSWORD; |
8 | 9 |
|
9 | | -const ADMIN_EMAIL = 'test'; |
10 | | -const ADMIN_PASSWORD = 'test'; |
| 10 | + console.log(ADMIN_EMAIL); |
| 11 | + |
| 12 | + const pb = new PocketBase('http://127.0.0.1:8090'); |
| 13 | + pb.autoCancellation(false); |
| 14 | + |
| 15 | + let isAuthenticated = false; |
11 | 16 |
|
12 | | -// Function to authenticate the admin user |
13 | | -export async function authenticate() { |
14 | | - if (!isAuthenticated) { |
15 | | - await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD); |
16 | | - isAuthenticated = true; |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -type RecordData = { [key: string]: any }; |
21 | | -export type AllData = { [collectionName: string]: RecordData[] }; |
22 | | - |
23 | | -// Fetch paginated data with existing logic |
24 | | -export async function fetchPaginatedData( |
25 | | - collectionName: string, |
26 | | - sendToChart: (data: RecordData[]) => void, |
27 | | - batchSize: number = 10 |
28 | | -) { |
29 | | - console.log(`Fetching data from collection: ${collectionName}`); |
30 | | - let page = 1; |
31 | | - let hasMoreData = true; |
32 | | - |
33 | | - try { |
34 | | - await authenticate(); // Authenticate once before fetching data |
35 | | - |
36 | | - while (hasMoreData) { |
37 | | - console.log('Fetching data from collection:', collectionName, 'Page:', page); |
38 | | - const records = await pb.collection(collectionName).getList(page, batchSize); |
39 | | - console.log('Fetched records:', records.items); |
40 | | - |
41 | | - if (records.items.length === 0) { |
42 | | - console.warn(`No records found for ${collectionName} on page ${page}.`); |
43 | | - break; |
44 | | - } |
45 | 17 |
|
46 | | - const dynamicKeys = Object.keys(records.items[0]).filter(key => key !== 'id' && key !== 'created'); |
47 | 18 |
|
48 | | - const transformedBatch: RecordData[] = records.items.map((record: RecordData) => { |
49 | | - const transformedRecord: RecordData = {}; |
50 | | - dynamicKeys.forEach(key => { |
51 | | - transformedRecord[key] = record[key]; |
| 19 | + // Function to authenticate the admin user |
| 20 | + export async function authenticate() { |
| 21 | + if (!isAuthenticated) { |
| 22 | + await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD); |
| 23 | + isAuthenticated = true; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + type RecordData = { [key: string]: any }; |
| 28 | + export type AllData = { [collectionName: string]: RecordData[] }; |
| 29 | + |
| 30 | + // Fetch paginated data with existing logic |
| 31 | + export async function fetchPaginatedData( |
| 32 | + collectionName: string, |
| 33 | + sendToChart: (data: RecordData[]) => void, |
| 34 | + batchSize: number = 10 |
| 35 | + ) { |
| 36 | + console.log(`Fetching data from collection: ${collectionName}`); |
| 37 | + let page = 1; |
| 38 | + let hasMoreData = true; |
| 39 | + |
| 40 | + try { |
| 41 | + await authenticate(); // Authenticate once before fetching data |
| 42 | + |
| 43 | + while (hasMoreData) { |
| 44 | + console.log('Fetching data from collection:', collectionName, 'Page:', page); |
| 45 | + const records = await pb.collection(collectionName).getList(page, batchSize); |
| 46 | + console.log('Fetched records:', records.items); |
| 47 | + |
| 48 | + if (records.items.length === 0) { |
| 49 | + console.warn(`No records found for ${collectionName} on page ${page}.`); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + const dynamicKeys = Object.keys(records.items[0]).filter(key => key !== 'id' && key !== 'created'); |
| 54 | + |
| 55 | + const transformedBatch: RecordData[] = records.items.map((record: RecordData) => { |
| 56 | + const transformedRecord: RecordData = {}; |
| 57 | + dynamicKeys.forEach(key => { |
| 58 | + transformedRecord[key] = record[key]; |
| 59 | + }); |
| 60 | + return transformedRecord; |
52 | 61 | }); |
53 | | - return transformedRecord; |
54 | | - }); |
55 | 62 |
|
56 | | - console.log('Transformed batch:', transformedBatch); |
57 | | - sendToChart(transformedBatch); |
| 63 | + console.log('Transformed batch:', transformedBatch); |
| 64 | + sendToChart(transformedBatch); |
58 | 65 |
|
59 | | - if (records.items.length < batchSize) { |
60 | | - hasMoreData = false; |
61 | | - } else { |
62 | | - page += 1; |
| 66 | + if (records.items.length < batchSize) { |
| 67 | + hasMoreData = false; |
| 68 | + } else { |
| 69 | + page += 1; |
| 70 | + } |
63 | 71 | } |
| 72 | + console.log('All data fetched for collection:', collectionName); |
| 73 | + } catch (error) { |
| 74 | + console.error('Error fetching paginated data for collection:', collectionName, error); |
64 | 75 | } |
65 | | - console.log('All data fetched for collection:', collectionName); |
66 | | - } catch (error) { |
67 | | - console.error('Error fetching paginated data for collection:', collectionName, error); |
68 | 76 | } |
69 | | -} |
70 | | - |
71 | | -// Real-time subscription to a collection |
72 | | -export async function subscribeToCollection( |
73 | | - collectionName: string, |
74 | | - handleDataUpdate: (data: RecordData) => void |
75 | | -) { |
76 | | - try { |
77 | | - await authenticate(); // Ensure we're authenticated before subscribing |
78 | | - |
79 | | - // Subscribe to the collection for any changes |
80 | | - pb.collection(collectionName).subscribe('*', function (e) { |
81 | | - console.log(`Received real-time update for collection ${collectionName}:`, e.record); |
82 | | - handleDataUpdate(e.record); |
83 | | - }); |
84 | | - |
85 | | - console.log(`Subscribed to real-time updates for collection: ${collectionName}`); |
86 | | - } catch (error) { |
87 | | - console.error(`Error subscribing to collection ${collectionName}:`, error); |
| 77 | + |
| 78 | + // Real-time subscription to a collection |
| 79 | + export async function subscribeToCollection( |
| 80 | + collectionName: string, |
| 81 | + handleDataUpdate: (data: RecordData) => void |
| 82 | + ) { |
| 83 | + try { |
| 84 | + await authenticate(); // Ensure we're authenticated before subscribing |
| 85 | + |
| 86 | + // Subscribe to the collection for any changes |
| 87 | + pb.collection(collectionName).subscribe('*', function (e) { |
| 88 | + console.log(`Received real-time update for collection ${collectionName}:`, e.record); |
| 89 | + handleDataUpdate(e.record); |
| 90 | + }); |
| 91 | + |
| 92 | + console.log(`Subscribed to real-time updates for collection: ${collectionName}`); |
| 93 | + } catch (error) { |
| 94 | + console.error(`Error subscribing to collection ${collectionName}:`, error); |
| 95 | + } |
88 | 96 | } |
89 | | -} |
90 | 97 |
|
91 | | -// Function to unsubscribe from a collection (optional) |
92 | | -export async function unsubscribeFromCollection(collectionName: string) { |
93 | | - try { |
94 | | - await authenticate(); // Ensure we're authenticated before unsubscribing |
| 98 | + // Function to unsubscribe from a collection (optional) |
| 99 | + export async function unsubscribeFromCollection(collectionName: string) { |
| 100 | + try { |
| 101 | + await authenticate(); // Ensure we're authenticated before unsubscribing |
95 | 102 |
|
96 | | - // Unsubscribe from the collection |
97 | | - pb.collection(collectionName).unsubscribe('*'); |
| 103 | + // Unsubscribe from the collection |
| 104 | + pb.collection(collectionName).unsubscribe('*'); |
98 | 105 |
|
99 | | - console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`); |
100 | | - } catch (error) { |
101 | | - console.error(`Error unsubscribing from collection ${collectionName}:`, error); |
| 106 | + console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`); |
| 107 | + } catch (error) { |
| 108 | + console.error(`Error unsubscribing from collection ${collectionName}:`, error); |
| 109 | + } |
102 | 110 | } |
103 | | -} |
|
0 commit comments