Quantcast
Channel: MongoDB | Blog
Viewing all articles
Browse latest Browse all 2423

Introduction to Serverless Functions in MongoDB Stitch

$
0
0

Serverless and Functions as a Service are relatively new development paradigms that are gaining in popularity. Let's take a quick look at what MongoDB Stitch offers in this space.

Functions in Stitch are written in JavaScript ECMAScript and can be called from other scripts in Stitch, or from external JavaScript leveraging the client SDK. Here's an example of a function in Stitch:

exports = function(message) {
  const mongodb = context.services.get("mongodb-atlas");
  const coll = mongodb.db("db").collection("users");
  const twilio = context.services.get("my-twilio-service");
  const yourTwilioNumber = context.values.get("twilioNumber");
  coll.find().toArray().then(users => {
    users.forEach(user => twilio.send({
      to: user.phone,
      from: yourTwilioNumber,
      body: message
    })
  );
});

A few things might stand out about the structure and content of this script. Let's take a closer look at some of these components.

The first thing you'll notice is exports. All Stitch functions run the JavaScript function assigned to the global variable exports. This is similar in theory and practice to the Node.js module.exports object.

Next, you're likely to see references to context. Access to resources from within Stitch scripts is facilitated through the use of a context object. This object has several elements:

context.services

context.services gives you access to pre-configured third-party services. There is one built-in service called "mongodb-atlas". This provides access to the underlying database within Atlas. Other services can be instantiated for third-party services including GitHub, AWS, Twilio and more. These additional services will be accessed using the name you provide during configuration.

const db = context.services.get("mongodb-atlas").db("ecommerce");

Notice how easily I'm able to begin accessing my database, and collection. context.values: Values are named constants that you can use in MongoDB Stitch functions and rules. These are like global variables available to all functions in a Stitch application. Similarly, we can configure access to third party services such as twilio as shown in the example.

context.functions

context.functions enables you to reference other serverless functions written and hosted in Stitch.

exports = function(a, b) {
 return context.functions.execute("sum", a, -1 * b);
};

context.users

context.users provides a view of the currently authenticated user.

{
  "id": "5a01f135b6fc810a19421c12",
  "type": "server",
  "data": {
    "name": "api-key"
  },
  "identities": [{
    "id": "5a09f135b6fc810f19421c13",
    "provider_type": "api-key"
  }]
}

context.values

Values are named constants that you can use in MongoDB Stitch functions and rules. To access a value in functions, use the context.values variable.

exports = function() {
    return context.values.get("test");
}

Utilities

Lastly, a resource available to developers of serverless functions in Stitch that we're not representing in the example function, but that bears mentioning is the set of utilities exposed as methods in Stitch functions. There are several pre-imported JavaScript libraries available for your serverless functions:

Function Description
console can be used to output to the debug console and console logs. console.log("Hello!")
JSON can be used to convert between string and object representations of standard JSON.
EJSON can be used to convert between string and object representations of MongoDB Extended JSON.
BSON can be used to construct and manipulate BSON types.
utils.crypto provides methods for working with cryptographic algorithms.

Utilities and context objects leveraged as a part of your application make it possible to create rich, powerfully integrated applications that don't need a lot of the boilerplate code set up.

Developing applications leveraging serverless is a bit different,  but once you get used to leveraging the tools available, it is extremely powerful and helps you create better applications – and do it faster as well. It's a huge benefit to using MongoDB Stitch in your development cycle.

To read more, and get started, check out the documentation, sign up for a free Atlas account and begin writing a serverless application today.


Viewing all articles
Browse latest Browse all 2423

Trending Articles