import type { Contract } from '@internal/contract/types'; import type { SqlStorage } from '@internal/sql-contract/types'; import { describe, expect, it } from 'vitest'; import { parseMutationInput } from '../../src/relation-mutations'; import { createGraph, updateFirstGraph } from '../../src/statement-graph/collection-graphs'; import type { RelationMutator } from '../../src/types'; import { createCollectionFor, relatedCollectionOf } from '../collection-fixtures'; import { getTestContext } from '../helpers'; import { printGraph } from './print-graph'; type Mutator = RelationMutator, string>; const relatedCollection = relatedCollectionOf(); function parsed(modelName: 'User' | 'Post', input: Record) { return parseMutationInput( getTestContext().contract, 'public', modelName, input as never, relatedCollection, ); } function printUpdate(input: Record): string { const target = { context: getTestContext(), state: createCollectionFor('User').collection.where({ id: 1 }).state, modelName: 'User', namespaceId: 'public', }; return printGraph(updateFirstGraph(target, {}, parsed('User', input).relationMutations)); } describe('a relation callback that returns an array of operations', () => { it('adds the operations in array order', () => { expect( printUpdate({ invitedUsers: (invited: Mutator) => [ invited.create([{ id: 10, name: 'Grace', email: 'g@h.io' }]), invited.connect({ id: 11 }), invited.disconnect([{ id: 12 }]), ], }), ).toBe( [ 'n1 Find users where id = 1', 'n2 Insert users <- PayloadData n1 (id->invited_by_id)', 'n3 Update users set nothing where id = 11 <- PayloadData n1 (id->invited_by_id)', 'n4 Update users set invited_by_id = null where id = 12 <- FilterData n1 (id->invited_by_id)', 'result: n1', ].join('\n'), ); }); it('adds nothing for an empty array', () => { expect(printUpdate({ posts: () => [] })).toBe( ['n1 Find users where id = 1', 'result: n1'].join('\n'), ); }); it('takes a one-element array on a to-one relation as its operation', () => { expect(parsed('Post', { author: (author: Mutator) => [author.connect({ id: 1 })] })).toEqual( parsed('Post', { author: (author: Mutator) => author.connect({ id: 1 }) }), ); }); it.each([ [ 'a nested array', 'User', { posts: (posts: Mutator) => [posts.connect({ id: 11 }), [posts.disconnect()]] }, 'ORM.ARGUMENT_INVALID', 'Relation field "posts" on model "User" returned a nested array at index 1; return one flat array of mutations', { relation: 'posts', model: 'User', problem: 'nested-array', index: 1 }, ], [ 'an element that is not an operation', 'User', { posts: (posts: Mutator) => [posts.connect({ id: 11 }), { id: 12 }] }, 'ORM.ARGUMENT_INVALID', 'Relation field "posts" on model "User" returned an invalid mutation descriptor at index 1', { relation: 'posts', model: 'User', problem: 'invalid-descriptor', index: 1 }, ], [ 'two operations on a to-one relation the parent holds the key for', 'Post', { author: (author: Mutator) => [author.connect({ id: 1 }), author.connect({ id: 2 })] }, 'ORM.OPERATION_UNSUPPORTED', 'Relation "author" on model "Post" is a to-one relation and takes one operation, not 2', { relation: 'author', model: 'Post', reason: 'to-one-relation', operations: 2 }, ], [ 'two operations on a to-one relation the child holds the key for', 'User', { profile: (profile: Mutator) => [profile.disconnect(), profile.disconnect()] }, 'ORM.OPERATION_UNSUPPORTED', 'Relation "profile" on model "User" is a to-one relation and takes one operation, not 2', { relation: 'profile', model: 'User', reason: 'to-one-relation', operations: 2 }, ], ] as const)('rejects %s', (_, modelName, input, code, message, meta) => { expect(() => parsed(modelName, input)).toThrow( expect.objectContaining({ code, message, meta }), ); }); it('rejects a disconnect inside an array in create()', () => { const target = { context: getTestContext(), state: createCollectionFor('User').collection.state, modelName: 'User', namespaceId: 'public', }; const input = { id: 1, name: 'Ada', email: 'ada@example.com', posts: (posts: Mutator) => [posts.connect({ id: 11 }), posts.disconnect([{ id: 12 }])], }; expect(() => createGraph(target, parsed('User', input))).toThrow( expect.objectContaining({ code: 'ORM.OPERATION_UNSUPPORTED', message: 'disconnect() is only supported in update() nested mutations', meta: { kind: 'disconnect', relation: 'posts' }, }), ); }); });