ORM that can convert one row to mulitple linked models


(Pavel) #1

Hello.

There are three linked models id DB (described below). We need iterate on all ads with proper size in active campaigns with single DB query (strict time limit).
It’s easy to write SQL query where each row will have both ad and campaign. But I cannot think out easy and maintainable way to map such row to models Ad, AdGroup and Campaign to access them in code in form Ad.AdGroup.Campaigns[0](or even better Ad.Campaign or Ad.AdGroup.Campaign because we need only selected one combination but not all available Campaigns for that Ad).

Which ORM could help to do that without creating “frankenstein” model with necessary fields from all models and mapping data row to it?

type Ad struct {
    Id int
    Height int
    Width int
    AdGroupId int
    // Each Ad in only one AdGroup
    AdGroup AdGroup 
}
type AdGroup struct {
    Id int
    // Multiple Ads in AdGroup
    Ads []Ad
}
type Campaign struct {
    Id int
    Active bool
    // many2many for Campaign<->AdGroup
    AdGroups []AdGroup
}
type AdGroupCampaign {
    AdGroupId int
    AdGroup AdGroup
    CampaignId int
    Campaign Campaign
}

(Pavel) #2

sqlboiler is suitable here.

Instructions: Youtube and GitHub