Giter Club home page Giter Club logo

connect-sqlite3's Introduction

Connect SQLite3

connect-sqlite3 is a SQLite3 session store modeled after the TJ's connect-redis store.

Installation

	  $ npm install connect-sqlite3

Options

  • table='sessions' Database table name
  • db='sessionsDB' Database file name (defaults to table name)
  • dir='.' Directory to save '.db' file
  • createDirIfNotExists='false' Directory 'dir' is created recursively if not exists
  • concurrentDB='false' Enables WAL mode (defaults to false)

Usage

    var connect = require('connect'),
        SQLiteStore = require('connect-sqlite3')(connect);

    connect.createServer(
      connect.cookieParser(),
      connect.session({ store: new SQLiteStore, secret: 'your secret' })
    );

with express

    3.x:
    var SQLiteStore = require('connect-sqlite3')(express);

    4.x:
    var session = require('express-session');
    var SQLiteStore = require('connect-sqlite3')(session);

    app.configure(function() {
      app.set('views', __dirname + '/views');
      app.set('view engine', 'ejs');
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(express.cookieParser());
      app.use(session({
        store: new SQLiteStore,
        secret: 'your secret',
        cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 } // 1 week
      }));
      app.use(app.router);
      app.use(express.static(__dirname + '/public'));
    });

Test

    $ npm test

connect-sqlite3's People

Contributors

akshayeshenoi avatar apkovy avatar dougluce avatar drfloob avatar driverjb avatar fauxpark avatar guilala avatar jdharvey-ibm avatar jniles avatar macil avatar pauloasilva avatar quinnhosler avatar rawberg avatar vicneanschi avatar wmertens 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

Watchers

 avatar  avatar

connect-sqlite3's Issues

dbCleanup not working

Perhaps it's just me, but the comparison in line 39 does not seem to be working, since expired was declared as a TEXT field. If I cast it as INTEGER it works as intended, e.g.:

function dbCleanup(store) {
    var now = new Date().getTime();
    store.db.run('DELETE FROM ' + store.table + ' WHERE ? > CAST(expired as INTEGER)', [now]);
}

It would probably be more performant to just declare expired as INTEGER when the db is created though?

How do I share sessions with express and websockets using connect-sqlite3?

I need to share sessions between express-session (v4) GET requests and WebSockets on('connection') using connect-sqlite3.

The following basic setup without sqlite3 (from Azmisov's answer at https://stackoverflow.com/questions/12182651/expressjs-websocket-session-sharing) works:

global.app = express();
var sessionParser = require('express-session')({
    secret:"some secret",
    resave: true,
    saveUninitialized: true
});
app.use(sessionParser);

app.get('*', function (req, res) {
	req.session.working = "Yes";
});

wss.on('connection', function connection(ws, req) {
	sessionParser(req, {}, function(){
		console.log("New websocket connection: Working = " + req.session.working + ".");
	});
});

However, when I try to implement this with connect-sqlite3:

var sessionParser = require('express-session');
var SQLiteStore = require('connect-sqlite3')(sessionParser);
app.use(sessionParser({
	store: new SQLiteStore({dir:configuration.data_dir, db:'sessions.s3db', table:'sessions',}),
	secret: 'some secret',
	resave: false,
	saveUninitialized: true,
	cookie: {
		maxAge: configuration.session_cookie_maxage_ms,
		name: 'shadowlands'
	}
}));

app.get('*', function (req, res) {
	req.session.working = "Yes";
});

wss.on('connection', function connection(ws, req) {
	sessionParser(req, {}, function(){
		console.log("New websocket connection: Working = " + req.session.working + ".");
	});
});

In wss on('connection') at sessionParser(req, {}, function(){ ...), I get:

express-session deprecated undefined resave option; provide resave option; provide resave option.
express-session deprecated undefined resave option; provide saveUninitialized option; provide saveUninitialized option.
express-session deprecated req.secret; provide secret option.

This error still occurs if I change to:

sessionParser(req, {secret: 'some secret', resave: false, saveUninitialized: true}, function(){

The error goes away if I take out "req":

sessionParser({secret: 'some secret', resave: false, saveUninitialized: true}, function(){

But then the sessionParser function does not get called, and req.session.working is not shown in the console.

Any help or an example in the readme much appreciated, thanks!
Eric T.

SQLITE_BUSY errors when running multiple processes

Hi,
I'm new to clustering. I opted to try pm2 as my clustering method, since that seemed easiest. However, this yields a lot of errors like:
Error: SQLITE_BUSY: database is locked

It also appears that many times, the session isn't being retrieved. I can see that the ./session file is getting updated, though.

I'm guessing that what's happening is that many small files are being accessed all at once, and that is logjamming the server, but I can't tell.

Do I have something misconfigured, or is this a limitation of the store?

can not install

hi, here is the error:

 npm install connect-sqlite3
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: protobufjs@^6.8.6 (node_modules/@google-cloud/firestore/node_modules/protobufjs):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Unexpected end of JSON input while parsing near '...":">=0.8"}},"4.0.0-b2'
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: protobufjs@^6.8.8 (node_modules/google-gax/node_modules/protobufjs):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Unexpected end of JSON input while parsing near '...":">=0.8"}},"4.0.0-b2'
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: protobufjs@^6.8.6 (node_modules/@grpc/proto-loader/node_modules/protobufjs):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Unexpected end of JSON input while parsing near '...":">=0.8"}},"4.0.0-b2'

npm ERR! Unexpected end of JSON input while parsing near '...":">=0.8"}},"4.0.0-b2'

Creating the directory for database file, if it does not exists

I migrated from session-file-store to connect-sqlite3 store.

What I am really missing is the feature, when the directory from dir option is created, if it did not exist already.

I checked the file-store code and they are using the fsExtra.mkdirsSync method to create even the nested path along the way, if it does not exists.

Thanks!

Sessions are too short

If you don't specify a maxAge you get oneDay in seconds, which is wrong. Should be oneDay in milliseconds. This causes sessions to be far too short.

dir option not working

It seems connect-sqlite3 is ignoring the dir option when it's added:

app.use(session({
  store: new SQLiteStore,
  secret: config.secret,
  cookie: { maxAge: 14 * 24 * 60 * 60 * 1000 },
  resave: false,
  saveUninitialized: false,
  dir: "./data"
}));

This still saves the file as ./session instead of ./data/session.

Cannot install sqlite3

When installing the package, it fails to install sqlite3 with node-waf: command not found. Is there any reason you're referencing github directly in the dependency and not npm? I cloned and linked the repo and it work if the sqlite dependency line is replaced with "sqlite3": "^3.0.4".

Session doesn't get saved to database

Hello.

my session gets not saved to the sqlite3 db. My db works, I can create users etc. and the sessions table gets also added to my db.

I implemented it like this:

Imports

import session from "express-session";
import SQLiteStore from "connect-sqlite3";

Implementation in my server.js

app.use(methodOverride());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
  session({
    resave: false,
    saveUninitialized: true,
    secret: process.env.SESSION_SECRET,
    cookie: { maxAge: 1000 * 60 * 60 * 24 },
    store: new SQLiteStore(session)({
      dir: "./db",
      db: "main.db",
      table: "sessions",
      concurrentDB: true,
    }),
  })
);

What else can be the problem?

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.