diff --git a/packages/core/src/recordApi/initialiseChildren.js b/packages/core/src/recordApi/initialiseChildren.js index bc8edc8637..a674968f33 100644 --- a/packages/core/src/recordApi/initialiseChildren.js +++ b/packages/core/src/recordApi/initialiseChildren.js @@ -1,7 +1,7 @@ import { isString, flatten, map, filter } from "lodash/fp" import { initialiseChildCollections } from "../collectionApi/initialise" import { _loadFromInfo } from "./load" -import { $ } from "../common" +import { $, joinKey } from "../common" import { getFlattenedHierarchy, isRecord, @@ -11,6 +11,7 @@ import { } from "../templateApi/hierarchy" import { initialiseIndex } from "../indexing/initialiseIndex" import { getRecordInfo } from "./recordInfo" +import { getAllIdsIterator } from "../indexing/allIds" export const initialiseChildren = async (app, recordInfoOrKey) => { const recordInfo = isString(recordInfoOrKey) @@ -29,7 +30,7 @@ export const initialiseChildrenForNode = async (app, recordNode) => { return } - const iterate = await getAllIdsIterator(app)(recordNode.parent().nodeKey()) + const iterate = await getAllIdsIterator(app)(recordNode.parent().collectionNodeKey()) let iterateResult = await iterate() while (!iterateResult.done) { const { result } = iterateResult diff --git a/packages/core/src/templateApi/upgradeData.js b/packages/core/src/templateApi/upgradeData.js index bce39f5381..aaa244818e 100644 --- a/packages/core/src/templateApi/upgradeData.js +++ b/packages/core/src/templateApi/upgradeData.js @@ -193,5 +193,5 @@ const runInitialiseRoot = async (_, newApp) => { } const runInitialiseChildRecord = async (_, newApp, diff) => { - await initialiseChildrenForNode(newApp.datastore, diff.newNode) + await initialiseChildrenForNode(newApp, diff.newNode) } \ No newline at end of file diff --git a/packages/core/test/templateApi.upgradeData.spec.js b/packages/core/test/templateApi.upgradeData.spec.js index 2cbc7ffcb5..945c5e0f43 100644 --- a/packages/core/test/templateApi.upgradeData.spec.js +++ b/packages/core/test/templateApi.upgradeData.spec.js @@ -8,6 +8,7 @@ import { $, splitKey } from "../src/common" import { keys, filter } from "lodash/fp" import { _listItems } from "../src/indexApi/listItems" import { _save } from "../src/recordApi/save" +import { _getNew } from "../src/recordApi/getNew" describe("upgradeData", () => { @@ -191,6 +192,52 @@ describe("upgradeData", () => { }) + it("should initialise a new root record", async () => { + const { oldSetup, newSetup } = await configure() + const invoice = newSetup.templateApi.getNewRecordTemplate(newSetup.root, "invoice", true) + invoice.collectionName = "invoices" + + const nameField = newSetup.templateApi.getNewField("string") + nameField.name = "name" + newSetup.templateApi.addField(invoice, nameField) + + await upgradeData(oldSetup.app)(newSetup.root) + + let itemsInNewIndex = await _listItems(newSetup.app, "/invoice_index") + + expect(itemsInNewIndex.length).toBe(0) + + const newInvoice = _getNew(invoice, "/invoices") + await _save(newSetup.app, newInvoice) + + itemsInNewIndex = await _listItems(newSetup.app, "/invoice_index") + + expect(itemsInNewIndex.length).toBe(1) + }) + + it("should initialise a new child record", async () => { + const { oldSetup, newSetup, records } = await configure() + const invoice = newSetup.templateApi.getNewRecordTemplate(newSetup.contact, "invoice", true) + invoice.collectionName = "invoices" + + const nameField = newSetup.templateApi.getNewField("string") + nameField.name = "name" + newSetup.templateApi.addField(invoice, nameField) + + await upgradeData(oldSetup.app)(newSetup.root) + + let itemsInNewIndex = await _listItems(newSetup.app, `${records.contact1.key}/invoice_index`) + + expect(itemsInNewIndex.length).toBe(0) + + const newInvoice = _getNew(invoice, `${records.contact1.key}/invoices`) + await _save(newSetup.app, newInvoice) + + itemsInNewIndex = await _listItems(newSetup.app, `${records.contact1.key}/invoice_index`) + + expect(itemsInNewIndex.length).toBe(1) + }) + }) const configure = async () => {