1.0.7 • Published 2 years ago

botbuilder-mongo-storage v1.0.7

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
2 years ago

logo

Bot Framework MongoDB Storage + Cache (Redis)

Table of Contents

Installation:

npm install botbuilder-mongo-storage

Usage

Normal

// Options
const options = {
  database: 'foobar',
  collection: 'conversations',
};

(async () => {
  // storage
  const storage = new MongoStore('mongodb://localhost:27017/', options);
  await storage.connect();
  const conversationState = new ConversationState(storage);
})();

Redis Cache (redis options)

// Options
const options = {
  database: 'foobar',
  collection: 'conversations',
  redis: {
    url: 'redis://localhost:6380', // "rediss://..." - for TLS
    password: 'foobared'
  },
};

(async () => {
  // storage
  const storage = new MongoStore('mongodb://localhost:27017/', options);
  await storage.connect();
  const conversationState = new ConversationState(storage);
})();

Options

ParameterTypeDefaut valueDescription
databaseStringbot-storagedatabase name in mongodb
collectionStringconversationscollection name to store states
mongoMongoOptions-optional mongo connection options
redisRedisOptions-optional cache redis options
cacheExpirationNumber1209600 - 14 daysoptional TTL in seconds for redis cached state (14 days is default Microsoft directline conversation inactivity expiration time)
disableWriteConcernBooleanfalse(w/ redis)true (w/o redis)optional only when using redis, mongodb queries will be executed with writeConcern: { w: 0 } for better perfomance. more details

Advanced Usages

1. Health Check

// Options
const storageOptions = {
  database: 'bot',
  collection: 'conversations',
  redis: {
    url: 'redis://localhost:6380', // "rediss://..." - for TLS
    password: 'foobared'
  },
  mongo: {
    tls: true,
  },
  cacheExpiration: 604800, // 7 days
};

(async () => {
  // storage
  const storage = new MongoStore('mongodb://localhost:27017/', storageOptions);
  await storage.connect();
  const conversationState = new ConversationState(storage);

  server.get('/health', (req, res) => {
    const storageHealth = await storage.health(); // always resolves promise - no need to catch error
    // const storageHealth = {
    //   ok: 1,
    //   mongo: 1,
    //   redis: 1, // only if redis is enabled
    // };
    res.status(200);
    res.send({
      ok: 1,
      storage: storageHealth,
    });
  });
})();

2. TTL Document Mongo

// Options
const storageOptions = {
  database: 'bot',
  collection: 'conversations',
};

(async () => {
  // storage
  const storage = new MongoStore('mongodb://localhost:27017/', storageOptions);
  await storage.connect();
  await storage.storage.createIndex(
    { date: 1 },
    { expireAfterSeconds: 14 * 24 * 60 * 60, background: true }
  ); // TTL - Auto deletes document after 14 days
  const conversationState = new ConversationState(storage);
})();

FAQ

Why ?

  • Supports latest botbuilder framework
  • Built-in cache layer
  • Uses latest drivers for database and cache
  • Provides more flixble options for custom usage
  • Typescript support