diff --git a/src/stateMachine/mutation/surql/build.ts b/src/stateMachine/mutation/surql/build.ts index 00ce2bc1..148fc099 100644 --- a/src/stateMachine/mutation/surql/build.ts +++ b/src/stateMachine/mutation/surql/build.ts @@ -200,7 +200,11 @@ export const buildSURQLMutation = async (flat: FlatBqlMutation, schema: Enriched const rest = oFilter(block, (k: string) => !k.startsWith('$')); const restString = JSON.stringify(rest); - const OUTPUT = `(CREATE ONLY Delta SET input = ${restString}, meta = {"$sid": $parent.id, "$id": record::id($parent.id)}, after = $after, before = $before RETURN VALUE $parent.id )`; + const meta = oFilter(block, (k: string) => k.startsWith('$')); + const metaString = Object.entries(meta) + .map(([key, value]) => (key === '$tempId' ? `'$tempId': '_:${value}'` : `'${key}': '${value}'`)) + .join(','); + const OUTPUT = `(CREATE ONLY Delta SET input = ${restString}, meta = {${metaString}, "$sid": $parent.id, "$id": record::id($parent.id)}, after = $after, before = $before RETURN VALUE $parent.id )`; const roleOneSchema = currentSchema.roles[roleA]; const isMany1 = roleOneSchema.cardinality === 'MANY'; diff --git a/tests/test.sh b/tests/test.sh index b593fc45..56f77798 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -56,7 +56,7 @@ DATA_FILE="./tests/adapters/surrealDB/mocks/${LINK}Data.surql" NAMESPACE="test_${LINK}" # Start the container -docker run --detach --rm --pull always -v $(pwd)/tests:/tests -p 8000:8000 --name $CONTAINER_NAME surrealdb/surrealdb:v2.3.7 start --allow-all -u $USER -p $PASSWORD --bind 0.0.0.0:8000 +docker run --detach --rm --pull always -v $(pwd)/tests:/tests -p 8000:8000 --name $CONTAINER_NAME surrealdb/surrealdb:v2.3.7 start --strict --allow-all -u $USER -p $PASSWORD --bind 0.0.0.0:8000 until [ "`docker inspect -f {{.State.Running}} $CONTAINER_NAME`"=="true" ]; do sleep 0.1; diff --git a/tests/unit/mutations/basic.ts b/tests/unit/mutations/basic.ts index 525a6454..daf07464 100644 --- a/tests/unit/mutations/basic.ts +++ b/tests/unit/mutations/basic.ts @@ -5,6 +5,7 @@ import { expect, it } from 'vitest'; import type { BQLResponse, BQLResponseMulti, BQLResponseSingle } from '../../../src'; import { createTest } from '../../helpers/createTest'; import { deepSort, expectArraysInObjectToContainSameElements } from '../../helpers/matchers'; +import { RecordId } from 'surrealdb'; export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { // some random issues forced a let here @@ -2552,4 +2553,165 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { } } }); + + it.skip('bfi1[create, $fields] Create with $fields to see what we get in output', async () => { + // Create a user with $fields specified but not updating those fields + await ctx.mutate( + { + $thing: 'User', + id: 'bfi1-test-user-fields', + name: 'Test User', + }, + { noMetadata: true }, + ); + + // create a space linked to that user + + const mutationResult = await ctx.mutate({ + $thing: 'User', + $id: 'bfi1-test-user-fields', + spaces: [ + { + $thing: 'Space', + id: 'bfi1-test-space', + }, + ], + }); + console.log('RES!', mutationResult); + + // For now, just expect the mutation to work (we're testing what we get) + expect(mutationResult).toBeDefined(); + + // Clean up + await ctx.mutate([ + { + $thing: 'User', + $op: 'delete', + $id: 'bfi1-test-user-fields', + }, + { + $thing: 'Space', + $op: 'delete', + $id: 'bfi1-test-space', + }, + ]); + }); + + it.skip('bfi2[create, $fields] Create with $fields to see what we get in output', async () => { + // Create a UserTagGroup relation with nested roleFields + await ctx.mutate( + { + $relation: 'UserTagGroup', + id: 'bfi2-test-group', + tags: [ + { + $thing: 'UserTag', + id: 'bfi2-test-tag-1', + name: 'Tag 1', + users: [ + { + $thing: 'User', + id: 'bfi2-test-user-1', + }, + { + $thing: 'User', + id: 'bfi2-test-user-3', + }, + ], + }, + { + $thing: 'UserTag', + id: 'bfi2-test-tag-2', + name: 'Tag 2', + users: [ + { + $thing: 'User', + id: 'bfi2-test-user-2', + }, + ], + }, + ], + }, + { noMetadata: true }, + ); + + // Now update the UserTagGroup with nested updates to see the mutation output + const mutationResult = await ctx.mutate({ + $relation: 'UserTagGroup', + $id: 'bfi2-test-group', + tags: [ + { + $id: 'bfi2-test-tag-1', + $op: 'update', + name: 'Updated Tag 1', + users: [{ $op: 'update', name: 'updated users' }], + }, + { + $id: 'bfi2-test-tag-2', + $op: 'update', + color: { $op: 'create', $entity: 'Color', id: 'bfi2-teal' }, + }, + ], + }); + + console.log('BFI2 MUTATION RESULT:', deepSort(mutationResult, 'id')); + + // Clean up + await ctx.mutate([ + { + $relation: 'UserTagGroup', + $op: 'delete', + $id: 'bfi2-test-group', + }, + { + $relation: 'UserTag', + $op: 'delete', + $id: ['bfi2-test-tag', 'bfi2-test-tag-1', 'bfi2-test-tag-2'], + }, + { + $thing: 'User', + $op: 'delete', + $id: 'bfi2-test-user-1', + }, + { + $thing: 'User', + $op: 'delete', + $id: 'bfi2-test-user-2', + }, + { + $thing: 'User', + $op: 'delete', + $id: 'bfi2-test-user-3', + }, + { + $thing: 'Color', + $op: 'delete', + $id: 'bfi2-teal', + }, + ]); + + expect(mutationResult.find(m => m.$id === 'bfi2-test-user-1')).toMatchObject({ + $op: 'update', + $id: 'bfi2-test-user-1', + id: 'bfi2-test-user-1', + name: 'updated users', + 'user-tags': [new RecordId('UserTag', 'bfi2-test-tag-1')], + }) + + expect(mutationResult.find(m => m.$id === 'bfi2-test-user-3')).toMatchObject({ + $op: 'update', + $id: 'bfi2-test-user-3', + id: 'bfi2-test-user-3', + name: 'updated users', + 'user-tags': [new RecordId('UserTag', 'bfi2-test-tag-1')], + }) + + expect(mutationResult.find(m => m.$id === 'bfi2-teal')).toMatchObject({ + $op: 'create', + $entity: 'Color', + $id: 'bfi2-teal', + id: 'bfi2-teal', + 'user-tags': [new RecordId('UserTag', 'bfi2-test-tag-2')], + }) + }); });