#7 'Single record' type initialise
This commit is contained in:
parent
01180d7718
commit
be90c27625
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
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,
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
Loading…
Reference in New Issue