1.0.0 • Published 22 days ago

@omegion1npm/recusandae-fuga-aliquid v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
22 days ago

connect-mongodb-session

MongoDB-backed session storage for connect and Express. Meant to be a well-maintained and fully-featured replacement for modules like connect-mongo

Build Status Coverage Status

MongoDBStore

This module exports a single function which takes an instance of connect (or Express) and returns a MongoDBStore class that can be used to store sessions in MongoDB.

It can store sessions for Express 4

If you pass in an instance of the express-session module the MongoDBStore class will enable you to store your Express sessions in MongoDB.

The MongoDBStore class has 3 required options:

  1. uri: a MongoDB connection string
  2. databaseName: the MongoDB database to store sessions in
  3. collection: the MongoDB collection to store sessions in

Note: You can pass a callback to the MongoDBStore constructor, but this is entirely optional. The Express 3.x example demonstrates that you can use the MongoDBStore class in a synchronous-like style: the module will manage the internal connection state for you.

var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);

var app = express();
var store = new MongoDBStore({
  uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
  collection: 'mySessions'
});

// Catch errors
store.on('error', function(error) {
  console.log(error);
});

app.use(require('express-session')({
  secret: 'This is a secret',
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
  },
  store: store,
  // Boilerplate options, see:
  // * https://www.npmjs.com/package/express-session#resave
  // * https://www.npmjs.com/package/express-session#saveuninitialized
  resave: true,
  saveUninitialized: true
}));

app.get('/', function(req, res) {
  res.send('Hello ' + JSON.stringify(req.session));
});

server = app.listen(3000);

It throws an error when it can't connect to MongoDB

You should pass a callback to the MongoDBStore constructor to catch errors. If you don't pass a callback to the MongoDBStore constructor, MongoDBStore will throw if it can't connect.

var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);

var app = express();
var store = new MongoDBStore(
  {
    uri: 'mongodb://bad.host:27000/connect_mongodb_session_test?connectTimeoutMS=10',
    databaseName: 'connect_mongodb_session_test',
    collection: 'mySessions'
  },
  function(error) {
    // Should have gotten an error
  });

store.on('error', function(error) {
  // Also get an error here
});

app.use(session({
  secret: 'This is a secret',
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
  },
  store: store,
  // Boilerplate options, see:
  // * https://www.npmjs.com/package/express-session#resave
  // * https://www.npmjs.com/package/express-session#saveuninitialized
  resave: true,
  saveUninitialized: true
}));

app.get('/', function(req, res) {
  res.send('Hello ' + JSON.stringify(req.session));
});

server = app.listen(3000);

It supports several other options

There are several other options you can pass to new MongoDBStore():

var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);

var store = new MongoDBStore({
  uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
  collection: 'mySessions',

  // By default, sessions expire after 2 weeks. The `expires` option lets
  // you overwrite that by setting the expiration in milliseconds
  expires: 1000 * 60 * 60 * 24 * 30, // 30 days in milliseconds

  // Lets you set options passed to `MongoClient.connect()`. Useful for
  // configuring connectivity or working around deprecation warnings.
  connectionOptions: {
    serverSelectionTimeoutMS: 10000
  }
});

Azure Cosmos MongoDB support

It can support MongoDB instances inside Azure Cosmos. As Cosmos can only support time-based index on fields called _ts, you will need to update your configuration. Unlike in MongoDB, Cosmos starts the timer at the point of document creation so the expiresAfterSeconds should have the same value as expires - as expires is in milliseconds, the expiresAfterSeconds must equal expires / 1000.

var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);

var store = new MongoDBStore({
  uri: 'mongodb://username:password@cosmosdb-name.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@cosmosdb-name@', 
  databaseName: 'myDb',
  collection: 'mySessions',

  // Change the expires key name
  expiresKey: `_ts`,
  // This controls the life of the document - set to same value as expires / 1000
  expiresAfterSeconds: 60 * 60 * 24 * 14 
});
censoroncermdirjsonduplexfastcopymkdirpdependenciesStreamsconfigurabledeepcopyloggerfunctionpropertyoptionbeanstalk@@toStringTagjsdiffelectronvaluesortedbrowserlistamazonless csssyntaxhooksauthenticationpromiseprefixhas-ownrateObject.keysnamemapcodespostcsshasOwnPropertyquoteArrayBuffer#sliceECMAScript 6core-jstddES2016fast-deep-copyMapcirculardynamodbtypesafeES2021equalcss-in-jsdefinegdprgetOwnPropertyDescriptorefficientstreamcallstreamsautoscalingwhichtoobjectRegExp#flagsoutputsliceargumentregexpregexmobilelook-updeletetrimutilitiesl10nprivate datawatchreact-testing-libraryavachannelbluebirdeslint-pluginfunctionalsettercloudformationflagECMAScript 2015Rxbootstrap cssReactiveXECMAScript 2020YAMLclicallboundformscomparetoolkitdateUint16Arrayyupfile systemObject.entriesTypeBoxrestflagsReactiveExtensionsfind-uphelperswidthcryptrm -fremittoArrayredactliveString.prototype.matchAllslotflatMapinstallerES2019endpointenvtaskmime-dbfastcloneopenstreams2phonepyyamlES2015settingsAsyncIteratorlanguagepasswordemojipipejsonpathprunereal-timerestfuli18nparentscloudsearchdom-testing-libraryESnexttoSortedes-abstractidlevalidatemkdircallbindes2016internalless.jssigintextendprettyguidmetadatacloudwatchES2022windowsJSON-SchemabcryptES2017iteratorsearchqueryObject.fromEntriesformatwalkinputwafurlgetintrinsicwatchFileconfigxtermdayjsReflect.getPrototypeOfimmer__proto__ECMAScript 2018variables in cssimmutabletextObservablessorttermrfc4122package managerhandlersfetchpackageimportexportserializeeast-asian-widthstable-0CSSStyleDeclarationratelimitshamgraphqlmatchesworkspace:*ebsFunction.prototype.namemacosbyteOffsetArrayBuffer.prototype.slicecss nestingpreprocessorinternal slotframeworkarktypelinewrapstylepredictabletouchboundstringifierreactInt16ArrayagentloadbalancingnodetesterclassnameindicatorFloat64ArrayesgenericswarningbuffersmimemiddlewarevalidUint8ClampedArraysignalendercharacterspersistentglacierestreepolyfillrapidreducerlibphonenumberlinkassertWebSocketwhatwgspinnervisualstructuredCloneparsingtypedarraysclienttypees-shimsbootstrap lessreduceansiless compilerfilterECMAScript 2019forEachroute53ES2023vpcpushconcatMapdirectoryhashecmascriptESmonorepoprogressnested csswatcherURLSearchParamsTypedArrayArray.prototype.findLastmkdirsECMAScript 2023awspackagesdropes8rm -rfshellWebSocketsinfullstringifypatchtslibIteratorzerostoragegatewayhotjsprototsclassesspecless mixinsmodulesArray.prototype.flatfullwidthmapreducees-shim APItestingdomttyieelasticachees6ECMAScript 2017extensionelbreact-hook-formsideenvironmentredux-toolkitoperating-systemfpsargsjsdomBigInt64Arraypackage.jsonstylingexpressionrangeerrorutil.inspectsymbolencryptionspeedexit-codebrowserslistcolumneslintconfigcompilersharedglobalsasciia11ygradients cssautoprefixercontainstraversecolourmimetypesjavascriptajves5enumerableObject.valuesassignlesscssaccessibilitycacheflatvaluesES2018limitintrinsiceveryjesttypedarrayobjectdependency managerreact-hookscall-bindbusywrapnamesperformantwordbreaklockfileterminaltapserializationidentifierspreserve-symlinksdeepjshintnodejsObject.definePropertyarraybufferfromstylesfindglobJSONgroupByreplayObjectglobalsequencebuffercollection.es6Int32ArrayserializerECMAScript 5numberform-validationECMAScript 3inspectstylesheetpropertiesfasttrimEndescapesetPrototypeOfTypeScripttypesuuidreadcommand-linesuperagent$.extendchromiumeslintpluginexitshrinkwrapsetperformanceObservablelimitedargvfilevestdirprotocol-bufferscryptoeventEmitteroffsetdotenvvariablescollectionsetImmediatemodulepropiamstarterRFC-6455waitbyteLengthlengthformattingStyleSheetreadableSymbol.toStringTaghttpMicrosoftgradients css3joirequirelinuxcurlwebisarray_.extendownObject.isuninstallArrayjwtsharedarraybuffersymlinksbreakviewcss variabletypeerrorprotobufgroupsnsisConcatSpreadabletc39styled-componentscomputed-typesgetterstringInt8ArrayarttypescriptStreamwritablemochaUnderscoreprivatefseventsRegExp.prototype.flagsrmcolumnsastcall-boundinvarianthasOwndebugerrorswftyped arrayirqsymbolsexeccopyWeakMap0WeakSetjasminetrimRightnegative zerosuperstructmoveminimalyamlchromefunctionstimePushaccessoriteratebannerruntimeentriesconnectObject.assignajaxdeepcloneparseweakmapomitunicodereduxkinesismatchString.prototype.trimxhrwritefast-copystatuschaiPromiseArray.prototype.flatMapappqscloneshimes2018importregularcompile lessdefinePropertypromisesassertscssreusefstestconcurrencyweaksetparserrouterkarmamatchAllqueueauthbrowserbundlermruFloat32ArrayArray.prototype.findLastIndexparentpnpm9mergeelmdatavieweventDispatcherES2020es2017scheme-validationArray.prototype.filterchinesecorsfigletbyteresolve256classnamesequalityconsume.envchecksqsio-tscharacterextraregular expressiondescriptorsconsoleArray.prototype.includesargparsedescriptorstatelesstypeofwgetnativeremovefindupdescriptionlistenersprocesspathUint8ArrayworkflowidArray.prototype.flattenincludesspinnersclass-validatorfixed-widthsomecallbacktostringtagCSSdeterministicformexpressquerystrings3trimStartiterationsignalsfindLastIndexlazyawaitec2proxycolorsinatratypanionUint32ArrayECMAScript 2021utilsymlinkcommandHyBilintdebuggerES6tapegetPrototypeOfSymbolvarshttpstoolsmakemake dircode pointsmixinscoercibleRxJSfolderdeep-clonerequestprototypepicomatch[[Prototype]]ponyfillhardlinkstakestyleguideURLwordwraphookformkoreansyntaxerrores7BigUint64ArraycreateSetjapanesewatchingregular expressionsnpmhigher-orderrdsloadingupArrayBuffereslintvalidationinterruptslogfast-deep-cloneES3cloudfrontES5bddstatedataViewawesomesaucebindworkerECMAScript 2022environmentsdatastructuregetopttrimLeftcommanderhasinferenceECMAScript 7schemakeysseslesstypedeventsfastifysameValueZeroes2015arrayscolorsjsxutilityES7apischemeemrlrudiffpositivenopekeyfindLastcss lessmulti-packageairbnbthroatobjbatchshebangapollooptimistECMAScript 2016fast-clonetelephonepluginES8recursive
1.0.0

22 days ago