From be90c276250bd4782929af3abad003d6fa3b9578 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Sun, 3 Nov 2019 11:26:21 +0000 Subject: [PATCH] #7 'Single record' type initialise --- .../core/src/appInitialise/initialiseData.js | 31 ++++++++++--------- packages/core/src/recordApi/getNew.js | 10 ++++-- packages/core/src/recordApi/save.js | 17 +++++----- packages/core/test/initialiseData.spec.js | 8 +++++ packages/core/test/recordApi.customId.spec.js | 4 +-- packages/core/test/recordApi.spec.js | 10 +++++- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/packages/core/src/appInitialise/initialiseData.js b/packages/core/src/appInitialise/initialiseData.js index c2824f4eda..20ce5eabf5 100644 --- a/packages/core/src/appInitialise/initialiseData.js +++ b/packages/core/src/appInitialise/initialiseData.js @@ -5,7 +5,8 @@ import { AUTH_FOLDER, USERS_LIST_FILE, ACCESS_LEVELS_FILE } from '../authApi/aut import { initialiseRootCollections } from '../collectionApi/initialise'; import { initialiseIndex } from '../indexing/initialiseIndex'; import { getFlattenedHierarchy, isGlobalIndex, isSingleRecord } from '../templateApi/hierarchy'; - +import { _getNew } from "../recordApi/getNew"; +import { _save } from "../recordApi/save"; export const initialiseData = async (datastore, applicationDefinition, accessLevels) => { await datastore.createFolder(configFolder); @@ -14,8 +15,6 @@ export const initialiseData = async (datastore, applicationDefinition, accessLev await initialiseRootCollections(datastore, applicationDefinition.hierarchy); await initialiseRootIndexes(datastore, applicationDefinition.hierarchy); - await initialiseRootSingleRecords(datastore, applicationDefinition.hierarchy); - await datastore.createFolder(TRANSACTIONS_FOLDER); await datastore.createFolder(AUTH_FOLDER); @@ -25,6 +24,8 @@ export const initialiseData = async (datastore, applicationDefinition, accessLev await datastore.createJson( ACCESS_LEVELS_FILE, accessLevels ? accessLevels : { version: 0, levels: [] }); + + await initialiseRootSingleRecords(datastore, applicationDefinition.hierarchy); }; const initialiseRootIndexes = async (datastore, hierarchy) => { @@ -38,20 +39,20 @@ const initialiseRootIndexes = async (datastore, hierarchy) => { } }; -const initialiseRootSingleRecords = async (datastore, hierachy) => { - const flathierarchy = getFlattenedHierarchy(hierachy); +const initialiseRootSingleRecords = async (datastore, hierarchy) => { + const app = { + publish:()=>{}, + cleanupTransactions: () => {}, + datastore, hierarchy + }; + + const flathierarchy = getFlattenedHierarchy(hierarchy); const singleRecords = $(flathierarchy, [ filter(isSingleRecord), ]); - /* for (let record of singleRecords) { - const result = getNew({ datastore: datastore, hierarchy: appDefinition.hierarchy }) - (record.nodeKey(), - record.name - ); - - _save({ datastore: datastore, hierarchy: appDefinition.hierarchy }, - result - ); - } */ + for (let record of singleRecords) { + const result = _getNew(record, ""); + await _save(app,result); + } }; diff --git a/packages/core/src/recordApi/getNew.js b/packages/core/src/recordApi/getNew.js index b6c8615b84..dffef119ca 100644 --- a/packages/core/src/recordApi/getNew.js +++ b/packages/core/src/recordApi/getNew.js @@ -2,7 +2,9 @@ import { keyBy, mapValues, } from 'lodash/fp'; import { generate } from 'shortid'; -import { getNodeForCollectionPath } from '../templateApi/hierarchy'; +import { + getNodeForCollectionPath, isSingleRecord +} from '../templateApi/hierarchy'; import { getNewFieldValue } from '../types'; import { $, joinKey, safeKey, apiWrapperSync, events, @@ -21,7 +23,7 @@ export const getNew = app => (collectionKey, recordTypeName) => { ); }; -const _getNew = (recordNode, collectionKey) => constructRecord(recordNode, getNewFieldValue, collectionKey); +export const _getNew = (recordNode, collectionKey) => constructRecord(recordNode, getNewFieldValue, collectionKey); const getRecordNode = (app, collectionKey) => { collectionKey = safeKey(collectionKey); @@ -38,7 +40,9 @@ export const constructRecord = (recordNode, getFieldValue, collectionKey) => { ]); record.id = `${recordNode.nodeId}-${generate()}`; - record.key = joinKey(collectionKey, record.id); + record.key = isSingleRecord(recordNode) + ? joinKey(collectionKey, recordNode.name) + : joinKey(collectionKey, record.id); record.isNew = true; record.type = recordNode.name; return record; diff --git a/packages/core/src/recordApi/save.js b/packages/core/src/recordApi/save.js index 8c21999d4e..c62689a9df 100644 --- a/packages/core/src/recordApi/save.js +++ b/packages/core/src/recordApi/save.js @@ -12,15 +12,10 @@ import { apiWrapper, events, $, joinKey, } from '../common'; import { - getFlattenedHierarchy, - getExactNodeForPath, - isRecord, - getNode, - getLastPartInKey, + getFlattenedHierarchy, getExactNodeForPath, + isRecord, getNode, isSingleRecord, fieldReversesReferenceToNode, } from '../templateApi/hierarchy'; -import { mapRecord } from '../indexing/evaluate'; -import { listItems } from '../indexApi/listItems'; import { addToAllIds } from '../indexing/allIds'; import { transactionForCreateRecord, @@ -52,7 +47,13 @@ export const _save = async (app, record, context, skipValidation = false) => { } if (recordClone.isNew) { - await addToAllIds(app.hierarchy, app.datastore)(recordClone); + const recordNode = getExactNodeForPath(app.hierarchy)(record.key); + if(!recordNode) + throw new Error("Cannot find node for " + record.key); + + if(!isSingleRecord(recordNode)) + await addToAllIds(app.hierarchy, app.datastore)(recordClone); + const transaction = await transactionForCreateRecord( app, recordClone, ); diff --git a/packages/core/test/initialiseData.spec.js b/packages/core/test/initialiseData.spec.js index 5ab440960e..edf38e9092 100644 --- a/packages/core/test/initialiseData.spec.js +++ b/packages/core/test/initialiseData.spec.js @@ -65,6 +65,14 @@ describe("initialiseData", () => { expect(levels.levels[0].name).toBe("owner"); }); + it("should initialise 'single record' type nodes", async () => { + const {appDef, datastore} = getApplicationDefinition(); + await initialiseData(datastore, appDef); + expect(await datastore.exists(`/settings`)).toBeTruthy(); + const settings = await datastore.loadJson("/settings/record.json"); + expect(settings.type).toBe("settings"); + }); + const getApplicationDefinition = () => { const {templateApi, app} = getMemoryTemplateApi(); const h = basicAppHierarchyCreator_WithFields_AndIndexes(templateApi); diff --git a/packages/core/test/recordApi.customId.spec.js b/packages/core/test/recordApi.customId.spec.js index 837c64c394..93832f0f25 100644 --- a/packages/core/test/recordApi.customId.spec.js +++ b/packages/core/test/recordApi.customId.spec.js @@ -1,6 +1,4 @@ -import {setupApphierarchy, basicAppHierarchyCreator_WithFields, - getNewFieldAndAdd, stubEventHandler} from "./specHelpers"; -import { iterateIndex } from "../src/indexing/read"; +import {setupApphierarchy, basicAppHierarchyCreator_WithFields} from "./specHelpers"; describe("get customId", () => { it("should generate an id with given value", async () => { diff --git a/packages/core/test/recordApi.spec.js b/packages/core/test/recordApi.spec.js index 364410aa74..c5425a2030 100644 --- a/packages/core/test/recordApi.spec.js +++ b/packages/core/test/recordApi.spec.js @@ -3,6 +3,7 @@ import {setupApphierarchy, basicAppHierarchyCreator_WithFields, import {events, isNonEmptyString} from "../src/common"; import { isBoolean } from "util"; import {permission} from "../src/authApi/permissions"; +import { _getNew } from "../src/recordApi/getNew"; describe("recordApi > getNew", () => { @@ -32,7 +33,7 @@ describe("recordApi > getNew", () => { it("should create object with all declared fields, and use inital values", async () => { const {recordApi} = await setupApphierarchy(templateApi => { const hierarchy = basicAppHierarchyCreator_WithFields(templateApi); - const {root, customerRecord} = hierarchy; + const {customerRecord} = hierarchy; customerRecord.fields = []; @@ -80,6 +81,13 @@ describe("recordApi > getNew", () => { app.withOnlyThisPermission(permission.createRecord.get(appHierarchy.customerRecord.nodeKey())); recordApi.getNew("/customers", "customer"); }); + + it("for 'single record' type, should create with key ending in node name", async () => { + const {appHierarchy} = await setupApphierarchy(basicAppHierarchyCreator_WithFields); + const {settingsRecord} = appHierarchy; + const result = _getNew(settingsRecord, ""); + expect(result.key).toBe("/settings") + }) }); describe('recordApi > save then load', () => {