Branchwell

AI Resource Pro / website / scripts / count-news-items.ts

1import { createClient } from '@supabase/supabase-js';
2import * as dotenv from 'dotenv';
3import * as path from 'path';
4import * as fs from 'fs';
5
6// Load environment variables
7const envLocalPath = path.join(process.cwd(), '.env.local');
8const envPath = path.join(process.cwd(), '.env');
9
10if (fs.existsSync(envLocalPath)) {
11 console.log(`Loading environment from: ${envLocalPath}`);
12 dotenv.config({ path: envLocalPath });
13} else if (fs.existsSync(envPath)) {
14 console.log(`Loading environment from: ${envPath}`);
15 dotenv.config({ path: envPath });
16} else {
17 console.log('No .env or .env.local file found');
18}
19
20async function main() {
21 // Check for required environment variables
22 const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
23 const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
24
25 if (!supabaseUrl || !supabaseServiceKey) {
26 console.error('Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and/or SUPABASE_SERVICE_ROLE_KEY');
27 process.exit(1);
28 }
29
30 // Initialize Supabase client
31 const supabase = createClient(supabaseUrl, supabaseServiceKey);
32 console.log('Supabase client initialized');
33
34 try {
35 // Count news items
36 const { count: newsCount, error: newsError } = await supabase
37 .from('news')
38 .select('*', { count: 'exact', head: true });
39
40 if (newsError) {
41 console.error('Error counting news items:', newsError.message);
42 process.exit(1);
43 }
44
45 console.log(`Total news items in database: ${newsCount}`);
46
47 // Count news_categories items
48 const { count: newsCategoriesCount, error: newsCategoriesError } = await supabase
49 .from('news_categories')
50 .select('*', { count: 'exact', head: true });
51
52 if (newsCategoriesError) {
53 console.error('Error counting news_categories items:', newsCategoriesError.message);
54 process.exit(1);
55 }
56
57 console.log(`Total news_categories items in database: ${newsCategoriesCount}`);
58
59 // Count categories
60 const { count: categoriesCount, error: categoriesError } = await supabase
61 .from('categories')
62 .select('*', { count: 'exact', head: true });
63
64 if (categoriesError) {
65 console.error('Error counting categories:', categoriesError.message);
66 process.exit(1);
67 }
68
69 console.log(`Total categories in database: ${categoriesCount}`);
70 } catch (error) {
71 console.error('Unexpected error:', error instanceof Error ? error.message : String(error));
72 process.exit(1);
73 }
74}
75
76main().catch(err => {
77 console.error('Unhandled error in main function:', err);
78 process.exit(1);
79});