Skip to content

Instantly share code, notes, and snippets.

@sumitpatel93
Created May 11, 2023 06:47
Show Gist options
  • Save sumitpatel93/1f0fbb585a5548619cff808a74fcc66d to your computer and use it in GitHub Desktop.
Save sumitpatel93/1f0fbb585a5548619cff808a74fcc66d to your computer and use it in GitHub Desktop.
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