postgres

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxResultSize = 100
)

Variables

View Source
var (
	ErrNamespaceNotFound = errors.New("namespace not found")
)

Functions

This section is empty.

Types

type AssetModel

type AssetModel struct {
	ID          string    `db:"id"`
	NamespaceID string    `db:"namespace_id"`
	URN         string    `db:"urn"`
	Type        string    `db:"type"`
	Name        string    `db:"name"`
	Service     string    `db:"service"`
	Description string    `db:"description"`
	Data        JSONMap   `db:"data"`
	URL         string    `db:"url"`
	Labels      JSONMap   `db:"labels"`
	Version     string    `db:"version"`
	UpdatedBy   UserModel `db:"updated_by"`
	CreatedAt   time.Time `db:"created_at"`
	UpdatedAt   time.Time `db:"updated_at"`
	// version specific information
	Changelog types.JSONText `db:"changelog"`
	Owners    types.JSONText `db:"owners"`
}

type AssetProbeModel

type AssetProbeModel struct {
	ID           string    `db:"id"`
	AssetURN     string    `db:"asset_urn"`
	Status       string    `db:"status"`
	StatusReason string    `db:"status_reason"`
	Metadata     JSONMap   `db:"metadata"`
	Timestamp    time.Time `db:"timestamp"`
	CreatedAt    time.Time `db:"created_at"`
}

type AssetRepository

type AssetRepository struct {
	// contains filtered or unexported fields
}

AssetRepository is a type that manages user operation to the primary database

func NewAssetRepository

func NewAssetRepository(c *Client, userRepo *UserRepository, defaultGetMaxSize int, defaultUserProvider string) (*AssetRepository, error)

NewAssetRepository initializes user repository clients

func (*AssetRepository) AddProbe

func (r *AssetRepository) AddProbe(ctx context.Context, ns *namespace.Namespace, assetURN string, probe *asset.Probe) error

func (*AssetRepository) BuildFilterQuery

func (r *AssetRepository) BuildFilterQuery(builder sq.SelectBuilder, flt asset.Filter) sq.SelectBuilder

BuildFilterQuery retrieves the sql query based on applied filter in the queryString

func (*AssetRepository) DeleteByID

func (r *AssetRepository) DeleteByID(ctx context.Context, id string) error

DeleteByID removes asset using its ID

func (*AssetRepository) DeleteByURN

func (r *AssetRepository) DeleteByURN(ctx context.Context, urn string) error

func (*AssetRepository) GetAll

func (r *AssetRepository) GetAll(ctx context.Context, flt asset.Filter) ([]asset.Asset, error)

GetAll retrieves list of assets with filters

func (*AssetRepository) GetByID

func (r *AssetRepository) GetByID(ctx context.Context, id string) (asset.Asset, error)

GetByID retrieves asset by its ID

func (*AssetRepository) GetByURN

func (r *AssetRepository) GetByURN(ctx context.Context, urn string) (asset.Asset, error)

func (*AssetRepository) GetByVersionWithID

func (r *AssetRepository) GetByVersionWithID(ctx context.Context, id string, version string) (asset.Asset, error)

GetByVersionWithID retrieves the specific asset version

func (*AssetRepository) GetByVersionWithURN

func (r *AssetRepository) GetByVersionWithURN(ctx context.Context, urn string, version string) (asset.Asset, error)

func (*AssetRepository) GetCount

func (r *AssetRepository) GetCount(ctx context.Context, flt asset.Filter) (total int, err error)

GetCount retrieves number of assets for every type

func (*AssetRepository) GetProbes

func (r *AssetRepository) GetProbes(ctx context.Context, assetURN string) ([]asset.Probe, error)

func (*AssetRepository) GetProbesWithFilter

func (r *AssetRepository) GetProbesWithFilter(ctx context.Context, flt asset.ProbesFilter) (map[string][]asset.Probe, error)

func (*AssetRepository) GetTypes

