-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
createAndDeleteEntities.js
105 lines (78 loc) · 2.81 KB
/
createAndDeleteEntities.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* This sample demonstrates create and delete entities in a table
*
* @summary creates and deletes a entities in a table
*/
const { TableClient, AzureNamedKeyCredential } = require("@azure/data-tables");
// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();
const tablesUrl = process.env["TABLES_URL"] || "";
const accountName = process.env["ACCOUNT_NAME"] || "";
const accountKey = process.env["ACCOUNT_KEY"] || "";
async function createSimpleDateEntity() {
// Note that this sample assumes that a table with tableName exists
const tableName = `createSimpleDateEntityTable`;
// See authenticationMethods sample for other options of creating a new client
const creds = new AzureNamedKeyCredential(accountName, accountKey);
const client = new TableClient(tablesUrl, tableName, creds);
await client.createTable();
const entity = {
partitionKey: "p1",
rowKey: "r1",
date: new Date()
};
await client.createEntity(entity);
await client.deleteTable();
}
async function createComplexDateEntity() {
// Note that this sample assumes that a table with tableName exists
const tableName = `createComplexDateEntityTable`;
// See authenticationMethods sample for other options of creating a new client
const creds = new AzureNamedKeyCredential(accountName, accountKey);
const client = new TableClient(tablesUrl, tableName, creds);
await client.createTable();
// For higher precision dates we need to pass the
const date = { type: "DateTime", value: "2016-06-10T21:42:24.7607389" };
const entity = {
partitionKey: "p2",
rowKey: "r2",
date
};
await client.createEntity(entity);
await client.deleteTable();
}
async function createAndDeleteEntities() {
console.log("== Create and delete entities Sample ==");
// Note that this sample assumes that a table with tableName exists
const tableName = `createAndDeleteEntitiesTable`;
// See authenticationMethods sample for other options of creating a new client
const creds = new AzureNamedKeyCredential(accountName, accountKey);
const client = new TableClient(tablesUrl, tableName, creds);
// Create the table
await client.createTable();
const entity = {
partitionKey: "Stationery",
rowKey: "A1",
name: "Marker Set",
price: 5.0,
quantity: 21
};
// Create the new entity
await client.createEntity(entity);
// Delete the entity
await client.deleteEntity(entity.partitionKey, entity.rowKey);
// Delete the table for cleanup
// Create the table
await client.deleteTable();
}
async function main() {
await createAndDeleteEntities();
await createSimpleDateEntity();
await createComplexDateEntity();
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});