Coder Social home page Coder Social logo

bradtaniguchi / connect-mssql-v2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jluboff/connect-mssql-v2

0.0 1.0 0.0 796 KB

MS SQL Server session store for Express Session

License: MIT License

TypeScript 96.25% JavaScript 3.52% Shell 0.23%

connect-mssql-v2's Introduction

Gitpod ready-to-code License: MIT npm npm GitHub issues GitHub issues

connect-mssql-v2

SQL Server session store for Connect/Express based on node-mssql and the deprecated/abandoned project connect-mssql.

Installation

npm install connect-mssql-v2

Prerequisites

Before you can use session store, you must create a table. Recommended table name is sessions but you can change it via options. The database user must have db_datareader, db_datawriter, and db_ddladmin permissions.

CREATE TABLE [dbo].[sessions](
    [sid] [nvarchar](255) NOT NULL PRIMARY KEY,
    [session] [nvarchar](max) NOT NULL,
    [expires] [datetime] NOT NULL
)

Usage

Javascript

const MSSQLStore = require('connect-mssql-v2');

const config = {
  user: '...',
  password: '...',
  server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
  database: '...',
  options: {
    encrypt: true, // Use this if you're on Windows Azure
    trustServerCertificate: true, // use this if your MS SQL instance uses a self signed certificate
  },
};

app.use(
  session({
    store: new MSSQLStore(config, options), // options are optional
    secret: 'supersecret',
  }),
);

Typescript

import MSSQLStore from 'connect-mssql-v2';

const config = {
  user: '...',
  password: '...',
  server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
  database: '...',
  options: {
    encrypt: true, // Use this if you're on Windows Azure
    trustServerCertificate: true, // use this if your MS SQL instance uses a self signed certificate
  },
};

app.use(
  session({
    store: new MSSQLStore(config, options), // options are optional
    secret: 'supersecret',
  }),
);

Options

  • options.table - Table to use as session store. Default: [sessions]
  • options.ttl - (Time To Live) Determines the expiration date. Default: 1000 * 60 * 60 * 24 (24 hours)
  • options.autoRemove - Determines if expired sessions should be autoremoved or not. If value is true then a new function, destroyExpired(), will autodelete expired sessions on a set interval. Default: false
  • options.autoRemoveInterval - Sets the timer interval for each call to destroyExpired(). Default: 1000 * 60 * 10 (10 min)
  • options.autoRemoveCallback - Is the callback function for destroyExpired(). Default: undefined
  • options.useUTC - Determines if we are to use the GETUTCDATE instead of GETDATE Default: true

Advanced usage

const store = new MSSQLStore(config, options);

store.on('connect', () => {
	// ... connection established
});

store.on('error', (error) => {
	// ... connection error
});

store.on('sessionError', (error, classMethod) => {
  // ... any error that occurs within a store method
  // classMethod will return the method name (get, set, length, etc)
})
app.use(session({
    store: store
    secret: 'supersecret'
}));

Configuration

To see all options please visit node-mssql docs.

Upgrading from v4.x.x to v5.x.x

Ensure you're running Node >=v15

Upgrading from v3.x.x to v4.x.x

Ensure you're running Node >=v13

Upgrading from v2.x.x to v3.x.x

The key step to upgrading is to include

trustServerCertificate: true;

in your options object for the store config (see either javascript or typescript example) if running a local instance of MS SQL with a self signed certificate. If you do not provide this, you will get a connection error

ConnectionError: Failed to connect to databaseserver:1433 - self signed certificate

Upgrading from v1.x.x to v2.x.x

It is no longer required to pass in the express-session store. Please see the Usage section on the updated import/require method.

Contributions

Contributions are welcome, please submit a PR which will be reviewed.

Reporting Issues

Please report issues/errors to Github's issue tracker: connect-mssql-v2 issue tracker. Include issue, expected behavior, and how to replicate the issue.

License

MIT License

connect-mssql-v2's People

Contributors

bradtaniguchi avatar dependabot[bot] avatar jluboff avatar master117 avatar naomi-lgbt avatar

Watchers

 avatar

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.