store

package
v0.0.0-...-5352646 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2020 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTransactionRecovered = errors.DefineInternal("transaction_recovered", "Internal Server Error")

ErrTransactionRecovered is returned when a panic is caught from a SQL transaction.

Functions

func AutoMigrate

func AutoMigrate(db *gorm.DB) *gorm.DB

AutoMigrate automatically migrates the database for the registered models.

func Check

func Check(db *gorm.DB) error

Check that the database contains all tables.

func Clear

func Clear(db *gorm.DB) error

Clear database tables for all registered models. This should be used with caution.

func Initialize

func Initialize(db *gorm.DB) error

Initialize initializes the database.

func Open

func Open(ctx context.Context, dsn string) (*gorm.DB, error)

Open opens a new database connection.

func SetLogger

func SetLogger(db *gorm.DB, log log.Interface)

SetLogger sets the database logger.

func Transact

func Transact(ctx context.Context, db *gorm.DB, f func(db *gorm.DB) error) (err error)

Transact executes f in a db transaction.

func WithOrder

func WithOrder(ctx context.Context, spec string) context.Context

WithOrder instructs the store to sort the results by the given field. If the field is prefixed with a minus, the order is reversed.

func WithPagination

func WithPagination(ctx context.Context, limit, page uint32, total *uint64) context.Context

WithPagination instructs the store to paginate the results, and set the total number of results into total.

Types

type APIKey

type APIKey struct {
	Model

	APIKeyID string `gorm:"type:VARCHAR;unique_index:api_key_id_index"`

	Key    string `gorm:"type:VARCHAR"`
	Rights Rights `gorm:"type:INT ARRAY"`
	Name   string `gorm:"type:VARCHAR"`

	EntityID   string `gorm:"type:UUID;index:api_key_entity_index;not null"`
	EntityType string `gorm:"type:VARCHAR(32);index:api_key_entity_index;not null"`
}

APIKey model.

type APIKeyStore

type APIKeyStore interface {
	// Create a new API key for the given entity.
	CreateAPIKey(ctx context.Context, entityID ttnpb.Identifiers, key *ttnpb.APIKey) error
	// Find API keys of the given entity.
	FindAPIKeys(ctx context.Context, entityID ttnpb.Identifiers) ([]*ttnpb.APIKey, error)
	// Get an API key by its ID.
	GetAPIKey(ctx context.Context, id string) (ttnpb.Identifiers, *ttnpb.APIKey, error)
	// Update key rights on an entity. Rights can be deleted by not passing any rights, in which case the returned API key will be nil.
	UpdateAPIKey(ctx context.Context, entityID ttnpb.Identifiers, key *ttnpb.APIKey) (*ttnpb.APIKey, error)
}

APIKeyStore interface for storing API keys for entities (applications, clients, gateways, organizations or users).

func GetAPIKeyStore

func GetAPIKeyStore(db *gorm.DB) APIKeyStore

GetAPIKeyStore returns an APIKeyStore on the given db (or transaction).

type AccessToken

type AccessToken struct {
	Model

	Client   *Client
	ClientID string `gorm:"type:UUID;index;not null"`

	User   *User
	UserID string `gorm:"type:UUID;index;not null"`

	UserSessionID *string `gorm:"type:UUID;index"`

	Rights Rights `gorm:"type:INT ARRAY"`

	TokenID string `gorm:"type:VARCHAR;unique_index:access_token_id_index;not null"`

	Previous   *AccessToken `gorm:"foreignkey:PreviousID;association_foreignkey:TokenID"`
	PreviousID string       `gorm:"type:VARCHAR;index:access_token_previous_index"`

	AccessToken  string `gorm:"type:VARCHAR;not null"`
	RefreshToken string `gorm:"type:VARCHAR;not null"`

	ExpiresAt time.Time
}

AccessToken model.

type Account

type Account struct {
	Model
	SoftDelete

	UID string `gorm:"type:VARCHAR(36);unique_index:account_uid_index"`

	AccountID   string `gorm:"type:UUID;index:account_id_index;not null"`
	AccountType string `gorm:"type:VARCHAR(32);index:account_id_index;not null"` // user or organization

	Memberships []*Membership
}

Account model.

func (Account) OrganizationOrUserIdentifiers

func (a Account) OrganizationOrUserIdentifiers() *ttnpb.OrganizationOrUserIdentifiers

OrganizationOrUserIdentifiers for the account, depending on its type.

type Application

type Application struct {
	Model
	SoftDelete

	// BEGIN common fields
	ApplicationID string       `gorm:"unique_index:application_id_index;type:VARCHAR(36);not null"`
	Name          string       `gorm:"type:VARCHAR"`
	Description   string       `gorm:"type:TEXT"`
	Attributes    []Attribute  `gorm:"polymorphic:Entity;polymorphic_value:application"`
	APIKeys       []APIKey     `gorm:"polymorphic:Entity;polymorphic_value:application"`
	Memberships   []Membership `gorm:"polymorphic:Entity;polymorphic_value:application"`
}

Application model.

type ApplicationStore

type ApplicationStore interface {
	CreateApplication(ctx context.Context, app *ttnpb.Application) (*ttnpb.Application, error)
	FindApplications(ctx context.Context, ids []*ttnpb.ApplicationIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.Application, error)
	GetApplication(ctx context.Context, id *ttnpb.ApplicationIdentifiers, fieldMask *types.FieldMask) (*ttnpb.Application, error)
	UpdateApplication(ctx context.Context, app *ttnpb.Application, fieldMask *types.FieldMask) (*ttnpb.Application, error)
	DeleteApplication(ctx context.Context, id *ttnpb.ApplicationIdentifiers) error
}

ApplicationStore interface for storing Applications.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetApplicationStore

func GetApplicationStore(db *gorm.DB) ApplicationStore

GetApplicationStore returns an ApplicationStore on the given db (or transaction).

type Attribute

type Attribute struct {
	ID string `gorm:"type:UUID;primary_key;default:gen_random_uuid()"`

	EntityID   string `gorm:"type:UUID;index:attribute_entity_index;not null"`
	EntityType string `gorm:"type:VARCHAR(32);index:attribute_entity_index;not null"`

	Key   string `gorm:"type:VARCHAR"`
	Value string `gorm:"type:VARCHAR"`
}

Attribute model.

type AuthorizationCode

type AuthorizationCode struct {
	Model

	Client   *Client
	ClientID string `gorm:"type:UUID;index;not null"`

	User   *User
	UserID string `gorm:"type:UUID;index;not null"`

	UserSessionID *string `gorm:"type:UUID;index"`

	Rights Rights `gorm:"type:INT ARRAY"`

	Code        string `gorm:"type:VARCHAR;unique_index:authorization_code_code_index;not null"`
	RedirectURI string `gorm:"type:VARCHAR;column:redirect_uri"`
	State       string `gorm:"type:VARCHAR"`
	ExpiresAt   time.Time
}

AuthorizationCode model.

type Client

type Client struct {
	Model
	SoftDelete

	// BEGIN common fields
	ClientID    string       `gorm:"unique_index:client_id_index;type:VARCHAR(36);not null"`
	Name        string       `gorm:"type:VARCHAR"`
	Description string       `gorm:"type:TEXT"`
	Attributes  []Attribute  `gorm:"polymorphic:Entity;polymorphic_value:client"`
	Memberships []Membership `gorm:"polymorphic:Entity;polymorphic_value:client"`

	ClientSecret string         `gorm:"type:VARCHAR"`
	RedirectURIs pq.StringArray `gorm:"type:VARCHAR ARRAY;column:redirect_uris"`

	State int `gorm:"not null"`

	SkipAuthorization bool `gorm:"not null"`
	Endorsed          bool `gorm:"not null"`

	Grants Grants `gorm:"type:INT ARRAY"`
	Rights Rights `gorm:"type:INT ARRAY"`
}

Client model.

type ClientAuthorization

type ClientAuthorization struct {
	Model

	Client   *Client
	ClientID string `gorm:"type:UUID;index;not null"`

	User   *User
	UserID string `gorm:"type:UUID;index;not null"`

	Rights Rights `gorm:"type:INT ARRAY"`
}

ClientAuthorization model. Is also embedded by other OAuth models.

type ClientStore

type ClientStore interface {
	CreateClient(ctx context.Context, cli *ttnpb.Client) (*ttnpb.Client, error)
	FindClients(ctx context.Context, ids []*ttnpb.ClientIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.Client, error)
	GetClient(ctx context.Context, id *ttnpb.ClientIdentifiers, fieldMask *types.FieldMask) (*ttnpb.Client, error)
	UpdateClient(ctx context.Context, cli *ttnpb.Client, fieldMask *types.FieldMask) (*ttnpb.Client, error)
	DeleteClient(ctx context.Context, id *ttnpb.ClientIdentifiers) error
}

ClientStore interface for storing Clients.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetClientStore

func GetClientStore(db *gorm.DB) ClientStore

GetClientStore returns an ClientStore on the given db (or transaction).

type ContactInfo

type ContactInfo struct {
	ID string `gorm:"type:UUID;primary_key;default:gen_random_uuid()"`

	ContactType   int    `gorm:"not null"`
	ContactMethod int    `gorm:"not null"`
	Value         string `gorm:"type:VARCHAR"`

	Public bool

	ValidatedAt *time.Time

	EntityID   string `gorm:"type:UUID;index:contact_info_entity_index;not null"`
	EntityType string `gorm:"type:VARCHAR(32);index:contact_info_entity_index;not null"`
}

ContactInfo model.

type ContactInfoStore

type ContactInfoStore interface {
	GetContactInfo(ctx context.Context, entityID ttnpb.Identifiers) ([]*ttnpb.ContactInfo, error)
	SetContactInfo(ctx context.Context, entityID ttnpb.Identifiers, contactInfo []*ttnpb.ContactInfo) ([]*ttnpb.ContactInfo, error)
	CreateValidation(ctx context.Context, validation *ttnpb.ContactInfoValidation) (*ttnpb.ContactInfoValidation, error)
	// Confirm a validation. Only the ID and Token need to be set.
	Validate(ctx context.Context, validation *ttnpb.ContactInfoValidation) error
}

ContactInfoStore interface for contact info validation.

func GetContactInfoStore

func GetContactInfoStore(db *gorm.DB) ContactInfoStore

GetContactInfoStore returns an ContactInfoStore on the given db (or transaction).

type ContactInfoValidation

type ContactInfoValidation struct {
	Model

	Reference string `gorm:"type:VARCHAR;index:contact_info_validation_id_index"`
	Token     string `gorm:"type:VARCHAR;index:contact_info_validation_id_index"`

	EntityID   string `gorm:"type:UUID;index:contact_info_validation_entity_index;not null"`
	EntityType string `gorm:"type:VARCHAR(32);index:contact_info_validation_entity_index;not null"`

	ContactMethod int    `gorm:"not null"`
	Value         string `gorm:"type:VARCHAR"`

	ExpiresAt time.Time
}

ContactInfoValidation model.

type EUI64

type EUI64 types.EUI64

EUI64 adds methods on a types.EUI64 so that it can be stored in an SQL database.

func (*EUI64) Scan

func (eui *EUI64) Scan(src interface{}) (err error)

Scan reads the value from the database into the EUI.

func (EUI64) Value

func (eui EUI64) Value() (driver.Value, error)

Value returns the value to store in the database.

type EndDevice

type EndDevice struct {
	Model

	ApplicationID string `gorm:"unique_index:end_device_id_index;type:VARCHAR(36);not null;index:end_device_application_index"`
	Application   *Application

	// BEGIN common fields
	DeviceID    string      `gorm:"unique_index:end_device_id_index;type:VARCHAR(36);not null"`
	Name        string      `gorm:"type:VARCHAR"`
	Description string      `gorm:"type:TEXT"`
	Attributes  []Attribute `gorm:"polymorphic:Entity;polymorphic_value:device"`

	JoinEUI *EUI64 `gorm:"unique_index:end_device_eui_index;index:end_device_join_eui_index;type:VARCHAR(16);column:join_eui"`
	DevEUI  *EUI64 `gorm:"unique_index:end_device_eui_index;index:end_device_dev_eui_index;type:VARCHAR(16);column:dev_eui"`

	BrandID         string `gorm:"type:VARCHAR"`
	ModelID         string `gorm:"type:VARCHAR"`
	HardwareVersion string `gorm:"type:VARCHAR"`
	FirmwareVersion string `gorm:"type:VARCHAR"`

	NetworkServerAddress     string `gorm:"type:VARCHAR"`
	ApplicationServerAddress string `gorm:"type:VARCHAR"`
	JoinServerAddress        string `gorm:"type:VARCHAR"`

	ServiceProfileID string `gorm:"type:VARCHAR"`

	Locations []EndDeviceLocation

	Picture   *Picture
	PictureID *string `gorm:"type:UUID;index:end_device_picture_index"`
}

EndDevice model.

type EndDeviceLocation

type EndDeviceLocation struct {
	Model

	EndDeviceID string `gorm:"type:UUID;unique_index:end_device_location_id_index;index:end_device_device_index;not null"`
	Service     string `gorm:"unique_index:end_device_location_id_index"`

	Location

	Source int `gorm:"not null"`
}

EndDeviceLocation model.

type EndDeviceStore

type EndDeviceStore interface {
	CreateEndDevice(ctx context.Context, dev *ttnpb.EndDevice) (*ttnpb.EndDevice, error)
	CountEndDevices(ctx context.Context, ids *ttnpb.ApplicationIdentifiers) (uint64, error)
	ListEndDevices(ctx context.Context, ids *ttnpb.ApplicationIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.EndDevice, error)
	FindEndDevices(ctx context.Context, ids []*ttnpb.EndDeviceIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.EndDevice, error)
	GetEndDevice(ctx context.Context, id *ttnpb.EndDeviceIdentifiers, fieldMask *types.FieldMask) (*ttnpb.EndDevice, error)
	UpdateEndDevice(ctx context.Context, dev *ttnpb.EndDevice, fieldMask *types.FieldMask) (*ttnpb.EndDevice, error)
	DeleteEndDevice(ctx context.Context, id *ttnpb.EndDeviceIdentifiers) error
}

EndDeviceStore interface for storing EndDevices.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetEndDeviceStore

func GetEndDeviceStore(db *gorm.DB) EndDeviceStore

GetEndDeviceStore returns an EndDeviceStore on the given db (or transaction).

type EntitySearch

type EntitySearch interface {
	FindEntities(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchEntitiesRequest, entityType string) ([]ttnpb.Identifiers, error)
	FindEndDevices(ctx context.Context, req *ttnpb.SearchEndDevicesRequest) ([]*ttnpb.EndDeviceIdentifiers, error)
}

EntitySearch interface for searching entities.

func GetEntitySearch

func GetEntitySearch(db *gorm.DB) EntitySearch

GetEntitySearch returns an EntitySearch on the given db (or transaction).

type Gateway

