Skip to content

Commit 3e8920d

Browse files
committed
feat: Support for nested values in SpecialKey
1 parent 61f4618 commit 3e8920d

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

lib/connect-dynamodb.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = function (connect) {
9494
this.initialized = true;
9595
})
9696
.catch((err) => {
97-
if (err.name == 'ResourceNotFoundException') {
97+
if (err.name == "ResourceNotFoundException") {
9898
return this.createSessionsTable().then(() => {
9999
this.initialized = true;
100100
});
@@ -226,10 +226,11 @@ module.exports = function (connect) {
226226

227227
const missingKeys = [];
228228
this.specialKeys.forEach((key) => {
229-
if (typeof sess[key.name] !== "undefined") {
229+
const value = getNestedValue(sess, key.name);
230+
if (value != undefined) {
230231
const item = {};
231-
item[key.type] = sess[key.name];
232-
params.Item[key.name] = item;
232+
item[key.type] = value;
233+
params.Item[toCamelCase(key.name)] = item;
233234
} else {
234235
missingKeys.push(key.name);
235236
}
@@ -250,6 +251,24 @@ module.exports = function (connect) {
250251
});
251252
};
252253

254+
function toCamelCase(name) {
255+
const parts = name.split(".");
256+
return parts
257+
.map((part, idx) => {
258+
if (idx === 0) {
259+
return part;
260+
}
261+
return part.charAt(0).toUpperCase() + part.slice(1);
262+
})
263+
.join("");
264+
}
265+
266+
function getNestedValue(sess, path) {
267+
return path.split(".").reduce((current, key) => {
268+
return current && current[key] !== undefined ? current[key] : undefined;
269+
}, sess);
270+
}
271+
253272
/**
254273
* Cleans up expired sessions
255274
*
@@ -346,7 +365,7 @@ module.exports = function (connect) {
346365
const now = Math.floor(Date.now() / 1000);
347366
const expires =
348367
typeof sess.cookie.maxAge === "number"
349-
? now + (sess.cookie.maxAge / 1000)
368+
? now + sess.cookie.maxAge / 1000
350369
: now + oneDayInSeconds;
351370
return expires;
352371
};

test/test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
CreateTableCommand,
77
DeleteTableCommand,
88
ScalarAttributeType,
9+
GetItemCommand,
910
} = require("@aws-sdk/client-dynamodb");
1011
const ConnectDynamoDB = require(__dirname + "/../lib/connect-dynamodb.js");
1112

@@ -41,6 +42,17 @@ describe("DynamoDBStore", () => {
4142
const store = new DynamoDBStore({
4243
client: client,
4344
table: tableName,
45+
specialKeys: [
46+
{
47+
name: "flat",
48+
type: "S",
49+
},
50+
{
51+
name: "nested.value",
52+
type: "S",
53+
},
54+
],
55+
skipThrowMissingSpecialKeys: true,
4456
});
4557
const sessionId = Math.random().toString();
4658

@@ -328,6 +340,56 @@ describe("DynamoDBStore", () => {
328340
}).timeout(5000);
329341
});
330342

343+
describe("SpecialKeys", () => {
344+
const name = Math.random().toString();
345+
346+
before((done) => {
347+
store.set(
348+
sessionId,
349+
{
350+
cookie: {
351+
maxAge: 2000,
352+
},
353+
name,
354+
flat: "flatValue",
355+
nested: {
356+
value: "nestedValue",
357+
},
358+
},
359+
done
360+
);
361+
});
362+
363+
it("should get data correctly", async () => {
364+
return new Promise((resolve, reject) => {
365+
const input = {
366+
TableName: tableName,
367+
Key: {
368+
["id"]: {
369+
S: "sess:" + sessionId,
370+
},
371+
},
372+
ConsistntRead: true,
373+
};
374+
const command = new GetItemCommand(input);
375+
client
376+
.send(command)
377+
.then((response) => {
378+
response["Item"]["nestedValue"].should.eql({ S: "nestedValue" });
379+
response["Item"]["flat"].should.eql({ S: "flatValue" });
380+
381+
resolve();
382+
})
383+
.catch((err) => reject(err));
384+
});
385+
});
386+
387+
it("does not crash on invalid session object", async () => {
388+
const session = store.getParsedSession({ Item: {} });
389+
should.not.exist(session);
390+
}).timeout(4000);
391+
});
392+
331393
after(async () => {
332394
// await client.send(new DeleteTableCommand({ TableName: tableName }));
333395
});

0 commit comments

Comments
 (0)