diff --git a/clients/typescript/src/client/conversions/input.ts b/clients/typescript/src/client/conversions/input.ts index f3c9de07d8..3dd3451f94 100644 --- a/clients/typescript/src/client/conversions/input.ts +++ b/clients/typescript/src/client/conversions/input.ts @@ -3,7 +3,7 @@ import { FieldName, Fields } from '../model/schema' import { fromSqlite, toSqlite } from './sqlite' import { InvalidArgumentError } from '../validation/errors/invalidArgumentError' import { mapObject } from '../util/functions' -import { PgDateType, PgType } from './types' +import { PgType } from './types' export enum Transformation { Js2Sqlite, @@ -318,25 +318,22 @@ function transformFieldsAllowingFilters( if (!pgType) throw new InvalidArgumentError(`Unknown field ${field}`) - switch (pgType) { - // Types that require transformations - case PgDateType.PG_DATE: - case PgDateType.PG_TIME: - case PgDateType.PG_TIMESTAMP: - case PgDateType.PG_TIMESTAMPTZ: - case PgDateType.PG_TIMETZ: - if (value instanceof Date) { - // transform it - return toSqlite(value, pgType) - } else { - // it must be an object containing filters - // transform the values that are nested in those filters - return transformFilterObject(field, value, pgType, fields) - } - // other types that don't require transformations - default: - return value + if (isFilterObject(value)) { + // transform the values that are nested in those filters + return transformFilterObject(field, value, pgType, fields) } + + return toSqlite(value, pgType) +} + +function isObject(v: any): boolean { + return typeof v === 'object' && !Array.isArray(v) && v !== null +} + +function isFilterObject(value: any): boolean { + // if it is an object it can only be a timestamp or a filter object + // because those are the only objects we support in where clauses + return isObject(value) && !(value instanceof Date) } /** @@ -388,6 +385,7 @@ function transformFilterObject( }) return { + ...o, ...transformedSimpleFilterObj, ...transformedArrayFilterObj, ...transformedNotFilterObj, diff --git a/clients/typescript/test/client/conversions/input.test.ts b/clients/typescript/test/client/conversions/input.test.ts index a482651049..18d6cd481b 100644 --- a/clients/typescript/test/client/conversions/input.test.ts +++ b/clients/typescript/test/client/conversions/input.test.ts @@ -76,6 +76,21 @@ test.serial('findFirst transforms JS objects to SQLite', async (t) => { t.deepEqual(res?.timestamp, new Date(date)) }) +test.serial('findFirst transforms booleans to integer in SQLite', async (t) => { + await electric.adapter.run({ + sql: `INSERT INTO DataTypes('id', 'bool') VALUES (1, 0), (2, 1)`, + }) + + const res = await tbl.findFirst({ + where: { + bool: true, + }, + }) + + t.is(res?.id, 2) + t.is(res?.bool, true) +}) + test.serial( 'findFirst transforms JS objects in equals filter to SQLite', async (t) => {