Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including @google-cloud/connect-datastore with all npm packages installed. Try it out:

require("@google-cloud/datastore/package.json"); // @google-cloud/datastore is a peer dependency. require("express-session/package.json"); // express-session is a peer dependency. var connectDatastore = require("@google-cloud/connect-datastore")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@google-cloud/connect-datastore v5.0.1

Google Cloud Datastore session store for Express/Connect

Google Inc. logo

Google Cloud Datastore Sessions

NPM

@google-cloud/connect-datastore is a Google Cloud Datastore session store backed by @google-cloud/datastore.

Note: Cloud Datastore is a persistent, distributed, transactional database. Often, it's more appropriate to choose a different storage solution for sessions such as Memcache or Redis as their designs offer much faster operation in this use case.

Installation

npm install @google-cloud/connect-datastore

Configuration

You must have a Google Cloud project and credentials.

See gcloud node's documentation on setting up authentication.

Usage Example

const {Datastore} = require('@google-cloud/datastore');
const {DatastoreStore} = require('@google-cloud/connect-datastore');
const express = require('express');
const session = require('express-session');

const app = express();

app.use(session({
  store: new DatastoreStore({
    kind: 'express-sessions',

    // Optional: expire the session after this many milliseconds.
    // note: datastore does not automatically delete all expired sessions
    // you may want to run separate cleanup requests to remove expired sessions
    // 0 means do not expire
    expirationMs: 0,

    dataset: new Datastore({

      // For convenience, @google-cloud/datastore automatically looks for the
      // GCLOUD_PROJECT environment variable. Or you can explicitly pass in a
      // project ID here:
      projectId: process.env.GCLOUD_PROJECT,

      // For convenience, @google-cloud/datastore automatically looks for the
      // GOOGLE_APPLICATION_CREDENTIALS environment variable. Or you can
      // explicitly pass in that path to your key file here:
      keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS
    })
  }),
  secret: 'my-secret'
}));

Expiration

If a session is fetched with the delta between the createdAt time and current time greater than expirationMs, the session will not be returned and will instead be destroyed.

Datastore does not support a ttl, and tokens are only deleted if a session is fetched. You will likely want to implement logic to occasionally delete expired sessions.

Contributing

License

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free