Overview

The MongoDB Stitch JavaScript SDK can be used to build web applications and Node.js applications that use MongoDB Stitch.

Links to Other Resources

Join the chat at https://gitter.im/mongodb/stitch

MongoDB Stitch Users - Google Group

MongoDB Stitch Announcements - Google Group

MongoDB Stitch Documentation

JavaScript SDK GitHub Repository

Basic Usage

Construct a simple app-wide client

import { StitchClientFactory } from 'mongodb-stitch';
let appId = 'sample-app-ovmyj';
let stitchClientPromise = StitchClientFactory.create(appId);

The StitchClient only needs to be resolved once from StitchClientFactory.create() and it can be used for the lifetime of an application.

Authenticate anonymously

stitchClientPromise.then(stitchClient => stitchClient.login())
  .then(() => console.log('logged in as: ' + stitchClient.authedId()))
  .catch(e => console.log('error: ', e));

Access MongoDB APIs

stitchClientPromise.then(stitchClient => {
  // mongodb1 is the name of the mongodb service registered with the app.
  let db = stitchClient.service('mongodb', 'mongodb1').db('app-ovmyj');
  let itemsCollection = db.collection('items');

  // CRUD operations:
  const userId = stitchClient.authedId();
  return itemsCollection.insertMany(
    [
      { owner_id: userId, x: 'item1' },
      { owner_id: userId, x: 'item2' },
      { owner_id: userId, x: 'item3' }
    ]
  );
}).then(result => console.log('success: ', result))
  .catch(e => console.log('error: ', e));

Execute a function

stitchClientPromise.then(stitchClient => 
  stitchClient.executeFunction('myFunc', 1, 'arg2', {arg3: true})
).then(result => console.log('success: ', result))
  .catch(e => console.log('error: ', e));

Execute a service function

stitchClientPromise.then(stitchClient =>
  stitchClient.executeServiceFunction('http1', 'get', {url: 'https://domain.org'})
).then(result => console.log('success: ', result))
  .catch(e => console.log('error: ', e));

Client

These classes offer fundamental functionality for communicating with MongoDB Stitch.

StitchClientFactory

src/client.js

StitchClientFactory is a singleton factory class which can be used to asynchronously create instances of StitchClient. StitchClientFactory is not meant to be instantiated. Use the static create() method to build a new StitchClient.

new StitchClientFactory()
Static Members
create(clientAppID, options)

StitchClient

src/client.js

StitchClient is the fundamental way of communicating with MongoDB Stitch in your application. Use StitchClient to authenticate users and to access Stitch services. StitchClient is not meant to be instantiated directly. Use a StitchClientFactory to create one.

new StitchClient()
Instance Members
login(email?, password?, options)
register(email, password, options)
linkWithProvider(providerType, options)
authenticate(providerType, options)
logout()
authError()
userProfile()
isAuthenticated()
authedId()
service(type, name)
executeFunction(name, args)
executeServiceFunction(service, action, args)
getApiKeys()
createApiKey(userApiKeyName)
getApiKeyByID(keyID)
deleteApiKeyByID(keyID)
enableApiKeyByID(keyID)
disableApiKeyByID(keyID)

MongoDB Service

These classes offer functionality for communicating with a MongoDB instance via Stitch.

Creates a new MongoDBService instance (not meant to be instantiated directly, use .service('mongodb', '<service-name>') on a StitchClient instance.

new MongoDBService(): MongoDBService
Returns
MongoDBService: a MongoDBService instance.
Instance Members
db(databaseName, options = {})

Creates a new DB instance (not meant to be instantiated directly, use .db() on a MongoDBService instance).

new DB(): DB
Returns
DB: a DB instance.
Instance Members
collection(name, options = {})

Creates a new Collection instance (not meant to be instantiated directly, use .collection() on a DB instance).

new Collection(): Collection
Returns
Collection: a Collection instance.
Instance Members
insertOne(doc)
insertMany(docs)
deleteOne(query)
deleteMany(query)
updateOne(query, update, options = {})
updateMany(query, update, options?)
find(query, project?)
findOne(query, project?)
aggregate(pipeline)
count(query, options)

Partner Services

These classes provide access to the functionality of MongoDB Stitch partner services. They are not meant to be instantiated directly, since they can be instantiated by calling the service() method on the StitchClient.

Convenience wrapper around AWS S3 service (not meant to be instantiated directly, use .service('aws-s3', '<service-name>') on a StitchClient instance).

new S3Service(stitchClient: any, serviceName: any): S3Service
Parameters
stitchClient (any)
serviceName (any)
Returns
S3Service: a S3Service instance.
Instance Members
put(bucket, key, acl, contentType, body)
signPolicy(bucket, key, acl, contentType)

Convenience wrapper around AWS SES service (not meant to be instantiated directly, use .service('aws-ses', '<service-name>') on a StitchClient instance).

new SESService(stitchClient: any, serviceName: any): SESService
Parameters
stitchClient (any)
serviceName (any)
Returns
SESService: a SESService instance.
Instance Members
send(fromAddress, toAddress, subject, body)

Convenience wrapper for HTTP service (not meant to be instantiated directly, use .service('http', '<service-name>') on a StitchClient instance).

new HTTPService(client: any, serviceName: any): HTTPService
Parameters
client (any)
serviceName (any)
Returns
HTTPService: a HTTPService instance.
Instance Members
get(urlOrOptions, options = {})
post(urlOrOptions, options = {})
put(urlOrOptions, options = {})
patch(urlOrOptions, options = {})
delete(urlOrOptions, options = {})
head(urlOrOptions, options = {})

Create a new TwilioService instance (not meant to be instantiated directly, use .service('twilio', '<service-name>') on a StitchClient instance).

new TwilioService(stitchClient: any, serviceName: any): TwilioService
Parameters
stitchClient (any)
serviceName (any)
Returns
TwilioService: a TwilioService instance.
Instance Members
send(from, to, body)

Authentication

Although most authentication tasks can be performed by the StitchClient, some authentication providers offer additional functionality that can be accessed via the "auth" property on the StitchClient. The providers which offer additional functionality are documented here.

userPassProvider

src/auth/providers.js

userPassProvider offers several methods for completing certain tasks necessary for email/password authentication. userPassProvider cannot be instantiated directly. To instantiate, use .auth.providers('userpass') on a StitchClient.

userPassProvider
Parameters
auth (any)
Instance Members
emailConfirm(tokenId, token)
sendEmailConfirm(email)
sendPasswordReset(email)
passwordReset(tokenId, token, password)
register(email, password)

Errors

Some functions in the MongoDB Stitch JavaScript SDK may result in an error. The types of errors that can be returned by the SDK are documented here.

StitchError

src/errors.js

Creates a new StitchError

new StitchError(message: String, code: Object): StitchError

Extends Error

Parameters
message (String) The error message.
code (Object) The error code.
Returns
StitchError: A StitchError instance.