I have the following relationship:
package models
import (
"github.com/jinzhu/gorm"
)
type User struct {
gorm.Model
Site Site
}
type Site struct {
gorm.Model
UserID string
Pageviews []Pageview
}
type Pageview struct {
gorm.Model
Site Site
SiteID string
Hostname string `gorm:"not null"`
Pathname string `gorm:"not null"`
IsNewVisitor bool `gorm:"not null"`
IsNewSession bool `gorm:"not null"`
IsUnique bool `gorm:"not null"`
IsBounce bool `gorm:"not null"`
IsFinished bool `gorm:"not null"`
Referrer string `gorm:"not null"`
Duration int64 `gorm:"not null"`
}
And typically, in a framework like Rails, the queries that you do are limited to the user context. For example:
GET /pageviews -> return all page views from the current user.
The same is true for Fathom, e.g:
func (api *API) GetSiteStatsPageviewsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetTotalSiteViews(params.SiteID, params.StartDate, params.EndDate)
if err != nil {
return err
}
return respond(w, http.StatusOK, envelope{Data: result})
}
Code in here.
Now: How can I do the same in a Lambda? Currently, I have to declare a relationship between saying that a Pageview belongs to a User (or a User has many pageviews), and then:
func (p *Pageview) FindAll(userId string, siteID string) ([]byte, error) {
p.UserID = userID
p.SiteID = siteID
db.Find(p)
return json.Marshal(p)
}
when I want to:
func (p *Pageview) FindAll(siteID string) ([]byte, error) {
p.SiteID = siteID
db.Find(p)
return json.Marshal(p)
}
Is there any other way that I can do it, without having the declare the relationship?
I have never asked myself how is possible to do a query limiting by the current user, without having to declare it in the query itself. Maybe if I understand I can emulate the same behavior.