Skip to content

Commit

Permalink
feat: Support for nested values in SpecialKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyuga-Tsukui committed Apr 17, 2024
1 parent 61f4618 commit d228e35
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/connect-dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = function (connect) {
this.initialized = true;
})
.catch((err) => {
if (err.name == 'ResourceNotFoundException') {
if (err.name == "ResourceNotFoundException") {
return this.createSessionsTable().then(() => {
this.initialized = true;
});
Expand Down Expand Up @@ -226,10 +226,11 @@ module.exports = function (connect) {

const missingKeys = [];
this.specialKeys.forEach((key) => {
if (typeof sess[key.name] !== "undefined") {
const value = getNestedValue(sess, key.name);
if (value != undefined) {
const item = {};
item[key.type] = sess[key.name];
params.Item[key.name] = item;
item[key.type] = value;
params.Item[key.name.replace(".", "_")] = item;
} else {
missingKeys.push(key.name);
}
Expand All @@ -250,6 +251,12 @@ module.exports = function (connect) {
});
};

function getNestedValue(sess, path) {
return path.split(".").reduce((current, key) => {
return current && current[key] !== undefined ? current[key] : undefined;
}, sess);
}

/**
* Cleans up expired sessions
*
Expand Down Expand Up @@ -346,7 +353,7 @@ module.exports = function (connect) {
const now = Math.floor(Date.now() / 1000);
const expires =
typeof sess.cookie.maxAge === "number"
? now + (sess.cookie.maxAge / 1000)
? now + sess.cookie.maxAge / 1000
: now + oneDayInSeconds;
return expires;
};
Expand Down
62 changes: 62 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
CreateTableCommand,
DeleteTableCommand,
ScalarAttributeType,
GetItemCommand,
} = require("@aws-sdk/client-dynamodb");
const ConnectDynamoDB = require(__dirname + "/../lib/connect-dynamodb.js");

Expand Down Expand Up @@ -41,6 +42,17 @@ describe("DynamoDBStore", () => {
const store = new DynamoDBStore({
client: client,
table: tableName,
specialKeys: [
{
name: "flat",
type: "S",
},
{
name: "nested.value",
type: "S",
},
],
skipThrowMissingSpecialKeys: true,
});
const sessionId = Math.random().toString();

Expand Down Expand Up @@ -328,6 +340,56 @@ describe("DynamoDBStore", () => {
}).timeout(5000);
});

describe("SpecialKeys", () => {
const name = Math.random().toString();

before((done) => {
store.set(
sessionId,
{
cookie: {
maxAge: 2000,
},
name,
flat: "flatValue",
nested: {
value: "nestedValue",
},
},
done
);
});

it("should get data correctly", async () => {
return new Promise((resolve, reject) => {
const input = {
TableName: tableName,
Key: {
["id"]: {
S: "sess:" + sessionId,
},
},
ConsistntRead: true,
};
const command = new GetItemCommand(input);
client
.send(command)
.then((response) => {
response["Item"]["nested_value"].should.eql({ S: "nestedValue" });
response["Item"]["flat"].should.eql({ S: "flatValue" });

resolve();
})
.catch((err) => reject(err));
});
});

it("does not crash on invalid session object", async () => {
const session = store.getParsedSession({ Item: {} });
should.not.exist(session);
}).timeout(4000);
});

after(async () => {
// await client.send(new DeleteTableCommand({ TableName: tableName }));
});
Expand Down

0 comments on commit d228e35

Please sign in to comment.