romeerez / orchid-orm

Orchid ORM
https://orchid-orm.netlify.app/
MIT License
481 stars 13 forks source link

Query helper applied to SetQueryReturnsOne return type is array #320

Closed IlyaSemenov closed 1 week ago

IlyaSemenov commented 2 weeks ago

When a query helper is applied to a SetQueryReturnsOne query, the return type is array and not object. This is only a typing bug, it works correctly in runtime.

import process from "node:process"

import { createBaseTable, orchidORM, raw, testTransaction } from "orchid-orm"

const BaseTable = createBaseTable()

class PostTable extends BaseTable {
  readonly table = "post"

  columns = this.setColumns(t => ({
    id: t.serial().primaryKey(),
    text: t.text(),
  }))
}

const db = orchidORM(
  { databaseURL: process.env.DATABASE_URL, log: true },
  {
    post: PostTable,
  },
)

await testTransaction.start(db)

await db.$query`
  create table "post" (
    id serial primary key,
    text text not null
  );
`

await db.post.insert({ text: "Hello, world!" })

const selectPost = db.post.makeHelper(q => q.select("text"))

// type of post is { text: string }[]
const post = await selectPost(db.post.take())

// prints { text: 'Hello, world!' }
console.log(post)

await testTransaction.close(db)
IlyaSemenov commented 2 weeks ago

This broke between 1.30.0 (worked) and 1.31.7 (broken).

romeerez commented 1 week ago

I published a fix, it was because missed update in one place when changed QueryReturnType's type to be undefined by default.

It's a small fix, sorry that couldn't do it earlier.