type Gateway struct {
	Model
	SoftDelete

	GatewayEUI *EUI64 `gorm:"unique_index:gateway_eui_index;type:VARCHAR(16);column:gateway_eui"`

	// BEGIN common fields
	GatewayID   string       `gorm:"unique_index:gateway_id_index;type:VARCHAR(36);not null"`
	Name        string       `gorm:"type:VARCHAR"`
	Description string       `gorm:"type:TEXT"`
	Attributes  []Attribute  `gorm:"polymorphic:Entity;polymorphic_value:gateway"`
	APIKeys     []APIKey     `gorm:"polymorphic:Entity;polymorphic_value:gateway"`
	Memberships []Membership `gorm:"polymorphic:Entity;polymorphic_value:gateway"`

	BrandID         string `gorm:"type:VARCHAR"`
	ModelID         string `gorm:"type:VARCHAR"`
	HardwareVersion string `gorm:"type:VARCHAR"`
	FirmwareVersion string `gorm:"type:VARCHAR"`

	GatewayServerAddress string `gorm:"type:VARCHAR"`

	AutoUpdate    bool   `gorm:"not null"`
	UpdateChannel string `gorm:"type:VARCHAR"`

	// Frequency Plan IDs separated by spaces.
	FrequencyPlanID string `gorm:"type:VARCHAR"`

	StatusPublic   bool `gorm:"not null"`
	LocationPublic bool `gorm:"not null"`

	ScheduleDownlinkLate   bool  `gorm:"not null"`
	EnforceDutyCycle       bool  `gorm:"not null"`
	ScheduleAnytimeDelay   int64 `gorm:"default:0 not null"`
	DownlinkPathConstraint int

	UpdateLocationFromStatus bool `gorm:"default:false not null"`

	Antennas []GatewayAntenna
}

Gateway model.

func (*Gateway) AfterDelete

func (gtw *Gateway) AfterDelete(db *gorm.DB) error

AfterDelete releases the EUI of a Gateway after it is deleted.

type GatewayAntenna

type GatewayAntenna struct {
	Model

	Gateway   *Gateway
	GatewayID string `gorm:"type:UUID;unique_index:gateway_antenna_id_index;index:gateway_antenna_gateway_index;not null"`
	Index     int    `gorm:"unique_index:gateway_antenna_id_index;not null"`

	Attributes []Attribute `gorm:"polymorphic:Entity;polymorphic_value:gateway"`

	Gain float32

	Location
}

GatewayAntenna model.

type GatewayStore

type GatewayStore interface {
	CreateGateway(ctx context.Context, gtw *ttnpb.Gateway) (*ttnpb.Gateway, error)
	FindGateways(ctx context.Context, ids []*ttnpb.GatewayIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.Gateway, error)
	GetGateway(ctx context.Context, id *ttnpb.GatewayIdentifiers, fieldMask *types.FieldMask) (*ttnpb.Gateway, error)
	UpdateGateway(ctx context.Context, gtw *ttnpb.Gateway, fieldMask *types.FieldMask) (*ttnpb.Gateway, error)
	DeleteGateway(ctx context.Context, id *ttnpb.GatewayIdentifiers) error
}

GatewayStore interface for storing Gateways.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetGatewayStore

func GetGatewayStore(db *gorm.DB) GatewayStore

GetGatewayStore returns an GatewayStore on the given db (or transaction).

type Grants

type Grants []ttnpb.GrantType

Grants adds methods on a []ttnpb.GrantType so that it can be stored in an SQL database.

func (*Grants) Scan

func (g *Grants) Scan(src interface{}) error

Scan reads the value from the database into the Grants.

func (Grants) Value

func (g Grants) Value() (driver.Value, error)

Value returns the value to store in the database.

type IndirectMembership

type IndirectMembership struct {
	RightsOnOrganization *ttnpb.Rights
	*ttnpb.OrganizationIdentifiers
	OrganizationRights *ttnpb.Rights
}

IndirectMembership returns an indirect membership through an organization.

type Invitation

type Invitation struct {
	Model

	Email     string `gorm:"type:VARCHAR;unique_index:invitation_email_index;not null"`
	Token     string `gorm:"type:VARCHAR;unique_index:invitation_token_index;not null"`
	ExpiresAt time.Time

	AcceptedBy   *User
	AcceptedByID *string `gorm:"type:UUID"`
	AcceptedAt   *time.Time
}

Invitation model.

type InvitationStore

type InvitationStore interface {
	CreateInvitation(ctx context.Context, invitation *ttnpb.Invitation) (*ttnpb.Invitation, error)
	FindInvitations(ctx context.Context) ([]*ttnpb.Invitation, error)
	GetInvitation(ctx context.Context, token string) (*ttnpb.Invitation, error)
	SetInvitationAcceptedBy(ctx context.Context, token string, accepter *ttnpb.UserIdentifiers) error
	DeleteInvitation(ctx context.Context, email string) error
}

