Skip to content

Commit d822727

Browse files
author
Thom Seddon
committed
Fix dynamo coding style
1 parent 0511416 commit d822727

File tree

2 files changed

+105
-112
lines changed

2 files changed

+105
-112
lines changed

examples/dynamodb/dal.js

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,88 @@
11
//helper for dealing with DynamoDB's proprietary attribute requirements
22

33
var Dal = function () {
4-
this.AWS = require('aws-sdk');
5-
//load your aws credentials from a file
6-
this.AWS.config.loadFromPath(__dirname + '/aws.json');
7-
//change the endpoint to match your dynamodb endpoint
8-
this.db = new this.AWS.DynamoDB({
9-
endpoint: "https://dynamodb.us-east-1.amazonaws.com/"
10-
});
11-
}
4+
this.AWS = require('aws-sdk');
5+
//load your aws credentials from a file
6+
this.AWS.config.loadFromPath(__dirname + '/aws.json');
7+
//change the endpoint to match your dynamodb endpoint
8+
this.db = new this.AWS.DynamoDB({
9+
endpoint: "https://dynamodb.us-east-1.amazonaws.com/"
10+
});
11+
};
1212

1313
Dal.prototype = {
14-
decodeValue: function (map) {
15-
if (typeof map.N !== 'undefined')
16-
return parseFloat(map.N);
17-
if (typeof map.S !== 'undefined')
18-
return map.S;
19-
return map.B
20-
},
21-
decodeValues: function (obj, vals) {
22-
var self = this;
23-
for (var key in vals) {
24-
if (vals.hasOwnProperty(key)) {
25-
obj[key] = self.decodeValue(vals[key]);
26-
}
27-
}
28-
},
29-
formatAttributes: function (obj) {
30-
var item = {};
31-
for (var p in obj) {
32-
if (obj.hasOwnProperty(p)) {
33-
if (obj[p] === 0 || typeof obj[p] == "number") {
34-
item[p] = {"N": obj[p].toString()};
35-
}
36-
else {
37-
item[p] = {"S": obj[p]};
38-
}
39-
}
40-
}
41-
return item;
42-
},
43-
deleteEmptyProperties: function (obj) {
44-
//DynamoDB does not allow you to store empty values
45-
for (var p in obj) {
46-
if (!obj.hasOwnProperty(p))
47-
continue;
48-
if (obj[p] !== 0 && (obj[p] == null || obj[p] == "")) {
49-
delete(obj[p]);
50-
}
51-
}
52-
},
53-
doGet: function (tableName, keyHash, consistent, callback) {
54-
consistent = typeof consistent !== 'undefined' ? consistent : false;
55-
var self = this;
56-
var result = this.db.getItem({
57-
TableName: tableName,
58-
Key: keyHash,
59-
ConsistentRead: consistent
60-
}, function (err, data) {
61-
var obj = {};
62-
if (err != null) {
63-
if (callback) callback(err, null);
64-
return;
65-
}
66-
if (typeof data.Item != "undefined") {
67-
self.decodeValues(obj, data.Item);
68-
}
69-
if (callback) callback(err, obj);
70-
});
71-
},
72-
doSet: function (obj, tableName, keyHash, callback) {
73-
this.deleteEmptyProperties(obj);
74-
this.db.putItem({
75-
TableName: tableName,
76-
Item: this.formatAttributes(obj)
77-
}, function (err, data) {
14+
decodeValue: function (map) {
15+
if (typeof map.N !== 'undefined')
16+
return parseFloat(map.N);
17+
if (typeof map.S !== 'undefined')
18+
return map.S;
19+
return map.B;
20+
},
21+
decodeValues: function (obj, vals) {
22+
var self = this;
23+
for (var key in vals) {
24+
if (vals.hasOwnProperty(key)) {
25+
obj[key] = self.decodeValue(vals[key]);
26+
}
27+
}
28+
},
29+
formatAttributes: function (obj) {
30+
var item = {};
31+
for (var p in obj) {
32+
if (obj.hasOwnProperty(p)) {
33+
if (obj[p] === 0 || typeof obj[p] === "number") {
34+
item[p] = {"N": obj[p].toString()};
35+
}
36+
else {
37+
item[p] = {"S": obj[p]};
38+
}
39+
}
40+
}
41+
return item;
42+
},
43+
deleteEmptyProperties: function (obj) {
44+
//DynamoDB does not allow you to store empty values
45+
for (var p in obj) {
46+
if (!obj.hasOwnProperty(p))
47+
continue;
48+
if (obj[p] !== 0 && (obj[p] === null || obj[p] === "")) {
49+
delete(obj[p]);
50+
}
51+
}
52+
},
53+
doGet: function (tableName, keyHash, consistent, callback) {
54+
consistent = typeof consistent !== 'undefined' ? consistent : false;
55+
var self = this;
56+
var result = this.db.getItem({
57+
TableName: tableName,
58+
Key: keyHash,
59+
ConsistentRead: consistent
60+
}, function (err, data) {
61+
var obj = {};
62+
if (err !== null) {
63+
if (callback) callback(err, null);
64+
return;
65+
}
66+
if (typeof data.Item !== "undefined") {
67+
self.decodeValues(obj, data.Item);
68+
}
69+
if (callback) callback(err, obj);
70+
});
71+
},
72+
doSet: function (obj, tableName, keyHash, callback) {
73+
this.deleteEmptyProperties(obj);
74+
this.db.putItem({
75+
TableName: tableName,
76+
Item: this.formatAttributes(obj)
77+
}, function (err, data) {
7878

79-
if (err != null) {
80-
callback(err, null);
81-
return;
82-
}
83-
callback(err, obj);
84-
});
85-
}
79+
if (err !== null) {
80+
callback(err, null);
81+
return;
82+
}
83+
callback(err, obj);
84+
});
85+
}
8686
};
8787

