Home > database >  Uploading Images Via API ( Javascript )
Uploading Images Via API ( Javascript )

Time:10-19

Well, I'm kinda stuck in a ( maybe simple ) problem. I would like to use a database / cloud sever with a Nest.Js API for a main purpuse to store images. It's my firt time experimenting with such concept and I would like to know first of all the 'best' solution. For example, is it ok to use something like MongoDB or Firebase Storage? If not, what would be the ideal solution to follow? Is it even good to use Nest for such a thing?

More specificly; ( How I think / plan for the project )

I would like to use a nonSQL database ( pref. MongoDB ) to store some data about a certain object. In this object I want a photo as well as part of it. So I am pretty sure I can't mix these types of data ( storing strings/numbers/etc AND images ) so maybe I could store a location to another DB ( Collection ) / Cloud Server and retrieve it from there. Is this practical? How is this consept suposed to work?


So far I've managed to obdain to store images with the help of Multer in a local /uploads folder. But I don't really like this solution. After a certain amount of images posted my server will ( probably ) run slower and slower ( ???? ).


Sorry for the silly question. But I'm kinda stuck here for a few days... :(

CodePudding user response:

The way you're doing it with filesystem is perfectly fine. Behind the hood, tools like mongodb are implemented using a file system anyways. Using server's file system can pose scalability issues when you need to spread application load over multiple servers, but for a single server solution it will have the highest performance due to minimal IO overhead.

I recommend using an object storage system (like amazon s3 or gcp bucket), if you want to take scalability seriously. All your files will be stored on a single managed system designed to scale, and you'll be able to treat each of your severs as stateless (and therefore provision multiple and guarantee you can access all files no matter which server handles each request).

These would be the two "industry standard" solutions. Not to say you cannot do this with mongo or firebase but they probably aren't as good solutions. The most important thing however, is to avoid storing files on a relational database, so as long as you aren't doing that it will be fine.

I'd say you're overthinking this. Stick with file system until you know you have actual scalability issues. Nestjs is highly modular you can implement your app in a way such that other modules don't "know" the underlying implementation of how you store files, so you can comfortably write your code with images stored on file system now then move later if needed.

Lastly, Nestjs is just a framework for writing HTTP servers. It isn't going to be any worse or better than any other HTTP server out there.

  • Related