mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
put models in file not schema
This commit is contained in:
14
db/booker.js
Normal file
14
db/booker.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import * as mongoose from 'mongoose'
|
||||
|
||||
const BookerSchema = new mongoose.Schema(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true, minlength: 5 },
|
||||
street: { type: String, required: true },
|
||||
zip: { type: String, required: true },
|
||||
city: { type: String, required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
|
||||
export default mongoose.models.Booker || mongoose.model('Booker', BookerSchema)
|
||||
@@ -1,21 +1,14 @@
|
||||
import { Schema } from 'mongoose'
|
||||
import * as mongoose from 'mongoose'
|
||||
|
||||
import { getDays, dateFormat } from '../helpers/date'
|
||||
|
||||
export const BookerSchema = new Schema(
|
||||
const BookingSchema = new mongoose.Schema(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true, minlength: 5 },
|
||||
street: { type: String, required: true },
|
||||
zip: { type: String, required: true },
|
||||
city: { type: String, required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
)
|
||||
|
||||
export const BookingSchema = new Schema(
|
||||
{
|
||||
booker: { type: Schema.Types.ObjectId, ref: 'Booker', required: true },
|
||||
booker: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Booker',
|
||||
required: true,
|
||||
},
|
||||
startDate: {
|
||||
type: Date,
|
||||
required: true,
|
||||
@@ -43,3 +36,6 @@ export const BookingSchema = new Schema(
|
||||
BookingSchema.virtual('days').get(function () {
|
||||
return getDays({ startDate: this.startDate, endDate: this.endDate })
|
||||
})
|
||||
|
||||
export default mongoose.models.Booking ||
|
||||
mongoose.model('Booking', BookingSchema)
|
||||
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