How can I upload a picture in couch db and retrieve it - couchdb

i am working on Couch DB ,i want to create a user profile after user signs up for my website.
During registration,the user uploads his image and then it is added to that user's document in Couch Db and retrieved so that all his details along with his image are displayed as his profile.

You can store a picture as an attachment, either in the user's profile, or, in preference, to a separate database. Here's uploading a single attachment to a new document:
curl -vXPUT $COUCH/testy/doc/attached-jpg --data-binary ~/tmp/swirl/docs/test/data/m74.jpg
Obviously if you're updating an existing document you'll need to provide _rev fields appropriately.
Typically the user profile is kept lean and the profile images are handled from a separate db which then doesn't require caching that in RAM on the server if not needed. A validation function can be used to ensure that the only user who updates a profile picture is the owning user.

Related

Using a MongoDB Atlas database; how does one set up user accounts, if a user account/password is required to access the DB? (Node, express, mongoose)

Alrighty, so I have a MongoDB Atlas database set up, containing several objects. My current API has several get, post, put and delete end points which are working correctly. However, I am in the process of setting up user accounts and I am a little confused.
My project is built with React on the front end and my server is built with Node, express and mongoose. My goal is that of your typical web application and is as follows:
Go to main web app URL
Home page is a login or "create an account" screen.
Once logged in (or an account is created and you sign into it) you will then have access to get, post, put and potentially delete (if an admin) objects in my DB.
I have built a very basic sign-in, sign-out and "register an account" server which is working however, I am confused as to how to properly go about integrating this with the rest of my API. I currently have the server connecting to my Atlas DB using what is provided in the "connect" menu in the Atlas dashboard (without the < >)
mongodb+srv://<username>:<password>#<cluster-name>.ntwp5.mongodb.net/<collection-name>
Obviously, the username and password (which I can set in my Atlas dashboard) needs to be passed into the "username" and "password" fields in-order to connect to it. I was planning to use the same cluster and have a separate collection for users and my data.
What's confusing me is that in-order to connect to the MongoDB server above and gain access to the two databases, I need to pass in some username/password. However, in order to create an account (ie, creating a new username and password), I would already be utilizing a username and password to connect to the server.
So say I have a group of people in the same company using this application. Would I essentially have a single administrative username/password used by everyone to connect to the server URL? From there, users would be able to access the "users" collection and create an account. Do I then setup my existing API routes (which point to my collection of data) to check that the "signed-in" user exists before returning a successful request and access to that collection?
Or perhaps, the proper way is to use two completely separate databases; one for users and one for my data?
Sorry, I am new to working with Databases and I think the above makes sense to me but I want to verify if that is the correct way to go about handling this. Thanks!

NodeJS App with waiting for the admin to accept

I'm trying to create a Full Stack App with the following requirements:
Have users that post photos
Have an admin that gets those photos and accepts/rejects the photo to be published
So, the user should be able to upload a photo, and, if the admin accepts, the photo should be rendered to the user screen. Otherwise, the user should get some kind of alert saying that the uploaded photo was rejected.
Do you guys know some kind of project where it was done so it can guide me? Or any advice on how to do it?
start with a register/login system first where the users will be given different roles ( admin and user). Then create an admin panel for the admin followed by a profile page for the user. Use a database to send the profile pictures to and display the data in the admin panel, where the admin has got the option to either reject or accept the data. if its accepted display the profile pic otherwise delete the data entry and send a message to the user who has uploaded the profile pic.

Temporary User Accounts: MongoDB vs Redis

I'm developing an application with NodeJS, ExpressJS and MongoDB. Currently I'm working on the user registration process. I would like to store the user account temporary until the user has verified his email address, if the email address is not verified within a certain amount of time, I would like to remove the temporary account.
Currently I've following two ideas to solve the issue:
Creating a mongoose TempUserModel (besides the UserModel), i.e. if the user does the registration a temp user will be created, as soon as the user verified his email address, the temporary user account will be copied to the real Users collection. Some cronjobs could be setup to delete not verified user accounts after a certain amount of time (probably there are better solutions to let expire a mongodb record)
Setup redis to store the temporary user account data, as soon as the email address get verified, a new user will be created in mongodb. With this solution an expire date could be set to remove not verified accounts after a certain amount of time)
Is it better to store a temporary user account in Redis or in MongoDB?
I would recommend storing the temporary user accounts in MongoDB. Storing them in MongoDB has three advantages over Redis:
If you store a temporary user in MongoDB, it will be very easy to convert them to a real user once they have verified. In fact, you could even have the temporary users and verified users share the same schema, with a has_verified field in that schema being the only difference between the two kinds of users. Changing has_verified to true is a lot easier than saving data from Redis to Mongo.
If you are not already planning to create a Redis database for your project, you will only have to maintain MongoDB. Maintaining MongoDB requires less effort and fewer computation resources than maintaining both Redis and MongoDB.
If you ever make changes to your user schema in the future, it would be easier to only make those changes in once place, i.e. MongoDB, rather than to make those changes in two places.

MongoDb: How to merge contents of two documents in mongoDB?

I allow user to skip login or login using FB. When user logs in I create user in User collection using FB profile ID and when he skips it I still create his user using some unique client ID. In the client app, user can bookmark articles and I sync it to server. User can login using FB at any time later. Once user logs in using FB I have to merge user created using FB and user created using Client ID so that his stored data maps perfectly.
What I have tried:
Consider user skipped login and I create a user in USER collection.
User logs in using FB in between life time of the app and I will search this FB ID in my collection. If this ID does not exist I update skipped user document with FB id. If it exist I will merge data from FB user document to skipped user document and delete FB user document.
Another method:
I don't sync any bookmark to the server until user logs in. Once he logs in I will sync all data at once.
What is the efficient way of doing this kind of operation?
If I understand correctly, you need to find the document with the same clientId, and add to it the facebook user document.
You can do this with mongo update command, using $set which merges the new document with the existing mongo document https://docs.mongodb.com/manual/reference/operator/update/set/
so your code would look like this
db.collection("users").update(
// first parameter is mongo query to match based on clientId
{ clientId: 100 },
// second parameter is the document that will be merged using $set
{ $set: facebookDocument })
The result of this will be the existing document merged with all the properties of facebookDocument

Using Facebook graph api, can and should I cache profile picture?

I am using log in with facebook in my web app. Because of this I will always have the userid of the current user at facebook. I want to use the profile picture in my app, I can do this easily by:
<img src="http://graph.facebook.com/<%- accountId %>/picture?width=200&height=200">
This seems fine if I just display the picture at the current users dashboard, but what if I want other users to see each others pictures? Is it a good idea to always link each users images as above? It will in this case be a bounch of requests to facebook for each view of my app. Or should I download each users profile pic when they register and display it from my database? Am I allowed to do that? In this case, how do I save the picture into my mongoDB database via waterline (sails.js)?

Resources