MongoDB Stitch

Server less platform for building applications. It is a collection of services that are offered by the mongoDB company while building application so that you can just focus on UI and core logic of the application. Even the logic should run on server i.e. backend but you need not to write code for setting up server, managing it, creating API on your own. All this is handled by stitch.

It is built with integration of Atlas. Core feature of Stitch is Authentication. Stitch gives access to mongoDB Atlas to user through its service where users can sign up and login in through its service and that user gets a temporary credential for access to database and we can lock what that user can do.

So this is managed by stitch behind the scenes. It makes sure that users have the access to database but with restrictions. Just before stitch, we were controlling the way how user can access the database by writing code and building logic for everything.

But with stitch, it can be managed very easily. Inside stitch, reactions to events can also be given.

For Example is something changes into database; we can send mail or log something or whatever we want to do.

We can execute some codes on the cloud. So it’s not just a tool for UI and client side code execution on cloud is also provided using some functions which we define but we do not need to write code for parsing request but we will just write the code we want to execute and that code can be called directly from client or inside any other function or we can set up trigger at some middle position at which a certain code snippet is executed.

For accessing the database, we have stitch queries which are used to run queries along with the rules that we set for the users. We can integrate stitch with other services. For example, AWS S3 which is used for uploading file because stitch don not have built in storage.

Using Stitch

  • App services are provided by the company behind the mongoDB. Go to cluster and use that to start your application creation.
  • First starting with starting with our application but this time we will not use atlas but stitch for backend.

Installing stitch client

  • There is a command in client section of stitch which to be added in project through command line.
  • After installing it we would not be working with backend folder of our project...
  • It does not mean to delete it because it still contains the logic to connect to database so our application will broke.
  • We need to import stitch where we need to use it. In our project that folder is product.js

This folder has fetch Data method to fetch the data and instead of this, stitch will be used. So in this file to use stitch, first some imports are needed to be mentioned.

Import {Stitch, RemoteMongoClient} from ‘mongodb-stitch-browser-sdk’;

Along with writing this command in product.js file, it also needs to be mentioned in the app.js file. To add stitch in the app.js file, a constructor needs to be created.

constructor (){
Stitch.initDefaultAppClient (“ID_of_application_from_cluster”);
}

The above three lines of code is need to be mentioned in every file of the project where we want to use the stitch.

Now in product.js file, the method to fetch the data can be changes. Instead of the pre-written code in the product.js file, we can add code that uses Stitch.

const mongodb = Stitch.defaultAppClient().getServiceClient(RemoteMongoClient.factory, ‘mongodb-atlas’);

‘RemoteMongoClient.factory’ is a reference to the method that will be internally used by the Stitch to initialize that mongoClient.

In product file after writing the code, it returns a mongodb instance that can be used now to access that database.

For Example, mongodb.db (‘name_of_db’).collection (‘name_of_collection’).find ().asArray ().then ().catch ();

Authentication

For authenticating users, go to app services for our application. Inside application users we can see many user providers like Google, Facebook etc.

To start with the authentication for anonymous users, login can be added inside the constructor where the client was created.

const client = Stitch.initDefaultAppClient (“ID_of_application_from_cluster”);
client.auth.loginWithCredential(new AnonymousCredential());

But before using the above mentioned command, import statement needs to be changed.

import { Stitch, AnonymousCredential } from ‘mongodb-stitch-browser-sdk’;

After this, we will get authenticated but still we get an error because we need to set some rules for accessing database because by default nothing is accessible when using stitch.