@budibase/server 2.4.26 → 2.4.27-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/builder/assets/{index.b0e3aca6.css → index.68ae453c.css} +2 -2
  2. package/builder/assets/{index.9e2f47ef.js → index.a661155b.js} +351 -350
  3. package/builder/index.html +2 -2
  4. package/dist/api/controllers/datasource.js +2 -1
  5. package/dist/api/controllers/public/metrics.js +113 -0
  6. package/dist/api/controllers/row/external.js +16 -8
  7. package/dist/api/controllers/row/index.js +5 -1
  8. package/dist/api/controllers/row/internal.js +1 -10
  9. package/dist/api/controllers/row/utils.js +3 -4
  10. package/dist/api/controllers/table/external.js +16 -12
  11. package/dist/api/controllers/table/utils.js +15 -1
  12. package/dist/api/routes/public/index.js +8 -0
  13. package/dist/api/routes/public/metrics.js +30 -0
  14. package/dist/constants/index.js +2 -1
  15. package/dist/integrations/googlesheets.js +125 -59
  16. package/dist/integrations/utils.js +17 -2
  17. package/dist/package.json +7 -7
  18. package/dist/tsconfig.build.tsbuildinfo +1 -1
  19. package/package.json +8 -8
  20. package/specs/openapi.json +39 -0
  21. package/specs/openapi.yaml +169 -0
  22. package/specs/resources/application.ts +11 -0
  23. package/specs/resources/index.ts +2 -0
  24. package/specs/resources/metrics.ts +81 -0
  25. package/src/api/controllers/datasource.ts +2 -1
  26. package/src/api/controllers/public/metrics.ts +251 -0
  27. package/src/api/controllers/row/external.ts +26 -16
  28. package/src/api/controllers/row/index.ts +7 -2
  29. package/src/api/controllers/row/internal.ts +0 -7
  30. package/src/api/controllers/row/utils.ts +3 -4
  31. package/src/api/controllers/table/external.ts +24 -17
  32. package/src/api/controllers/table/index.ts +9 -9
  33. package/src/api/controllers/table/utils.ts +18 -2
  34. package/src/api/routes/public/index.ts +10 -1
  35. package/src/api/routes/public/metrics.ts +28 -0
  36. package/src/api/routes/public/tests/metrics.spec.js +34 -0
  37. package/src/constants/index.ts +1 -0
  38. package/src/definitions/openapi.ts +15 -0
  39. package/src/integrations/googlesheets.ts +143 -71
  40. package/src/integrations/utils.ts +16 -4
@@ -15,11 +15,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const types_1 = require("@budibase/types");
16
16
  const google_auth_library_1 = require("google-auth-library");
17
17
  const utils_1 = require("./utils");
18
- const constants_1 = require("../constants");
19
18
  const google_spreadsheet_1 = require("google-spreadsheet");
20
19
  const node_fetch_1 = __importDefault(require("node-fetch"));
21
20
  const backend_core_1 = require("@budibase/backend-core");
22
21
  const shared_core_1 = require("@budibase/shared-core");
22
+ const constants_1 = require("../constants");
23
+ const ALLOWED_TYPES = [
24
+ types_1.FieldType.STRING,
25
+ types_1.FieldType.FORMULA,
26
+ types_1.FieldType.NUMBER,
27
+ types_1.FieldType.LONGFORM,
28
+ types_1.FieldType.DATETIME,
29
+ types_1.FieldType.OPTIONS,
30
+ types_1.FieldType.BOOLEAN,
31
+ types_1.FieldType.BARCODEQR,
32
+ ];
23
33
  const SCHEMA = {
24
34
  plus: true,
25
35
  auth: {
@@ -135,6 +145,7 @@ class GoogleSheetsIntegration {
135
145
  });
136
146
  }