InvitationStore interface for storing user invitations.

func GetInvitationStore

func GetInvitationStore(db *gorm.DB) InvitationStore

GetInvitationStore returns an InvitationStore on the given db (or transaction).

type Location

type Location struct {
	Latitude  float64
	Longitude float64
	Altitude  int32
	Accuracy  int32
}

Location can be embedded in other models.

type Membership

type Membership struct {
	Model

	Account    *Account
	AccountID  string `gorm:"type:UUID;index:membership_account_index;not null"`
	Rights     Rights `gorm:"type:INT ARRAY"`
	EntityID   string `gorm:"type:UUID;index:membership_entity_index;not null"`
	EntityType string `gorm:"type:VARCHAR(32);index:membership_entity_index;not null"`
}

Membership model.

type MembershipStore

type MembershipStore interface {
	// Find direct and optionally also indirect memberships of the organization or user.
	FindMemberships(ctx context.Context, id *ttnpb.OrganizationOrUserIdentifiers, entityType string, includeIndirect bool) ([]ttnpb.Identifiers, error)
	// Find indirect memberships (through organizations) between the user and entity.
	FindIndirectMemberships(ctx context.Context, userID *ttnpb.UserIdentifiers, entityID ttnpb.Identifiers) ([]IndirectMembership, error)

	// Find direct members and rights of the given entity.
	FindMembers(ctx context.Context, entityID ttnpb.Identifiers) (map[*ttnpb.OrganizationOrUserIdentifiers]*ttnpb.Rights, error)
	// Get direct member rights on an entity.
	GetMember(ctx context.Context, id *ttnpb.OrganizationOrUserIdentifiers, entityID ttnpb.Identifiers) (*ttnpb.Rights, error)
	// Set direct member rights on an entity. Rights can be deleted by not passing any rights.
	SetMember(ctx context.Context, id *ttnpb.OrganizationOrUserIdentifiers, entityID ttnpb.Identifiers, rights *ttnpb.Rights) error
}

MembershipStore interface for storing membership (collaboration) relations between accounts (users or organizations) and entities (applications, clients, gateways or organizations).

As the operations in this store may be quite expensive, the results of FindXXX operations should typically be cached. The recommended cache behavior is:

func GetMembershipCache

func GetMembershipCache(store MembershipStore, redis *redis.Client, ttl time.Duration) MembershipStore

GetMembershipCache wraps the MembershipStore with a cache. Make sure to not call FindIndirectMemberships or GetMember after calling SetMember in the same transaction, this may result in an inconsistent cache.

func GetMembershipStore

func GetMembershipStore(db *gorm.DB) MembershipStore

GetMembershipStore returns an MembershipStore on the given db (or transaction).

type Model

type Model struct {
	ID        string    `gorm:"type:UUID;primary_key;default:gen_random_uuid()"`
	CreatedAt time.Time `gorm:"not null"`
	UpdatedAt time.Time `gorm:"not null"`
	// contains filtered or unexported fields
}

Model is the base of database models.

func (Model) PrimaryKey

func (m Model) PrimaryKey() string

PrimaryKey returns the primary key of the model.

func (*Model) SetContext

func (m *Model) SetContext(ctx context.Context)

SetContext needs to be called before creating models.

type OAuthStore

