Skip to content

Made it possible to use JSON fields for data in the database #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ interface SequelizeStoreOptions {
extendDefaultFields?: (defaults: DefaultFields, session: any) => Data;
checkExpirationInterval?: number;
expiration?: number;
isDataFieldText?: boolean;
}

declare class SequelizeStore extends Store {
sync(): void
touch: (sid: string, data: any, callback?: (err: any) => void) => void
touch: (sid: string, data: any, callback?: (err: any) => void) => void;
stopExpiringSessions: () => void
}

Expand Down
29 changes: 20 additions & 9 deletions lib/connect-session-sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/

const Op = require('sequelize').Op || {}
const defaultModel = require('./model')
const modelText = require('./model')
const modelJson = require('./modelJson')
const debug = require('debug')('connect:session-sequelize')
const defaultOptions = {
checkExpirationInterval: 15 * 60 * 1000, // The interval at which to cleanup expired sessions.
expiration: 24 * 60 * 60 * 1000, // The maximum age (in milliseconds) of a valid session. Used when cookie.expires is not set.
disableTouch: false, // When true, we will not update the db in the touch function call. Useful when you want more control over db writes.
modelKey: 'Session',
tableName: 'Sessions'
tableName: 'Sessions',
isDataFieldText: true
}

function promisify (promise, fn) {
Expand Down Expand Up @@ -64,6 +66,7 @@ module.exports = function SequelizeSessionInit (Store) {
// No Table specified, default to ./model
debug('No table specified, using default table.')
const modelKey = options.modelKey || 'Session'
const defaultModel = options.isDataFieldText? modelText : modelJson;
this.sessionModel = options.db.define(modelKey, defaultModel, {
tableName: options.tableName || modelKey
})
Expand All @@ -79,25 +82,25 @@ module.exports = function SequelizeSessionInit (Store) {
return promisify(
this.sessionModel
.findOne({ where: { sid: sid } })
.then(function (session) {
.then((session) => {
if (!session) {
debug('Did not find session %s', sid)
return null
}
debug('FOUND %s with data %s', session.sid, session.data)

return JSON.parse(session.data)
return this.reverseConvertData(session.data)
}),
fn
)
}

set (sid, data, fn) {
debug('INSERT "%s"', sid)
const stringData = JSON.stringify(data)
const convertedData = this.convertData(data);
const expires = this.expiration(data)

let defaults = { data: stringData, expires: expires }
let defaults = { data: convertedData, expires: expires }
if (this.options.extendDefaultFields) {
defaults = this.options.extendDefaultFields(defaults, data)
}
Expand All @@ -109,7 +112,7 @@ module.exports = function SequelizeSessionInit (Store) {
defaults: defaults,
raw: false
})
.then(function sessionCreated ([session]) {
.then(([session]) => {
let changed = false
Object.keys(defaults).forEach(function (key) {
if (key === 'data') {
Expand All @@ -121,8 +124,8 @@ module.exports = function SequelizeSessionInit (Store) {
changed = true
}
})
if (session.data !== stringData) {
session.data = JSON.stringify(data)
if (session.data !== convertedData) {
session.data = this.convertData(data)
changed = true
}
if (changed) {
Expand Down Expand Up @@ -212,6 +215,14 @@ module.exports = function SequelizeSessionInit (Store) {
}
return new Date(Date.now() + this.options.expiration)
}

convertData(data) {
return this.options.isDataFieldText? JSON.stringify(data) : data;
}
reverseConvertData(data) {
return this.options.isDataFieldText? JSON.parse(data) : data;
}

}

return SequelizeStore
Expand Down
13 changes: 13 additions & 0 deletions lib/modelJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Session Model with JSON data field
*/
const DataTypes = require('sequelize').DataTypes

module.exports = {
sid: {
type: DataTypes.STRING(36),
primaryKey: true
},
expires: DataTypes.DATE,
data: DataTypes.JSON
}