data model rewrite

This commit is contained in:
Martin McKeaveney 2020-06-25 22:34:38 +01:00
parent 66aa2008c9
commit 6ef4ba086c
6 changed files with 53 additions and 12 deletions

View File

@ -29,19 +29,24 @@
"builderStore(.*)$": "<rootDir>/src/builderStore$1" "builderStore(.*)$": "<rootDir>/src/builderStore$1"
}, },
"moduleFileExtensions": [ "moduleFileExtensions": [
"js" "js",
"svelte"
], ],
"moduleDirectories": [ "moduleDirectories": [
"node_modules" "node_modules"
], ],
"transform": { "transform": {
"^.+js$": "babel-jest" "^.+js$": "babel-jest",
"^.+.svelte$": "svelte-jester"
}, },
"transformIgnorePatterns": [ "transformIgnorePatterns": [
"/node_modules/(?!svelte).+\\.js$" "/node_modules/(?!svelte).+\\.js$"
], ],
"modulePathIgnorePatterns": [ "modulePathIgnorePatterns": [
"<rootDir>/cypress/" "<rootDir>/cypress/"
],
"setupFilesAfterEnv": [
"@testing-library/jest-dom/extend-expect"
] ]
}, },
"eslintConfig": { "eslintConfig": {
@ -76,6 +81,8 @@
"@rollup/plugin-alias": "^3.0.1", "@rollup/plugin-alias": "^3.0.1",
"@rollup/plugin-json": "^4.0.3", "@rollup/plugin-json": "^4.0.3",
"@sveltech/routify": "1.7.11", "@sveltech/routify": "1.7.11",
"@testing-library/jest-dom": "^5.11.0",
"@testing-library/svelte": "^3.0.0",
"babel-jest": "^24.8.0", "babel-jest": "^24.8.0",
"browser-sync": "^2.26.7", "browser-sync": "^2.26.7",
"cypress": "^4.8.0", "cypress": "^4.8.0",
@ -97,7 +104,8 @@
"rollup-plugin-terser": "^4.0.4", "rollup-plugin-terser": "^4.0.4",
"rollup-plugin-url": "^2.2.2", "rollup-plugin-url": "^2.2.2",
"start-server-and-test": "^1.11.0", "start-server-and-test": "^1.11.0",
"svelte": "3.23.x" "svelte": "3.23.x",
"svelte-jester": "^1.0.6"
}, },
"gitHead": "115189f72a850bfb52b65ec61d932531bf327072" "gitHead": "115189f72a850bfb52b65ec61d932531bf327072"
} }

View File

@ -8,7 +8,9 @@
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
function showErrorBanner() { 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 () => { onMount(async () => {

View File

@ -1,7 +1,7 @@
import { writable } from "svelte/store" import { writable } from "svelte/store"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { uuid } from "builderStore/uuid"
import api from "../api" import api from "../api"
import { update } from "lodash"
export const getBackendUiStore = () => { export const getBackendUiStore = () => {
const INITIAL_BACKEND_UI_STATE = { const INITIAL_BACKEND_UI_STATE = {
@ -68,11 +68,25 @@ export const getBackendUiStore = () => {
return state return state
}), }),
save: async ({ model }) => { 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 SAVE_MODEL_URL = `/api/models`
const response = await api.post(SAVE_MODEL_URL, model) console.log(updatedModel);
const json = await response.json() const response = await api.post(SAVE_MODEL_URL, updatedModel)
const savedModel = await response.json()
await store.actions.models.fetch() await store.actions.models.fetch()
store.actions.models.select(json) store.actions.models.select(savedModel)
}, },
addField: field => { addField: field => {
store.update(state => { store.update(state => {
@ -82,7 +96,7 @@ export const getBackendUiStore = () => {
state.draftModel.schema = { state.draftModel.schema = {
...state.draftModel.schema, ...state.draftModel.schema,
[field.name]: field, [field.name]: cloneDeep(field),
} }
state.selectedField = field.name state.selectedField = field.name
state.tabs.NAVIGATION_PANEL = "NAVIGATE" state.tabs.NAVIGATION_PANEL = "NAVIGATE"

View File

@ -11,7 +11,7 @@
let open = false let open = false
let model let model
$: FIELDS_TO_HIDE = [$backendUiStore.selectedModel._id, field.modelId] $: FIELDS_TO_HIDE = [$backendUiStore.selectedModel.name]
async function fetchRecords() { async function fetchRecords() {
const response = await api.post("/api/records/search", { const response = await api.post("/api/records/search", {

View File

@ -16,7 +16,7 @@
backendUiStore.actions.models.save({ backendUiStore.actions.models.save({
model: { model: {
...model, ...model,
...rest ...rest,
}, },
}) })
notifier.success(`${model.name} model created.`) notifier.success(`${model.name} model created.`)

View File

@ -1,5 +1,6 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const newid = require("../../db/newid") const newid = require("../../db/newid")
const { rename } = require("fs-extra")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.instanceId)
@ -24,6 +25,22 @@ exports.save = async function(ctx) {
...ctx.request.body, ...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) const result = await db.post(modelToSave)
modelToSave._rev = result.rev modelToSave._rev = result.rev
@ -39,7 +56,7 @@ exports.save = async function(ctx) {
modelId: modelToSave._id, modelId: modelToSave._id,
constraints: { constraints: {
type: "array", type: "array",
}, }
} }
await db.put(linkedModel) await db.put(linkedModel)
} }