-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
createAndDeleteTable.js
56 lines (42 loc) · 1.71 KB
/
createAndDeleteTable.js
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* This sample demonstrates create and delete a table
*
* @summary creates and deletes a table
*/
const { TableServiceClient, TableClient } = require("@azure/data-tables");
const { v4 } = require("uuid");
// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();
const sasConnectionString = process.env["SAS_CONNECTION_STRING"] || "";
const tableSufix = v4().replace(/-/g, "");
async function createAndDeleteTable() {
console.log("== Delete and create table Sample ==");
// See authenticationMethods sample for other options of creating a new client
const serviceClient = TableServiceClient.fromConnectionString(sasConnectionString);
// Create a new table
const tableName = `SampleCreateAndDeleteTable${tableSufix}`;
await serviceClient.createTable(tableName);
// Deletes the table
await serviceClient.deleteTable(tableName);
}
async function createAndDeleteTableWithTableClient() {
// A table can also be created and deleted using a TableClient
console.log("== Delete and create table with TableClient Sample ==");
const tableName = "SampleCreateAndDeleteTable2";
// Creating a new table client doesn't do a network call
const client = TableClient.fromConnectionString(sasConnectionString, tableName);
// Will attempt to create a table with the tableName specified above
await client.createTable();
// Will attempt to delete the table with the tableName specified above
await client.deleteTable();
}
async function main() {
await createAndDeleteTable();
await createAndDeleteTableWithTableClient();
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});