From 6ef4ba086ccfb86d7123b1c45c780f20f8f7d875 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 25 Jun 2020 22:34:38 +0100 Subject: [PATCH] data model rewrite --- packages/builder/package.json | 14 ++++++++--- packages/builder/src/App.svelte | 4 +++- .../builder/src/builderStore/store/backend.js | 24 +++++++++++++++---- .../ModelDataTable/LinkedRecord.svelte | 2 +- .../nav/ModelNavigator/EmptyModel.svelte | 2 +- packages/server/src/api/controllers/model.js | 19 ++++++++++++++- 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/packages/builder/package.json b/packages/builder/package.json index 26d6ee3e61..dd449b2256 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -29,19 +29,24 @@ "builderStore(.*)$": "/src/builderStore$1" }, "moduleFileExtensions": [ - "js" + "js", + "svelte" ], "moduleDirectories": [ "node_modules" ], "transform": { - "^.+js$": "babel-jest" + "^.+js$": "babel-jest", + "^.+.svelte$": "svelte-jester" }, "transformIgnorePatterns": [ "/node_modules/(?!svelte).+\\.js$" ], "modulePathIgnorePatterns": [ "/cypress/" + ], + "setupFilesAfterEnv": [ + "@testing-library/jest-dom/extend-expect" ] }, "eslintConfig": { @@ -76,6 +81,8 @@ "@rollup/plugin-alias": "^3.0.1", "@rollup/plugin-json": "^4.0.3", "@sveltech/routify": "1.7.11", + "@testing-library/jest-dom": "^5.11.0", + "@testing-library/svelte": "^3.0.0", "babel-jest": "^24.8.0", "browser-sync": "^2.26.7", "cypress": "^4.8.0", @@ -97,7 +104,8 @@ "rollup-plugin-terser": "^4.0.4", "rollup-plugin-url": "^2.2.2", "start-server-and-test": "^1.11.0", - "svelte": "3.23.x" + "svelte": "3.23.x", + "svelte-jester": "^1.0.6" }, "gitHead": "115189f72a850bfb52b65ec61d932531bf327072" } diff --git a/packages/builder/src/App.svelte b/packages/builder/src/App.svelte index 163a78ac73..b4ab4b7791 100644 --- a/packages/builder/src/App.svelte +++ b/packages/builder/src/App.svelte @@ -8,7 +8,9 @@ import { notifier } from "builderStore/store/notifications" function showErrorBanner() { - notifier.danger("Whoops! Looks like we're having trouble. Please refresh the page.") + notifier.danger( + "Whoops! Looks like we're having trouble. Please refresh the page." + ) } onMount(async () => { diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index ecb2b6550e..4015cd6279 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -1,7 +1,7 @@ import { writable } from "svelte/store" import { cloneDeep } from "lodash/fp" -import { uuid } from "builderStore/uuid" import api from "../api" +import { update } from "lodash" export const getBackendUiStore = () => { const INITIAL_BACKEND_UI_STATE = { @@ -68,11 +68,25 @@ export const getBackendUiStore = () => { return state }), save: async ({ model }) => { + const updatedModel = cloneDeep(model) + + // update any renamed schema keys to reflect their names + for (let key in updatedModel.schema) { + const field = updatedModel.schema[key] + // field has been renamed + if (field.name && field.name !== key) { + updatedModel.schema[field.name] = field + updatedModel._rename = { old: key, updated: field.name } + delete updatedModel.schema[key] + } + } + const SAVE_MODEL_URL = `/api/models` - const response = await api.post(SAVE_MODEL_URL, model) - const json = await response.json() + console.log(updatedModel); + const response = await api.post(SAVE_MODEL_URL, updatedModel) + const savedModel = await response.json() await store.actions.models.fetch() - store.actions.models.select(json) + store.actions.models.select(savedModel) }, addField: field => { store.update(state => { @@ -82,7 +96,7 @@ export const getBackendUiStore = () => { state.draftModel.schema = { ...state.draftModel.schema, - [field.name]: field, + [field.name]: cloneDeep(field), } state.selectedField = field.name state.tabs.NAVIGATION_PANEL = "NAVIGATE" diff --git a/packages/builder/src/components/database/ModelDataTable/LinkedRecord.svelte b/packages/builder/src/components/database/ModelDataTable/LinkedRecord.svelte index dde2c75bec..0cbf9ab307 100644 --- a/packages/builder/src/components/database/ModelDataTable/LinkedRecord.svelte +++ b/packages/builder/src/components/database/ModelDataTable/LinkedRecord.svelte @@ -11,7 +11,7 @@ let open = false let model - $: FIELDS_TO_HIDE = [$backendUiStore.selectedModel._id, field.modelId] + $: FIELDS_TO_HIDE = [$backendUiStore.selectedModel.name] async function fetchRecords() { const response = await api.post("/api/records/search", { diff --git a/packages/builder/src/components/nav/ModelNavigator/EmptyModel.svelte b/packages/builder/src/components/nav/ModelNavigator/EmptyModel.svelte index 436f3e8eef..da01963937 100644 --- a/packages/builder/src/components/nav/ModelNavigator/EmptyModel.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/EmptyModel.svelte @@ -16,7 +16,7 @@ backendUiStore.actions.models.save({ model: { ...model, - ...rest + ...rest, }, }) notifier.success(`${model.name} model created.`) diff --git a/packages/server/src/api/controllers/model.js b/packages/server/src/api/controllers/model.js index 79c285d784..7afc422be5 100644 --- a/packages/server/src/api/controllers/model.js +++ b/packages/server/src/api/controllers/model.js @@ -1,5 +1,6 @@ const CouchDB = require("../../db") const newid = require("../../db/newid") +const { rename } = require("fs-extra") exports.fetch = async function(ctx) { const db = new CouchDB(ctx.user.instanceId) @@ -24,6 +25,22 @@ exports.save = async function(ctx) { ...ctx.request.body, } + // update renamed record fields when model is updated + const { _rename } = modelToSave + if (_rename) { + const records = await db.query(`database/all_${modelToSave._id}`, { + include_docs: true + }); + const docs = records.rows.map(({ doc }) => { + doc[_rename.updated] = doc[_rename.old] + delete doc[_rename.old] + return doc + }) + + await db.bulkDocs(docs) + delete modelToSave._rename + } + const result = await db.post(modelToSave) modelToSave._rev = result.rev @@ -39,7 +56,7 @@ exports.save = async function(ctx) { modelId: modelToSave._id, constraints: { type: "array", - }, + } } await db.put(linkedModel) }