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
4 changes: 1 addition & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ export const getParentNode = (blocks: Record<any, any>, 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
};
Expand Down
3 changes: 3 additions & 0 deletions src/stateMachine/query/surql2/processResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion src/stateMachine/query/surql2/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const deepSort = <T>(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;
}
Expand Down
57 changes: 33 additions & 24 deletions tests/unit/mutations/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
},
Expand Down Expand Up @@ -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,
});
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/mutations/edges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
],
},
Expand All @@ -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({
Expand Down Expand Up @@ -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'],
},
],
},
Expand Down Expand Up @@ -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'],
},
],
},
Expand Down Expand Up @@ -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'],
},
],
},
Expand Down Expand Up @@ -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'] },
],
},
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/mutations/refFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
});
});

Expand Down Expand Up @@ -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')],
});
});

Expand Down Expand Up @@ -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'],
});
});

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down