Pepguy commited on
Commit
ea4a035
·
verified ·
1 Parent(s): 40194b1

Update db.js

Browse files
Files changed (1) hide show
  1. db.js +19 -55
db.js CHANGED
@@ -1,78 +1,42 @@
1
- const admin = require('firebase-admin');
2
  const MiniSearch = require('minisearch');
 
3
 
4
- // 1. Initialize Firestore (Exact logic from your paystack-webhook.js)
5
- const { FIREBASE_CREDENTIALS } = process.env;
6
-
7
- if (!admin.apps.length) {
8
- try {
9
- if (FIREBASE_CREDENTIALS) {
10
- // Parse the JSON string from ENV
11
- const svc = JSON.parse(FIREBASE_CREDENTIALS);
12
-
13
- // Initialize exactly like your working code
14
- admin.initializeApp({
15
- credential: admin.credential.cert(svc),
16
- databaseURL: "https://agentq-io-default-rtdb.firebaseio.com"
17
-
18
- });
19
-
20
- console.log('🔥 Firebase admin initialized successfully.');
21
-
22
- console.log(admin.apps.length);
23
- } else {
24
- console.warn('⚠️ FIREBASE_CREDENTIALS not provided.');
25
- }
26
- } catch (e) {
27
- console.error('❌ Failed to init Firebase admin:', e.message);
28
- process.exit(1); // Crash hard if creds are bad so we know immediately
29
- }
30
- }
31
-
32
- const db = admin.firestore();
33
-
34
- const TOOLS_COLLECTION = 'mcp_tools';
35
 
36
  // 2. Initialize In-Memory Search Engine
37
  const searchEngine = new MiniSearch({
38
  fields: ['name', 'description', 'keywords'],
39
- storeFields: ['id', 'name', 'description', 'url', 'install_command', 'stats', 'reputation', 'analytics'],
40
- searchOptions: {
41
- boost: { name: 2, keywords: 1.5 },
42
- fuzzy: 0.2,
43
- prefix: true
44
- }
45
  });
46
 
47
- // 3. Hydrate RAM from Disk
48
  async function hydrateMemory() {
49
- console.log("💧 Hydrating In-Memory Index...");
50
  try {
51
- const snapshot = await db.collection(TOOLS_COLLECTION).get();
52
-
53
- // Handle empty DB case gracefully
54
- if (snapshot.empty) {
55
- console.log("⚠️ Firestore is empty. Run scraper to populate.");
56
- return;
57
- }
58
 
59
- const tools = [];
60
- snapshot.forEach(doc => {
61
- tools.push(doc.data());
62
- });
63
 
64
  searchEngine.removeAll();
65
- searchEngine.addAll(tools);
66
- console.log(`✅ Loaded ${tools.length} MCP tools into RAM.`);
 
 
67
  } catch (error) {
68
  console.error("❌ Hydration Failed:", error.message);
69
  }
70
  }
71
 
72
- // 4. Fast Search Wrapper
73
  function searchTools(query) {
74
  if (searchEngine.documentCount === 0) return [];
75
  return searchEngine.search(query);
76
  }
77
 
78
- module.exports = { db, searchEngine, hydrateMemory, searchTools, TOOLS_COLLECTION };
 
1
+ const { createClient } = require('@supabase/supabase-js');
2
  const MiniSearch = require('minisearch');
3
+ // require('dotenv').config();
4
 
5
+ // 1. Initialize Supabase
6
+ const supabaseUrl = process.env.SUPABASE_URL;
7
+ const supabaseKey = process.env.SUPABASE_KEY;
8
+ const supabase = createClient(supabaseUrl, supabaseKey);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  // 2. Initialize In-Memory Search Engine
11
  const searchEngine = new MiniSearch({
12
  fields: ['name', 'description', 'keywords'],
13
+ storeFields: ['id', 'name', 'description', 'url', 'install_command', 'stars', 'reputation_score'],
14
+ searchOptions: { fuzzy: 0.2, prefix: true }
 
 
 
 
15
  });
16
 
17
+ // 3. Hydrate RAM from Supabase
18
  async function hydrateMemory() {
19
+ console.log("💧 Hydrating In-Memory Index from Supabase...");
20
  try {
21
+ const { data, error } = await supabase
22
+ .from('mcp_tools')
23
+ .select('*');
 
 
 
 
24
 
25
+ if (error) throw error;
 
 
 
26
 
27
  searchEngine.removeAll();
28
+ if (data && data.length > 0) {
29
+ searchEngine.addAll(data);
30
+ }
31
+ console.log(`✅ Loaded ${data.length} tools into RAM.`);
32
  } catch (error) {
33
  console.error("❌ Hydration Failed:", error.message);
34
  }
35
  }
36
 
 
37
  function searchTools(query) {
38
  if (searchEngine.documentCount === 0) return [];
39
  return searchEngine.search(query);
40
  }
41
 
42
+ module.exports = { supabase, searchEngine, hydrateMemory, searchTools };