Skip to content

Querying nested JSON with SelectDoc #44

Open

Description

@auwtch

First of all, thank you for this great library.

But I have encountered one problem. Dat cannot populate nested json with single trip to Postgres.

I have some nested structs (simplified):

type Item struct {
    ID          int             `db:"id" json:"id"`
    Name        string          `db:"name" json:"name"`
    CatalogPart ItemCatalogPart `json:"catalog_part"`
}

type ItemCatalogPart struct {
    Description string      `db:"description" json:"description"`
    Fields      []ItemField     `json:"fields"`
}

type ItemField struct {
    ID           int    `db:"id" json:"id"`
    Value        string `db:"value" json:"value"`
}

And I trying to query Item with SelectDoc:

func (repo *DBItemRepo) FindByID(id int) (item domain.Item) {
    repo.dbHandler.
        SelectDoc("id", "name").
        One("catalog_part", "SELECT description FROM items AS i WHERE i.id = items.id").
        Many("fields", "SELECT id, value FROM item_fields WHERE item_id = items.id ORDER BY id").
        From("items").
        Where("id = $1", id).
        QueryStruct(&item)
    return
}

Of course I get nil. How to do it with one trip to DB?
Thank you.

Activity

  1. mgutz commented on Apr 5, 2016

    @mgutz
    Owner

    Try using pointers to ItemCatalogPart and ItemPart? What you're doing is how we use dat in our project. I'll try to find some time later this week if that doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions