component-dbs-core
Version:
DTO objects shared among device backend
170 lines • 7.35 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceDb = void 0;
class DeviceDb {
constructor(deviceTableName, modelSerialGsi, dynamodbClient, accessTokenGsi) {
this.deviceTableName = deviceTableName;
this.modelSerialGsi = modelSerialGsi;
this.dynamodbClient = dynamodbClient;
this.accessTokenGsi = accessTokenGsi;
this.deviceUuidKeyCondition = 'id = :id and #type = :type';
this.getDeviceByModelSerial = (model, serial) => __awaiter(this, void 0, void 0, function* () {
/* eslint-disable */
const params = {
TableName: this.deviceTableName,
IndexName: this.modelSerialGsi,
ExpressionAttributeValues: {
':modelSerial': this.createModelSerialKey(model, serial),
},
KeyConditionExpression: 'modelSerial = :modelSerial',
};
return this.dynamodbClient.query(params).promise();
});
this.saveDeviceUuid = (model, serial, id) => __awaiter(this, void 0, void 0, function* () {
const modelSerial = this.createModelSerialKey(model, serial);
const params = {
TableName: this.deviceTableName,
Item: {
id,
modelSerial,
model,
serial,
type: "core" /* core */,
},
};
return this.dynamodbClient.put(params).promise();
});
this.getDeviceByUuid = (id) => {
const params = {
TableName: this.deviceTableName,
ExpressionAttributeValues: {
':id': id,
':type': "core" /* core */,
},
KeyConditionExpression: this.deviceUuidKeyCondition,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
this.getDeviceRecordByType = (id, type) => {
const params = {
TableName: this.deviceTableName,
ExpressionAttributeValues: {
":id": id,
":type": type,
},
KeyConditionExpression: this.deviceUuidKeyCondition,
ExpressionAttributeNames: { "#type": "type" },
};
return this.dynamodbClient.query(params).promise();
};
this.findDeviceByTokens = (deviceUuid, accessToken, refreshToken, idToken) => {
const attrValues = {
':id': deviceUuid,
':type': "core" /* core */,
':accessToken': accessToken,
};
let filterExpression = 'accessToken = :accessToken';
if (idToken) {
filterExpression += ' and idToken = :idToken';
attrValues[':idToken'] = idToken;
}
if (refreshToken) {
filterExpression += ' and refreshToken = :refreshToken';
attrValues[':refreshToken'] = refreshToken;
}
const params = {
TableName: this.deviceTableName,
ExpressionAttributeValues: attrValues,
KeyConditionExpression: this.deviceUuidKeyCondition,
FilterExpression: filterExpression,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
this.updateDeviceRecord = (deviceUuid, type, values) => __awaiter(this, void 0, void 0, function* () {
let updateExpression = "";
const reservedWords = ['status', 'name'];
const attrValues = {};
const attrNames = {};
const keys = Object.keys(values);
keys.forEach((key, i) => {
const v = values[key];
if (v) {
if (i === 0) {
updateExpression = "set";
}
if (!reservedWords.some(w => w === key)) {
updateExpression += ` ${key} = :${key}`;
}
else {
updateExpression += ` #${key} = :${key}`;
attrNames[`#${key}`] = key;
}
attrValues[`:${key}`] = v;
if (i < keys.length - 1) {
updateExpression += ',';
}
}
});
if (updateExpression.endsWith(',')) {
updateExpression = updateExpression.slice(0, -1);
}
const params = {
TableName: this.deviceTableName,
Key: {
id: deviceUuid,
type,
},
UpdateExpression: updateExpression,
ExpressionAttributeValues: attrValues
};
if (Object.keys(attrNames).length > 0) {
params.ExpressionAttributeNames = attrNames;
}
return this.dynamodbClient.update(params).promise();
});
this.findDeviceByPin = (pin, deviceUuid) => {
const attrValues = {
':id': deviceUuid,
':type': "settings" /* settings */,
':pin': pin,
};
const filterExpression = 'pin = :pin';
const params = {
TableName: this.deviceTableName,
ExpressionAttributeValues: attrValues,
KeyConditionExpression: this.deviceUuidKeyCondition,
FilterExpression: filterExpression,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
this.createModelSerialKey = (model, serial) => {
return `${model}.${serial}`;
};
this.findEntityUuidByAccessToken = (token) => {
// TODO: we need some way to verify entity uuid based on access token
const params = {
TableName: this.deviceTableName,
IndexName: this.accessTokenGsi,
ExpressionAttributeValues: {
':accessToken': token,
},
KeyConditionExpression: 'accessToken = :accessToken',
};
return this.dynamodbClient.query(params).promise();
};
}
}
exports.DeviceDb = DeviceDb;
//# sourceMappingURL=deviceDb.js.map