mongosaas

package
v0.0.0-...-f5b340a Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Db

type Db struct {
	Client *mongo.Client
	DBName string
	db.Getter
	db.Creator
	db.Deleter
	db.Updater
	db.CursorLister
}

func (*Db) Coll

func (r *Db) Coll(data interface{}) *mongo.Collection

func (*Db) Create

func (r *Db) Create(ctx context.Context, id t.String, data interface{}) error

func (*Db) CursorList

func (r *Db) CursorList(ctx context.Context, startAtKey string, limit int64, out interface{}) (*db.CursorListResult, error)

func (*Db) Delete

func (r *Db) Delete(ctx context.Context, id t.String, out interface{}) error

func (*Db) Get

func (r *Db) Get(ctx context.Context, id t.String, out interface{}) error

func (*Db) Init

func (r *Db) Init()
func EnsureIndex(ctx context.Context, c *mongo.Collection, keys []string, opts *mongo.IndexOptionsBuilder) error {
	idxs := c.Indexes()

	ks := bson.D{}
	for _, k := range keys {
		// todo - add support for sorting index.
		ks.Append({})
	}
	idm := mongo.IndexModel{
		Keys:    ks,
		Options: opts.Build(),
	}

	v := idm.Options.Lookup("name")
	if v == nil {
		return errors.New("must provide a key name for index")
	}
	expectedName := v.StringValue()

	cur, err := idxs.List(ctx)
	if err != nil {
		return errors.Wrap(err, "unable to list indexes")
	}

	found := false
	for cur.Next(ctx) {
		d := bson.NewDocument()

		if err := cur.Decode(d); err != nil {
			return errors.Wrap(err, "unable to decode bson index document")
		}

		v := d.Lookup("name")
		if v != nil && v.StringValue() == expectedName {
			found = true
			break
		}
	}

	if found {
		return nil
	}

	_, err = idxs.CreateOne(ctx, idm)
	return err
}

func (*Db) Update

func (r *Db) Update(ctx context.Context, id t.String, data interface{}) error

type MongoSaasModel

type MongoSaasModel struct {
	db.SaasModel
	ID t.String
}

Jump to

Keyboard shortcuts

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