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

feat: Support for nested values in SpecialKey #96

Open
wants to merge 1 commit 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
29 changes: 24 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[toCamelCase(key.name)] = item;
} else {
missingKeys.push(key.name);
}
Expand All @@ -250,6 +251,24 @@ module.exports = function (connect) {
});
};

function toCamelCase(name) {
const parts = name.split(".");
return parts
.map((part, idx) => {
if (idx === 0) {
return part;
}
return part.charAt(0).toUpperCase() + part.slice(1);
})
.join("");
}

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 +365,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"]["nestedValue"].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