-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (52 loc) · 1.6 KB
/
index.js
File metadata and controls
57 lines (52 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { MongoClient, ObjectId } from 'mongodb'
import dotenv from "dotenv"
dotenv.config()
const client = new MongoClient(process.env.MONGO_CONNECTION_STRING)
const newID = () => new ObjectId().toHexString()
const isValidID = (id) => ObjectId.isValid(id)
const connected = async function () {
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 }).catch(err => err)
return true
}
const db = client.db(process.env.MONGODBNAME)?.collection(process.env.MONGODBCOLLECTION)
const connect = async () => {
await client.connect()
console.dir({
db : process.env.MONGODBNAME,
coll : process.env.MONGODBCOLLECTION
})
}
connect().catch(console.dir)
/**
* Find a single record based on a query object.
* @param {JSON} matchDoc Query Object to match properties.
* @param {JSON} options Just mongodb passthru for now
* @param {function} callback Callback function if needed
* @returns Single matched document or `null` if there is none found.
* @throws MongoDB error if matchDoc is malformed or server is unreachable; E11000 duplicate key error collection
*/
function getMatching(matchDoc, options, callback) {
return db.findOne(matchDoc, options, (err, doc) => {
if (typeof callback === 'function') return callback(err, doc)
if (err) throw err
return doc
})
}
function isObject(obj) {
return obj?.constructor == Object
}
function isValidURL(url) {
try {
new URL(url)
return true
} catch (_) {
return false
}
}
export {
newID,
isValidID,
connected,
db
}