Git Product home page Git Product logo

botkit-storage-mongo's Introduction

botkit-storage-mongo

npm David npm bitHound Overall Score

This module works with the legacy version of Botkit

A Mongo storage module for Botkit that provides a simple system for storing information about a user, a channel, or a team.

This project is an extension to Botkit Core, a richly featured developer toolkit for building bots and conversational apps.

This module conforms to [Botkit's storage plugin convention] (https://github.com/howdyai/botkit-docs/blob/master/docs/storage.md).

Usage

Just require botkit-storage-mongo and pass it a config with a mongoUri option. In addition, you may pass a tables parameter, which will create methods for accessing additional tables (other than user, channel and team) in your Mongo database. Then pass the returned storage when creating your Botkit controller. Botkit will do the rest.

Make sure everything you store has an id property, that's what you'll use to look it up later.

var Botkit = require('botkit'),
    mongoStorage = require('botkit-storage-mongo')({mongoUri: '...', tables: ['optional','list', 'of', 'custom','tables', 'to', 'add']}),
    controller = Botkit.slackbot({
        storage: mongoStorage
    });
// then you can use the Botkit storage api, make sure you have an id property
var beans = {id: 'cool', beans: ['pinto', 'garbanzo']};
controller.storage.teams.save(beans);
controller.storage.teams.get('cool', function(error, beans){
    // do something with beans
});

You can also get all entries from a table or find a selected set depending on a parameters.

storage.users.all(function(error, users){
    // do something with users
});
storage.users.find({team_id: team_id}, function(error, users){
    // do something with users
});

As of 1.0.6, all functions also support Promise syntax:

storage.users.all().then(function(list_of_users) {

  // do something

});

botkit-storage-mongo's People

Contributors

alexsmanning avatar anonrig avatar benbrown avatar colestrode avatar johanhenrikssn avatar mattcobb avatar oiorain avatar shindakun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

botkit-storage-mongo's Issues

Use with new versions of mongodb package fails to connect because of a change in the core mongo driver

Monk, which is used by botkit-storage-mongo was using the option safe prior to version 4.0.0. Versions of mongodb package after 2.2.21 fail to connect giving the following error message:

debug:mongodb Cannot connect to mongo +6m
debug:mongodb MongoError: option safe is not supported

Option safe was removed in monk version 4.0.0 but botkit-storage-mongo is still relying on an old version. Please update to monk 4.0.0 in order to support recent versions of the mongodb driver.

Support passing options to `.find()`

It doesn't seem to be possible to use sort or limit with botkit-storage-mongo because the exposed API does not accept options and pass them through to monk.

This means you can only ever retrieve all of the items in a collection that meet your query parameters and they will be returned in an order determined by Mongo.

This is less than ideal for many usecases.

I plan to submit a PR to address this, but am not sure whether you'd rather the exposed API become polymorphic and allow the options to be an optional second parameter, which I think the monk API supports, or if you'd prefer we break the convention of the monk API and add the options as an argument after the callback. Either of these options should be backwards compatible and not break any existing code that uses botkit-storage-mongo.

Refactor tests to be true unit tests

Right now the tests require a local instance of MongoDB to run. I think we should refactor the tests to be true unit tests with mocked deps (similar to botkit-storage-redis). That way we will be able to plug in something like TravisCI to validate PR's, etc.

Add keywords to package.json

It would be good to add keywords to the package.json so that they are searchable on NPM. I suggest botkit, slack, and mongo (or mongodb) to be in line with the botkit-storage-redis module.

This will require republishing to NPM, so we might wait until we get sufficient changes for a version bump before publishing.

Make Delete take data, not an id.

delete: function(id, cb) {
    return table.findOneAndDelete({id: id}, cb);
}

Could this be changed to the following?

delete: function(data, cb) {
    return table.findOneAndDelete(data, cb);
}

That way we can use it in the following manner:

controller.storage.users.delete({ something: { $lt: 30 } }).catch((error) => {
    console.log(error);   
});

Thanks for all the great work!

Can get method return a promise?

Running the following example given in README.md, controller.storage.teams.get('cool') returns null

var beans = {id: 'cool', beans: ['pinto', 'garbanzo']}; controller.storage.teams.save(beans); beans = controller.storage.teams.get('cool');

It looks like the underlying Monk api returns a promise, but that promise is not returned by the get function so we get beans === null.

How would people feel about changing table.findOne({id: id}, cb); to return table.findOne({id: id}, cb); in the get function and table.find({}, cb); to return table.find({}, cb); in the all function? This way plugin users can handle the promise themselves.

I can submit a pull request for this.

I realize there is a callback option...why not support both promises and callbacks?

TypeError: Cannot read property 'name' of undefined at makeSkinClass

When I run the program with mongodb storage adaptor, I get this error. Does anyone have any suggestions or run into this issue? @anonrig

/Users/HFischberg/internship/ichimaster/node_modules/monk/node_modules/mongoskin/lib/utils.js:33
  var skinClassName = 'Skin' + NativeClass.name;
                                          ^

TypeError: Cannot read property 'name' of undefined
    at makeSkinClass (/Users/HFischberg/internship/ichimaster/node_modules/monk/node_modules/mongoskin/lib/utils.js:33:43)
    at Object.<anonymous> (/Users/HFischberg/internship/ichimaster/node_modules/monk/node_modules/mongoskin/lib/grid.js:6:35)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/HFischberg/internship/ichimaster/node_modules/monk/node_modules/mongoskin/lib/db.js:22:16)

