Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 10x 10x 39x 39x 39x 39x 39x 5x 5x 5x 1x 5x 5x 136x 5x 5x 39x 102x 102x 102x 102x 102x 39x 3x 3x 3x 3x 2x 1x 1x 39x 138x 138x 138x 138x 136x 138x 39x 138x 138x | import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { v4 as uuidv4 } from 'uuid';
import {
Site,
SiteTableKeySchema,
SiteConnection,
CreateSiteInput,
} from '../types.d';
import { DeviceType } from '../devices/deviceTypes.d';
export class SiteDb {
constructor(
private readonly tableName: string,
private readonly documentClient: DocumentClient,
private readonly deviceTableName: string,
private readonly siteUuidGsi: string,
) {}
getSites = async (
entityUuid: string,
limit: number,
nextToken?: DocumentClient.Key,
): Promise<SiteConnection> => {
const params: DocumentClient.QueryInput = {
TableName: this.tableName,
KeyConditionExpression: 'entityUuid = :entityUuid',
ExpressionAttributeValues: { ':entityUuid': entityUuid },
Limit: limit,
};
if (nextToken) {
params.ExclusiveStartKey = nextToken;
}
const outputItems = await this.documentClient.query(params).promise();
Eif (outputItems.Items && outputItems.Items.length > 0) {
const proms = outputItems.Items.map((item) => this.getSiteWithDevices(item));
const sites = await Promise.all(proms);
return {
sites,
nextToken: outputItems.LastEvaluatedKey as SiteTableKeySchema,
};
}
return { sites: [] };
};
createSite = async (
entityUuid: string,
siteInput: CreateSiteInput,
): Promise<string> => {
const site = {
entityUuid,
siteUuid: uuidv4(),
name: siteInput.name,
address: siteInput.address,
state: siteInput.state,
};
const params: DocumentClient.PutItemInput = {
TableName: this.tableName,
Item: site,
};
await this.documentClient.put(params).promise();
return site.siteUuid;
};
getSite = async (entityUuid: string, siteUuid: string): Promise<Site> => {
const params: DocumentClient.QueryInput = {
TableName: this.tableName,
KeyConditionExpression:
'entityUuid = :entityUuid AND siteUuid = :siteUuid',
ExpressionAttributeValues: {
':entityUuid': entityUuid,
':siteUuid': siteUuid,
},
};
console.log('query params:', params);
const siteItem = await this.documentClient.query(params).promise();
if (siteItem.Items && siteItem.Items.length > 0) {
return this.getSiteWithDevices(siteItem.Items[0]);
}
console.log(`Cant find site (${siteUuid}) in entity (${entityUuid})`);
throw new Error(`Cant find site (${siteUuid}).`);
};
getSiteWithDevices = async (
siteItem: DocumentClient.AttributeMap,
): Promise<Site> => {
const site: Site = {
id: siteItem.siteUuid,
name: siteItem.name,
address: siteItem.address,
state: siteItem.state,
devices: [],
};
const deviceItems = await this.getSiteDevices(site.id);
if (deviceItems.Items && deviceItems.Items.length > 0) {
site.devices = deviceItems.Items.map((item) => ({
id: item.id,
model: item.model,
serial: item.serial,
name: item.name,
}));
}
return site;
};
getSiteDevices = (siteUuid: string): Promise<DocumentClient.QueryOutput> => {
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
IndexName: this.siteUuidGsi,
KeyConditionExpression: 'siteUuid = :siteUuid AND #type = :type',
ExpressionAttributeValues: {
':siteUuid': siteUuid,
':type': DeviceType.core,
},
ExpressionAttributeNames: { '#type': 'type' },
};
return this.documentClient.query(params).promise();
};
}
|