@akadenia/azure-storage
Version:
Microsoft Azure storage helper methods
169 lines (168 loc) • 6.44 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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableStorage = void 0;
const data_tables_1 = require("@azure/data-tables");
/**
* @class TableStorage - A class that contains azure table storage helpers
*/
class TableStorage {
constructor(connectionString, tableName) {
if (!connectionString) {
throw new Error("Missing connection string");
}
if (!tableName) {
throw new Error("Missing table name");
}
this.tableClient = data_tables_1.TableClient.fromConnectionString(connectionString, tableName);
}
/**
* @returns {TableClient} - A TableClient object
*/
getTableClient() {
return this.tableClient;
}
/** Creates a table in the storage account
* @returns {Promise<void>} - A promise that resolves when the table is created
* @returns {Promise<boolean>} - A promise that resolves to true if the table was created
**/
createTable() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.createTable();
}
catch (error) {
return false;
}
return true;
});
}
/** Deletes a table in the storage account
* @returns {Promise<void>} - A promise that resolves when the table is deleted
* @returns {Promise<boolean>} - A promise that resolves to true if the table was deleted
**/
deleteTable() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.deleteTable();
}
catch (error) {
return false;
}
return true;
});
}
/**
* @param {ITableEntity} entity - The entity to insert
* @returns {Promise<boolean>} - A promise that resolves to true if the entity was inserted
*/
insert(entity) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.createEntity(entity);
}
catch (error) {
return false;
}
return true;
});
}
/**
* @param {ITableEntity} entity - The entity to update
* @returns {Promise<boolean>} - A promise that resolves to true if the entity was updated
*/
update(entity) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.updateEntity(entity);
}
catch (error) {
return false;
}
return true;
});
}
/**
* @param {ITableEntity} entity - The entity to upsert
* @returns {Promise<boolean>} - A promise that resolves to true if the entity was upserted
*/
upsert(entity) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.upsertEntity(entity);
}
catch (error) {
return false;
}
return true;
});
}
/**
* @param {string} partitionKey - The partition key
* @param {string} rowKey - The row key
* @returns {Promise<ITableEntity>} - The deleted entity
*/
delete(partitionKey, rowKey) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.tableClient.deleteEntity(partitionKey, rowKey);
}
catch (error) {
return false;
}
return true;
});
}
/**
* @param {string} partitionKey - The partition key
* @param {string} rowKey - The row key
* @returns {Promise<ITableEntity>} - The retrieved entity
*/
get(partitionKey, rowKey) {
return __awaiter(this, void 0, void 0, function* () {
return this.tableClient.getEntity(partitionKey, rowKey);
});
}
list(options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
const iterator = this.tableClient.listEntities(options);
let entities = [];
let i = 1;
try {
for (var _d = true, iterator_1 = __asyncValues(iterator), iterator_1_1; iterator_1_1 = yield iterator_1.next(), _a = iterator_1_1.done, !_a; _d = true) {
_c = iterator_1_1.value;
_d = false;
const entity = _c;
entities.push(entity);
this;
i++;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = iterator_1.return)) yield _b.call(iterator_1);
}
finally { if (e_1) throw e_1.error; }
}
return entities;
});
}
}
exports.TableStorage = TableStorage;