diff options
author | 2021-09-02 22:28:11 +0200 | |
---|---|---|
committer | 2021-09-02 23:54:56 +0200 | |
commit | f08f6ca0a9d337efff280d4d1669a41b5d9c31c2 (patch) | |
tree | 7dee778ba742deb5f499f2aa08a1ba040606d633 /configs/dbConnect.js | |
parent | 9f74c550927671f4ded301d0cf3e9d592716375c (diff) | |
download | my_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.tar.gz my_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.tar.bz2 my_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.zip |
finish translations, force maximize
Diffstat (limited to 'configs/dbConnect.js')
-rw-r--r-- | configs/dbConnect.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/configs/dbConnect.js b/configs/dbConnect.js new file mode 100644 index 0000000..0a0d200 --- /dev/null +++ b/configs/dbConnect.js @@ -0,0 +1,46 @@ +import mongoose from 'mongoose' + +const MONGODB_URI = process.env.MYAPPS_MONGODB_URI +// const MONGODB_URI = 'mongodb://localhost:27017/myapps' + +if (!MONGODB_URI) { + throw new Error( + 'MONGODB_URI missing in dbConnect' + ) +} + +/** + * Global is used here to maintain a cached connection across hot reloads + * in development. This prevents connections growing exponentially + * during API Route usage. + */ +let cached = global.mongoose + +if (!cached) { + cached = global.mongoose = {conn: null, promise: null} +} + +async function dbConnect() { + if (cached.conn) { + return cached.conn + } + + if (!cached.promise) { + const opts = { + useNewUrlParser: true, + useUnifiedTopology: true, + bufferCommands: false, + bufferMaxEntries: 0, + useFindAndModify: false, + useCreateIndex: true, + } + + cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => { + return mongoose + }) + } + cached.conn = await cached.promise + return cached.conn +} + +export default dbConnect |