data model rewrite

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

View File

@ -29,19 +29,24 @@
"builderStore(.*)$": "<rootDir>/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": [
"<rootDir>/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"
}

View File

@ -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 () => {

View File

@ -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"

View File

@ -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", {

View File

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

View File

@ -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)
}