init from origin + update
This commit is contained in:
35
server/utils/common.js
Normal file
35
server/utils/common.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const getAnswer = (errors, data, success = true) => {
|
||||
if (errors) {
|
||||
return {
|
||||
success: false,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success,
|
||||
body: data,
|
||||
}
|
||||
}
|
||||
|
||||
const getResponse = (errors, data, success = true) => {
|
||||
if (errors.length) {
|
||||
return {
|
||||
success: false,
|
||||
errors,
|
||||
warnings: [],
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success,
|
||||
body: data,
|
||||
errors: [],
|
||||
warnings: [],
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAnswer,
|
||||
getResponse,
|
||||
}
|
||||
34
server/utils/mongo.js
Normal file
34
server/utils/mongo.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const MDBClient = require('mongodb').MongoClient
|
||||
|
||||
const rc = require('../../.serverrc')
|
||||
|
||||
// Connection URL
|
||||
const url = `mongodb://${rc.mongoAddr}:${rc.mongoPort}`
|
||||
|
||||
const dbInstanses = {
|
||||
}
|
||||
const mongoDBConnect = async () => {
|
||||
try {
|
||||
const MongoClient = new MDBClient(url, {
|
||||
useUnifiedTopology: true,
|
||||
})
|
||||
return await MongoClient.connect()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
const client = mongoDBConnect()
|
||||
|
||||
const getDB = async (dbName) => {
|
||||
try {
|
||||
const cl = await client
|
||||
dbInstanses[dbName] = await cl.db(dbName)
|
||||
return dbInstanses[dbName]
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getDB,
|
||||
}
|
||||
Reference in New Issue
Block a user