npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

botbuilder-storage-mongodb

v1.0.17

Published

This project provides a MongoDb storage mechanism for [Bot Framework-JS SDK V4.](https://github.com/Microsoft/botbuilder-js).

Downloads

2,901

Readme

State Storage for Bot Framework using MongoDB

This project provides a MongoDb storage mechanism for Bot Framework-JS SDK V4..

It allows you to store bot state in MongoDB, so that you can scale out your bot, and be more resilient to bot server failures.

Build and test

Requirements

  • NodeJS 13.x was used to create and test this library.
  • MongoDB database, or a database exposing basic MongoDB syntax.

Installation

npm install botbuilder-storage-mongodb

Sample Usage

(async () => {
  const mongoClient = new MongoClient("mongodb://localhost:27017/", { useUnifiedTopology: true });
  await mongoClient.connect();
  const collection = MongoDbStorage.getCollection(mongoClient);
  const mongoStorage = new MongoDbStorage(collection);

  // now we have a storage component instantiated:
  const conversationState = new ConversationState(mongoStorage);

  //... rest of your code
})();

See example code for more details.

Specifying Mongo Collection

The convenience method MongoDbStorage.getCollection(mongoClient) returns a MongoDB Collection object. It leverages default values MongoDbStorage.DEFAULT_DB_NAME and MongoDbStorage.DEFAULT_COLLECTION_NAME for the database and collection names respectively, resulting in the default collection BotFreamework.BotFrameworkState.

You may alternatively call the method and supply dbName and collectionName to suit your needs:

  MongoDbStorage.getCollection(mongoClient, 'MyDbName','MyCollectionName')
  // collection "MyCollectionName" in the database "MyDbName"

If using the method MongoDbStorage.getCollection(mongoClient) to obtain a collection, the mongoClient object must already be instantiated and connected!

You can also skip using the convenience method and supply a Collection instance to the constructor by obtaining a collection reference from your application configured to your liking. One reason you may want or need to do so is if you want to provide collection specific options not exposed by the convenience method.

⚠ Caution: you should not store Mongo URL in code! Get the url from a configuration such as environment variable or a secrets store in your environment. It may contain sensitive password in the clear and should never be stored in code!

See MongoDB Connection URI format in the official documentation to learn more about the connection url parameter value.

Stale State

Persisted state is stored indefinitely by Mongo, as the BotFramework on its own does not clean up persisted state.

You can leverage MongoDB's TTL index to automatically delete state records a certain time after their creation. The field dt on the state items is updated each time a state is saved. It is therefore a good candidate for expire-after-x type cleanup.

Using the MongoDB Shell, the commands below creates a TTL index on the dt field, causing MongoDB compliant engines to automatically delete documents older than 30 days:


  use BotFramework
   db.BotFrameworkState.createIndex({dt: 1}, {expireAfterSeconds: (60 * 60 * 24 * 30)});