diff --git a/examples/postgresql/model.js b/examples/postgresql/model.js index 7e0ebc5..154affd 100644 --- a/examples/postgresql/model.js +++ b/examples/postgresql/model.js @@ -3,22 +3,24 @@ * Module dependencies. */ -var pg = require('pg-promise')(process.env.DATABASE_URL); +var pgp = require('pg-promise')({ + // initialization options +}); + +var db = pgp(process.env.DATABASE_URL); /* * Get access token. */ module.exports.getAccessToken = function *(bearerToken) { - var result = yield pg.query('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE access_token = $1', [bearerToken]); - var token = result.rows[0]; - - return { - accessToken: token.access_token, - clientId: token.client_id, - expires: token.expires, - userId: token.userId - }; + var token = yield db.one('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE access_token = $1', [bearerToken]); + return { + accessToken: token.access_token, + clientId: token.client_id, + expires: token.expires, + userId: token.userId + }; }; /** @@ -26,17 +28,16 @@ module.exports.getAccessToken = function *(bearerToken) { */ module.exports.getClient = function *(clientId, clientSecret) { - var result = yield pg.query('SELECT client_id, client_secret, redirect_uri FROM oauth_clients WHERE client_id = $1 AND client_secret = $2', [clientId, clientSecret]); - var oAuthClient = result.rows[0]; + var oAuthClient = yield db.oneOrNone('SELECT client_id, client_secret, redirect_uri FROM oauth_clients WHERE client_id = $1 AND client_secret = $2', [clientId, clientSecret]); - if (!oAuthClient) { - return; - } + if (!oAuthClient) { + return; + } - return { - clientId: oAuthClient.client_id, - clientSecret: oAuthClient.client_secret - }; + return { + clientId: oAuthClient.client_id, + clientSecret: oAuthClient.client_secret + }; }; /** @@ -44,9 +45,8 @@ module.exports.getClient = function *(clientId, clientSecret) { */ module.exports.getRefreshToken = function *(bearerToken) { - var result = yield pg.query('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE refresh_token = $1', [bearerToken]); - - return result.rowCount ? result.rows[0] : false; + var tokens = yield db.any('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE refresh_token = $1', [bearerToken]); + return tokens.length?tokens[0]:false; }; /* @@ -54,9 +54,8 @@ module.exports.getRefreshToken = function *(bearerToken) { */ module.exports.getUser = function *(username, password) { - var result = yield pg.query('SELECT id FROM users WHERE username = $1 AND password = $2', [username, password]); - - return result.rowCount ? result.rows[0] : false; + var users = yield db.any('SELECT id FROM users WHERE username = $1 AND password = $2', [username, password]); + return users.length?users[0]:false; }; /** @@ -64,14 +63,15 @@ module.exports.getUser = function *(username, password) { */ module.exports.saveAccessToken = function *(token, client, user) { - var result = yield pg.query('INSERT INTO oauth_tokens(access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id) VALUES ($1, $2, $3, $4)', [ - token.accessToken, - token.accessTokenExpiresOn, - client.id, - token.refreshToken, - token.refreshTokenExpiresOn, - user.id - ]); - - return result.rowCount ? result.rows[0] : false; + yield pg.none('INSERT INTO oauth_tokens(access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id) VALUES ($1, $2, $3, $4)', [ + token.accessToken, + token.accessTokenExpiresOn, + client.id, + token.refreshToken, + token.refreshTokenExpiresOn, + user.id + ]); + + // none gets you no results, as you do not return anything. + //return result.rowCount ? result.rows[0] : false; };