title: what would be an extensible structure for node.js with native mongodb driver in Typescript
body: <p>using node.js with the native mongodb driver is there a way to enforce a schema/schemaless structure by using classes or interfaces with Typescript (ES6). For example, if we have a collection called users. and we do something along the lines of </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//Interface Library
  interface INewUser {
    name: string,
    age: number,
    location: string
  }
  
  //DAL
  function insertUser(data: INewUser, cb: MongoCallBack &lt; InsertOneWriteOpResult &gt; ) {
  
    //for best practices we would have an instance connected to db already
    //so we do not have to open a new connection on every request
    MongoClient.connect(uri, function(err, db) {
  
      //handle err first
  
      db.collection("users", function(err, col) {
  
        //handle err first
  
        col.insertOne(data, function(err, response: InsertOneWriteOpResult) {
          //MongoCallBack expects (MongoError, &lt;T&gt;) T = InsertOneWriteOpResult
          return cb(null, response)
        });
  
      });
  
    });
  }
  
  
  //api endpoint
  app.post('/user', function(req, res) {
    //do some checks to make sure the req.body is valid -- i.e not empty no xss attacks..
    let newUser: INewUser = req.body;
  
    insertUser(newUser, function(err, res) {
      if (res.res.ok == 1) {
        res.json({
          success: true
        });
      }
  
    });
  });
  
  
  
  
  /*example data being posted to /user
  {
    name: "test",
    age: 33,
    location: "NA"
  } */</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm looking to find some of the best styles/techniques that could be used to structure data flow throughout an application utilizing typescript.</p>