#Spread in astro:db

3 messages · Page 1 of 1 (latest)

ember sierra
#

I want to select a table an inner join another one into it, can I do something like:

    const items = await dbInstance
      .select({
        ...CartItem,
        product: Product,
      })

instead of

    const items = await dbInstance
      .select({
        item: CartItem,
        product: PlaqueProduct,
      })

rugged kelpBOT
#

Hi @ember sierra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'm sorry, but the knowledge sources do not provide explicit information about using the JavaScript spread operator (...) within the select method of Astro DB's Drizzle ORM to automatically expand all columns of a table in a join query. The documented examples show selecting joined data by specifying each table as a property in the select object, like this:

const comments = await db.select()
  .from(Comment)
  .innerJoin(Author, eq(Comment.authorId, Author.id));

and then accessing the results as { Author, Comment } in your template or code. There is no mention of using the spread operator to flatten or expand table columns directly in the select call. You would typically specify each table or alias as a property in the select object, as in your second example:

const items = await dbInstance
  .select({
    item: CartItem,
    product: PlaqueProduct,
  })

If you need to flatten or restructure the result, you may need to do so after fetching the data, not within the select call itself. For more advanced query shapes, you may want to consult the Drizzle join reference for further options, but the Astro DB documentation does not cover the spread pattern you described Query your database: Relationships.

If you need more flexibility in shaping your result, you may need to handle that in your application logic after the query.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: