Skip to content

Commit e91ee95

Browse files
author
Sebastian Gellweiler
committed
Add more methods
1 parent 4805a4a commit e91ee95

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lib/dynamodb.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const _ = require('lodash');
2-
3-
const { DynamoDBDocument } = require("@aws-sdk/lib-dynamodb");
4-
const { DynamoDB } = require("@aws-sdk/client-dynamodb");
5-
2+
const AWS = require("aws-sdk");
63
const RSVP = require('rsvp');
74

85
function DynamodbAdapter(logger, opts) {
@@ -14,28 +11,51 @@ function DynamodbAdapter(logger, opts) {
1411
maxRetries: 'maxRetries' in opts ? opts.maxRetries : 4
1512
};
1613

17-
this.settings = {
14+
this.dynamodbsettings = {
1815
logger: logger,
1916
table: opts.table
2017
};
2118

22-
this.docClient = DynamoDBDocument.from(new DynamoDB(docClientOptions))
23-
24-
_.bindAll(this, 'get', 'set', 'del');
19+
this.docClient = new AWS.DynamoDB.DocumentClient(docClientOptions)
20+
21+
_.bindAll(
22+
this,
23+
"get",
24+
"set",
25+
"del",
26+
"isMemoryStore",
27+
"getAllClientInfos",
28+
"saveInstallation",
29+
);
2530
}
2631

2732
var proto = DynamodbAdapter.prototype;
2833

34+
proto.isMemoryStore = function () {
35+
return false;
36+
};
37+
38+
proto.getAllClientInfos = async function () {
39+
const self = this;
40+
return await self.get({ key: "clientInfo" });
41+
};
42+
43+
proto.saveInstallation = async function (val, clientKey) {
44+
await this.connectionPromise;
45+
const clientSetting = await this.set("clientInfo", val, clientKey);
46+
return clientSetting;
47+
};
48+
2949
// return a promise to a single object identified by 'key' in the data belonging to tenant 'clientKey'
3050
proto.get = async function (key, clientKey) {
3151
try {
3252
var data = await this.docClient.get({
33-
"TableName": this.settings.table,
53+
"TableName": this.dynamodbsettings.table,
3454
"Key": {
3555
"key": key,
3656
"clientKey": clientKey
3757
}
38-
});
58+
}).promise();
3959

4060
if ('Item' in data) {
4161
data = data.Item;
@@ -54,13 +74,13 @@ proto.get = async function (key, clientKey) {
5474
proto.set = async function (key, value, clientKey) {
5575
try {
5676
await this.docClient.put({
57-
"TableName": this.settings.table,
77+
"TableName": this.dynamodbsettings.table,
5878
"Item": {
5979
"key": key,
6080
"clientKey": clientKey,
6181
"val": value
6282
}
63-
});
83+
}).promise();
6484
return value;
6585
} catch (err) {
6686
throw new Error("atlassian-connect-express-dynamodb set failed: " + err.message);
@@ -72,24 +92,24 @@ proto._del_multiple = function (keys, clientKey) {
7292
for (var i = 0; i < keys.length; i++) {
7393
var key = keys[i];
7494
promisses.push(this.docClient.delete({
75-
"TableName": this.settings.table,
95+
"TableName": this.dynamodbsettings.table,
7696
"Key": {
7797
"clientKey": clientKey,
7898
"key": key
7999
}
80-
}));
100+
}).promise());
81101
}
82102
return Promise.all(promisses);
83103
};
84104

85105
proto._query_by_client_key = function (clientKey) {
86106
return this.docClient.query({
87-
TableName: this.settings.table,
107+
TableName: this.dynamodbsettings.table,
88108
KeyConditionExpression: "clientKey = :clientKey",
89109
ExpressionAttributeValues: {
90110
":clientKey": clientKey
91111
}
92-
});
112+
}).promise();
93113
};
94114

95115
proto.del = async function (key, clientKey) {
@@ -117,4 +137,4 @@ module.exports = function (logger, opts) {
117137
return DynamodbAdapter;
118138
}
119139
return new DynamodbAdapter(logger, opts);
120-
};
140+
};

0 commit comments

Comments
 (0)