Skip to content

karol-preiskorn/3d-inventory-mongo-api

Repository files navigation

3d-inventory-mongo-api

wakatimeGitHub latest commitGitHub issuesmade-with-javascriptNpm package versionGitHub licenseGitHub stars

Purposes

Create REST API for ✨3d-inventory✨ application connected to Mongo Atlas — solution that allows you to build a spatial and database representation of yours datacenters.

Architecture

Architecture ✨3d-inventory✨ Mongo API

MEAN stack

  • MongoDB is a NoSQL database that stores data in a flexible, JSON-like format.
  • Express.js is a web application framework for Node.js that simplifies the process of building web servers and APIs.
  • AngularJS is a 1JavaScript` framework for building dynamic web applications on the client-side.
  • Node.js is a JavaScript runtime that allows you to run 1JavaScript1 code on the server-side.

Data Model

Data Model

API documentation

Swagger API definition.

Use MongoDB

We can use MongoDB or programming Mongo Atlas API.

https://www.mongodb.com/developer/languages/typescript/

Example of using a cursor async with promises

new Promise(function (resolve, reject) {
  collection
    .find(query)
    .cursor()
    .on('data', function (doc) {
      // ...
    })
    .on('error', reject)
    .on('end', resolve)
}).then(function () {
  // ...
})

It is generic code that can be tucked away in a utility function so it doesn't clutter application code and obstruct actual application logic.

In your example simplest utility function would be like

function streamToPromise(stream) {
  return new Promise(function (resolve, reject) {
    stream.on('end', resolve)
    stream.on('error', reject)
  })
}

And then the application code is simply

Promise.map(files, function (file) {
  var stream = API.getStream(file)
  stream.pipe(endPoint)
  return streamToPromise(stream)
})

Reference

Install node stack

npm install
npm start

Tools and ideas

Todo

  • move to Typepscript (in progress)