Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/stateMachine/mutation/surql/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 )`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Invalid SURQL Query Syntax with Empty Meta

The generated SURQL query contains invalid syntax in the meta object when no $-prefixed keys are present in the input block. This results in a leading comma, like meta = {, ...}, which causes a syntax error when the query is executed.

Fix in Cursor Fix in Web


const roleOneSchema = currentSchema.roles[roleA];
const isMany1 = roleOneSchema.cardinality === 'MANY';
Expand Down
2 changes: 1 addition & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
162 changes: 162 additions & 0 deletions tests/unit/mutations/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove debug logging: Remove the console.log in the pf2 test (BFI2 MUTATION RESULT) if not needed.

Suggested change
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'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that the deletion array includes the id 'bfi2-test-tag', but only 'bfi2-test-tag-1' and 'bfi2-test-tag-2' are created. Is 'bfi2-test-tag' a typo, and should it be removed or corrected?

Suggested change
$id: ['bfi2-test-tag', 'bfi2-test-tag-1', 'bfi2-test-tag-2'],
$id: ['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')],
})
});
});