Skip to content

Commit

Permalink
fix(postgres): infer outtype for if, ifNull, revert computed eq (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest authored Nov 23, 2023
1 parent 709fb07 commit b7ae5b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ jobs:
fail-fast: false
matrix:
postgres-image:
- postgres:16.1
- postgres:15.5
- postgres:14.10
- postgres:16
- postgres:15
- postgres:14
node-version: [18, 20]

services:
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ function comparator<K extends keyof Eval.Static>(key: K, callback: BinaryCallbac
operators[`$${key}`] = (args, data) => {
const left = executeEval(data, args[0])
const right = executeEval(data, args[1])
return callback(left?.valueOf(), right?.valueOf())
if (isNullable(left) || isNullable(right)) return true
return callback(left.valueOf(), right.valueOf())
}
return (...args: any) => Eval(key, args) as any
}
Expand Down
10 changes: 8 additions & 2 deletions packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ class PostgresBuilder extends Builder {

this.evalOperators = {
...this.evalOperators,
$if: (args) => `(SELECT CASE WHEN ${this.parseEval(args[0])} THEN ${this.parseEval(args[1])} ELSE ${this.parseEval(args[2])} END)`,
$ifNull: (args) => `coalesce(${args.map(arg => this.parseEval(arg)).join(', ')})`,
$if: (args) => {
const type = this.getLiteralType(args[1]) ?? this.getLiteralType(args[2]) ?? 'text'
return `(SELECT CASE WHEN ${this.parseEval(args[0], 'boolean')} THEN ${this.parseEval(args[1], type)} ELSE ${this.parseEval(args[2], type)} END)`
},
$ifNull: (args) => {
const type = args.map(this.getLiteralType).find(x => x) ?? 'text'
return `coalesce(${args.map(arg => this.parseEval(arg, type)).join(', ')})`
},

// number
$add: (args) => `(${args.map(arg => this.parseEval(arg, 'double precision')).join(' + ')})`,
Expand Down
18 changes: 5 additions & 13 deletions packages/tests/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ namespace ObjectOperations {
{ meta: { a: '666', embed: { c: 'world' } } },
])
})

it('nested property', async () => {
await setup(database)
await expect(database.get('object', row => $.eq(row.meta.embed.b, 2), ['meta']))
.to.eventually.deep.equal([
{ meta: { a: '233', embed: { b: 2, c: 'hello' } } },
])
await expect(database.get('object', row => $.eq(row.meta.embed.c, 'hello'), ['meta']))
.to.eventually.deep.equal([
{ meta: { a: '233', embed: { b: 2, c: 'hello' } } },
])
})
}

export const upsert = function Upsert(database: Database<Tables>) {
Expand Down Expand Up @@ -113,7 +101,11 @@ namespace ObjectOperations {
it('expressions w/ json object', async () => {
const table = await setup(database)
table[0]!.meta!.a = table[0]!.meta!.embed!.c + 'a'
await database.upsert('object', row => [{ id: '0', meta: { a: $.concat(row.meta.embed.c, 'a') } }])
table[1]!.meta!.embed!.b = 1
await database.upsert('object', row => [
{ id: '0', meta: { a: $.concat(row.meta.embed.c, 'a') } },
{ id: '1', 'meta.embed.b': $.add($.ifNull(row.meta.embed.b, 0), 1) },
])
await expect(database.get('object', {})).to.eventually.have.deep.members(table)
})

Expand Down

0 comments on commit b7ae5b7

Please sign in to comment.