8888
module.exports = new Dal();
89-
90-

examples/dynamodb/model.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,43 @@ var OAuthUserTable = "oauth2user";
2525
// node-oauth2-server callbacks
2626
//
2727
model.getAccessToken = function (bearerToken, callback) {
28-
console.log('in getAccessToken (bearerToken: ' + bearerToken + ')');
28+
console.log('in getAccessToken (bearerToken: ' + bearerToken + ')');
2929

30-
dal.doGet(OAuthAccessTokenTable,
31-
{"access_token": {"S": bearerToken}}, true, callback);
30+
dal.doGet(OAuthAccessTokenTable, { access_token: { S: bearerToken }}, true, callback);
3231
};
3332

3433
model.getClient = function (clientId, clientSecret, callback) {
35-
console.log('in getClient (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ')');
36-
dal.doGet(OAuthClientTable,
37-
{"client_id": {"S": clientId}, "client_secret": {"S": clientSecret}},
38-
true, callback);
34+
console.log('in getClient (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ')');
35+
36+
dal.doGet(OAuthClientTable, { client_id: { S: clientId }, client_secret: { S: clientSecret }},
37+
true, callback);
3938
};
4039

4140
// This will very much depend on your setup, I wouldn't advise doing anything exactly like this but
4241
// it gives an example of how to use the method to restrict certain grant types
4342
var authorizedClientIds = ['abc1', 'def2'];
4443
model.grantTypeAllowed = function (clientId, grantType, callback) {
45-
console.log('in grantTypeAllowed (clientId: ' + clientId + ', grantType: ' + grantType + ')');
44+
console.log('in grantTypeAllowed (clientId: ' + clientId + ', grantType: ' + grantType + ')');
4645

47-
if (grantType === 'password') {
48-
callback(false, authorizedClientIds.indexOf(clientId) >= 0);
49-
return;
50-
}
46+
if (grantType === 'password') {
47+
return callback(false, authorizedClientIds.indexOf(clientId) >= 0);
48+
}
5149

52-
callback(false, true);
50+
callback(false, true);
5351
};
5452

5553
model.saveAccessToken = function (accessToken, clientId, userId, expires, callback) {
56-
console.log('in saveAccessToken (accessToken: ' + accessToken + ', clientId: ' + clientId + ', userId: ' + userId + ', expires: ' + expires + ')');
54+
console.log('in saveAccessToken (accessToken: ' + accessToken + ', clientId: ' + clientId +
55+
', userId: ' + userId + ', expires: ' + expires + ')');
5756

58-
var token = {};
59-
token.access_token = accessToken;
60-
token.client_id = clientId;
61-
token.user_id = userId;
62-
token.expires = expires.getTime()/1000; // store as a unix timestamp
57+
var token = {
58+
access_token: accessToken,
59+
client_id: clientId,
60+
user_id: userId,
61+
expires: parseInt(expires / 1000, 10) // store as a unix timestamp
62+
};
6363

64-
dal.doSet(token, OAuthAccessTokenTable, {"access_token": {"S": accessToken}}, callback);
64+
dal.doSet(token, OAuthAccessTokenTable, { access_token: { S: accessToken }}, callback);
6565
};
6666

6767
/*
@@ -70,15 +70,10 @@ model.saveAccessToken = function (accessToken, clientId, userId, expires, callba
7070
//This will probably just be a hook into your existing user database but
7171
//must return an object with a numeric or string `id` property
7272
model.getUser = function (username, password, callback) {
73-
console.log('in getUser (username: ' + username + ', password: ' + password + ')');
73+
console.log('in getUser (username: ' + username + ', password: ' + password + ')');
7474

75-
dal.doGet(OAuthUserTable,
76-
{"username": {"S": username}, "password": {"S": password}}, true, function(err, data) {
77-
if (err) {
78-
callback(err);
79-
return;
80-
}
81-
82-
callback({id: data.username});
83-
});
84-
};
75+
dal.doGet(OAuthUserTable, { username: { S: username }, password: { S: password }}, true,
76+
function(err, data) {
77+
callback(err, data && { id: data.username });
78+
});
79+
};

0 commit comments

Comments
 (0)