Add Remove

Right now there is no way to remove records using this library. This would be helpful and is something the simple storage can already do.

Outdated monk package

It seems that this module is using an outdated version of Monk, which is causing some weird errors with dependencies.

$ npm install
[...]
โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ””โ”€โ”ฌ [email protected] 
โ”‚   โ”œโ”€โ”€ UNMET PEER DEPENDENCY mongodb@~1.4
โ”‚   โ”œโ”€โ”€ [email protected] 
โ”‚   โ””โ”€โ”€ [email protected] 
[...]
npm WARN [email protected] requires a peer of mongodb@~1.4 but none was installed.

This is a bug with monk, not your app, but you're using [email protected], and the latest version is [email protected]. 2.1.0 has the issue fixed (since, like, last year).

Fails to connect even though mongod says connection accepted

Just a note to anyone else who comes across this error, I used the mongo hookup with slakcbutton_bot_interactivemsg.js, but had already tested my program using simple storage. Long story short, the program bypassed creating a new bot since I already had a token, and attempted to populate my "teams" list, which was where the program ultimately hung up (the teams list was empty, so it couldn't make a proper call to startRTM). The failure to connect message was misleading and is not the cause of the issue. It all works fine.

Error: No connection URI provided.

var Botkit = require('../botkit');
var botkit_storage_mongo = require("botkit-storage-mongo")({mongoUri: "<uri removed for security reasons>"});

var controller = Botkit.slackbot({
  debug: false,
  storage: botkit_storage_mongo
});
/home/ubuntu/workspace/slack-bot-predict2/node_modules/monk/lib/manager.js:28
    throw Error('No connection URI provided.');
    ^

Error: No connection URI provided.

Extending db

Hi,

I am building a bot which would track records of time. E.g.: user calls it like this /work start and the record of date/time would be written.

I am looking for some ideas where to write this. Could I use the engine you wrote?

Thank you for pointers in advance.

Ziga

Error: slash in host identifier

to repo:

  • set MONGO_URI to the uri of my mongodb (mongodb://AdminUser:[email protected]:27017,cluster0-shard-00-01-sdfgf.mongodb.net:27017,cluster0-shard-00-02-sdfgd.mongodb.net:27017/test?replicaSet=Cluster0-shard-0)

  • run botkit starter

  • go to /login and click authorize app

  • server crashes with this error:

(node:28411) UnhandledPromiseRejectionWarning: Error: Error: slash in host identifier
    at /Users/jameslynch/Git-Projects/Kate-From-HR/chatbot-slack-starter-kate/node_modules/botkit-storage-mongo/src/index.js:24:13
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
(node:28411) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:28411) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

botkit-storage-mongo: 1.0.6
node -v: 8.16.0

MongoDB connection error: Error: slash in host identifier

Hello, I'm developing a node.js project with mongodb, express, but I get this error when I start the server:

MongoDB connection error: Error: slash in host identifier
[nodemon] app crashed - waiting for file changes before starting ...

this is my files:
mongoose.js file:

const { mongo, env } = require('./vars');

// set mongoose Promise to Bluebird
mongoose.Promise = Promise;

// Exit application on error
mongoose.connection.on('error', (err) => {
  console.error(`MongoDB connection error: ${err}`);
  process.exit(-1);
});

// print mongoose logs in dev env
if (env === 'development') {
  mongoose.set('debug', true);
}

/**
* Connect to mongo db
*
* @returns {object} Mongoose connection
* @public
*/
exports.connect = () => {
  mongoose.connect(mongo.uri, {
    keepAlive: 1,
    useMongoClient: true,
  });
  return mongoose.connection;
};

my var.js file:

const path = require('path');

// import .env variables
require('dotenv-safe').load({
  path: path.join(__dirname, '../../.env'),
  sample: path.join(__dirname, '../../.env.example'),
});

module.exports = {
  env: process.env.NODE_ENV,
  port: process.env.PORT,
  jwtSecret: process.env.JWT_SECRET,
  jwtExpirationInterval: process.env.JWT_EXPIRATION_MINUTES,
  masterAccount: process.env.MASTER_ACCOUNT_NUMBER,
  masterAccountPassword: process.env.MASTER_ACCOUNT_PASSWORD,
  mongo: {
    uri: process.env.NODE_ENV === 'test'
      ? process.env.MONGO_URI_TESTS
      : process.env.MONGO_URI,
  },
  logs: process.env.NODE_ENV === 'production' ? 'combined' : 'dev',
};

the index.js file:

Promise = require('bluebird'); // eslint-disable-line no-global-assign
const { port, env } = require('./config/vars');
const app = require('./config/express');
const mongoose = require('./config/mongoose');

// open mongoose connection
mongoose.connect();

// listen to requests
app.listen(port, () => console.info(`server started on port ${port} (${env})`));

/**
* Exports express
* @public
*/
module.exports = app;

thanks for you help

Error when incrementing with save

UnhandledPromiseRejectionWarning: MongoError: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage

      var user = {id: uid, $inc: {ref: 1}}
      controller.storage.users.save(user);

is there any way I can do that without setting the separate findOneAndUpdate update option?

Test suite fails in Initialization 'should initialize monk with mongoUri'

Specifications

  • Version: 1.0.7
  • MacOS High Sierra 10.13.6
  • Node Version: 11.4.0

Expected Behavior

Expected the test suite to pass

Actual Behavior

It fails on the 'should initialize monk with mongoUri' test on line 42. All subsequent tests also fail. Basically, the monkMock function isn't being called.

If I comment out line 43 and then manually call monkMock(config), the assertion on line 44 passes, but line 45 still fails it.

Steps to Reproduce

I haven't touched anything. Simply forked the project, installed the dependencies and ran $ npm run test.

Screenshots, GIFs, Error Output

screen shot 2019-02-19 at 1 02 40 pm

MongoDB this.targetCollection.find is not a function

Just recently having an issue with MongoDB and not sure how to go about resolving it.
I am following the directions on botkit docs exactly.
It was working fine until today I did do an npm update and also deleted the node modules and reinstalled
image

Error when getting documents using the _id field

Hi team,

I'm getting an error when I'm querying for a specific record by _id

var business = controller.storage.businesses.get(
    user.businessId
,
function (error, business) {
   ...
});

Where inside user.businessId it is stored the _id of the user that created that specific business.

When executing, I get the following error:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

and according to the stack overflow topics I found _id should be fine containing any data (it's a little bit tricky since none of them is specific to botkit storage). Could you help? Is there any additional process I need to do to the _id field to be able to find those records?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.