func (r *AssetRepository) GetTypes(ctx context.Context, flt asset.Filter) (map[asset.Type]int, error)

GetTypes fetches types with assets count for all available types and returns them as a map[typeName]count

func (*AssetRepository) GetVersionHistory

func (r *AssetRepository) GetVersionHistory(ctx context.Context, flt asset.Filter, id string) (avs []asset.Asset, err error)

GetVersionHistory retrieves the versions of an asset

func (*AssetRepository) Upsert

func (r *AssetRepository) Upsert(ctx context.Context, ns *namespace.Namespace, ast *asset.Asset) (string, error)

Upsert creates a new asset if it does not exist yet. It updates if asset does exist. Checking existence is done using "urn", "type", and "service" fields.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapper over sqlx for strict multi-tenancy using postgres RLS

func NewClient

func NewClient(cfg Config) (*Client, error)

NewClient initializes database connection

func (*Client) Close

func (c *Client) Close() error

func (*Client) ExecContext

func (c *Client) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error)

func (*Client) ExecQueries

func (c *Client) ExecQueries(ctx context.Context, queries []string) error

ExecQueries is used for executing list of _db query

func (*Client) GetContext

func (c *Client) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*Client) Migrate

func (c *Client) Migrate(cfg Config) (ver uint, err error)

func (*Client) MigrateDown

func (c *Client) MigrateDown(cfg Config) (ver uint, err error)

func (*Client) QueryFn

func (c *Client) QueryFn(ctx context.Context, f func(*sqlx.Conn) error) error

func (*Client) RunWithinTx

func (c *Client) RunWithinTx(ctx context.Context, f func(tx *sqlx.Tx) error) error

func (*Client) SelectContext

func (c *Client) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

type Config

type Config struct {
	Host     string `mapstructure:"host" default:"localhost"`
	Port     int    `mapstructure:"port" default:"5432"`
	Name     string `mapstructure:"name" default:"postgres"`
	User     string `mapstructure:"user" default:"root"`
	Password string `mapstructure:"password" default:""`
	SSLMode  string `mapstructure:"sslmode" default:"disable"`
}

func (*Config) ConnectionURL

func (c *Config) ConnectionURL() *url.URL

ConnectionURL

type DiscussionModel

type DiscussionModel struct {
	ID          string         `db:"id"`
	NamespaceID string         `db:"namespace_id"`
	Title       string         `db:"title"`
	Body        string         `db:"body"`
	Type        string         `db:"type"`
	State       string         `db:"state"`
	Owner       UserModel      `db:"owner"`
	Labels      pq.StringArray `db:"labels"`
	Assets      pq.StringArray `db:"assets"`
	Assignees   pq.StringArray `db:"assignees"`
	CreatedAt   time.Time      `db:"created_at"`
	UpdatedAt   time.Time      `db:"updated_at"`
}

type DiscussionRepository

type DiscussionRepository struct {
	// contains filtered or unexported fields
}

DiscussionRepository is a type that manages discussion operation to the primary database

func NewDiscussionRepository

func NewDiscussionRepository(c *Client, defaultGetMaxSize int) (*DiscussionRepository, error)

NewDiscussionRepository initializes discussion repository clients

func (*DiscussionRepository) Create

Create inserts a new discussion data

func (*DiscussionRepository) CreateComment

CreateComment adds a new comment to a specific discussion

func (*DiscussionRepository) DeleteComment

func (r *DiscussionRepository) DeleteComment(ctx context.Context, cid string, did string) error

Delete removes a comment

func (*DiscussionRepository) Get

Get returns a specific discussion by id

func (*DiscussionRepository) GetAll

GetAll fetchs all discussion data

func (*DiscussionRepository) GetAllComments

func (r *DiscussionRepository) GetAllComments(ctx context.Context, did string, flt discussion.Filter) ([]discussion.Comment, error)

GetAllComments fetches all comments of a specific discussion

func (*DiscussionRepository) GetComment

