Skip to content

Commit

Permalink
fix(sql): query list type of size 0 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed May 4, 2024
1 parent 06d6562 commit 538b748
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/postgres/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export class PostgresBuilder extends Builder {
$regex: (key, value) => this.createRegExpQuery(key, value),
$regexFor: (key, value) => `${this.escape(value)} ~ ${key}`,
$size: (key, value) => {
if (!value) return this.logicalNot(key)
if (this.isJsonQuery(key)) {
return `${this.jsonLength(key)} = ${this.escape(value)}`
} else {
if (!value) return `COALESCE(ARRAY_LENGTH(${key}, 1), 0) = 0`
return `${key} IS NOT NULL AND ARRAY_LENGTH(${key}, 1) = ${value}`
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export class Builder {
}
},
$size: (key, value) => {
if (!value) return this.logicalNot(key)
if (this.isJsonQuery(key)) {
return `${this.jsonLength(key)} = ${this.escape(value)}`
} else {
if (!value) return this.logicalNot(key)
return `${key} AND LENGTH(${key}) - LENGTH(REPLACE(${key}, ${this.escape(',')}, ${this.escape('')})) = ${this.escape(value)} - 1`
}
},
Expand Down
6 changes: 6 additions & 0 deletions packages/tests/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ namespace JsonTests {
{ size: 3 },
{ size: 3 },
])

await expect(database.select('baz', {
nums: { $size: 0 },
}).project({
size: row => $.length(row.nums),
}).execute()).to.eventually.have.length(0)
})

it('$el', async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/tests/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ namespace QueryOperators {
await expect(database.get('temp1', {
list: { $size: 1 },
})).eventually.to.have.length(2).with.shape([{ id: 2 }, { id: 3 }])

await expect(database.get('temp1', {
list: { $size: 0 },
})).eventually.to.have.length(1).with.shape([{ id: 1 }])
})

size && it('$.length', async () => {
Expand Down

0 comments on commit 538b748

Please sign in to comment.