3.1.0
The MongoDB Stitch JavaScript SDK can be used to build web applications and Node.js applications that use MongoDB Stitch.
MongoDB Stitch Users - Google Group
MongoDB Stitch Announcements - Google Group
JavaScript SDK GitHub Repository
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.
stitchClientPromise.then(stitchClient => stitchClient.login())
.then(() => console.log('logged in as: ' + stitchClient.authedId()))
.catch(e => console.log('error: ', e));
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));
stitchClientPromise.then(stitchClient =>
stitchClient.executeFunction('myFunc', 1, 'arg2', {arg3: true})
).then(result => console.log('success: ', result))
.catch(e => console.log('error: ', e));
stitchClientPromise.then(stitchClient =>
stitchClient.executeServiceFunction('http1', 'get', {url: 'https://domain.org'})
).then(result => console.log('success: ', result))
.catch(e => console.log('error: ', e));
These classes offer fundamental functionality for communicating with MongoDB Stitch.
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.
Creates a new StitchClient.
(String)
the app ID of the Stitch application, which can be found in
the "Clients" page of the Stitch admin console.
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.
Login to Stitch instance, optionally providing a username and password. In the event that these are omitted, anonymous authentication is used.
(String?)
the email address used for login
(String?)
the password for the provided email address
(Object
= {}
)
additional authentication options
Promise
:
which resolve to a String value: the authenticated user ID.
Send a request to the server indicating the provided email would like
to sign up for an account. This will trigger a confirmation email containing
a token which must be used with the emailConfirm
method of the userpass
auth provider in order to complete registration. The user will not be able
to log in until that flow has been completed.
(String)
the email used to sign up for the app
(String)
the password used to sign up for the app
(Object
= {}
)
additional authentication options
Promise
:
Submits an authentication request to the specified provider providing any included options (read: user data). If auth data already exists and the existing auth data has an access token, then these credentials are returned.
(String)
the provider used for authentication (The possible
options are 'anon', 'userpass', 'custom', 'facebook', 'google',
and 'apiKey')
(Object
= {}
)
additional authentication options
Promise
:
which resolves to a String value: the authenticated user ID
any
:
Returns any error from the Stitch authentication system.
Executes a service function.
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.
MongoDBService
:
a MongoDBService instance.
Creates a new DB instance (not meant to be instantiated directly, use .db()
on
a MongoDBService instance).
DB
:
a DB instance.
Returns a Collection instance representing a MongoDB Collection object.
Collection
:
returns a Collection instance representing a MongoDb collection.
Creates a new Collection instance (not meant to be instantiated directly,
use .collection()
on a DB instance).
Collection
:
a Collection instance.
Finds documents.
MongoQuery
:
An object which allows for
limit
and
sort
parameters to be set.
execute
will return a {Promise} for the operation.
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).
(any)
(any)
S3Service
:
a S3Service instance.
Put an object to S3 via Stitch. For small uploads
(String)
which S3 bucket to use
(String)
which key (filename) to use
(String)
which policy to apply
(String)
content type of uploaded data
((String | BSON.Binary))
the content to put in the bucket
Promise
:
which resolves to an object containing a single field "location"
which is the URL of the object that was put into the S3 bucket
Convenience wrapper around AWS SES service (not meant to be instantiated directly,
use .service('aws-ses', '<service-name>')
on a StitchClient instance).
(any)
(any)
SESService
:
a SESService instance.
Send an email
(String)
the email to send from
(String)
the email to send to
(String)
the subject of the email
(String)
the body of the email
Promise
:
resolving to an object which contains the single string field
"messageId", which is the SES message ID for the email message.
Convenience wrapper for HTTP service (not meant to be instantiated directly,
use .service('http', '<service-name>')
on a StitchClient instance).
(any)
(any)
HTTPService
:
a HTTPService instance.
Send a GET request to a resource (result must be application/json)
Promise
:
Send a POST request to a resource with payload
Promise
:
Send a PUT request to a resource with payload
Promise
:
Send a PATCH request to a resource with payload
Promise
:
Send a DELETE request to a resource
Promise
:
Create a new TwilioService instance (not meant to be instantiated directly,
use .service('twilio', '<service-name>')
on a StitchClient instance).
(any)
(any)
TwilioService
:
a TwilioService instance.
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 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.
(any)
Use information returned from the Stitch server to complete the password reset flow for a given email account, providing a new password for the account.
(String)
the tokenId provided by the Stitch server
(String)
the token provided by the Stitch server
(String)
the new password requested for this account
Promise
:
Will trigger an email to the requested account containing a link with the token and tokenId that must be returned to the server using emailConfirm() to activate the account.
(String)
the requested email for the account
(String)
the requested password for the account
Promise
:
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.
Creates a new StitchError
Extends Error
StitchError
:
A StitchError instance.