func (r *DiscussionRepository) GetComment(ctx context.Context, cid string, did string) (discussion.Comment, error)

Get fetchs a comment

func (*DiscussionRepository) Patch

Patch will update a field in discussion

func (*DiscussionRepository) UpdateComment

func (r *DiscussionRepository) UpdateComment(ctx context.Context, cmt *discussion.Comment) error

Update updates a comment

type JSONMap

type JSONMap map[string]interface{}

func (JSONMap) MarshalJSON

func (m JSONMap) MarshalJSON() ([]byte, error)

MarshalJSON to output encoded []byte

func (*JSONMap) Scan

func (m *JSONMap) Scan(value interface{}) error

func (*JSONMap) UnmarshalJSON

func (m *JSONMap) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (JSONMap) Value

func (m JSONMap) Value() (driver.Value, error)

type LineageEdgeModel

type LineageEdgeModel struct {
	NamespaceID string  `db:"namespace_id"`
	Source      string  `db:"source"`
	Target      string  `db:"target"`
	Prop        JSONMap `db:"prop"`
}

type LineageGraphModel

type LineageGraphModel []LineageEdgeModel

type LineageRepository

type LineageRepository struct {
	// contains filtered or unexported fields
}

func NewLineageRepository

func NewLineageRepository(client *Client) (*LineageRepository, error)

NewLineageRepository initializes lineage repository

func (*LineageRepository) DeleteByURN

func (repo *LineageRepository) DeleteByURN(ctx context.Context, urn string) error

func (*LineageRepository) GetGraph

func (repo *LineageRepository) GetGraph(ctx context.Context, urn string, query asset.LineageQuery) (asset.LineageGraph, error)

GetGraph returns a graph that contains list of relations of a given node

func (*LineageRepository) Upsert

func (repo *LineageRepository) Upsert(ctx context.Context, ns *namespace.Namespace, urn string, upstreams, downstreams []string) error

Upsert insert or delete connections of a given node by comparing them with current state

type NamespaceModel

type NamespaceModel struct {
	ID        uuid.UUID  `db:"id"`
	Name      string     `db:"name"`
	State     string     `db:"state"`
	Metadata  JSONMap    `db:"metadata"`
	CreatedAt time.Time  `db:"created_at"`
	UpdatedAt time.Time  `db:"updated_at"`
	DeletedAt *time.Time `db:"deleted_at"`
}

func BuildNamespaceModel

func BuildNamespaceModel(ns namespace.Namespace) (*NamespaceModel, error)

type NamespaceRepository

type NamespaceRepository struct {
	// contains filtered or unexported fields
}

func NewNamespaceRepository

func NewNamespaceRepository(c *Client) *NamespaceRepository

NewNamespaceRepository initializes namespace repository

func (*NamespaceRepository) Create

Create insert a new namespace in the database

func (*NamespaceRepository) GetByID

GetByID retrieves namespace given the uuid

func (*NamespaceRepository) GetByName

func (n *NamespaceRepository) GetByName(ctx context.Context, name string) (*namespace.Namespace, error)

func (*NamespaceRepository) List

List retrieves all namespaces

func (*NamespaceRepository) Update

Update an existing namespace to the database

type StarClauses

type StarClauses struct {
	Limit            int
	Offset           int
	SortKey          string
	SortDirectionKey string
}

type StarRepository

type StarRepository struct {
	// contains filtered or unexported fields
}

StarRepository is a type that manages star operation to the primary database

func NewStarRepository

func NewStarRepository(c *Client) (*StarRepository, error)

NewStarRepository initializes star repository clients

func (*StarRepository) Create

func (r *StarRepository) Create(ctx context.Context, ns *namespace.Namespace, userID string, assetID string) (string, error)

Create insert a new record in the stars table

func (*StarRepository) Delete

func (r *StarRepository) Delete(ctx context.Context, userID string, assetID string) error