type OAuthStore interface {
	ListAuthorizations(ctx context.Context, userIDs *ttnpb.UserIdentifiers) ([]*ttnpb.OAuthClientAuthorization, error)
	GetAuthorization(ctx context.Context, userIDs *ttnpb.UserIdentifiers, clientIDs *ttnpb.ClientIdentifiers) (*ttnpb.OAuthClientAuthorization, error)
	Authorize(ctx context.Context, req *ttnpb.OAuthClientAuthorization) (authorization *ttnpb.OAuthClientAuthorization, err error)
	DeleteAuthorization(ctx context.Context, userIDs *ttnpb.UserIdentifiers, clientIDs *ttnpb.ClientIdentifiers) error

	CreateAuthorizationCode(ctx context.Context, code *ttnpb.OAuthAuthorizationCode) error
	GetAuthorizationCode(ctx context.Context, code string) (*ttnpb.OAuthAuthorizationCode, error)
	DeleteAuthorizationCode(ctx context.Context, code string) error

	CreateAccessToken(ctx context.Context, token *ttnpb.OAuthAccessToken, previousID string) error
	ListAccessTokens(ctx context.Context, userIDs *ttnpb.UserIdentifiers, clientIDs *ttnpb.ClientIdentifiers) ([]*ttnpb.OAuthAccessToken, error)
	GetAccessToken(ctx context.Context, id string) (*ttnpb.OAuthAccessToken, error)
	DeleteAccessToken(ctx context.Context, id string) error
}

OAuthStore interface for the OAuth server.

For internal use (by the OAuth server) only.

func GetOAuthStore

func GetOAuthStore(db *gorm.DB) OAuthStore

GetOAuthStore returns an OAuthStore on the given db (or transaction).

type Organization

type Organization struct {
	Model
	SoftDelete

	Account Account `gorm:"polymorphic:Account;polymorphic_value:organization"`

	// BEGIN common fields
	Name        string       `gorm:"type:VARCHAR"`
	Description string       `gorm:"type:TEXT"`
	Attributes  []Attribute  `gorm:"polymorphic:Entity;polymorphic_value:organization"`
	APIKeys     []APIKey     `gorm:"polymorphic:Entity;polymorphic_value:organization"`
	Memberships []Membership `gorm:"polymorphic:Entity;polymorphic_value:organization"`
}

Organization model.

func (*Organization) AfterDelete

func (org *Organization) AfterDelete(db *gorm.DB) error

AfterDelete deletes the Account of an Organization after it is deleted.

func (*Organization) SetContext

func (org *Organization) SetContext(ctx context.Context)

SetContext sets the context on the organization model and the embedded account model.

type OrganizationStore

type OrganizationStore interface {
	CreateOrganization(ctx context.Context, org *ttnpb.Organization) (*ttnpb.Organization, error)
	FindOrganizations(ctx context.Context, ids []*ttnpb.OrganizationIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.Organization, error)
	GetOrganization(ctx context.Context, id *ttnpb.OrganizationIdentifiers, fieldMask *types.FieldMask) (*ttnpb.Organization, error)
	UpdateOrganization(ctx context.Context, org *ttnpb.Organization, fieldMask *types.FieldMask) (*ttnpb.Organization, error)
	DeleteOrganization(ctx context.Context, id *ttnpb.OrganizationIdentifiers) error
}

OrganizationStore interface for storing Organizations.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetOrganizationStore

func GetOrganizationStore(db *gorm.DB) OrganizationStore

GetOrganizationStore returns an OrganizationStore on the given db (or transaction).

type Picture

type Picture struct {
	Model
	SoftDelete // Filter on deleted_at not being NULL to clean up storage bucket.

	Data []byte `gorm:"type:BYTEA"`
}

Picture model.

type Populator

type Populator struct {
	Applications  []*ttnpb.Application
	Clients       []*ttnpb.Client
	Gateways      []*ttnpb.Gateway
	Organizations []*ttnpb.Organization
	Users         []*ttnpb.User

	APIKeys     map[*ttnpb.EntityIdentifiers][]*ttnpb.APIKey
	Memberships map[*ttnpb.EntityIdentifiers][]*ttnpb.Collaborator
}

Populator is intended to populate a database with test data.

func NewPopulator

func NewPopulator(size int, seed int64) *Populator

NewPopulator returns a new database populator with a population of the given size. It is seeded by the given seed.

func (*Populator) Populate

func (p *Populator) Populate(ctx context.Context, db *gorm.DB) (err error)

Populate the database.

type Rights

type Rights ttnpb.Rights

Rights adds methods on a ttnpb.Rights so that it can be stored in an SQL database.

func (*Rights) Scan

func (r *Rights) Scan(src interface{}) error

