-
Notifications
You must be signed in to change notification settings - Fork 2
/
hubdb.js
215 lines (177 loc) · 4.48 KB
/
hubdb.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const path = require('path');
const fs = require('fs-extra');
const prettier = require('prettier');
const {
createTable,
updateTable,
createRows,
fetchTable,
fetchRows,
publishTable,
deleteRows,
} = require('./api/hubdb');
const { getCwd } = require('./path');
function validateJsonPath(src) {
if (path.extname(src) !== '.json') {
throw new Error('The HubDB table file must be a ".json" file');
}
}
function validateJsonFile(src) {
try {
const stats = fs.statSync(src);
if (!stats.isFile()) {
throw new Error(`The "${src}" path is not a path to a file`);
}
} catch (e) {
throw new Error(`The "${src}" path is not a path to a file`);
}
validateJsonPath(src);
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
async function addRowsToHubDbTable(accountId, tableId, rows) {
const rowsToUpdate = rows.map(row => {
const values = row.values;
return {
childTableId: 0,
isSoftEditable: false,
...row,
values,
};
});
if (rowsToUpdate.length > 0) {
await createRows(accountId, tableId, rowsToUpdate);
}
const { rowCount } = await publishTable(accountId, tableId);
return {
tableId,
rowCount,
};
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
async function createHubDbTable(accountId, src) {
validateJsonFile(src);
const table = fs.readJsonSync(src);
const { rows, ...schema } = table;
const { columns, id } = await createTable(accountId, schema);
return addRowsToHubDbTable(accountId, id, rows, columns);
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
async function updateHubDbTable(accountId, tableId, src) {
validateJsonFile(src);
const table = fs.readJsonSync(src);
const { ...schema } = table;
return updateTable(accountId, tableId, schema);
}
function convertToJSON(table, rows) {
const {
allowChildTables,
allowPublicApiAccess,
columns,
dynamicMetaTags,
enableChildTablePages,
label,
name,
useForPages,
} = table;
const cleanedColumns = columns
.filter(column => !column.deleted || !column.archived)
.map(column => {
const cleanedColumn = {
...column,
};
delete cleanedColumn.id;
delete cleanedColumn.deleted;
delete cleanedColumn.archived;
delete cleanedColumn.foreignIdsByName;
delete cleanedColumn.foreignIdsById;
return cleanedColumn;
});
const cleanedRows = rows.map(row => {
return {
path: row.path,
name: row.name,
values: row.values,
};
});
return {
name,
useForPages,
label,
allowChildTables,
allowPublicApiAccess,
dynamicMetaTags,
enableChildTablePages,
columns: cleanedColumns,
rows: cleanedRows,
};
}
async function fetchAllRows(accountId, tableId) {
let rows = [];
let after = null;
do {
const { paging, results } = await fetchRows(
accountId,
tableId,
after ? { after } : null
);
rows = rows.concat(results);
after = paging && paging.next ? paging.next.after : null;
} while (after !== null);
return rows;
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
async function downloadHubDbTable(accountId, tableId, dest) {
const table = await fetchTable(accountId, tableId);
dest = path.resolve(getCwd(), dest || `${table.name}.hubdb.json`);
if (fs.pathExistsSync(dest)) {
validateJsonFile(dest);
} else {
validateJsonPath(dest);
}
const rows = await fetchAllRows(accountId, tableId);
const tableToWrite = JSON.stringify(convertToJSON(table, rows));
const tableJson = prettier.format(tableToWrite, {
parser: 'json',
});
await fs.outputFile(dest, tableJson);
return { filePath: dest };
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
async function clearHubDbTableRows(accountId, tableId) {
const rows = await fetchAllRows(accountId, tableId);
await deleteRows(
accountId,
tableId,
rows.map(row => row.id)
);
return {
deletedRowCount: rows.length,
};
}
module.exports = {
createHubDbTable,
downloadHubDbTable,
clearHubDbTableRows,
updateHubDbTable,
addRowsToHubDbTable,
};