updating az tables
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
export interface TableOptions {
|
||||
connectionString;
|
||||
accountName: string;
|
||||
accountKey: string;
|
||||
accountUrl: string;
|
||||
allowInsecureConnection: boolean;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { TableOptions } from './az-table.interface';
|
||||
import { TABLE_OPTIONS } from './az-table.constants';
|
||||
import { TableClient } from '@azure/data-tables';
|
||||
import { TableClient, AzureNamedKeyCredential, TableServiceClient, TableServiceClientOptions } from '@azure/data-tables';
|
||||
|
||||
@Injectable()
|
||||
export class TableService {
|
||||
@@ -10,9 +10,38 @@ export class TableService {
|
||||
private tablesOptions: TableOptions
|
||||
) {}
|
||||
|
||||
async checkTableExists(tableName: string, tableServiceClient: TableServiceClient): Promise<boolean> {
|
||||
const tables = tableServiceClient.listTables();
|
||||
let tableExists: boolean = false;
|
||||
|
||||
for await (const table of tables) {
|
||||
if (table.name === tableName) {
|
||||
tableExists = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return tableExists;
|
||||
}
|
||||
|
||||
async getTableClient(tableName: string): Promise<TableClient> {
|
||||
const tableClient: TableClient = new TableClient(this.tablesOptions.connectionString, tableName);
|
||||
try {
|
||||
const credential: AzureNamedKeyCredential = new AzureNamedKeyCredential(this.tablesOptions.accountName, this.tablesOptions.accountKey);
|
||||
const options: TableServiceClientOptions = { allowInsecureConnection: this.tablesOptions.allowInsecureConnection };
|
||||
const tableServiceClient: TableServiceClient = new TableServiceClient(this.tablesOptions.accountUrl, credential, options);
|
||||
const tableExists: boolean = await this.checkTableExists(tableName, tableServiceClient);
|
||||
|
||||
if (!tableExists) {
|
||||
await tableServiceClient.createTable(tableName);
|
||||
}
|
||||
|
||||
const tableClient: TableClient = new TableClient(this.tablesOptions.accountUrl, tableName, credential, options);
|
||||
|
||||
return tableClient;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@noahspan/noahspan-modules",
|
||||
"version": "0.4.0",
|
||||
"version": "0.4.7",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"license": "UNLICENSED",
|
||||
|
||||
Reference in New Issue
Block a user