Delete will delete/unstar a starred asset for a user id

func (*StarRepository) GetAllAssetsByUserID

func (r *StarRepository) GetAllAssetsByUserID(ctx context.Context, flt star.Filter, userID string) ([]asset.Asset, error)

GetAllAssetsByUserID fetch list of assets starred by a user

func (*StarRepository) GetAssetByUserID

func (r *StarRepository) GetAssetByUserID(ctx context.Context, userID string, assetID string) (asset.Asset, error)

GetAssetByUserID fetch a specific starred asset by user id

func (*StarRepository) GetStargazers

func (r *StarRepository) GetStargazers(ctx context.Context, flt star.Filter, assetID string) ([]user.User, error)

GetStargazers fetch list of user IDs that star an asset

type TagJoinTemplateFieldModel

type TagJoinTemplateFieldModel struct {
	Template TagTemplateModel      `db:"tag_templates"`
	Field    TagTemplateFieldModel `db:"tag_template_fields"`
}

TagJoinTemplateFieldModel is a placeholder for joined template and field

type TagJoinTemplateFieldModels

type TagJoinTemplateFieldModels []TagJoinTemplateFieldModel

TagJoinTemplateFieldModels is a slice of placeholder for joined template and field

type TagJoinTemplateTagFieldModel

type TagJoinTemplateTagFieldModel struct {
	Template TagTemplateModel      `db:"tag_templates"`
	Tag      TagModel              `db:"tags"`
	Field    TagTemplateFieldModel `db:"tag_template_fields"`
}

TemplateField is a placeholder for joined template, tag, and field

type TagJoinTemplateTagFieldModels

type TagJoinTemplateTagFieldModels []TagJoinTemplateTagFieldModel

TagJoinTemplateTagFieldModels is a slice of placeholder for joined template, tag, and field

type TagModel

type TagModel struct {
	ID          uint                  `db:"id"`
	NamespaceID string                `db:"namespace_id"`
	Value       string                `db:"value"`
	AssetID     string                `db:"asset_id"`
	FieldID     uint                  `db:"field_id"`
	CreatedAt   time.Time             `db:"created_at"`
	UpdatedAt   time.Time             `db:"updated_at"`
	Field       TagTemplateFieldModel `db:"-"`
}

TagModel is a model for tag value in database table

type TagModels

type TagModels []TagModel

type TagRepository

type TagRepository struct {
	// contains filtered or unexported fields
}

TagRepository is a type that manages tag operation ot the primary database

func NewTagRepository

func NewTagRepository(client *Client) (*TagRepository, error)

NewTagRepository initializes tag repository all methods in tag repository uses passed by reference which will mutate the reference variable in method's argument

func (*TagRepository) Create

func (r *TagRepository) Create(ctx context.Context, ns *namespace.Namespace, domainTag *tag.Tag) error

Create inserts tag to database

func (*TagRepository) Delete

func (r *TagRepository) Delete(ctx context.Context, domainTag tag.Tag) error

Delete deletes tags from database

func (*TagRepository) Read

func (r *TagRepository) Read(ctx context.Context, filter tag.Tag) ([]tag.Tag, error)

Read reads tags grouped by its template

func (*TagRepository) Update

func (r *TagRepository) Update(ctx context.Context, domainTag *tag.Tag) error

Update updates tags in the database

type TagTemplateFieldModel

type TagTemplateFieldModel struct {
	ID          uint             `db:"id"`
	URN         string           `db:"urn"`
	DisplayName string           `db:"display_name"`
	Description string           `db:"description"`
	DataType    string           `db:"data_type"`
	Options     *string          `db:"options"`
	Required    bool             `db:"required"`
	TemplateURN string           `db:"template_urn"`
	CreatedAt   time.Time        `db:"created_at"`
	UpdatedAt   time.Time        `db:"updated_at"`
	Template    TagTemplateModel `db:"-"`
	NamespaceID uuid.UUID        `db:"namespace_id"`
}

