package kms import ( "context" "errors" "github.com/hashicorp/boundary/internal/db" ) // Repository is the iam database repository type Repository struct { reader db.Reader writer db.Writer // defaultLimit provides a default for limiting the number of results returned from the repo defaultLimit int } // NewRepository creates a new kms Repository. Supports the options: WithLimit // which sets a default limit on results returned by repo operations. func NewRepository(r db.Reader, w db.Writer, opt ...Option) (*Repository, error) { if r == nil { return nil, errors.New("error creating db repository with nil reader") } if w == nil { return nil, errors.New("error creating db repository with nil writer") } opts := getOpts(opt...) if opts.withLimit == 0 { // zero signals the boundary defaults should be used. opts.withLimit = db.DefaultLimit } return &Repository{ reader: r, writer: w, defaultLimit: opts.withLimit, }, nil } // list will return a listing of resources and honor the WithLimit option or the // repo defaultLimit func (r *Repository) list(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error { opts := getOpts(opt...) limit := r.defaultLimit var dbOpts []db.Option if opts.withLimit != 0 { // non-zero signals an override of the default limit for the repo. limit = opts.withLimit } dbOpts = append(dbOpts, db.WithLimit(limit)) if opts.withOrder != "" { dbOpts = append(dbOpts, db.WithOrder(opts.withOrder)) } return r.reader.SearchWhere(ctx, resources, where, args, dbOpts...) } // DefaultLimit returns the default limit for listing as set on the repo func (r *Repository) DefaultLimit() int { return r.defaultLimit }