项目作者: JLuboff

项目描述 :
MS SQL Server session store for Express Session
高级语言: TypeScript
项目地址: git://github.com/JLuboff/connect-mssql-v2.git
创建时间: 2019-11-06T03:23:47Z
项目社区:https://github.com/JLuboff/connect-mssql-v2

开源协议:MIT License

下载


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

  1. 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.

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

Usage

Javascript

  1. const MSSQLStore = require('connect-mssql-v2');
  2. const config = {
  3. user: '...',
  4. password: '...',
  5. server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
  6. database: '...',
  7. options: {
  8. encrypt: true, // Use this if you're on Windows Azure
  9. trustServerCertificate: true, // use this if your MS SQL instance uses a self signed certificate
  10. },
  11. };
  12. app.use(
  13. session({
  14. store: new MSSQLStore(config, options), // options are optional
  15. secret: 'supersecret',
  16. }),
  17. );

Typescript

  1. import MSSQLStore from 'connect-mssql-v2';
  2. const config = {
  3. user: '...',
  4. password: '...',
  5. server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
  6. database: '...',
  7. options: {
  8. encrypt: true, // Use this if you're on Windows Azure
  9. trustServerCertificate: true, // use this if your MS SQL instance uses a self signed certificate
  10. },
  11. };
  12. app.use(
  13. session({
  14. store: new MSSQLStore(config, options), // options are optional
  15. secret: 'supersecret',
  16. }),
  17. );

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.preRemoveCallback - Is the callback function for destroyExpired() that is called before the actual removal.
    If this returns a promise, the removal will wait for the promise to resolve. Default: undefined
  • 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
  • options.retries - The number of times to retry a DB connection before failing. Default: 0 (tries once)
  • options.retryDelay - The retry delay in milliseconds. Default: 1000

Advanced usage

  1. const store = new MSSQLStore(config, options);
  2. store.on('connect', () => {
  3. // ... connection established
  4. });
  5. store.on('error', (error) => {
  6. // ... connection error
  7. });
  8. store.on('sessionError', (error, classMethod) => {
  9. // ... any error that occurs within a store method
  10. // classMethod will return the method name (get, set, length, etc)
  11. })
  12. app.use(session({
  13. store: store
  14. secret: 'supersecret'
  15. }));

Configuration

To see all options please visit node-mssql docs.

Upgrading from v5.x.x to v6.x.x

Ensure you’re running Node >= v17

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

  1. 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

  1. 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