forked from matzayonc/data-node-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-table.js
More file actions
68 lines (56 loc) · 2.42 KB
/
Copy pathcreate-table.js
File metadata and controls
68 lines (56 loc) · 2.42 KB
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
'use strict';
// Run once to provision the BigQuery tables:
// node create-table.js
const { BigQuery } = require('@google-cloud/bigquery');
const { GoogleAuth, Impersonated } = require('google-auth-library');
const PROJECT = 'calmal';
const DATASET = 'lending_poc';
const SA = process.env.IMPERSONATE_SA || 'lending-poc@calmal.iam.gserviceaccount.com';
const POOL_SCHEMA = [
{ name: 'id', type: 'INT64', mode: 'REQUIRED' },
{ name: 'reservePubkey', type: 'STRING', mode: 'REQUIRED' },
{ name: 'symbol', type: 'STRING', mode: 'REQUIRED' },
{ name: 'mintAddress', type: 'STRING', mode: 'REQUIRED' },
{ name: 'lending', type: 'STRING', mode: 'REQUIRED' },
{ name: 'chain', type: 'STRING', mode: 'REQUIRED' },
{ name: 'market', type: 'STRING', mode: 'REQUIRED' },
];
const SNAPSHOTS_SCHEMA = [
{ name: 'poolId', type: 'INT64', mode: 'REQUIRED' },
{ name: 'tvl', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'utilization', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'supplyAPY', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'borrowRate', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'borrowAPY', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'totalBorrowUsd',type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'liquidityUsd', type: 'NUMERIC', mode: 'REQUIRED' },
{ name: 'fetchedAt', type: 'TIMESTAMP', mode: 'REQUIRED' },
];
(async () => {
const base = new GoogleAuth();
const sourceClient = await base.getClient();
const impersonated = new Impersonated({
sourceClient,
targetPrincipal: SA,
lifetime: 300,
targetScopes: ['https://www.googleapis.com/auth/bigquery'],
});
const bq = new BigQuery({ projectId: PROJECT, authClient: impersonated });
console.log(`[bq] impersonating ${SA}`);
const ds = bq.dataset(DATASET);
for (const name of ['pool', 'snapshots']) {
const table = ds.table(name);
const [exists] = await table.exists();
if (exists) {
await table.delete();
console.log(`Deleted ${DATASET}.${name}`);
}
}
const [pool] = await ds.createTable('pool', { schema: POOL_SCHEMA });
const [snapshots] = await ds.createTable('snapshots', { schema: SNAPSHOTS_SCHEMA });
console.log(`Created ${PROJECT}.${DATASET}.${pool.id}`);
console.log(`Created ${PROJECT}.${DATASET}.${snapshots.id}`);
})().catch(err => {
console.error(err.message);
process.exit(1);
});