• Stars
    star
    217
  • Rank 182,446 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple, minimal PostgreSQL session store for Connect/Express

Connect PG Simple

A simple, minimal PostgreSQL session store for Express/Connect

npm version npm downloads Module type: CJS js-semistandard-style Follow @voxpelli@mastodon.social

Installation

npm install connect-pg-simple

Once npm installed the module, you need to create the "session" table in your database.

For that you can use the table.sql file provided with the module:

psql mydatabase < node_modules/connect-pg-simple/table.sql

Or simply play the file via a GUI, like the pgAdminIII queries tool.

Or instruct this module to create it itself, by setting the createTableIfMissing option.

Note that connect-pg-simple requires PostgreSQL version 9.5 or above.

Usage

Examples are based on Express 4.

Simple example:

const session = require('express-session');

app.use(session({
  store: new (require('connect-pg-simple')(session))({
    // Insert connect-pg-simple options here
  }),
  secret: process.env.FOO_COOKIE_SECRET,
  resave: false,
  cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
  // Insert express-session options here
}));

Advanced example showing some custom options:

const pg = require('pg');
const expressSession = require('express-session');
const pgSession = require('connect-pg-simple')(expressSession);

const pgPool = new pg.Pool({
    // Insert pool options here
});

app.use(expressSession({
  store: new pgSession({
    pool : pgPool,                // Connection pool
    tableName : 'user_sessions'   // Use another table-name than the default "session" one
    // Insert connect-pg-simple options here
  }),
  secret: process.env.FOO_COOKIE_SECRET,
  resave: false,
  cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
  // Insert express-session options here
}));

Advanced options

Connection options

Listed in the order they will be picked up. If multiple are defined, then the first in the lists that is defined will be used, the rest ignored.

  • pool - The recommended one – Connection pool object (compatible with pg.Pool) for the underlying database module.
  • pgPromise - Database object from pg-promise to be used for DB communications.
  • conObject - If you don't specify a pool object, use this option or conString to specify a PostgreSQL Pool connection object and this module will create a new pool for you.
  • conString - If you don't specify a pool object, use this option or conObject to specify a PostgreSQL connection string like postgres://user:password@host:5432/database and this module will create a new pool for you. If there's a connection string in the DATABASE_URL environment variable (as it is by default on eg. Heroku) then this module will fallback to that if no other connection method has been specified.

