Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
final mongodb fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9play committed Mar 4, 2020
1 parent 1fac3d0 commit a725ebb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
34 changes: 34 additions & 0 deletions api/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import mongoose from 'mongoose'

const postSchema = new mongoose.Schema({
title: String,
content: String,
author: String,
userId: String,
url: String,
encryptionEnabled: Boolean,
expireAt: Date
}, { timestamps: true })

export const Post = mongoose.model("Post", postSchema)

let cachedDb = null

console.log('outside-cachedDB:', cachedDb)
async function connectToDatabase(uri) {
if (cachedDb) {
console.log('=> using cached database instance')
return cachedDb
}

const db = await mongoose.connect(uri, { useNewUrlParser: true })

console.log('New MongoDB Connected')

cachedDb = db
return db
}

export const mongoo = async () => {
await connectToDatabase(process.env.MONGODB_URI)
}
42 changes: 17 additions & 25 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import express from 'express'
import mongoose from 'mongoose'
import { mongoo, Post } from './db'
import Joi from 'joi'
import { slugify } from 'transliteration'
import moment from 'moment'
import bodyParser from 'body-parser'

mongoose.connect(
`mongodb://${process.env.MONGODB_HOST}`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
user: process.env.MONGODB_LOGIN,
pass: process.env.MONGODB_PASS,
dbName: 'tinypaste',
},
err => { throw err; },
);
mongoo()

// mongoose.connect(
// `mongodb://${process.env.MONGODB_HOST}`,
// {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useFindAndModify: false,
// useCreateIndex: true,
// user: process.env.MONGODB_LOGIN,
// pass: process.env.MONGODB_PASS,
// dbName: 'tinypaste',
// },
// err => { throw err; },
// )

const app = express()

//схема для постов
const postSchema = new mongoose.Schema({
title: String,
content: String,
author: String,
userId: String,
url: String,
encryptionEnabled: Boolean,
expireAt: Date
}, { timestamps: true })

const Post = mongoose.model("Post", postSchema)

const urlencodedParser = bodyParser.urlencoded({ extended: false })


Expand Down
4 changes: 1 addition & 3 deletions now.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
}
],
"env": {
"MONGODB_HOST": "@mongodb_host",
"MONGODB_LOGIN": "@mongodb_login",
"MONGODB_PASS": "@mongodb_pass"
"MONGODB_URI": "@mongodb_uri"
}
}

1 comment on commit a725ebb

@vercel
Copy link

@vercel vercel bot commented on a725ebb Mar 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.