Skip to content
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

Express 4.x compatibility #7

Open
wants to merge 2 commits 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
24 changes: 22 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,27 @@ appropriate for your system.

2. Include the requirement for connect-pg.

`var PGStore = require('connect-pg');`
* With express4:

```
var session = require('express-session');
var PGStore = require('connect-pg')(session);
```

* With express<4:

```
var express = require('express');
var PGStore = require('connect-pg')(express);
```

* With connect:

```
var connect = require('connect');
var PGStore = require('connect-pg')(connect);
```


3. Setup the session software to use the connect-pg for storage.

Expand Down Expand Up @@ -146,4 +166,4 @@ features that have not been installed yet.
## LICENSE

This software is using the [MIT](./connect-pg/blob/master/LICENSE) to match
the connect license.
the connect license.
190 changes: 99 additions & 91 deletions lib/connect-pg.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,109 @@
/*
* PGStore - Connect storage in a PostgreSQL database.
*/
var Store = require('connect').session.Store;
module.exports = function (connect) {
if (!connect) {
// For backwards compatibility
connect = require('connect');
}
var Store = connect.Store || connect.session.Store;

var PGStore = module.exports = function PGStore(clientFn) {
if (typeof clientFn != 'function') {
throw TypeError;
}
this.getClient = clientFn;
};
function PGStore (clientFn) {
if (typeof clientFn != 'function') {
throw TypeError;
}
this.getClient = clientFn;
}

PGStore.prototype = new Store();
PGStore.prototype = new Store();

PGStore.prototype.set = function (sid, sessData, callback) {
this.getClient(function (client) {
var expiration = null;
if (sessData.cookie) {
if (sessData.cookie.expires) {
expiration = sessData.cookie.expires;
}
}
client.query('select web.set_session_data($1, $2, $3)',
[sid, JSON.stringify(sessData), expiration],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};
PGStore.prototype.set = function (sid, sessData, callback) {
this.getClient(function (client) {
var expiration = null;
if (sessData.cookie) {
if (sessData.cookie.expires) {
expiration = sessData.cookie.expires;
}
}
client.query('select web.set_session_data($1, $2, $3)',
[sid, JSON.stringify(sessData), expiration],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};

PGStore.prototype.get = function (sid, callback) {
this.getClient(function (client) {
client.query('select web.get_session_data($1)',
[sid],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
if (result.rows.length) {
callback(null, JSON.parse(result.rows[0].get_session_data));
} else {
callback(null, null);
}
}
}
);
});
};
PGStore.prototype.get = function (sid, callback) {
this.getClient(function (client) {
client.query('select web.get_session_data($1)',
[sid],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
if (result.rows.length) {
callback(null, JSON.parse(result.rows[0].get_session_data));
} else {
callback(null, null);
}
}
}
);
});
};

PGStore.prototype.destroy = function (sid, callback) {
this.getClient(function (client) {
client.query('select web.destroy_session($1)',
[sid],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};
PGStore.prototype.destroy = function (sid, callback) {
this.getClient(function (client) {
client.query('select web.destroy_session($1)',
[sid],
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};

PGStore.prototype.length = function (callback) {
this.getClient(function (client) {
client.query('select web.count_sessions()',
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback(null, result.rows[0].count_sessions);
}
}
);
});
};
PGStore.prototype.length = function (callback) {
this.getClient(function (client) {
client.query('select web.count_sessions()',
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback(null, result.rows[0].count_sessions);
}
}
);
});
};

PGStore.prototype.clear = function (callback) {
this.getClient(function (client) {
client.query('select web.clear_sessions()',
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};

PGStore.prototype.clear = function (callback) {
this.getClient(function (client) {
client.query('select web.clear_sessions()',
function (err, result) {
if (err) {
console.log(err.message);
}
if (result) {
callback && callback();
}
}
);
});
};
return PGStore;
};