From 84fcea8a80d1e90815f8ececc0e6a29d902d0333 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Sun, 11 Oct 2020 20:42:30 +0100 Subject: [PATCH 01/14] fix: no fields required by default --- packages/builder/src/constants/backend/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/constants/backend/index.js b/packages/builder/src/constants/backend/index.js index 55a2d2c6c7..a735e4b864 100644 --- a/packages/builder/src/constants/backend/index.js +++ b/packages/builder/src/constants/backend/index.js @@ -15,7 +15,7 @@ export const FIELDS = { type: "options", constraints: { type: "string", - presence: { allowEmpty: true }, + presence: false, inclusion: [], }, }, @@ -67,7 +67,7 @@ export const FIELDS = { type: "link", constraints: { type: "array", - presence: { allowEmpty: true }, + presence: false, }, }, } From dd2a84d58a5aa2805b48b3e6d0bbb4d7022c52a0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 12 Oct 2020 11:57:37 +0100 Subject: [PATCH 02/14] support for external webhooks --- .../components/settings/tabs/APIKeys.svelte | 12 ++++++++- packages/server/src/middleware/authorized.js | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/settings/tabs/APIKeys.svelte b/packages/builder/src/components/settings/tabs/APIKeys.svelte index 8a3c9fc710..8e569596a4 100644 --- a/packages/builder/src/components/settings/tabs/APIKeys.svelte +++ b/packages/builder/src/components/settings/tabs/APIKeys.svelte @@ -1,6 +1,7 @@ + {#each urls as url} + {/each} diff --git a/packages/builder/src/components/userInterface/temporaryPanelStructure.js b/packages/builder/src/components/userInterface/temporaryPanelStructure.js index 3f853efcfb..b628e86e2e 100644 --- a/packages/builder/src/components/userInterface/temporaryPanelStructure.js +++ b/packages/builder/src/components/userInterface/temporaryPanelStructure.js @@ -356,7 +356,7 @@ export default { { label: "destinationUrl", key: "destinationUrl", - control: Input, + control: ScreenSelect, placeholder: "/table/_id", }, ], @@ -405,7 +405,7 @@ export default { { label: "Link Url", key: "linkUrl", - control: Input, + control: ScreenSelect, placeholder: "Link URL", }, { @@ -480,7 +480,7 @@ export default { { label: "Link Url", key: "linkUrl", - control: Input, + control: ScreenSelect, placeholder: "Link URL", }, { From f03e314710bc9b44b67ef59d40101efe33edb9d3 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 12 Oct 2020 15:32:49 +0100 Subject: [PATCH 07/14] fix: view filter displaying incorrect options --- .../components/backend/DataTable/popovers/FilterPopover.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/components/backend/DataTable/popovers/FilterPopover.svelte b/packages/builder/src/components/backend/DataTable/popovers/FilterPopover.svelte index 3d6241f0ab..0a76ee1efa 100644 --- a/packages/builder/src/components/backend/DataTable/popovers/FilterPopover.svelte +++ b/packages/builder/src/components/backend/DataTable/popovers/FilterPopover.svelte @@ -79,7 +79,7 @@ } function fieldOptions(field) { - return viewModel.schema[field].type === "string" + return viewModel.schema[field].type === "options" ? viewModel.schema[field].constraints.inclusion : [true, false] } From a1fb9aea6b3066cb32cf79f7d36a42caacaf6795 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 12 Oct 2020 15:51:03 +0100 Subject: [PATCH 08/14] Analytics - record screen template used --- .../src/components/userInterface/NewScreenModal.svelte | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/userInterface/NewScreenModal.svelte b/packages/builder/src/components/userInterface/NewScreenModal.svelte index 0a9bc81987..0fb31edcd3 100644 --- a/packages/builder/src/components/userInterface/NewScreenModal.svelte +++ b/packages/builder/src/components/userInterface/NewScreenModal.svelte @@ -3,6 +3,7 @@ import { Input, Button, Spacer, Select, ModalContent } from "@budibase/bbui" import getTemplates from "builderStore/store/screenTemplates" import { some } from "lodash/fp" + import analytics from "analytics" const CONTAINER = "@budibase/standard-components/container" @@ -29,7 +30,7 @@ const templateChanged = newTemplateIndex => { if (newTemplateIndex === undefined) return - + const template = templates[newTemplateIndex] draftScreen = templates[newTemplateIndex].create() if (draftScreen.props._instanceName) { name = draftScreen.props._instanceName @@ -63,6 +64,13 @@ store.createScreen(draftScreen) + if (templateIndex !== undefined) { + const template = templates[templateIndex] + analytics.captureEvent("Screen Created", { + template: template.id || template.name, + }) + } + finished() } From 45e583dc95af027b7fc75a1be7f63cf244040dd4 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 12 Oct 2020 16:26:54 +0100 Subject: [PATCH 09/14] unused "options" member in prop types --- .../builder/src/components/userInterface/pagesParsing/types.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/builder/src/components/userInterface/pagesParsing/types.js b/packages/builder/src/components/userInterface/pagesParsing/types.js index b5f073e689..b8867cc8d8 100644 --- a/packages/builder/src/components/userInterface/pagesParsing/types.js +++ b/packages/builder/src/components/userInterface/pagesParsing/types.js @@ -10,7 +10,6 @@ export const TYPE_MAP = { }, options: { default: [], - options: [], }, event: { default: [], From 8956c7d9c9bf3659396332fb532b963a9e5afb6f Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 12 Oct 2020 16:27:34 +0100 Subject: [PATCH 10/14] fix: Default props were mutated - cause very strange issue with event handlers --- .../components/userInterface/pagesParsing/createProps.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/components/userInterface/pagesParsing/createProps.js b/packages/builder/src/components/userInterface/pagesParsing/createProps.js index 44d23c5c3e..b628b6e15b 100644 --- a/packages/builder/src/components/userInterface/pagesParsing/createProps.js +++ b/packages/builder/src/components/userInterface/pagesParsing/createProps.js @@ -1,4 +1,4 @@ -import { isString, isUndefined } from "lodash/fp" +import { isString, isUndefined, cloneDeep } from "lodash/fp" import { TYPE_MAP } from "./types" import { assign } from "lodash" import { uuid } from "builderStore/uuid" @@ -83,13 +83,13 @@ const parsePropDef = propDef => { if (isString(propDef)) { if (!TYPE_MAP[propDef]) return error(`Type ${propDef} is not recognised.`) - return TYPE_MAP[propDef].default + return cloneDeep(TYPE_MAP[propDef].default) } const type = TYPE_MAP[propDef.type] if (!type) return error(`Type ${propDef.type} is not recognised.`) - return propDef.default + return cloneDeep(propDef.default) } export const arrayElementComponentName = (parentComponentName, arrayPropName) => From 1e14da71087e1592c72fbcc90ca4990167a9e300 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 12 Oct 2020 16:28:37 +0100 Subject: [PATCH 11/14] code reivew - unused code --- .../builder/src/components/userInterface/ScreenSelect.svelte | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/builder/src/components/userInterface/ScreenSelect.svelte b/packages/builder/src/components/userInterface/ScreenSelect.svelte index baff9a782b..8bc22e95a4 100644 --- a/packages/builder/src/components/userInterface/ScreenSelect.svelte +++ b/packages/builder/src/components/userInterface/ScreenSelect.svelte @@ -33,11 +33,6 @@ models: $backendUiStore.models, }) - const idBindingForModel = modelId => { - for (let bindableProp of bindableProperties) { - } - } - const detailScreens = $store.screens.filter(screen => screen.props._component.endsWith("/rowdetail") ) From 9bf568ecb13d50ae75455f3566963203477c1396 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 12 Oct 2020 16:37:08 +0100 Subject: [PATCH 12/14] Fixes for deleting records when a table is deleted. --- packages/server/src/api/controllers/model.js | 8 ++++---- packages/server/src/db/utils.js | 21 ++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/server/src/api/controllers/model.js b/packages/server/src/api/controllers/model.js index 87a2449f76..74e3494eba 100644 --- a/packages/server/src/api/controllers/model.js +++ b/packages/server/src/api/controllers/model.js @@ -105,11 +105,8 @@ exports.save = async function(ctx) { exports.destroy = async function(ctx) { const instanceId = ctx.user.instanceId const db = new CouchDB(instanceId) - const modelToDelete = await db.get(ctx.params.modelId) - await db.remove(modelToDelete) - // Delete all records for that model const records = await db.allDocs( getRecordParams(ctx.params.modelId, null, { @@ -117,7 +114,7 @@ exports.destroy = async function(ctx) { }) ) await db.bulkDocs( - records.rows.map(record => ({ _id: record.id, _deleted: true })) + records.rows.map(record => ({ ...record.doc, _deleted: true })) ) // update linked records @@ -127,6 +124,9 @@ exports.destroy = async function(ctx) { model: modelToDelete, }) + // don't remove the table itself until very end + await db.remove(modelToDelete) + ctx.eventEmitter && ctx.eventEmitter.emitModel(`model:delete`, instanceId, modelToDelete) ctx.status = 200 diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index a167966a4e..56572e1170 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -57,21 +57,26 @@ exports.generateModelID = () => { /** * Gets the DB allDocs/query params for retrieving a record. - * @param {string} modelId The model in which the records have been stored. + * @param {string|null} modelId The model in which the records have been stored. * @param {string|null} recordId The ID of the record which is being specifically queried for. This can be * left null to get all the records in the model. * @param {object} otherProps Any other properties to add to the request. * @returns {object} Parameters which can then be used with an allDocs request. */ -exports.getRecordParams = (modelId, recordId = null, otherProps = {}) => { +exports.getRecordParams = ( + modelId = null, + recordId = null, + otherProps = {} +) => { if (modelId == null) { - throw "Cannot build params for records without a model ID" + return getDocParams(DocumentTypes.RECORD, null, otherProps) + } else { + const endOfKey = + recordId == null + ? `${modelId}${SEPARATOR}` + : `${modelId}${SEPARATOR}${recordId}` + return getDocParams(DocumentTypes.RECORD, endOfKey, otherProps) } - const endOfKey = - recordId == null - ? `${modelId}${SEPARATOR}` - : `${modelId}${SEPARATOR}${recordId}` - return getDocParams(DocumentTypes.RECORD, endOfKey, otherProps) } /** From 47b97225a8d1bfe8a2f0166333d2614929b06c44 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 12 Oct 2020 16:51:41 +0100 Subject: [PATCH 13/14] Improving consistency of model saving, making sure that any validation which could fail happens before any updates are carried out. --- packages/server/src/api/controllers/model.js | 17 ++++++++++------- .../src/db/linkedRecords/LinkController.js | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/model.js b/packages/server/src/api/controllers/model.js index 74e3494eba..d9277c2f3d 100644 --- a/packages/server/src/api/controllers/model.js +++ b/packages/server/src/api/controllers/model.js @@ -33,6 +33,7 @@ exports.save = async function(ctx) { views: {}, ...rest, } + let renameDocs = [] // if the model obj had an _id then it will have been retrieved const oldModel = ctx.preExisting @@ -49,14 +50,11 @@ exports.save = async function(ctx) { include_docs: true, }) ) - - const docs = records.rows.map(({ doc }) => { + renameDocs = records.rows.map(({ doc }) => { doc[_rename.updated] = doc[_rename.old] delete doc[_rename.old] return doc }) - - await db.bulkDocs(docs) delete modelToSave._rename } @@ -69,9 +67,6 @@ exports.save = async function(ctx) { modelView.schema = modelToSave.schema } - const result = await db.post(modelToSave) - modelToSave._rev = result.rev - // update linked records await linkRecords.updateLinks({ instanceId, @@ -82,6 +77,14 @@ exports.save = async function(ctx) { oldModel: oldModel, }) + // don't perform any updates until relationships have been + // checked by the updateLinks function + if (renameDocs.length !== 0) { + await db.bulkDocs(renameDocs) + } + const result = await db.post(modelToSave) + modelToSave._rev = result.rev + ctx.eventEmitter && ctx.eventEmitter.emitModel(`model:save`, instanceId, modelToSave) diff --git a/packages/server/src/db/linkedRecords/LinkController.js b/packages/server/src/db/linkedRecords/LinkController.js index b006e798c2..fc28320d70 100644 --- a/packages/server/src/db/linkedRecords/LinkController.js +++ b/packages/server/src/db/linkedRecords/LinkController.js @@ -161,7 +161,7 @@ class LinkController { }) // now add the docs to be deleted to the bulk operation operations.push(...toDeleteDocs) - // replace this field with a simple entry to denote there are links + // remove the field from this row, link doc will be added to record on way out delete record[fieldName] } } From 1955c736851683a40c54ca38446f4c3c7265a0f0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 12 Oct 2020 17:02:52 +0100 Subject: [PATCH 14/14] Handling empty relationship column the same way other columns are handled, it won't do anything until it is valid - but doesn't error. --- .../src/db/linkedRecords/LinkController.js | 10 +++++++++- packages/server/src/db/linkedRecords/index.js | 17 +++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/server/src/db/linkedRecords/LinkController.js b/packages/server/src/db/linkedRecords/LinkController.js index fc28320d70..48e459a1cb 100644 --- a/packages/server/src/db/linkedRecords/LinkController.js +++ b/packages/server/src/db/linkedRecords/LinkController.js @@ -234,8 +234,16 @@ class LinkController { for (let fieldName of Object.keys(schema)) { const field = schema[fieldName] if (field.type === "link") { + // handle this in a separate try catch, want + // the put to bubble up as an error, if can't update + // table for some reason + let linkedModel + try { + linkedModel = await this._db.get(field.modelId) + } catch (err) { + continue + } // create the link field in the other model - const linkedModel = await this._db.get(field.modelId) linkedModel.schema[field.fieldName] = { name: field.fieldName, type: "link", diff --git a/packages/server/src/db/linkedRecords/index.js b/packages/server/src/db/linkedRecords/index.js index ba93f3d2e8..09a58c1062 100644 --- a/packages/server/src/db/linkedRecords/index.js +++ b/packages/server/src/db/linkedRecords/index.js @@ -42,6 +42,7 @@ exports.updateLinks = async function({ model, oldModel, }) { + const baseReturnObj = record == null ? model : record if (instanceId == null) { throw "Cannot operate without an instance ID." } @@ -50,12 +51,16 @@ exports.updateLinks = async function({ arguments[0].modelId = model._id } let linkController = new LinkController(arguments[0]) - if ( - !(await linkController.doesModelHaveLinkedFields()) && - (oldModel == null || - !(await linkController.doesModelHaveLinkedFields(oldModel))) - ) { - return record + try { + if ( + !(await linkController.doesModelHaveLinkedFields()) && + (oldModel == null || + !(await linkController.doesModelHaveLinkedFields(oldModel))) + ) { + return baseReturnObj + } + } catch (err) { + return baseReturnObj } switch (eventType) { case EventType.RECORD_SAVE: