DEV Community

Nicolas-Leveque
Nicolas-Leveque

Posted on

Day 2 of 100 days of code

I decided that my first project for this would be a rework of one of my openClassrooms project: the social media site (link if you're interested )

Yesterday I created a postgres database in Heroku and modified some of my code to connect with it via sequelize, nothing big but I noticed a problem: I initially made the decision to put pictures ( user's avatar and posts ) in the database using the blob type. Well PostgreSQL doesn't support Blob format and even if it did, the size of the database hosted on the free tier of Heroku is limited, and I'm still quite poor at the moment so in this version, pictures will go in a folder on the backend.

As I don't have a lot of time today, my goal will be to evaluate the damage and take note of all the file I need to modify to make this work.

I already made some changes in my multer config to create a destination folder and added a middleware to check if the file is an image and rename it.

First the models: to use the BLOB type I added 3 fields in the database:

imageType: {

    type: Sequelize.STRING,

    defaultValue: 'image/jpeg'

},

imageName: {

    type: Sequelize.STRING,

    defaultValue: 'avatar.jpg'

},

imageData: {

    type: Sequelize.BLOB('long'),

}

Enter fullscreen mode Exit fullscreen mode

Since the pictures will be stored on the server for now on, this won't be needed anymore and a simple string with the URL of the pictures will do. This is true for the users and posts models.

imageUrl: {
    type: Sequelize.STRING
}
Enter fullscreen mode Exit fullscreen mode

Then, the user.controller file: this is where I put the functions that Create, Read, Update or Delete users ( see what I did there? CRUD... nevermind ).
Initially the user couldn't select an avatar during the signup and modify processes and there was a route dedicated to the avatar ( also a react component but I'll deal with the front-end later ).
Since the pictures are only stored in the db as URL I should be able to do add it in the signup and modifyUser functions and get rid of the uploadAvatar function (note to myself: do not forget to delete the user's avatar when the user is deleted ).

Last (for now anyway ) but not least, the post.controller file. There is 2 functions that create posts: one for text post and another for picture post, again using URL I should be able to reduce that to one, also this would simplify the front-end as well and allow the user to add text to a picture ( again I'll deal with that later anyway ).
I'll also need to modify the SQL requests inside my get functions as they use imageType and imageData.

Well that wasn't as bad as I initially thought, I plan to do all those modifications and will be wirting unit and integration tests using Jest as I go during the next weeks and then do the same with the front-end.

Top comments (0)