Skip to content

Commit

Permalink
feat: support spread in nested $.object
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Sep 14, 2024
1 parent 7fde9d8 commit 4df9639
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/core/src/eval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineProperty, isNullable, mapValues } from 'cosmokit'
import { defineProperty, filterKeys, isNullable, mapValues } from 'cosmokit'
import { AtomicTypes, Comparable, Flatten, isComparable, isEmpty, makeRegExp, Row, Values } from './utils.ts'
import { Type } from './type.ts'
import { Field, Relation } from './model.ts'
Expand Down Expand Up @@ -320,6 +320,8 @@ Eval.object = (fields: any) => {
.map(([k]) => [k.slice(prefix.length), fields[k.slice(prefix.length)]]))
return Object.assign(Eval('object', fields, Type.Object(mapValues(fields, (value) => Type.fromTerm(value)))), fields)
}
// filter out nested spread $object
fields = filterKeys(fields, key => key !== '$object')
return Object.assign(Eval('object', fields, Type.Object(mapValues(fields, (value) => Type.fromTerm(value)))), fields)
}

Expand Down
18 changes: 18 additions & 0 deletions packages/tests/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ namespace JsonTests {
])
})

it('$.object using spread', async () => {
const res = await database.select('foo')
.project({
obj: row => $.object({
id2: row.id,
...row,
})
})
.orderBy(row => row.obj.id)
.execute()

expect(res).to.deep.equal([
{ obj: { id2: 1, id: 1, value: 0 } },
{ obj: { id2: 2, id: 2, value: 2 } },
{ obj: { id2: 3, id: 3, value: 2 } },
])
})

it('$.object in json', async () => {
const res = await database.select('bar')
.project({
Expand Down

0 comments on commit 4df9639

Please sign in to comment.