2021-03-24 19:21:23 +01:00
|
|
|
let { merge } = require("lodash")
|
2020-10-28 21:35:06 +01:00
|
|
|
let env = require("../environment")
|
2020-10-06 20:13:41 +02:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
const AWS_REGION = env.AWS_REGION ? env.AWS_REGION : "eu-west-1"
|
2021-04-19 23:36:14 +02:00
|
|
|
exports.AWS_REGION = AWS_REGION
|
2020-10-08 18:34:41 +02:00
|
|
|
|
2020-10-06 20:13:41 +02:00
|
|
|
const TableInfo = {
|
|
|
|
API_KEYS: {
|
|
|
|
name: "beta-api-key-table",
|
|
|
|
primary: "pk",
|
|
|
|
},
|
|
|
|
USERS: {
|
|
|
|
name: "prod-budi-table",
|
|
|
|
primary: "pk",
|
|
|
|
sort: "sk",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let docClient = null
|
|
|
|
|
|
|
|
class Table {
|
|
|
|
constructor(tableInfo) {
|
|
|
|
if (!tableInfo.name || !tableInfo.primary) {
|
|
|
|
throw "Table info must specify a name and a primary key"
|
|
|
|
}
|
|
|
|
this._name = tableInfo.name
|
|
|
|
this._primary = tableInfo.primary
|
|
|
|
this._sort = tableInfo.sort
|
|
|
|
}
|
|
|
|
|
|
|
|
async get({ primary, sort, otherProps }) {
|
|
|
|
let params = {
|
|
|
|
TableName: this._name,
|
|
|
|
Key: {
|
|
|
|
[this._primary]: primary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if (this._sort && sort) {
|
|
|
|
params.Key[this._sort] = sort
|
|
|
|
}
|
|
|
|
if (otherProps) {
|
2021-03-24 19:21:23 +01:00
|
|
|
params = merge(params, otherProps)
|
2020-10-06 20:13:41 +02:00
|
|
|
}
|
|
|
|
let response = await docClient.get(params).promise()
|
|
|
|
return response.Item
|
|
|
|
}
|
|
|
|
|
|
|
|
async update({
|
|
|
|
primary,
|
|
|
|
sort,
|
|
|
|
expression,
|
|
|
|
condition,
|
|
|
|
names,
|
|
|
|
values,
|
2020-10-08 18:34:41 +02:00
|
|
|
exists,
|
2020-10-06 20:13:41 +02:00
|
|
|
otherProps,
|
|
|
|
}) {
|
|
|
|
let params = {
|
|
|
|
TableName: this._name,
|
|
|
|
Key: {
|
|
|
|
[this._primary]: primary,
|
|
|
|
},
|
|
|
|
ExpressionAttributeNames: names,
|
|
|
|
ExpressionAttributeValues: values,
|
|
|
|
UpdateExpression: expression,
|
|
|
|
}
|
|
|
|
if (condition) {
|
|
|
|
params.ConditionExpression = condition
|
|
|
|
}
|
|
|
|
if (this._sort && sort) {
|
|
|
|
params.Key[this._sort] = sort
|
|
|
|
}
|
2020-10-08 18:34:41 +02:00
|
|
|
if (exists) {
|
|
|
|
params.ExpressionAttributeNames["#PRIMARY"] = this._primary
|
|
|
|
if (params.ConditionExpression) {
|
|
|
|
params.ConditionExpression += " AND "
|
|
|
|
}
|
|
|
|
params.ConditionExpression += "attribute_exists(#PRIMARY)"
|
|
|
|
}
|
2020-10-06 20:13:41 +02:00
|
|
|
if (otherProps) {
|
2021-03-24 19:21:23 +01:00
|
|
|
params = merge(params, otherProps)
|
2020-10-06 20:13:41 +02:00
|
|
|
}
|
|
|
|
return docClient.update(params).promise()
|
|
|
|
}
|
|
|
|
|
|
|
|
async put({ item, otherProps }) {
|
|
|
|
if (
|
|
|
|
item[this._primary] == null ||
|
|
|
|
(this._sort && item[this._sort] == null)
|
|
|
|
) {
|
|
|
|
throw "Cannot put item without primary and sort key (if required)"
|
|
|
|
}
|
|
|
|
let params = {
|
|
|
|
TableName: this._name,
|
|
|
|
Item: item,
|
|
|
|
}
|
|
|
|
if (otherProps) {
|
2021-03-24 19:21:23 +01:00
|
|
|
params = merge(params, otherProps)
|
2020-10-06 20:13:41 +02:00
|
|
|
}
|
|
|
|
return docClient.put(params).promise()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.init = endpoint => {
|
2020-10-06 20:13:41 +02:00
|
|
|
let AWS = require("aws-sdk")
|
2020-10-08 18:34:41 +02:00
|
|
|
AWS.config.update({
|
|
|
|
region: AWS_REGION,
|
|
|
|
})
|
2020-10-06 20:13:41 +02:00
|
|
|
let docClientParams = {
|
|
|
|
correctClockSkew: true,
|
|
|
|
}
|
2020-10-08 18:34:41 +02:00
|
|
|
if (endpoint) {
|
|
|
|
docClientParams.endpoint = endpoint
|
2020-10-28 21:35:06 +01:00
|
|
|
} else if (env.DYNAMO_ENDPOINT) {
|
|
|
|
docClientParams.endpoint = env.DYNAMO_ENDPOINT
|
2020-10-06 20:13:41 +02:00
|
|
|
}
|
|
|
|
docClient = new AWS.DynamoDB.DocumentClient(docClientParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.apiKeyTable = new Table(TableInfo.API_KEYS)
|
|
|
|
exports.userTable = new Table(TableInfo.USERS)
|
2020-10-08 18:34:41 +02:00
|
|
|
|
2021-03-24 19:21:23 +01:00
|
|
|
if (env.isProd()) {
|
2020-10-08 18:34:41 +02:00
|
|
|
exports.init(`https://dynamodb.${AWS_REGION}.amazonaws.com`)
|
|
|
|
} else {
|
2020-10-28 21:35:06 +01:00
|
|
|
env._set("AWS_ACCESS_KEY_ID", "KEY_ID")
|
|
|
|
env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY")
|
2020-10-08 18:34:41 +02:00
|
|
|
exports.init("http://localhost:8333")
|
|
|
|
}
|