Created
December 6, 2024 19:01
-
-
Save neewy/65b3da65a942a4bc93eff4592b47ab90 to your computer and use it in GitHub Desktop.
QuickNode Function to stream data to a MongoDB instance
This file contains hidden or 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
| // ================================================================= | |
| // SETUP INSTRUCTIONS | |
| // ================================================================= | |
| // 1. Start MongoDB locally using Docker: | |
| // Quick start without auth: | |
| // docker run -d --name mongodb -p 27017:27017 mongo:latest | |
| // | |
| // With authentication (recommended): | |
| // docker run -d --name mongodb \ | |
| // -e MONGO_INITDB_ROOT_USERNAME=admin \ | |
| // -e MONGO_INITDB_ROOT_PASSWORD=your_password_here \ | |
| // -p 27017:27017 \ | |
| // mongo:latest | |
| // | |
| // 2. (Optional) Start Mongo Express UI: | |
| // docker run -d --name mongo-express \ | |
| // -p 8081:8081 \ | |
| // -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \ | |
| // -e ME_CONFIG_MONGODB_ADMINPASSWORD=your_password_here \ | |
| // -e ME_CONFIG_MONGODB_SERVER=host.docker.internal \ | |
| // mongo-express | |
| // Then access UI at: http://localhost:8081 -> admin/pass | |
| // | |
| // 3. Install ngrok and expose MongoDB: | |
| // ngrok tcp 27017 | |
| // This will give you a URL like: tcp://2.tcp.ngrok.io:12345 | |
| // ================================================================= | |
| const https = require('https'); | |
| const { MongoClient } = require('mongodb'); //also mongoose is supported | |
| // Update this URI with your ngrok URL or local MongoDB URL | |
| // Format for local: mongodb://admin:password@localhost:27017 | |
| // Format for ngrok: mongodb://admin:[email protected]:12345 | |
| const MONGODB_URI = 'mongodb://admin:[email protected]:15786'; | |
| // Reusable connection function | |
| async function connectMongo() { | |
| const client = new MongoClient(MONGODB_URI, { | |
| maxPoolSize: 10, | |
| minPoolSize: 1 | |
| }); | |
| await client.connect(); | |
| return client; | |
| } | |
| async function main(params) { | |
| const userData = params.user_data; | |
| const payload = { | |
| user_data: userData, | |
| payload: params, | |
| timestamp: new Date() | |
| }; | |
| try { | |
| const client = await connectMongo(); | |
| const collection = client.db('quicknode_data').collection('stream_data'); | |
| // Store the data and get the result | |
| const result = await collection.insertOne(payload); | |
| await client.close(); | |
| // Return successful operation details | |
| return { | |
| success: true, | |
| operation: 'insert', | |
| documentId: result.insertedId.toString(), | |
| timestamp: new Date(), | |
| collection: 'stream_data', | |
| database: 'quicknode_data' | |
| }; | |
| } catch (error) { | |
| // Return error details | |
| return { | |
| success: false, | |
| error: error.message, | |
| errorCode: error.code, | |
| timestamp: new Date(), | |
| collection: 'stream_data', | |
| database: 'quicknode_data' | |
| }; | |
| } | |
| } | |
| module.exports = { main }; | |
| // ================================================================= | |
| // TROUBLESHOOTING | |
| // ================================================================= | |
| // 1. Check Docker container status: | |
| // docker ps | |
| // docker logs mongodb | |
| // | |
| // 2. Test MongoDB connection: | |
| // mongosh mongodb://localhost:27017 | |
| // or with auth: | |
| // mongosh mongodb://admin:your_password_here@localhost:27017 | |
| // | |
| // 3. Common issues: | |
| // - If using auth, make sure credentials match in all places | |
| // - When using ngrok, the URL changes each time you restart | |
| // - If Mongo Express can't connect, check if 'host.docker.internal' resolves | |
| // Alternative: use container IP or network | |
| // ================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment