diff --git a/src/app.test.ts b/src/app.test.ts index bac286d98..0a210b5b3 100644 --- a/src/app.test.ts +++ b/src/app.test.ts @@ -1,4 +1,4 @@ - import assert from 'node:assert/strict' +import assert from 'node:assert/strict' import { writeFileSync } from 'node:fs' import { join } from 'node:path' import test from 'node:test' @@ -137,11 +137,17 @@ await test('createApp', async (t) => { assert.deepEqual(data, [{ id: '1', title: 'foo' }]) }) + await t.test('GET /posts?_where=... supports plain equality', async () => { + const where = encodeURIComponent(JSON.stringify({ title: 'foo' })) + const response = await fetch(`http://localhost:${port}/posts?_where=${where}`) + assert.equal(response.status, 200) + const data = await response.json() + assert.deepEqual(data, [{ id: '1', title: 'foo' }]) + }) + await t.test('GET /posts?_where=... overrides query params', async () => { const where = encodeURIComponent(JSON.stringify({ title: { eq: 'foo' } })) - const response = await fetch( - `http://localhost:${port}/posts?title:eq=bar&_where=${where}`, - ) + const response = await fetch(`http://localhost:${port}/posts?title:eq=bar&_where=${where}`) assert.equal(response.status, 200) const data = await response.json() assert.deepEqual(data, [{ id: '1', title: 'foo' }]) diff --git a/src/matches-where.test.ts b/src/matches-where.test.ts index 26a2be4a9..f75129833 100644 --- a/src/matches-where.test.ts +++ b/src/matches-where.test.ts @@ -8,6 +8,12 @@ import { matchesWhere } from './matches-where.ts' await test('matchesWhere', async (t) => { const obj: JsonObject = { a: 10, b: 20, c: 'x', nested: { a: 10, b: 20 } } const cases: [JsonObject, boolean][] = [ + [{ a: 10 }, true], + [{ a: 11 }, false], + [{ c: 'x' }, true], + [{ c: 'z' }, false], + [{ a: 10, c: 'x' }, true], + [{ a: 10, c: 'z' }, false], [{ a: { eq: 10 } }, true], [{ a: { eq: 11 } }, false], [{ c: { ne: 'y' } }, true], diff --git a/src/matches-where.ts b/src/matches-where.ts index 036b00c69..9b73d3dd9 100644 --- a/src/matches-where.ts +++ b/src/matches-where.ts @@ -79,9 +79,7 @@ export function matchesWhere(obj: JsonObject, where: JsonObject): boolean { continue } - if (field === undefined) return false - - return false + if (field === undefined || field !== value) return false } return true