TagTemplateFieldModel is a model for field tag in database table

type TagTemplateFieldModels

type TagTemplateFieldModels []TagTemplateFieldModel

type TagTemplateModel

type TagTemplateModel struct {
	URN         string                 `db:"urn"`
	DisplayName string                 `db:"display_name"`
	Description string                 `db:"description"`
	CreatedAt   time.Time              `db:"created_at"`
	UpdatedAt   time.Time              `db:"updated_at"`
	Fields      TagTemplateFieldModels `db:"-"`
	NamespaceID uuid.UUID              `db:"namespace_id"`
}

TagTemplateModel is a model for template database table

type TagTemplateModels

type TagTemplateModels []TagTemplateModel

type TagTemplateRepository

type TagTemplateRepository struct {
	// contains filtered or unexported fields
}

TagTemplateRepository is a type that manages template operation to the primary database

func NewTagTemplateRepository

func NewTagTemplateRepository(c *Client) (*TagTemplateRepository, error)

NewTagTemplateRepository initializes template repository clients all methods in template repository uses passed by reference which will mutate the reference variable in method's argument

func (*TagTemplateRepository) Create

func (r *TagTemplateRepository) Create(ctx context.Context, ns *namespace.Namespace, templateDomain *tag.Template) error

Create inserts template to database

func (*TagTemplateRepository) Delete

func (r *TagTemplateRepository) Delete(ctx context.Context, templateURN string) error

Delete deletes template and its fields from database

func (*TagTemplateRepository) Read

func (r *TagTemplateRepository) Read(ctx context.Context, templateURN string) ([]tag.Template, error)

Read reads template from database by URN

func (*TagTemplateRepository) ReadAll

func (r *TagTemplateRepository) ReadAll(ctx context.Context) ([]tag.Template, error)

Read reads all template from database

func (*TagTemplateRepository) Update

func (r *TagTemplateRepository) Update(ctx context.Context, ns *namespace.Namespace, targetURN string, templateDomain *tag.Template) error

Update updates template into database

type UserModel

type UserModel struct {
	ID          sql.NullString `db:"id"`
	NamespaceID string         `db:"namespace_id"`
	UUID        sql.NullString `db:"uuid"`
	Email       sql.NullString `db:"email"`
	Provider    sql.NullString `db:"provider"`
	CreatedAt   sql.NullTime   `db:"created_at"`
	UpdatedAt   sql.NullTime   `db:"updated_at"`
}

type UserModels

type UserModels []UserModel

type UserRepository

type UserRepository struct {
	// contains filtered or unexported fields
}

UserRepository is a type that manages user operation to the primary database

func NewUserRepository

func NewUserRepository(c *Client) (*UserRepository, error)

NewUserRepository initializes user repository clients

func (*UserRepository) Create

func (r *UserRepository) Create(ctx context.Context, ns *namespace.Namespace, ud *user.User) (string, error)

Create insert a user to the database a new data is still inserted if either uuid or email is empty but returns error if both uuid and email are empty

func (*UserRepository) CreateWithTx

func (r *UserRepository) CreateWithTx(ctx context.Context, tx *sqlx.Tx, ns *namespace.Namespace, ud *user.User) (string, error)

CreateWithTx insert a user to the database using given transaction as client

func (*UserRepository) GetByEmail

func (r *UserRepository) GetByEmail(ctx context.Context, email string) (user.User, error)

GetByEmail retrieves user UUID given the email

func (*UserRepository) GetByUUID

func (r *UserRepository) GetByUUID(ctx context.Context, uuid string) (user.User, error)

GetByUUID retrieves user given the uuid

func (*UserRepository) UpsertByEmail

func (r *UserRepository) UpsertByEmail(ctx context.Context, ns *namespace.Namespace, ud *user.User) (string, error)

UpsertByEmail updates a row if email match and uuid is empty if email not found, insert a new row

Jump to

Keyboard shortcuts

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