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
14 changes: 10 additions & 4 deletions src/app.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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' }])
Expand Down
6 changes: 6 additions & 0 deletions src/matches-where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 1 addition & 3 deletions src/matches-where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down