diff --git a/libs/modules/src/azTable/az-table.interface.ts b/libs/modules/src/azTable/az-table.interface.ts index d30fd89..d628881 100644 --- a/libs/modules/src/azTable/az-table.interface.ts +++ b/libs/modules/src/azTable/az-table.interface.ts @@ -1,3 +1,6 @@ export interface TableOptions { - connectionString; + accountName: string; + accountKey: string; + accountUrl: string; + allowInsecureConnection: boolean; } \ No newline at end of file diff --git a/libs/modules/src/azTable/az-table.service.ts b/libs/modules/src/azTable/az-table.service.ts index 39bdb5e..01cba9f 100644 --- a/libs/modules/src/azTable/az-table.service.ts +++ b/libs/modules/src/azTable/az-table.service.ts @@ -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 getTableClient(tableName: string): Promise { - const tableClient: TableClient = new TableClient(this.tablesOptions.connectionString, tableName); + async checkTableExists(tableName: string, tableServiceClient: TableServiceClient): Promise { + const tables = tableServiceClient.listTables(); + let tableExists: boolean = false; - return tableClient; + for await (const table of tables) { + if (table.name === tableName) { + tableExists = true; + + break; + } + } + + return tableExists; + } + + async getTableClient(tableName: string): Promise { + 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; + } } } \ No newline at end of file diff --git a/package.json b/package.json index 80cd8e9..fc72e7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.4.0", + "version": "0.4.7", "description": "", "author": "", "license": "UNLICENSED",