Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set connection on database with mongoDB #23

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions db/models/File.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Schema, model, models } from 'mongoose'

const fileSchema = new Schema({
name: {
type: String,
trim: true,
required: true,
},
content: {
type: String,
},
playground_id: {
required: true,
type: Schema.Types.ObjectId,
ref: 'Playground'
}
})

export default models.File || model('File', fileSchema)
25 changes: 25 additions & 0 deletions db/models/Playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Schema, model, models } from 'mongoose'

const playgroundSchema = new Schema({
language: {
type: String,
trim: true,
required: true,
},
extension: {
type: String,
required: true,
trim: true,
},
files: [{
type: Schema.Types.ObjectId,
ref: 'File'
}],
user_id: {
required: true,
type: Schema.Types.ObjectId,
ref: 'User'
}
}, { timestamps: true, versionKey: false })

export default models.Playground || model('Playground', playgroundSchema)
26 changes: 26 additions & 0 deletions db/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Schema, model, models } from 'mongoose'

const userSchema = new Schema({
username: {
type: String,
trim: true,
required: [true, 'Username is required'],
unique: true
},
email: {
type: String,
trim: true,
required: true,
unique: true
},
photoUrl: {
type: String,
trim: true,
},
playgrounds: [{
type: Schema.Types.ObjectId,
ref: 'Playground'
}]
}, { timestamps: true, versionKey: false })

export default models.User || model('User', userSchema)
30 changes: 30 additions & 0 deletions db/utils/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { connect, connection } from 'mongoose'

const { DB_USERNAME, DB_PASSWORD, DB_CLUSTER_NAME, DB_NAME } = process.env
const connectionString = `mongodb+srv://${DB_USERNAME}:${DB_PASSWORD}@${DB_CLUSTER_NAME}/${DB_NAME}?retryWrites=true&w=majority`

if (connectionString === '') {
throw new Error('ConnectionString: Please define the process.env[variables] environment variable inside .env.local file')
}

const conn = {
isConnected: false,
}

export async function dbConnect() {
try {
if (conn.isConnected) return
const db = await connect(connectionString)
conn.isConnected = db.connections[0].readyState
} catch (error) {
console.error(error.message)
}
}

connection.on('connected', () => {
console.log('MongoDB is connected')
})

connection.on('error', (error) => {
console.error(error.message)
})
8 changes: 8 additions & 0 deletions pages/api/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextApiResponse, NextApiRequest } from 'next'
/**
* @param {NextApiRequest} req
* @param {NextApiResponse} res
*/
export default async function handler(req, res) {
res.status(200).json('pong')
}
29 changes: 29 additions & 0 deletions pages/api/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { dbConnect } from "db/utils/database"
import User from 'db/models/User'

dbConnect()
/**
* @param {NextApiRequest} req
* @param {NextApiResponse} res
*/
export default async function handler(req, res) {
const { method } = req

switch (method) {
case 'GET':
try {
const user = await User.find({})
return res.status(200).json(user)
} catch (error) {
console.error(error)
}
case 'POST':
return res.status(200).json('posting files')
case 'UPDATE':
return res.status(200).json('updating file')
case 'DELETE':
return res.status(200).json('deleting file')
default:
return res.status(400).json('invalid method')
}
}