137
147
  connect() {
148
+ var _a;
138
149
  return __awaiter(this, void 0, void 0, function* () {
139
150
  try {
140
151
  // Initialise oAuth client
@@ -159,12 +170,36 @@ class GoogleSheetsIntegration {
159
170
  yield this.client.loadInfo();
160
171
  }
161
172
  catch (err) {
173
+ // this happens for xlsx imports
174
+ if ((_a = err.message) === null || _a === void 0 ? void 0 : _a.includes("operation is not supported")) {
175
+ err.message =
176
+ "This operation is not supported - XLSX sheets must be converted.";
177
+ }
162
178
  console.error("Error connecting to google sheets", err);
163
179
  throw err;
164
180
  }
165
181
  });
166
182
  }
167
- buildSchema(datasourceId) {
183
+ getTableSchema(title, headerValues, id) {
184
+ // base table
185
+ const table = {
186
+ name: title,
187
+ primary: [constants_1.GOOGLE_SHEETS_PRIMARY_KEY],
188
+ schema: {},
189
+ };
190
+ if (id) {
191
+ table._id = id;
192
+ }
193
+ // build schema from headers
194
+ for (let header of headerValues) {
195
+ table.schema[header] = {
196
+ name: header,
197
+ type: types_1.FieldType.STRING,
198
+ };
199
+ }
200
+ return table;
201
+ }
202
+ buildSchema(datasourceId, entities) {
168
203
  return __awaiter(this, void 0, void 0, function* () {
169
204
  yield this.connect();
170
205
  const sheets = this.client.sheetsByIndex;
@@ -172,56 +207,47 @@ class GoogleSheetsIntegration {
172
207
  for (let sheet of sheets) {
173
208
  // must fetch rows to determine schema
174
209
  yield sheet.getRows();
175
- // build schema
176
- const schema = {};
177
- // build schema from headers
178
- for (let header of sheet.headerValues) {
179
- schema[header] = {
180
- name: header,
181
- type: constants_1.FieldTypes.STRING,
182
- };
183
- }
184
- // create tables
185
- tables[sheet.title] = {
186
- _id: (0, utils_1.buildExternalTableId)(datasourceId, sheet.title),
187
- name: sheet.title,
188
- primary: ["rowNumber"],
189
- schema,
190
- };
210
+ const id = (0, utils_1.buildExternalTableId)(datasourceId, sheet.title);
211
+ tables[sheet.title] = this.getTableSchema(sheet.title, sheet.headerValues, id);
191
212
  }
192
- this.tables = tables;
213
+ const final = (0, utils_1.finaliseExternalTables)(tables, entities);
214
+ this.tables = final.tables;
215
+ this.schemaErrors = final.errors;
193
216
  });
194
217
  }
195
218
  query(json) {
219
+ var _a, _b, _c, _d, _e, _f, _g, _h;
196
220
  return __awaiter(this, void 0, void 0, function* () {
197
221
  const sheet = json.endpoint.entityId;
198
- const handlers = {
199
- [constants_1.DataSourceOperation.CREATE]: () => this.create({ sheet, row: json.body }),
200
- [constants_1.DataSourceOperation.READ]: () => this.read(Object.assign(Object.assign({}, json), { sheet })),
201
- [constants_1.DataSourceOperation.UPDATE]: () => {
202
- var _a, _b, _c;
222
+ switch (json.endpoint.operation) {
223
+ case types_1.Operation.CREATE:
224
+ return this.create({ sheet, row: json.body });
225
+ case types_1.Operation.BULK_CREATE:
226
+ return this.createBulk({ sheet, rows: json.body });
227
+ case types_1.Operation.READ:
228
+ return this.read(Object.assign(Object.assign({}, json), { sheet }));
229
+ case types_1.Operation.UPDATE:
203
230
  return this.update({
204
231
  // exclude the header row and zero index
205
232
  rowIndex: ((_c = (_b = (_a = json.extra) === null || _a === void 0 ? void 0 : _a.idFilter) === null || _b === void 0 ? void 0 : _b.equal) === null || _c === void 0 ? void 0 : _c.rowNumber) - 2,
206
233
  sheet,
207
234
  row: json.body,
208
235
  });
209
- },
210
- [constants_1.DataSourceOperation.DELETE]: () => {
211
- var _a, _b, _c;
236
+ case types_1.Operation.DELETE:
212
237
  return this.delete({
213
238
  // exclude the header row and zero index
214
- rowIndex: ((_c = (_b = (_a = json.extra) === null || _a === void 0 ? void 0 : _a.idFilter) === null || _b === void 0 ? void 0 : _b.equal) === null || _c === void 0 ? void 0 : _c.rowNumber) - 2,
239
+ rowIndex: ((_f = (_e = (_d = json.extra) === null || _d === void 0 ? void 0 : _d.idFilter) === null || _e === void 0 ? void 0 : _e.equal) === null || _f === void 0 ? void 0 : _f.rowNumber) - 2,
215
240
  sheet,
216
241
  });
217
- },
218
- [constants_1.DataSourceOperation.CREATE_TABLE]: () => { var _a; return this.createTable((_a = json === null || json === void 0 ? void 0 : json.table) === null || _a === void 0 ? void 0 : _a.name); },
219
- [constants_1.DataSourceOperation.UPDATE_TABLE]: () => this.updateTable(json.table),
220
- [constants_1.DataSourceOperation.DELETE_TABLE]: () => { var _a; return this.deleteTable((_a = json === null || json === void 0 ? void 0 : json.table) === null || _a === void 0 ? void 0 : _a.name); },
221
- };
222
- // @ts-ignore
223
- const internalQueryMethod = handlers[json.endpoint.operation];
224
- return yield internalQueryMethod();
242
+ case types_1.Operation.CREATE_TABLE:
243
+ return this.createTable((_g = json === null || json === void 0 ? void 0 : json.table) === null || _g === void 0 ? void 0 : _g.name);
244
+ case types_1.Operation.UPDATE_TABLE:
245
+ return this.updateTable(json.table);
246
+ case types_1.Operation.DELETE_TABLE:
247
+ return this.deleteTable((_h = json === null || json === void 0 ? void 0 : json.table) === null || _h === void 0 ? void 0 : _h.name);
248
+ default:
249
+ throw new Error(`GSheets integration does not support "${json.endpoint.operation}".`);
250
+ }
225
251
  });
226
252
  }
227
253
  buildRowObject(headers, values, rowNumber) {
@@ -234,9 +260,12 @@ class GoogleSheetsIntegration {
234
260
  }
235
261
  createTable(name) {
236
262
  return __awaiter(this, void 0, void 0, function* () {
263
+ if (!name) {
264
+ throw new Error("Must provide name for new sheet.");
265
+ }
237
266
  try {
238
267
  yield this.connect();
239
- return yield this.client.addSheet({ title: name, headerValues: ["test"] });
268
+ return yield this.client.addSheet({ title: name, headerValues: [name] });
240
269
  }
241
270
  catch (err) {
242
271
  console.error("Error creating new table in google sheets", err);
@@ -246,34 +275,53 @@ class GoogleSheetsIntegration {
246
275
  }
247
276
  updateTable(table) {
248
277
  return __awaiter(this, void 0, void 0, function* () {
249
- try {
250
- yield this.connect();
251
- const sheet = this.client.sheetsByTitle[table.name];
252
- yield sheet.loadHeaderRow();
253
- if (table._rename) {
254
- const headers = [];
255
- for (let header of sheet.headerValues) {
256
- if (header === table._rename.old) {
257
- headers.push(table._rename.updated);
258
- }
259
- else {
260
- headers.push(header);
261
- }
278
+ yield this.connect();
279
+ const sheet = this.client.sheetsByTitle[table.name];
280
+ yield sheet.loadHeaderRow();
281
+ if (table._rename) {
282
+ const headers = [];
283
+ for (let header of sheet.headerValues) {
284
+ if (header === table._rename.old) {
285
+ headers.push(table._rename.updated);
286
+ }
287
+ else {
288
+ headers.push(header);
262
289
  }
290
+ }
291
+ try {
263
292
  yield sheet.setHeaderRow(headers);
264
293
  }
265
- else {
266
- const updatedHeaderValues = [...sheet.headerValues];
267
- const newField = Object.keys(table.schema).find(key => !sheet.headerValues.includes(key));
268
- if (newField) {
269
- updatedHeaderValues.push(newField);
294
+ catch (err) {
295
+ console.error("Error updating column name in google sheets", err);
296
+ throw err;
297
+ }
298
+ }
299
+ else {
300
+ const updatedHeaderValues = [...sheet.headerValues];
301
+ // add new column - doesn't currently exist
302
+ for (let [key, column] of Object.entries(table.schema)) {
303
+ if (!ALLOWED_TYPES.includes(column.type)) {
304
+ throw new Error(`Column type: ${column.type} not allowed for GSheets integration.`);
305
+ }
306
+ if (!sheet.headerValues.includes(key) &&
307
+ column.type !== types_1.FieldType.FORMULA) {
308
+ updatedHeaderValues.push(key);
309
+ }
310
+ }
311
+ // clear out deleted columns
312
+ for (let key of sheet.headerValues) {
313
+ if (!Object.keys(table.schema).includes(key)) {
314
+ const idx = updatedHeaderValues.indexOf(key);
315
+ updatedHeaderValues.splice(idx, 1);
270
316
  }
317
+ }
318
+ try {
271
319
  yield sheet.setHeaderRow(updatedHeaderValues);
272
320
  }
273
- }
274
- catch (err) {
275
- console.error("Error updating table in google sheets", err);
276
- throw err;
321
+ catch (err) {
322
+ console.error("Error updating table in google sheets", err);
323
+ throw err;
324
+ }
277
325
  }
278
326
  });
279
327
  }
@@ -307,6 +355,24 @@ class GoogleSheetsIntegration {
307
355
  }
308
356
  });
309
357
  }
358
+ createBulk(query) {
359
+ return __awaiter(this, void 0, void 0, function* () {
360
+ try {
361
+ yield this.connect();
362
+ const sheet = this.client.sheetsByTitle[query.sheet];
363
+ let rowsToInsert = [];
364
+ for (let row of query.rows) {
365
+ rowsToInsert.push(typeof row === "string" ? JSON.parse(row) : row);
366
+ }
367
+ const rows = yield sheet.addRows(rowsToInsert);
368
+ return rows.map(row => this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber));
369
+ }
370
+ catch (err) {
371
+ console.error("Error bulk writing to google sheets", err);
372
+ throw err;
373
+ }
374
+ });
375
+ }
310
376
  read(query) {
311
377
  return __awaiter(this, void 0, void 0, function* () {
312
378
  try {
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.finaliseExternalTables = exports.isIsoDateString = exports.isSQL = exports.getSqlQuery = exports.convertSqlType = exports.breakRowIdField = exports.convertRowId = exports.isRowId = exports.generateRowIdField = exports.breakExternalTableId = exports.buildExternalTableId = exports.isExternalTable = exports.SqlClient = void 0;
3
+ exports.finaliseExternalTables = exports.shouldCopySpecialColumn = exports.shouldCopyRelationship = exports.isIsoDateString = exports.isSQL = exports.getSqlQuery = exports.convertSqlType = exports.breakRowIdField = exports.convertRowId = exports.isRowId = exports.generateRowIdField = exports.breakExternalTableId = exports.buildExternalTableId = exports.isExternalTable = exports.SqlClient = void 0;
4
4
  const types_1 = require("@budibase/types");
5
5
  const utils_1 = require("../db/utils");
6
6
  const constants_1 = require("../constants");
7
7
  const DOUBLE_SEPARATOR = `${utils_1.SEPARATOR}${utils_1.SEPARATOR}`;
8
8
  const ROW_ID_REGEX = /^\[.*]$/g;
9
+ const ENCODED_SPACE = encodeURIComponent(" ");
9
10
  const SQL_NUMBER_TYPE_MAP = {
10
11
  integer: constants_1.FieldTypes.NUMBER,
11
12
  int: constants_1.FieldTypes.NUMBER,
@@ -67,6 +68,10 @@ function isExternalTable(tableId) {
67
68
  }
68
69
  exports.isExternalTable = isExternalTable;
69
70
  function buildExternalTableId(datasourceId, tableName) {
71
+ // encode spaces
72
+ if (tableName.includes(" ")) {
73
+ tableName = encodeURIComponent(tableName);
74
+ }
70
75
  return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}`;
71
76
  }
72
77
  exports.buildExternalTableId = buildExternalTableId;
@@ -78,6 +83,10 @@ function breakExternalTableId(tableId) {
78
83
  let datasourceId = parts.shift();
79
84
  // if they need joined
80
85
  let tableName = parts.join(DOUBLE_SEPARATOR);
86
+ // if contains encoded spaces, decode it
87
+ if (tableName.includes(ENCODED_SPACE)) {
88
+ tableName = decodeURIComponent(tableName);
89
+ }
81
90
  return { datasourceId, tableName };
82
91
  }
83
92
  exports.breakExternalTableId = breakExternalTableId;
@@ -193,6 +202,7 @@ function shouldCopyRelationship(column, tableIds) {
193
202
  column.tableId &&
194
203
  tableIds.includes(column.tableId));
195
204
  }
205
+ exports.shouldCopyRelationship = shouldCopyRelationship;
196
206
  /**
197
207
  * Similar function to the shouldCopyRelationship function, but instead this looks for options and boolean
198
208
  * types. It is possible to switch a string -> options and a number -> boolean (and vice versus) need to make
@@ -217,6 +227,7 @@ function shouldCopySpecialColumn(column, fetchedColumn) {
217
227
  return (specialTypes.indexOf(column.type) !== -1 ||
218
228
  (fetchedIsNumber && column.type === constants_1.FieldTypes.BOOLEAN));
219
229
  }
230
+ exports.shouldCopySpecialColumn = shouldCopySpecialColumn;
220
231
  /**
221
232
  * Looks for columns which need to be copied over into the new table definitions, like relationships
222
233
  * and options types.
@@ -226,10 +237,14 @@ function shouldCopySpecialColumn(column, fetchedColumn) {
226
237
  * @param tableIds The IDs of the tables which exist now, to check if anything has been removed.
227
238
  */
228
239
  function copyExistingPropsOver(tableName, table, entities, tableIds) {
240
+ var _a, _b, _c;
229
241
  if (entities && entities[tableName]) {
230
- if (entities[tableName].primaryDisplay) {
242
+ if ((_a = entities[tableName]) === null || _a === void 0 ? void 0 : _a.primaryDisplay) {
231
243
  table.primaryDisplay = entities[tableName].primaryDisplay;
232
244
  }
245
+ if ((_b = entities[tableName]) === null || _b === void 0 ? void 0 : _b.created) {
246
+ table.created = (_c = entities[tableName]) === null || _c === void 0 ? void 0 : _c.created;
247
+ }
233
248
  const existingTableSchema = entities[tableName].schema;
234
249
  for (let key in existingTableSchema) {
235
250
  if (!existingTableSchema.hasOwnProperty(key)) {
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.4.25",
4
+ "version": "2.4.26",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -43,12 +43,12 @@
43
43
  "license": "GPL-3.0",
44
44
  "dependencies": {
45
45
  "@apidevtools/swagger-parser": "10.0.3",
46
- "@budibase/backend-core": "^2.4.25",
47
- "@budibase/client": "^2.4.25",
48
- "@budibase/pro": "2.4.25",
49
- "@budibase/shared-core": "^2.4.25",
50
- "@budibase/string-templates": "^2.4.25",
51
- "@budibase/types": "^2.4.25",
46
+ "@budibase/backend-core": "^2.4.26",
47
+ "@budibase/client": "^2.4.26",
48
+ "@budibase/pro": "2.4.26",
49
+ "@budibase/shared-core": "^2.4.26",
50
+ "@budibase/string-templates": "^2.4.26",
51
+ "@budibase/types": "^2.4.26",
52
52
  "@bull-board/api": "3.7.0",
53
53
  "@bull-board/koa": "3.9.4",
54
54
  "@elastic/elasticsearch": "7.10.0",