@fanoutio/apollo-server-lambda-grip
Version:
Apollo Server that runs on AWS Lambda API Gateway and GRIP to pump subscriptions
101 lines • 3.99 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import AWS from 'aws-sdk';
export function isDynamoDbOptions(options) {
return options != null && 'tableName' in options;
}
export class DynamoDbPersistence {
constructor(options) {
this.options = options;
// Create DynamoDB document client
this.awsDynamoDbDocClient = new AWS.DynamoDB.DocumentClient({
region: this.options.awsRegion,
accessKeyId: this.options.awsAccessKeyId,
secretAccessKey: this.options.awsSecretAccessKey,
apiVersion: '2012-08-10',
});
}
getPersistedMessages() {
return __awaiter(this, void 0, void 0, function* () {
const scanParams = {
TableName: this.options.tableName,
};
let scanResults;
try {
scanResults = yield this.awsDynamoDbDocClient.scan(scanParams).promise();
}
catch (ex) {
console.log("Could not scan", ex, scanParams);
scanResults = null;
}
const items = scanResults === null || scanResults === void 0 ? void 0 : scanResults.Items;
const persistedMessages = {};
if (items != null) {
for (const item of items) {
const wsId = item.wsId;
const messages = item.messages;
persistedMessages[wsId] = messages;
}
}
return persistedMessages;
});
}
persistMessage(wsId, message) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const getParams = {
TableName: this.options.tableName,
Key: {
'wsId': wsId,
},
};
let getResults;
try {
getResults = yield this.awsDynamoDbDocClient.get(getParams).promise();
}
catch (ex) {
console.log("Could not get row", ex, getParams);
getResults = null;
}
const messages = (_b = (_a = getResults === null || getResults === void 0 ? void 0 : getResults.Item) === null || _a === void 0 ? void 0 : _a.messages) !== null && _b !== void 0 ? _b : [];
messages.push(message);
const updateParams = {
TableName: this.options.tableName,
Item: {
'wsId': wsId,
'messages': messages,
},
};
try {
yield this.awsDynamoDbDocClient.put(updateParams).promise();
}
catch (ex) {
console.log("Could not put", ex, updateParams);
}
});
}
purgePersistedMessage(wsId) {
return __awaiter(this, void 0, void 0, function* () {
const purgeParams = {
TableName: this.options.tableName,
Key: {
'wsId': wsId,
},
};
try {
yield this.awsDynamoDbDocClient.delete(purgeParams).promise();
}
catch (ex) {
console.log("Error deleting row", ex, purgeParams);
}
});
}
}
//# sourceMappingURL=persistence.js.map