An IBM DB2 session store for express.js.
Install via npm:
npm install connect-db2 express-session --save
Pass the express-session
store into connect-db2
to create a Db2Store
constructor.
Using a data source name (DSN)var session = require('express-session');
var Db2Store = require('connect-db2')(session);
Â
var options = {
    host: 'localhost',
    port: 50000,
    username: 'db2user',
    password: 'password',
    database: 'BLUDB'
};
Â
var sessionStore = new Db2Store(options);
app.use(session({
    store: sessionStore,
    secret: 'keyboard cat'
}));
An altenative to supplying individual settings is to supply the full DSN string in the config instead:
var session = require('express-session');
var Db2Store = require('connect-db2')(session);
Â
var options = {
    dsn: 'DRIVER={DB2};DATABASE=BLUDB;HOSTNAME=loclhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;'
};
Â
var sessionStore = new Db2Store(options);
app.use(session({
    store: sessionStore,
    secret: 'keyboard cat'
}));
Note: When a DSN is available in the store config it will always be preferred over individual connection settings.
Using an existing connectionEnabling SSLvar session = require('express-session');
var Db2Store = require('connect-db2')(session);
var ibmdb = require('ibm_db');
Â
var dsn = 'DRIVER={DB2};DATABASE=BLUDB;HOSTNAME=loclhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;';
var options = {};
var conn = ibmdb.openSync(dsn);
Â
var sessionStore = new Db2Store(options, conn);
app.use(session({
    store: sessionStore,
    secret: 'keyboard cat'
}));
Set options.use_ssl
to true
if you want to connect using SSL when using individual settings.
var session = require('express-session');
var Db2Store = require('connect-db2')(session);
Â
var options = {
    host: 'localhost',
    port: 50001,              Â
    username: 'db2user',
    password: 'password',
    database: 'BLUDB',
    use_ssl: true
};
Â
var sessionStore = new Db2Store(options);
app.use(session({
    store: sessionStore,
    secret: 'keyboard cat'
}));
When using a DSN, you can either set the options.dsn
to an SSL connection string or set options.use_ssl = true
, and use the options.ssldsn
property.
var session = require('express-session');
var Db2Store = require('connect-db2')(session);
Â
var options = {
    ssldsn: 'DRIVER={DB2};DATABASE=BLUDB;HOSTNAME=loclhost;PORT=50001;PROTOCOL=TCPIP;UID=db2user;PWD=password;Security=SSL;',
    use_ssl: true
};
Â
var sessionStore = new Db2Store(options);
app.use(session({
    store: sessionStore,
    secret: 'keyboard cat'
}));
So when using DSNs, options.ssldsn
and options.dsn
can both be set and valid and you can choose between them using the options.use_ssl
flag. This makes it convenient when working within the Bluemix environment where both properties are pre-set in the service config.
You can ask the store to create the session table for you:
sessionStore.createDatabaseTable(function(error){
    if(error){
       Â
        return;
    }
});
This will ofcourse fail if the table already exists. To create the table only when it does not exist, use:
Closing the session storesessionStore.hasDatabaseTable(function(error, hasTable){
    if(error){
       Â
        return;
    }
    if(hasTable === false) {
        sessionStore.createDatabaseTable(function(error){
Â
        });
    }
});
To cleanly close the session store:
OptionssessionStore.close(function(error){
    if(error){
       Â
        return;
    }
});
Here is a list of all available options together with their default values:
Contributingvar options = {
    host: 'localhost',        Â
    port: 50000,              Â
    username: 'db2user',      Â
    password: 'password',     Â
    database: 'BLUDB',        Â
    expiration: 2592000,      Â
    use_ssl: false            Â
    schema: {
        tableName: 'sessions',
        columnNames: {
            session_id: 'session_id',
            expires: 'expires',
            data: 'data'
        }
    },
    allowDrop: false           Â
};
There are a number of ways you can contribute:
Before you contribute code, please read through at least some of the source code for the project. I would appreciate it if any pull requests for source code changes follow the coding style of the rest of the project. Use npm run lint
to lint your code before submission.
First, you'll need to pull down the code from GitHub:
git clone https://github.com/wallali/connect-db2.git
Step 2: Install Dependencies
Second, you'll need to install the project dependencies as well as the dev dependencies. To do this, simply run the following from the directory you created in step 1:
npm install
Step 3: Set Up the Test Database
Now, you'll need to set up a local test database or create a free dashDB instance on IBM Bluemix:
{
    host: 'localhost',
    port: 50000,
    username: 'db2user',
    password: 'password',
    database: 'BLUDB',
    dsn: ''
};
The test database settings are located in test/config.js
Alternatively, you can provide custom database configurations via environment variables:
DB_HOST="localhost"
DB_PORT="50000"
DB_USER="db2user"
DB_PASS="password"
DB_NAME="BLUDB"
or a DSN via environment variables:
DB_DSN="DRIVER={DB2};DATABASE=BLUDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;"
Running Tests
With your local environment configured, running tests is as simple as:
npm test
Debugging
connect-db2
uses the debug module to output debug messages to the console. To output all debug messages, run your node app with the DEBUG
environment variable:
DEBUG=connect:db2 node your-app.js
This will output debugging messages from connect-db2
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4