Scan reads the value from the database into the Rights.

func (Rights) Value

func (r Rights) Value() (driver.Value, error)

Value returns the value to store in the database.

type SoftDelete

type SoftDelete struct {
	DeletedAt *time.Time `gorm:"index"`
}

SoftDelete makes a Delete operation set a DeletedAt instead of actually deleting the model.

type User

type User struct {
	Model
	SoftDelete

	Account Account `gorm:"polymorphic:Account;polymorphic_value:user"`

	// BEGIN common fields
	Name        string      `gorm:"type:VARCHAR"`
	Description string      `gorm:"type:TEXT"`
	Attributes  []Attribute `gorm:"polymorphic:Entity;polymorphic_value:user"`
	APIKeys     []APIKey    `gorm:"polymorphic:Entity;polymorphic_value:user"`

	Sessions []*UserSession

	PrimaryEmailAddress            string     `gorm:"type:VARCHAR;not null;unique_index"`
	PrimaryEmailAddressValidatedAt *time.Time // should be cleared when email changes

	Password              string    `gorm:"type:VARCHAR;not null"` // this is the hash
	PasswordUpdatedAt     time.Time `gorm:"not null"`
	RequirePasswordUpdate bool      `gorm:"not null"`

	State int `gorm:"not null"`

	Admin bool `gorm:"not null"`

	TemporaryPassword          string `gorm:"type:VARCHAR"`
	TemporaryPasswordCreatedAt *time.Time
	TemporaryPasswordExpiresAt *time.Time

	ProfilePicture   *Picture
	ProfilePictureID *string `gorm:"type:UUID;index:user_profile_picture_index"`
}

User model.

func (*User) AfterDelete

func (usr *User) AfterDelete(db *gorm.DB) error

AfterDelete deletes the Account of a User after it is deleted.

func (*User) SetContext

func (usr *User) SetContext(ctx context.Context)

SetContext sets the context on both the Model and Account.

type UserSession

type UserSession struct {
	Model

	User   *User
	UserID string `gorm:"type:UUID;index:user_session_user_index;not null"`

	ExpiresAt *time.Time
}

UserSession is the session of a logged in user.

type UserSessionStore

type UserSessionStore interface {
	CreateSession(ctx context.Context, sess *ttnpb.UserSession) (*ttnpb.UserSession, error)
	FindSessions(ctx context.Context, userIDs *ttnpb.UserIdentifiers) ([]*ttnpb.UserSession, error)
	GetSession(ctx context.Context, userIDs *ttnpb.UserIdentifiers, sessionID string) (*ttnpb.UserSession, error)
	UpdateSession(ctx context.Context, sess *ttnpb.UserSession) (*ttnpb.UserSession, error)
	DeleteSession(ctx context.Context, userIDs *ttnpb.UserIdentifiers, sessionID string) error
}

UserSessionStore interface for storing User sessions.

For internal use (by the OAuth server) only.

func GetUserSessionStore

func GetUserSessionStore(db *gorm.DB) UserSessionStore

GetUserSessionStore returns an UserSessionStore on the given db (or transaction).

type UserStore

type UserStore interface {
	CreateUser(ctx context.Context, usr *ttnpb.User) (*ttnpb.User, error)
	FindUsers(ctx context.Context, ids []*ttnpb.UserIdentifiers, fieldMask *types.FieldMask) ([]*ttnpb.User, error)
	GetUser(ctx context.Context, id *ttnpb.UserIdentifiers, fieldMask *types.FieldMask) (*ttnpb.User, error)
	UpdateUser(ctx context.Context, usr *ttnpb.User, fieldMask *types.FieldMask) (*ttnpb.User, error)
	DeleteUser(ctx context.Context, id *ttnpb.UserIdentifiers) error
}

UserStore interface for storing Users.

All functions assume the input and fieldMask to be validated, and assume sufficient rights to perform the action.

func GetUserStore

func GetUserStore(db *gorm.DB) UserStore

GetUserStore returns an UserStore on the given db (or transaction).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL