UNPKG

hfs-utilities

Version:

Health Fund Solution's internal utilities library for Typescript projects

162 lines 6.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureDataTables = void 0; const data_tables_1 = require("@azure/data-tables"); const utilities_1 = require("./utilities"); /** * AzureDataTables is a convenience wrapper around @azure/data-tables library. */ class AzureDataTables { /** * Creates a new instance of AzureDataTables. * @param connectionString Azure Data Tables connection string. * @param tableName Azure Data Tables table name. * @param options Azure Data Tables client options. */ constructor(connectionString, tableName, options) { this.connectionString = connectionString; this.tableName = tableName; this.options = options; this.client = data_tables_1.TableClient.fromConnectionString(connectionString, tableName, options); this.serviceClient = data_tables_1.TableServiceClient.fromConnectionString(connectionString); } /** * @description - converts object and array properties to string as needed * @param { TableEntity } obj - object to be converted to a table row * @returns { TableRow } - returns a table row that can safely be used in a transaction */ toTableRow(obj) { const newObj = {}; for (const key of Object.keys(obj)) { if (typeof obj[key] === 'object' && obj[key] !== null) { newObj[key] = JSON.stringify(obj[key]); } else { newObj[key] = obj[key]; } } return newObj; } /** * @description - determines if a table row is valid * @param { any } obj - object to be validated * @returns {boolean} - returns true if the object is a valid table record, i.e. has partitionKey and rowKey that are * strings longer than 0 and all other object values are either a string, null, number, or boolean */ isTableRecord(obj) { if ((0, utilities_1.isEmpty)(obj)) return false; let returnValue = true; if (typeof obj.partitionKey === 'string' && obj.partitionKey.length > 0 && typeof obj.rowKey === 'string' && obj.rowKey.length > 0) { for (const key of Object.keys(obj)) { if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) { returnValue = false; } else if (typeof obj[key] === 'object' && Array.isArray(obj[key])) { returnValue = false; } } } else { returnValue = false; } return returnValue; } /** * @description maps table rows by partition key since we need to do transactions by partitionKey. * @param tableRows Table rows to be grouped by partition key. * @returns a Map of partition key to table rows. */ mapByPartitionKey(tableRows) { const map = new Map(); for (const row of tableRows) { if (!map.has(row.partitionKey)) { map.set(row.partitionKey, []); } map.get(row.partitionKey).push(row); } return map; } /** * @description updates a table row or rows. * @param {TableEntity | TableEntity[]} tableRowOrRows - one or more table rows to be updated. * @param {UpdateMode} updateMode - Mode of update. * @returns {Promise<TableTransactionResponse[]} - returns an array of TableTransactionResponse. */ async upsert(tableRowOrRows, updateMode) { await this.serviceClient.createTable(this.tableName); const responses = []; const tableRows = Array.isArray(tableRowOrRows) ? tableRowOrRows : [tableRowOrRows]; const map = this.mapByPartitionKey(tableRows); for (const key of map.keys()) { const rows = map.get(key); const transaction = new data_tables_1.TableTransaction(); for (const row of rows) { transaction.upsertEntity(row, updateMode); } const res = await this.client.submitTransaction(transaction.actions); responses.push(res); } return responses; } /** * @description creates a table row or rows. * @param {TableEntity | TableEntity[]} tableRowOrRows - one or more table rows to be created. * @returns {Promise<TableTransactionResponse[]>} - returns an array of TableTransactionResponse. */ async create(tableRowOrRows) { await this.serviceClient.createTable(this.tableName); const responses = []; const tableRows = Array.isArray(tableRowOrRows) ? tableRowOrRows : [tableRowOrRows]; const map = this.mapByPartitionKey(tableRows); for (const key of map.keys()) { const rows = map.get(key); const transaction = new data_tables_1.TableTransaction(); for (const row of rows) { transaction.createEntity(row); } const res = await this.client.submitTransaction(transaction.actions); responses.push(res); } return responses; } /** * @description deletes a table row or rows. * @param {TableEntity | TableEntity[]} tableRowOrRows - one or more table rows to be created. * @returns {Promise<TableTransactionResponse[]>} - returns an array of TableTransactionResponse. */ async delete(tableRowOrRows) { const responses = []; const tableRows = Array.isArray(tableRowOrRows) ? tableRowOrRows : [tableRowOrRows]; const map = this.mapByPartitionKey(tableRows); for (const key of map.keys()) { const rows = map.get(key); const transaction = new data_tables_1.TableTransaction(); for (const row of rows) { transaction.deleteEntity(row.partitionKey, row.rowKey); } const res = await this.client.submitTransaction(transaction.actions); responses.push(res); } return responses; } /** * @desecription - returns query results. * @param query - query to be executed * @param select - an array of columns to be selected * @returns - returns a promise that resolves to an array of table rows. */ async query(query, select) { const results = await this.client.listEntities({ queryOptions: { filter: query, select: select !== null && select !== void 0 ? select : [] } }); const resultsToReturn = []; for await (const result of results) { resultsToReturn.push(result); } return resultsToReturn; } } exports.AzureDataTables = AzureDataTables; //# sourceMappingURL=dataTables.js.map