diff --git a/packages/core/src/__tests__/memdb.test.ts b/packages/core/src/__tests__/memdb.test.ts index a5d33c1628..063498c6d3 100644 --- a/packages/core/src/__tests__/memdb.test.ts +++ b/packages/core/src/__tests__/memdb.test.ts @@ -370,4 +370,27 @@ describe('memdb', () => { expect(attached).toBeDefined() expect(Hierarchy.mixinOrClass(attached as Doc)).toEqual(test.mixin.TaskMixinTodos) }) + + it('createDoc for AttachedDoc', async () => { + expect.assertions(1) + const { model } = await createModel() + + const client = new TxOperations(model, core.account.System) + const spaces = await client.findAll(core.class.Space, {}) + const task = await client.createDoc(test.class.Task, spaces[0]._id, { + name: 'TSK1', + number: 1, + state: 0 + }) + try { + await client.createDoc(test.class.TestMixinTodo, spaces[0]._id, { + text: '', + attachedTo: task, + attachedToClass: test.mixin.TaskMixinTodos, + collection: 'todos' + }) + } catch (e) { + expect(e).toEqual(new Error('createDoc cannot be used for objects inherited from AttachedDoc')) + } + }) }) diff --git a/packages/core/src/operations.ts b/packages/core/src/operations.ts index 702b7c1262..7ee9ea01c2 100644 --- a/packages/core/src/operations.ts +++ b/packages/core/src/operations.ts @@ -59,6 +59,10 @@ export class TxOperations implements Omit { modifiedOn?: Timestamp, modifiedBy?: Ref ): Promise> { + const hierarchy = this.client.getHierarchy() + if (hierarchy.isDerived(_class, core.class.AttachedDoc)) { + throw new Error('createDoc cannot be used for objects inherited from AttachedDoc') + } const tx = this.txFactory.createTxCreateDoc(_class, space, attributes, id, modifiedOn, modifiedBy) await this.client.tx(tx) return tx.objectId