-
Notifications
You must be signed in to change notification settings - Fork 14
/
init-dev-storage.js
executable file
·108 lines (96 loc) · 2.97 KB
/
init-dev-storage.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
106
107
108
#!/usr/bin/env node
/*
Requires your local env file to be configured with the following...
- AZURE_STORAGE_CONNECTION_STRING
*/
require('dotenv').config()
const azure = require('azure-storage')
const bluebird = require('bluebird')
const names = require('../../deploy/storage/tables-queues.json')
if (!process.env.AZURE_STORAGE_CONNECTION_STRING) {
process.exitCode = -1
console.error('env var $AZURE_STORAGE_CONNECTION_STRING is required')
}
const queueNames = names.queues
const tableNames = names.tables
const poisonQueues = queueNames.map(q => q + '-poison')
const allQueues = queueNames.concat(poisonQueues)
const tableService = getPromisifiedService(azure.createTableService())
const queueService = getPromisifiedService(azure.createQueueService())
async function deleteTableEntities (tables) {
const deletions = []
for (let index = 0; index < tables.length; index++) {
const table = tables[index]
const query = new azure.TableQuery() // Azure Table Storage has a max of 1000 records returned
let done = false
let batch = 1
while (!done) {
const data = await tableService.queryEntitiesAsync(table, query, null)
const entities = data.result.entries
if (entities.length === 0) {
done = true
}
console.log(`Found ${entities.length} entities to delete in batch ${batch++} from ${table}`)
entities.forEach(entity => {
deletions.push(tableService.deleteEntityAsync(table, entity))
})
await Promise.all(deletions)
}
}
}
async function createTables (tables) {
const tableCreates = tables.map(table => {
return tableService.createTableIfNotExistsAsync(table)
})
return Promise.all(tableCreates)
}
async function deleteQueueMessages (queues) {
const queueDeletes = queues.map(q => {
return queueService.clearMessagesAsync(q)
})
return Promise.all(queueDeletes)
}
async function createQueues (queues) {
const queueCreates = queues.map(q => {
return queueService.createQueueIfNotExistsAsync(q)
})
return Promise.all(queueCreates)
}
async function main () {
await createQueues(allQueues)
await deleteQueueMessages(allQueues)
await createTables(tableNames)
await deleteTableEntities(tableNames)
}
main()
.then(() => {
console.log('done')
},
(error) => {
console.error(error.message)
})
.catch(error => {
console.error('Caught error: ' + error.message)
})
/**
* Promisify and cache the azureTableService library as it still lacks Promise support
*/
function getPromisifiedService (storageService) {
bluebird.promisifyAll(storageService, {
promisifier: (originalFunction) => function (...args) {
return new Promise((resolve, reject) => {
try {
originalFunction.call(this, ...args, (error, result, response) => {
if (error) {
return reject(error)
}
resolve({ result, response })
})
} catch (error) {
reject(error)
}
})
}
})
return storageService
}