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.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# THIS REPOSITORY AND PACKAGE WILL BE DEPRECATED IN JULY 2024\n\nVersion latestkeyboard_arrow_down\n\n- [7.0.1 (latest)](/nodejs/docs/reference/connect-datastore/latest)\n- [7.0.0](/nodejs/docs/reference/connect-datastore/7.0.0)\n- [6.0.1](/nodejs/docs/reference/connect-datastore/6.0.1)\n- [5.0.3](/nodejs/docs/reference/connect-datastore/5.0.3) \n\nGoogle Cloud Datastore Sessions\n===============================\n\n[](https://www.npmjs.org/package/@google-cloud/connect-datastore)\n\n**@google-cloud/connect-datastore** is a [Google Cloud Datastore](https://cloud.google.com/datastore/docs)\nsession store backed by [@google-cloud/datastore](https://www.npmjs.com/package/@google-cloud/datastore).\n\n**Note:** Cloud Datastore is a persistent, distributed, transactional database.\nOften, it's more appropriate to choose a different storage solution for sessions\nsuch as Memcache or Redis as their designs offer much faster operation in this\nuse case.\n\nInstallation\n------------\n\n npm install @google-cloud/connect-datastore\n\nConfiguration\n-------------\n\nYou must have a Google Cloud project and credentials.\n\nSee [gcloud node's documentation](https://cloud.google.com/docs/authentication/getting-started) on setting up authentication.\n\nUsage Example\n-------------\n\n const {Datastore} = require('https://cloud.google.com/nodejs/docs/reference/datastore/latest/overview.html');\n const {DatastoreStore} = require('https://cloud.google.com/nodejs/docs/reference/connect-datastore/latest/overview.html');\n const express = require('express');\n const session = require('express-session');\n\n const app = express();\n\n app.use(session({\n store: new https://cloud.google.com/nodejs/docs/reference/connect-datastore/latest/connect-datastore/datastorestore.html({\n kind: 'express-sessions',\n\n // Optional: expire the session after this many milliseconds.\n // note: datastore does not automatically delete all expired sessions\n // you may want to run separate cleanup requests to remove expired sessions\n // 0 means do not expire\n expirationMs: 0,\n\n dataset: new Datastore({\n\n // For convenience, @google-cloud/datastore automatically looks for the\n // GCLOUD_PROJECT environment variable. Or you can explicitly pass in a\n // project ID here:\n projectId: process.env.GCLOUD_PROJECT,\n\n // For convenience, @google-cloud/datastore automatically looks for the\n // GOOGLE_APPLICATION_CREDENTIALS environment variable. Or you can\n // explicitly pass in that path to your key file here:\n keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS\n })\n }),\n secret: 'my-secret'\n }));\n\nExpiration\n----------\n\nIf a session is fetched with the delta between the createdAt time and current\ntime greater than expirationMs, the session will not be returned and will\ninstead be destroyed.\n\nDatastore does not support a `ttl`, and tokens are only deleted if a session\nis fetched. You will likely want to implement logic to occasionally delete\nexpired sessions.\n\nContributing\n------------\n\n- See [CONTRIBUTING.md](https://github.com/googleapis/nodejs-datastore-session/blob/master/CONTRIBUTING.md)\n\nLicense\n-------\n\n- Apache 2.0 - See [LICENSE](https://github.com/googleapis/nodejs-datastore-session/blob/master/LICENSE)"]]