Other options

  • ttl - the time to live for the session in the database – specified in seconds. Defaults to the cookie maxAge if the cookie has a maxAge defined and otherwise defaults to one day.
  • createTableIfMissing - if set to true then creates the table in the case where the table does not already exist. Defaults to false.
  • disableTouch – boolean value that if set to true disables the updating of TTL in the database when using touch. Defaults to false.
  • schemaName - if your session table is in another Postgres schema than the default (it normally isn't), then you can specify that here.
  • tableName - if your session table is named something else than session, then you can specify that here.
  • pruneSessionInterval - sets the delay in seconds at which expired sessions are pruned from the database. Default is 60 seconds. If set to false no automatic pruning will happen. By default every delay is randomized between 50% and 150% of set value, resulting in an average delay equal to the set value, but spread out to even the load on the database. Automatic pruning will happen pruneSessionInterval seconds after the last pruning (includes manual prunes).
  • pruneSessionRandomizedInterval – if set to false, then the exact value of pruneSessionInterval will be used in all delays. No randomization will happen. If multiple instances all start at once, disabling randomization can mean that multiple instances are all triggering pruning at once, causing unnecessary load on the database. Can also be set to a method, taking a numeric delay parameter and returning a modified one, thus allowing a custom delay algorithm if wanted.
  • errorLog – the method used to log errors in those cases where an error can't be returned to a callback. Defaults to console.error(), but can be useful to override if one eg. uses Bunyan for logging.

Useful methods

  • close() – if this module used its own database module to connect to Postgres, then this will shut that connection down to allow a graceful shutdown. Returns a Promise that will resolve when the database has shut down.
  • pruneSessions([callback(err)]) – will prune old sessions. Only really needed to be called if pruneSessionInterval has been set to false – which can be useful if one wants improved control of the pruning.

For enterprise

Available as part of the Tidelift Subscription.

The maintainers of connect-pg-simple and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.

More Repositories

1

types-in-js

Tips and tricks for working with types in JavaScript
232
star
2

node-pg-pubsub

A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN
JavaScript
231
star
3

webpage-micropub-to-github

Self-hosteable Micropub endpoint that publishes to Jekyll by committing to GitHub
JavaScript
181
star
4

sass-color-helpers

A collection of Sass color helpers that enables easier, more fool-proof design implementations (+ some math helpers)
SCSS
93
star
5

node-installed-check

Checks that all dependencies in your package.json have supported versions installed and complies with your specified node engine version range
JavaScript
79
star
6

webpage-webmentions

A hosted Disqus-like comment service that enables WebMention receiving on any kind of site
JavaScript
72
star
7

node-webmention-testpinger

A tool to ping your site with a variety of real world WebMentions
HTML
47
star
8

pony-cause

Ponyfill and helpers for the standardized Error Causes
JavaScript
44
star
9

relspider

A web crawler that indexes the identity/social graph of profiles on the web
JavaScript
37
star
10

node-micropub-express

Provides a Micropub route for Express 4.x
JavaScript
36
star
11

node-github-publish

Publishes a file to a repository through the GitHub Contents API
JavaScript
22
star
12

jquery-loadmore

jQuery plugin for easily adding a "more"-link based pagination to an element on ones site
JavaScript
21
star
13

moz-urlbarbutton

Urlbar Buttons for Mozilla Add-on SDK
JavaScript
17
star
14

drupal-connector

Drupal module for creating connections between a third party site user and a Drupal user
PHP
17
star
15

async-htm-to-string

Renders a htm tagged template asyncly into a string
JavaScript
14
star
16

node-format-microformat

Formats a Microformat JSON representation into eg. a Jekyll post
JavaScript
13
star
17

drupal-oauthconnector

Drupal module that makes it possible to log in through third party API:s supporting OAuth.
PHP
13
star
18

drupal-oembed

An oEmbed client and provider for Drupal
PHP
12
star
19

node-one-page

Quick and easy one pagers for events and other small information pages
JavaScript
12
star
20

voxpelli.github.com

My personal blog, based on Jekyll
HTML
9
star
21

node-jekyll-utils

A collection of Jekyll utilility methods to generate eg. Jekyll permalinks and slugs
JavaScript
8
star
22

paw-humangenerator

Paw extension providing a generator geared towards human readable exports
JavaScript
6
star
23

node-promised-retry

A generic promised based retry mechanism. Useful for eg. ensuring an available database or message queue connection
JavaScript
6
star
24

node-fetch-politely

Ensures polite outgoing HTTP requests that respects robots.txt and aren't made too close to each other
JavaScript
6
star
25

drupal-fbconnector

Sub-module to the drupal-connector module
PHP
6
star
26

moz-showforpage

Experimental simplified page load events for Mozilla Add-on SDK
JavaScript
6
star
27

compare-eslint-configs

Compare ESLint configs
JavaScript
5
star
28

node-installed-check-core

Checks whether installed modules fulfills the requirements of package.json
JavaScript
5
star
29

linemod-core

Comment driven instructions to modify lines
JavaScript
5
star
30

gm-othermes

A Greasemonkey script that adds icons to tweets showing the Twitter users' profiles on other social networks.
JavaScript
5
star
31

drupal-og-enhanced

Some enhancement modules for the Organic Groups module
4
star
32

indie-action-component

Web Component for WebActions: <indie-action>
JavaScript
4
star
33

drupal-designreview

Drupal module which should make it easier to get an overview of a themes design by gathering important elements on one site
4
star
34

drupal-closeblock

Lets users close blocks permanently on their site
JavaScript
4
star
35

tsconfig

My personal tsconfig base configs
4
star
36

wp-newspapermeta

Adds fields for newspaper related meta data to Wordpress posts
4
star
37

drupal-multiping

A fork of the Multiping module from Drupal.org
PHP
4
star
38

list-installed

A modern typed async alternative to read-installed / readdir-scoped-modules
JavaScript
4
star
39

node-vptweestream

Twitter Streaming API library supporting replacing a stream with a new one - work in progress
JavaScript
4
star
40

linemod

Comment driven line modifications
JavaScript
3
star
41

jquery-flyout

A fork of jQuery Image Flyout
JavaScript
3
star
42

node-bunyan-adaptor

Maps the major Pino / Bunyan logging methods to custom methods
JavaScript
3
star
43

drupal-services-views

Drupal module making it possible to expose Views from Views.module through Service.module's API:s
3
star
44

webpage-svpt-nu

Sweden's first Twitter service. Tracks what swedes says on Twitter
HTML
3
star
45

node-metrics-statsmix

A Node.js reporter for the StatsMix service that's eg. available as a Heroku addon.
JavaScript
3
star
46

node-multiphpmd

A script for in parallell checking multiple specific files in PHPMD.
JavaScript
3
star
47

drupal-biurnal

Drupal module that dynamically colorizes themes
JavaScript
3
star
48

eslint-config

My personal ESLint config. Based on semistandard
JavaScript
3
star
49

drupal-domain-relationships

A personal development copy of the Domain Relationships module for Drupal
2
star
50

ghatemplates

My personal reusable GitHub Actions – both `ghat` ones and native ones
2
star
51

drupal-pathautosuffix

Adds the possibility of defining extra suffixes in the Pathauto module, like "edit", which extra aliases are generated for
2
star
52

eslint-config-jsdoc-ts

Subset of my personal ESLint config, focused on TypeScript-validated JSDoc
JavaScript
2
star
53

drupal-materialized-views

Mirror and perhaps development copy of the Materialized View API repo on Launchpad
2
star
54

node-fulfills

Checks whether or not an object fulfills a specified condition
JavaScript
2
star
55

semver-set

Set operations for semver.
JavaScript
2
star
56

drupal-ie6notify

Notifies users of Internet Explorer 6 and lower that they should upgrade their browser
2
star
57

drupal-tipafriend

2
star
58

drupal-viewssetsblocks

Makes it possible to get a block for every item in a set - eg for every term in a vocabulary
2
star
59

jquery-alterbyobject

A jQuery plugin for modifying your page in a way defined by an object that you can have received from eg. an ajax request.
JavaScript
2
star
60

node-tema

An asynchronous node.js theme layer that adds the notion of themes and sub-themes on top of your template rendering engine.
JavaScript
2
star
61

metadataparser-mf2

Extended Metadataparser with Microformats 2 support
JavaScript
2
star
62

drupal-fbconnect

Temporary fork of the Facebook Connect module for Drupal - used to work on some patches for it
2
star
63

activityspam-filter

Extracted from https://gitlab.com/evanp/activityspam
JavaScript
1
star
64

drupal-realname

Temporary fork of the Drupal module RealName for work on a hook patch
PHP
1
star
65

bb-anonymous-posting

Fork of bb-anonymous-postin
PHP
1
star
66

jquery-positionrelativeto

A small helper function for positioning elements, like dropdowns and tooltips, relative to another element
JavaScript
1
star
67

node-knex-migrator-extension

An extension of the knex migrator. Initial version based on knex 0.7, still works on knex 0.12
JavaScript
1
star
68

badges-cjs-esm

A selection of shield.io badges to indicate ones type of module
1
star
69

drupal-views-term-argument-default

1
star
70

drupal-sets

Drupal module for exposing different set-related functions - like iterating through them Token-style.
1
star
71

renovate-config

Personal preset package for Renovate
1
star
72

drupal-simplecommentlink

Drupal module that makes the comment link always look like "Comments 123"
1
star
73

drupal-environment

Personal fork of Environment to develop some new functions to contribute back.
PHP
1
star
74

.github

1
star
75

peowly-commands

Helper for handling commands with peowly
JavaScript
1
star
76

node-vptweestream-demo

An app to demo the vptweetstream node module
JavaScript
1
star
77

drupal-plain-views

The simplest Views display possible
PHP
1
star
78

drupal-views-taxonomy-index

A module providing a taxonomy index for the Views module for Drupal
1
star
79

version-guard

Used to ensure modern CLI scripts fail silently on old node.js versions
JavaScript
1
star
80

drupal-disabledefaultlogin

A Drupal module usable to disable the default login system of a site
PHP
1
star