-
Notifications
You must be signed in to change notification settings - Fork 14
/
applySchema.js
55 lines (48 loc) · 1.32 KB
/
applySchema.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
const {
PROJECT_ID,
DATASET_NAME,
TRANSACTION_TABLE_NAME,
LEDGER_TABLE_NAME,
transactionSchema,
ledgerSchema,
} = require('./schema')
const BigQuery = require('@google-cloud/bigquery')
const bigquery = new BigQuery({ projectId: PROJECT_ID })
const createTable = async (name, schema) => {
return new Promise((resolve, reject) => {
bigquery.dataset(DATASET_NAME).createTable(name, { schema: schema })
.then(r => {
console.log(` -- BigQuery Table ${r[0].id} created`)
resolve()
})
.catch(e => {
reject(e)
})
})
}
const deleteTable = async (name) => {
return new Promise((resolve, reject) => {
bigquery.dataset(DATASET_NAME).table(name).delete().then(() => {
console.log(` -- BigQuery Table ${name} removed`)
resolve()
}).catch(e => {
if (e.errors[0].reason === 'notFound') {
resolve()
} else{
reject(e)
}
})
})
}
const recreateTable = async (name, schema) => {
console.log(`Dropping and creating table [ ${name} ] in dataset [ ${DATASET_NAME} ] @ Google BigQuery`)
await deleteTable(name)
await createTable(name, schema)
}
(async () => {
await Promise.all([
recreateTable(TRANSACTION_TABLE_NAME, transactionSchema),
recreateTable(LEDGER_TABLE_NAME, ledgerSchema),
])
console.log(`Done\n`)
})()