diff --git a/src/helpers.ts b/src/helpers.ts index abfd7772..3e0f1d45 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -348,9 +348,7 @@ export const getParentNode = (blocks: Record, parent: any, meta: Trave const currentPath = meta.nodePath || ''; const pathParts = currentPath.split('.'); const parentPath = isArray(parent) - ? pathParts - .slice(0, -2) - .join('.') // Remove last two parts for an array parent + ? pathParts.slice(0, -2).join('.') // Remove last two parts for an array parent : pathParts.slice(0, -1).join('.'); // Remove only the last part for a non-array parent return parent ? getNodeByPath(blocks, parentPath) : {}; //undefined parent for root }; diff --git a/src/stateMachine/query/surql2/processResults.ts b/src/stateMachine/query/surql2/processResults.ts index 771012f7..865f3217 100644 --- a/src/stateMachine/query/surql2/processResults.ts +++ b/src/stateMachine/query/surql2/processResults.ts @@ -46,6 +46,9 @@ const processQueryResult = (params: { throw new Error(`Thing ${query.$thing} not found in schema`); } if (Array.isArray(result)) { + if (returnNulls && result.length === 0) { + return null; + } return result.map((r) => transformResultObject({ query, result: r, thing, schema, metadata, returnNulls })); } return transformResultObject({ query, result, thing, schema, metadata, returnNulls }); diff --git a/src/stateMachine/query/surql2/run.ts b/src/stateMachine/query/surql2/run.ts index f5489a6b..b947a72e 100644 --- a/src/stateMachine/query/surql2/run.ts +++ b/src/stateMachine/query/surql2/run.ts @@ -74,7 +74,7 @@ export const runSurrealDbQueryMachine2 = async ( log( ['runSurrealDbQueryMachine2', 'runSurrealDbQueryMachine2/finalResult'], `> runSurrealDbQueryMachine2/finalResult ${id}\n`, - JSON.stringify(finalResult), + finalResult, ); const end = performance.now(); log( diff --git a/tests/helpers/matchers.ts b/tests/helpers/matchers.ts index 6c67e04c..a7a3d705 100644 --- a/tests/helpers/matchers.ts +++ b/tests/helpers/matchers.ts @@ -38,6 +38,10 @@ export const deepSort = (obj: T, key = '$id'): T => { return newObj.sort(sorter) as T; } + if (obj instanceof Date) { + return obj; + } + if (typeof obj === 'object' && obj !== null) { return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, deepSort(v, key)])) as T; } diff --git a/tests/unit/mutations/basic.ts b/tests/unit/mutations/basic.ts index 525a6454..ad6025ad 100644 --- a/tests/unit/mutations/basic.ts +++ b/tests/unit/mutations/basic.ts @@ -116,14 +116,17 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { { noMetadata: true }, ); - const res3 = (await ctx.query([ - { $entity: 'User', $id: ['bo-u1', 'bo-u2', 'bo-u3'] }, - { - $relation: 'UserTag', - $id: 'bo-ut1', - $fields: ['id', { $path: 'users', $fields: ['id', 'name'] }], - }, - ])) as BQLResponseMulti; + const res3 = (await ctx.query( + [ + { $entity: 'User', $id: ['bo-u1', 'bo-u2', 'bo-u3'] }, + { + $relation: 'UserTag', + $id: 'bo-ut1', + $fields: ['id', { $path: 'users', $fields: ['id', 'name'] }], + }, + ], + { returnNulls: true }, + )) as BQLResponseMulti; expect(res3[0]).toBeNull(); expect(res3[1]).toMatchObject({ @@ -141,13 +144,16 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { { noMetadata: true }, ); - const res4 = (await ctx.query([ - { $entity: 'User', $id: ['bo-u1', 'bo-u2', 'bo-u3', 'bo-u4'] }, - { - $relation: 'UserTag', - $id: 'bo-ut1', - }, - ])) as BQLResponseMulti; + const res4 = (await ctx.query( + [ + { $entity: 'User', $id: ['bo-u1', 'bo-u2', 'bo-u3', 'bo-u4'] }, + { + $relation: 'UserTag', + $id: 'bo-ut1', + }, + ], + { returnNulls: true }, + )) as BQLResponseMulti; expect(res4[0]).toBeNull(); expect(res4[1]).toBeNull(); @@ -395,10 +401,13 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { }, ]); - const isCleanRes = (await ctx.query([ - { $entity: 'User', $id: 'l1-u1' }, - { $relation: 'UserTag', $id: ['l1-utg1', 'l1-utg2', 'l1-utg3'] }, - ])) as BQLResponseMulti; + const isCleanRes = (await ctx.query( + [ + { $entity: 'User', $id: 'l1-u1' }, + { $relation: 'UserTag', $id: ['l1-utg1', 'l1-utg2', 'l1-utg3'] }, + ], + { returnNulls: true }, + )) as BQLResponseMulti; expect(isCleanRes[0]).toBeNull(); expect(isCleanRes[1]).toBeNull(); @@ -1381,7 +1390,7 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { expect(sessions).toEqual([ { - expires: '2023-06-10T14:58:09.066Z', + expires: new Date('2023-06-10T14:58:09.066Z'), id: expect.any(String), sessionToken: '8ac4c6d7-e8ba-4e63-9e30-1d662b626ad4', user: 'user1', @@ -1425,7 +1434,7 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { expect(deepSort(colors, 'id')).toEqual([ { id: 'dateColor', - freeForAll: '2023-06-10T14:58:09.066Z', + freeForAll: new Date('2023-06-10T14:58:09.066Z'), }, { id: 'numberColor', @@ -1485,7 +1494,7 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { expect(deepSort(colors, 'id')).toEqual([ { id: 'blue', - freeForAll: '2023-06-10T14:58:09.066Z', + freeForAll: new Date('2023-06-10T14:58:09.066Z'), }, { id: 'red', @@ -2321,7 +2330,7 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { expect(sessions).toEqual([ { - expires: '2023-06-10T14:58:09.066Z', + expires: new Date('2023-06-10T14:58:09.066Z'), id: expect.any(String), user: 'god1', }, @@ -2358,7 +2367,7 @@ export const testBasicMutation = createTest('Mutation: Basic', (ctx) => { ); expect(sessions).toEqual({ - expires: '2023-06-10T14:58:09.066Z', + expires: new Date('2023-06-10T14:58:09.066Z'), id: expect.any(String), user: undefined, }); diff --git a/tests/unit/mutations/edges.ts b/tests/unit/mutations/edges.ts index 6972799c..321728a2 100644 --- a/tests/unit/mutations/edges.ts +++ b/tests/unit/mutations/edges.ts @@ -2406,7 +2406,7 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { 'id', { $path: 'dataFields', - $fields: ['id', { $path: 'values', $fields: ['id', 'dataFields'] }, 'expression'], + $fields: ['id', { $path: 'values', $fields: ['id'] }, 'expression'], }, ], }, @@ -2415,11 +2415,11 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { const expressions = await ctx.query({ $relation: 'Expression', - }); + }, { returnNulls: true }); const values = await ctx.query({ $relation: 'DataValue', - }); + }, { returnNulls: true }); // cleaning await ctx.mutate({ @@ -2517,7 +2517,7 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { 'id', { $path: 'dataFields', - $fields: ['id', { $path: 'values', $fields: ['id', 'dataFields'] }, 'expression'], + $fields: ['id', { $path: 'values', $fields: ['id'] }, 'expression'], }, ], }, @@ -2609,7 +2609,7 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { 'id', { $path: 'dataFields', - $fields: ['id', { $path: 'values', $fields: ['id', 'dataFields'] }, 'expression'], + $fields: ['id', { $path: 'values', $fields: ['id'] }, 'expression'], }, ], }, @@ -2723,7 +2723,7 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { 'id', { $path: 'dataFields', - $fields: ['id', { $path: 'values', $fields: ['id', 'dataFields'] }, 'expression'], + $fields: ['id', { $path: 'values', $fields: ['id'] }, 'expression'], }, ], }, @@ -2839,7 +2839,7 @@ export const testEdgesMutation = createTest('Mutation: Edges', (ctx) => { $fields: [ 'id', 'type', - { $path: 'values', $fields: ['id', 'dataFields', 'type'] }, + { $path: 'values', $fields: ['id', 'type'] }, { $path: 'expression', $fields: ['id', 'value'] }, ], }, diff --git a/tests/unit/mutations/refFields.ts b/tests/unit/mutations/refFields.ts index c01ba2e7..d01ad3a8 100644 --- a/tests/unit/mutations/refFields.ts +++ b/tests/unit/mutations/refFields.ts @@ -480,7 +480,7 @@ export const testRefFieldsMutations = createTest('Mutation: RefFields', (ctx) => //Run the test expect(res).toEqual({ id: 'fl4-ref1', - flexReferences: ['hey', 'fl4-u1', 8, 'fl4-u2', new Date('2024-01-01').toISOString()], + flexReferences: ['hey', 'fl4-u1', 8, 'fl4-u2', new Date('2024-01-01')], }); }); @@ -990,7 +990,7 @@ export const testRefFieldsMutations = createTest('Mutation: RefFields', (ctx) => expect(res).toEqual({ id: 'flr4-ref1', - flexReferences: ['hey', 'flr4-u1', 8, 'flr4-u2', new Date('2024-01-01').toISOString()], + flexReferences: ['hey', 'flr4-u1', 8, 'flr4-u2', new Date('2024-01-01')], }); }); @@ -1048,7 +1048,7 @@ export const testRefFieldsMutations = createTest('Mutation: RefFields', (ctx) => expect(res).toEqual({ id: 'flr5-ref1', - flexReferences: ['1990-10-10T00:00:00.000Z', 9, 'hello', 'flr5-u2'], + flexReferences: [new Date('1990-10-10T00:00:00.000Z'), 9, 'hello', 'flr5-u2'], }); }); diff --git a/tests/unit/queries/query.ts b/tests/unit/queries/query.ts index e9347871..578420fb 100644 --- a/tests/unit/queries/query.ts +++ b/tests/unit/queries/query.ts @@ -2828,7 +2828,7 @@ export const testQuery = createTest('Query', (ctx) => { }); }); - it('TODO{T}:ref1n[ref, ONE, nested] Get also nested data', async () => { + it('TODO{TS}:ref1n[ref, ONE, nested] Get also nested data', async () => { // SELECT // "0" AS `$$queryPath`, // (id && record::id(id)) || null AS `$id`, @@ -2869,7 +2869,7 @@ export const testQuery = createTest('Query', (ctx) => { }); }); - it('TODO{T}:ref1nf[ref, ONE, nested, someFields] Get also nested data but only some fields', async () => { + it('TODO{TS}:ref1nf[ref, ONE, nested, someFields] Get also nested data but only some fields', async () => { // SELECT // "0" AS `$$queryPath`, // id && record::id(id) || null AS `$id`, @@ -2937,7 +2937,7 @@ export const testQuery = createTest('Query', (ctx) => { }); }); - it('TODO{T}:ref4nf[ref, flex, MANY, nested] Get flexReferences with nested data', async () => { + it('TODO{TS}:ref4nf[ref, flex, MANY, nested] Get flexReferences with nested data', async () => { // SELECT // "0" AS `$$queryPath`, // id && record::id(id) || null AS `$id`, @@ -2978,7 +2978,7 @@ export const testQuery = createTest('Query', (ctx) => { }); }); - it('TODO{T}:ref4n[ref, flex, MANY, nested, $fields] Get flexReferences with nested data but only some fields', async () => { + it('TODO{TS}:ref4n[ref, flex, MANY, nested, $fields] Get flexReferences with nested data but only some fields', async () => { // SELECT // "0" AS `$$queryPath`, // (id && record::id(id)) || null AS `$id`,