mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 23:17:12 +01:00
put models in file not schema
This commit is contained in:
34
db/index.js
34
db/index.js
@@ -1,24 +1,25 @@
|
||||
import mongoose from 'mongoose'
|
||||
import { BookingSchema, BookerSchema } from './schema'
|
||||
import * as mongoose from 'mongoose'
|
||||
|
||||
mongoose.connect(process.env.MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
connectTimeoutMS: 1000,
|
||||
serverSelectionTimeoutMS: 5000,
|
||||
})
|
||||
import Booker from './booker'
|
||||
import Booking from './booking'
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
mongoose.modelNames().includes('Booker') && mongoose.deleteModel('Booker')
|
||||
mongoose.modelNames().includes('Booking') && mongoose.deleteModel('Booking')
|
||||
let connectedPromise
|
||||
|
||||
function connect() {
|
||||
if (connectedPromise) {
|
||||
return
|
||||
}
|
||||
|
||||
connectedPromise = mongoose.connect(process.env.MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
})
|
||||
|
||||
return connectedPromise
|
||||
}
|
||||
|
||||
export const Booker =
|
||||
mongoose.models.Booker || mongoose.model('Booker', BookerSchema)
|
||||
export const Booking =
|
||||
mongoose.models.Booking || mongoose.model('Booking', BookingSchema)
|
||||
|
||||
export async function getBookedDays() {
|
||||
await connect()
|
||||
|
||||
const bookings = await Booking.find(
|
||||
{
|
||||
status: { $ne: 'rejected' },
|
||||
@@ -48,6 +49,7 @@ export async function createBooking({
|
||||
zip,
|
||||
city,
|
||||
}) {
|
||||
await connect()
|
||||
const booking = new Booking({ startDate, endDate, purpose, org, destination })
|
||||
const bookedDays = await getBookedDays()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user