Created
May 11, 2023 06:47
-
-
Save sumitpatel93/1f0fbb585a5548619cff808a74fcc66d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MongoClient } = require('mongodb'); | |
const { fetchDuneData } = require('./duneAPI'); | |
const queryConfig = require('./queryConfig.json'); | |
const uri = 'mongodb+srv://<username>:<password>@cluster0.11spvpz.mongodb.net/?retryWrites=true&w=majority'; | |
const client = new MongoClient(uri); | |
async function connectToDB() { | |
try { | |
await client.connect(); | |
console.log('Connected to MongoDB'); | |
const { queries, apiKey } = queryConfig; | |
for (const { queryId } of queries) { | |
const duneData = await fetchDuneData(queryId, apiKey); | |
const db = client.db('defiData'); | |
const collection = db.collection(queryId); | |
await collection.insertMany(duneData); | |
console.log(`Inserted ${duneData.length} documents for query ${queryId} into MongoDB`); | |
} | |
} catch (err) { | |
console.error(err); | |
} finally { | |
await client.close(); | |
console.log('Disconnected from MongoDB'); | |
} | |
} | |
connectToDB(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment