1import { Schema, model, Document, Model } from 'mongoose';
2
3declare interface IContact extends Document{
4 name: string;
5 email: string;
6 phone?: string;
7 message?: string;
8 course_enquiry?: string;
9 creation_date: Date;
10}
11
12export interface ContactModel extends Model<IContact> {};
13
14export class Contact {
15
16 private _model: Model<IContact>;
17
18 constructor() {
19 const schema = new Schema({
20 name: { type: String, required: true },
21 email: { type: String, required: true },
22 phone: { type: String },
23 message: { type: String },
24 course_enquiry: { type: String },
25 creation_date: { type: Date, default: Date.now }
26 });
27
28 this._model = model<IContact>('User', schema);
29 }
30
31 public get model(): Model<IContact> {
32 return this._model
33 }
34}Copied!
1import mongoose from 'mongoose'
2import consola from 'consola'
3import q from 'q'
4
5export const mongooseConnection = (): void => {
6 mongoose.Promise = q.Promise
7
8 mongoose.connect(process.env.MONGO_URI, {
9 minPoolSize: 1,
10 maxPoolSize: 20,
11 compression: { compressors: ['zlib'] },
12 useUnifiedTopology: true,
13 useNewUrlParser: true,
14 useFindAndModify: false,
15 socketTimeoutMS: 60000,
16 serverSelectionTimeoutMS: 60000,
17 loggerLevel: 'error'
18 })
19
20 mongoose.connection.on('connecting', () => consola.info('database connecting'))
21 mongoose.connection.on('connected', () => consola.success('database connected'))
22 mongoose.connection.on('disconnecting', () => consola.info('database disconnecting'))
23 mongoose.connection.on('disconnected', () => consola.info('database disconnected'))
24 mongoose.connection.on('error', () => consola.error('database error'))
25}
26