Skip to content

Commit

Permalink
Prevent crash if batch contains empty values (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 12, 2025
1 parent f0c7e76 commit e86ac41
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DeepCallable, ResultRecord } from '@/src/queries/types';
import type { Model } from '@/src/schema';
import { mutateStructure, setProperty } from '@/src/utils';
import { isPlainObject, mutateStructure, setProperty } from '@/src/utils';
import { type ModelField, QUERY_SYMBOLS, type Query } from '@ronin/compiler';

/**
Expand Down Expand Up @@ -219,6 +219,10 @@ export const getBatchProxy = (
// objects, thereby making development more difficult. To avoid this, we are creating a
// plain object containing the same properties as the `Proxy` instances.
return queries.map((details) => {
// If a placeholder value such as `null` is located inside the batch, we need to
// return it as-is instead of processing it as a query.
if (!isPlainObject(details)) return { structure: details };

const item: SyntaxItem = {
structure: (details as unknown as Record<typeof QUERY_SYMBOLS.QUERY, Query>)[
QUERY_SYMBOLS.QUERY
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const getProperty = (obj: object, path: string): unknown => {
*
* @returns A boolean indicating whether the object is plain, or not.
*/
const isPlainObject = (value: unknown): boolean => {
export const isPlainObject = (value: unknown): boolean => {
return Object.prototype.toString.call(value) === '[object Object]';
};

Expand Down
32 changes: 31 additions & 1 deletion tests/queries/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, spyOn, test } from 'bun:test';

import { op } from '@/src/helpers/expressions';
import { getBatchProxy, getSyntaxProxy } from '@/src/queries';
import { type SyntaxItem, getBatchProxy, getSyntaxProxy } from '@/src/queries';
import { concat, string } from '@/src/schema';
import {
type AddQuery,
Expand Down Expand Up @@ -437,6 +437,36 @@ describe('syntax proxy', () => {
});
});

test('using queries with placeholder in batch', () => {
const getProxy = getSyntaxProxy<GetQuery>({ root: `${QUERY_SYMBOLS.QUERY}.get` });

const queries = getBatchProxy(() => [
getProxy.account(),
null as unknown as SyntaxItem<Query>,
getProxy.team(),
]);

expect(queries).toMatchObject([
{
structure: {
get: {
account: {},
},
},
},
{
structure: null,
},
{
structure: {
get: {
team: {},
},
},
},
]);
});

test('using options for query in batch', () => {
const getProxy = getSyntaxProxy<GetQuery>({ root: `${QUERY_SYMBOLS.QUERY}.get` });

Expand Down

0 comments on commit e86ac41

Please sign in to comment.