Last active
April 7, 2023 16:17
-
-
Save john-guerra/bedc5b69fff5fdabcf398f2adefe5abf to your computer and use it in GitHub Desktop.
MongoClient Server Code
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
module.exports = { | |
env: { | |
browser: true, | |
es6: true, | |
node: true, | |
}, | |
extends: ["eslint:recommended", "prettier"], | |
parserOptions: { | |
ecmaVersion: "latest", | |
sourceType: "module", | |
ecmaFeatures: { | |
jsx: true, | |
}, | |
}, | |
rules: { | |
indent: ["error", 2, { SwitchCase: 1 }], | |
"linebreak-style": ["error", "unix"], | |
quotes: ["error", "double"], | |
semi: ["error", "always"], | |
"no-console": 0, | |
}, | |
}; |
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
.DS_Store | |
node_modules/** |
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
Boilerplate code for helping connecting to Mongo locally from Observable, as detailed on this Notebook https://observablehq.com/@kasivisu4/mongodb-local |
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
import { MongoClient } from "mongodb"; | |
function MyMongoDB() { | |
const myDB = {}; | |
let client; | |
myDB.init = async (url) => { | |
try { | |
client = new MongoClient(url); | |
await client.connect(); | |
return { success: true }; | |
} catch (err) { | |
console.log(err); | |
return { success: false }; | |
} | |
}; | |
myDB.find = async (dbName, collectionName, query = {}) => { | |
try { | |
const db = client.db(dbName); | |
const collection = db.collection(collectionName); | |
let res = await collection.find(query).toArray(); | |
return res; | |
} catch (err) { | |
console.error(err); | |
return err; | |
} | |
}; | |
myDB.insert = async (dbName, collectionName, data = {}) => { | |
try { | |
const db = client.db(dbName); | |
const collection = await db.collection(collectionName); | |
console.log(data); | |
let res = await collection.insertMany(data); | |
return res; | |
} catch (err) { | |
console.error(err); | |
return err; | |
} | |
}; | |
myDB.delete = async (dbName, collectionName, query = {}) => { | |
try { | |
const db = client.db(dbName); | |
const collection = db.collection(collectionName); | |
let res = await collection.deleteMany(query); | |
return res; | |
} catch (err) { | |
console.error(err); | |
return err; | |
} | |
}; | |
myDB.closeConnection = async () => { | |
client.close(); | |
}; | |
return myDB; | |
} | |
export const databaseManager = MyMongoDB(); |
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
{ | |
"name": "mongolocal", | |
"version": "1.0.0", | |
"description": "", | |
"main": "mongoDBClient.js", | |
"type": "module", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node ./node_modules/nodemon/bin/nodemon.js server.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"cors": "^2.8.5", | |
"express": "^4.18.2", | |
"mongodb": "^5.2.0", | |
"nodemon": "^2.0.22" | |
}, | |
"devDependencies": { | |
"eslint": "^8.37.0", | |
"eslint-config-prettier": "^8.8.0", | |
"prettier": "^2.8.7" | |
} | |
} |
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
import express from "express"; | |
import { databaseManager } from "./mongoDBClient.js"; | |
import bodyParser from "body-parser"; | |
import cors from "cors"; | |
const app = express(); | |
let corsOptions = { | |
origin: function (origin, callback) { | |
// Accept connections from Observable | |
if (origin.endsWith("static.observableusercontent.com")) { | |
callback(null, true) | |
} else { | |
callback(new Error('Not allowed by CORS')) | |
} | |
} | |
} | |
app.use(cors(corsOptions)); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
const PORT = 5001; | |
app.post("/api/init", async (req, res) => { | |
let body = req.body; | |
console.log(body); | |
let { url } = body; | |
console.log("Received init request", url); | |
let result = await databaseManager.init(url); | |
res.status(200).json(result); | |
}); | |
app.post("/api/insert", async (req, res) => { | |
let { db, collection, data } = req.body; | |
console.log("Received Insert request", db, collection, data); | |
let result = await databaseManager.insert(db, collection, data); | |
res.status(200).json(result); | |
}); | |
app.post("/api/find", async (req, res) => { | |
let { db, collection, query } = req.body; | |
console.log("Received Find request", db, collection, query); | |
let result = await databaseManager.find(db, collection, query); | |
res.status(200).json(result); | |
}); | |
app.get("/api/closeConnection", async (req, res) => { | |
console.log("Received Close Connection request"); | |
let result = await databaseManager.closeConnection(); | |
res.status(200).json(result); | |
}); | |
app.post("/api/delete", async (req, res) => { | |
let { db, collection, query } = req.body; | |
console.log("Received Find request", db, collection, query); | |
let result = await databaseManager.delete(db, collection, query); | |
res.status(200).json(result); | |
}); | |
// app.get("/api/delete", async (req, res) => { | |
// let body = req.body; | |
// console.log("Received Delete request", body); | |
// let result = await databaseManager.delete(); | |
// res.status(200).json(result); | |
// }); | |
app.listen(PORT, console.log(`Mongodb Server running on port ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment