-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
88 lines (74 loc) · 2.46 KB
/
db.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
require('dotenv').config()
const Airtable = require('airtable');
var fs = require("fs");
class Table {
constructor(name) {
this.name = name;
this.base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY})
.base(process.env.AIRTABLE_ID);
this.view = "Grid view";
}
async getAll() {
const recordsObject = {};
const records = await this.base(this.name)
.select({ view: this.view })
.firstPage();
records.forEach((record) => recordsObject[record._rawJson.id] = { id: record._rawJson.id, ...record._rawJson.fields });
return recordsObject;
}
async find(id) {
return this.base(this.name).find(id);
}
}
const tables = ['bands', 'artists', 'producers', 'albums', 'songs', 'collaborations'];
const data = {};
// Write to files
// (async function(){
// for (let table of tables) {
// const ResultTable = new Table(table);
// const results = await ResultTable.getAll();
// const writeStream = fs.createWriteStream(table + '.json');
// writeStream.write(JSON.stringify(results));
// writeStream.end();
// }
// })()
(async function(){
const BandsTable = new Table('bands');
const bands = await BandsTable.getAll();
data.bands = bands;
const ArtistsTable = new Table('artists');
const artists = await ArtistsTable.getAll();
data.artists = artists;
const CollaboratorsTable = new Table('collaborations');
const collaborations = await CollaboratorsTable.getAll();
data.collaborations = collaborations;
for (let x in bands) {
for (let y = 0; y < data.bands[x].artists.length; y++) {
data.bands[x].artists[y] = data.artists[data.bands[x].artists[y]];
}
}
const nodes = [];
const links = [];
// add collbaorations
for (let x in collaborations) {
links.push({
source: collaborations[x].band[0],
target: collaborations[x].artist[0],
kind: 'collbaoration',
})
}
for (let x in bands) {
const bandName = bands[x].name;
const bandID = bands[x].id;
nodes.push({ id: bands[x].id, name: bandName, type: 'band' });
for (let artist of bands[x].artists) {
// if we have not already added this artist
if (!nodes.find(e => e.id === artist.id)) {
const artistInfo = { id: artist.id, name: artist.name, type: 'artist', instruments: artist.instruments }
nodes.push(artistInfo);
}
links.push({ source: artist.id, target: bandID, kind: 'member' });
}
}
console.log(JSON.stringify({nodes, links}));
})()