title: Typescript and mongoose: saving to database
body: <p>I have the following app saving into memory repo. I connected a local mongodb but I have issues saving the posted data into mongodb.
  When the app is saving to the memory it works just fine and I can use curl to display the array of different event saved there. I now would like to make it save into my DB so I can then work with the data saved and I couldn't find any clear tutorial on that
  Can someone advice how it should be done?<br>
  Mongodb schema:</p>
  <pre><code>import { mongoose } from '././http'
  
  const CompetitionSchema = new Schema({
    id: String,
    place: String,
    time: String,
    subscriptions: [],
    date: new Date(),
    cost: {
      currency: String,
      amount: Number,
    },
  })
  
  export const CompetitionModel = mongoose.model(
    'CompetitionModel',
    CompetitionSchema,
  )
  
  export default CompetitionSchema
  </code></pre>
  <p>http:</p>
  <pre><code>export const mongoose = require('mongoose')
  
  mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
  const db = mongoose.connection
  db.on('error', console.error.bind(console, 'An error has occured: '))
  db.once('open', function () {
    console.log('Connected to Mongodb')
  })
  
  const express = require('express')
  const app = express()
  const bodyParser = require('body-parser')
  
  app.use(bodyParser.json())
  app.get('/events', (_req: any, res: any) =&gt; {
    res.send(eventApplication.getAll())
  })
  
  app.post('/event', async (req: any, res: any) =&gt; {
    await eventApplication.createAnEvent(req.body)
    res.json({
      success: true,
    })
  })
  
  app.listen(8000)
  </code></pre>
  <p>in memory and mongodb repository</p>
  <pre><code>
  export interface EventRepositoryInterface {
    // will require ansyc call will have return promise of all below - refactor needed
    save: (event: CompetitionEvent) =&gt; void
    getById: (id: string) =&gt; CompetitionEvent | undefined
    getAll: () =&gt; CompetitionEvent[]
  }
  
  export class InMemoryEventRepository implements EventRepositoryInterface {
    constructor(private events: CompetitionEvent[] = []) {}
  
    public save = (event: CompetitionEvent) =&gt;
      (this.events = [...this.events, event])
    public getById = (id: string) =&gt; this.events.find((e) =&gt; e.id === id)
    public getAll = () =&gt; this.events
  }
  
  export class MongoCompetitionEventRepository
    implements EventRepositoryInterface {
    constructor(private events: CompetitionEvent[] = []) {}
  
    //here event should go to DB and NOT in the array memory
    public save = (event: CompetitionEvent) =&gt;
      (this.events = [...this.events, event])
    public getById = (id: string) =&gt; this.events.find((e) =&gt; e.id === id)
    public getAll = () =&gt; this.events
  }
  </code></pre>
  <p>Please let me know if something is missing I'll edit the post</p>