Reexport basic mongo structs

v2
Nikita Tokarchuk 5 years ago
parent 8267abe5be
commit e7a05d94e1
  1. 4
      mongox/database/count.go
  2. 13
      mongox/database/database.go
  3. 4
      mongox/database/deleteone.go
  4. 5
      mongox/database/loadarray.go
  5. 7
      mongox/database/loadone.go
  6. 6
      mongox/database/loadstream.go
  7. 34
      mongox/database/streamloader.go
  8. 20
      mongox/errors.go
  9. 12
      mongox/mongox.go

@ -3,9 +3,9 @@ package database
import ( import (
"fmt" "fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -21,7 +21,7 @@ func (d *Database) Count(target interface{}, filters ...interface{}) (int64, err
opts.Skip = composed.Skipper() opts.Skip = composed.Skipper()
result, err := collection.CountDocuments(d.Context(), composed.M(), opts) result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
if err == mongo.ErrNoDocuments { if err == mongox.ErrNoDocuments {
return 0, err return 0, err
} }
if err != nil { if err != nil {

@ -8,7 +8,6 @@ import (
"strings" "strings"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
@ -17,13 +16,13 @@ import (
// Database handler // Database handler
type Database struct { type Database struct {
client *mongo.Client client *mongox.Client
dbname string dbname string
ctx context.Context ctx context.Context
} }
// NewDatabase function creates new database instance with mongo client and empty context // NewDatabase function creates new database instance with mongo client and empty context
func NewDatabase(client *mongo.Client, dbname string) mongox.Database { func NewDatabase(client *mongox.Client, dbname string) mongox.Database {
db := &Database{} db := &Database{}
db.client = client db.client = client
@ -33,7 +32,7 @@ func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
} }
// Client function returns a mongo client // Client function returns a mongo client
func (d *Database) Client() *mongo.Client { func (d *Database) Client() *mongox.Client {
return d.client return d.client
} }
@ -67,7 +66,7 @@ func (d *Database) New(ctx context.Context) mongox.Database {
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"` // base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
// ... // ...
// Will panic if there is no «collection» tag // Will panic if there is no «collection» tag
func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection { func (d *Database) GetCollectionOf(document interface{}) *mongox.Collection {
el := reflect.TypeOf(document).Elem() el := reflect.TypeOf(document).Elem()
numField := el.NumField() numField := el.NumField()
@ -86,7 +85,7 @@ func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
panic(fmt.Errorf("document %v does not have a collection tag", document)) panic(fmt.Errorf("document %v does not have a collection tag", document))
} }
func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) { func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
collection := d.GetCollectionOf(target) collection := d.GetCollectionOf(target)
opts := options.Find() opts := options.Find()
@ -98,7 +97,7 @@ func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (
return collection.Find(d.Context(), composed.M(), opts) return collection.Find(d.Context(), composed.M(), opts)
} }
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) { func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
collection := d.GetCollectionOf(target) collection := d.GetCollectionOf(target)
opts := options.Aggregate() opts := options.Aggregate()

@ -5,9 +5,9 @@ import (
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -38,7 +38,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
} }
err := result.Decode(target) err := result.Decode(target)
if err == mongo.ErrNoDocuments { if err == mongox.ErrNoDocuments {
return err return err
} }
if err != nil { if err != nil {

@ -4,8 +4,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"go.mongodb.org/mongo-driver/mongo" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -36,7 +35,7 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
zeroElem := reflect.Zero(targetSliceElemT) zeroElem := reflect.Zero(targetSliceElemT)
hasPreloader, _ := composed.Preloader() hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor var result *mongox.Cursor
var err error var err error
if hasPreloader { if hasPreloader {

@ -3,8 +3,7 @@ package database
import ( import (
"fmt" "fmt"
"go.mongodb.org/mongo-driver/mongo" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -15,7 +14,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
composed := query.Compose(append(filters, query.Limit(1))...) composed := query.Compose(append(filters, query.Limit(1))...)
hasPreloader, _ := composed.Preloader() hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor var result *mongox.Cursor
var err error var err error
if hasPreloader { if hasPreloader {
@ -32,7 +31,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
return err return err
} }
if !hasNext { if !hasNext {
return mongo.ErrNoDocuments return mongox.ErrNoDocuments
} }
base.Reset(target) base.Reset(target)

@ -3,8 +3,6 @@ package database
import ( import (
"fmt" "fmt"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -12,7 +10,7 @@ import (
// LoadStream function loads documents one by one into a target channel // LoadStream function loads documents one by one into a target channel
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) { func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
var cursor *mongo.Cursor var cursor *mongox.Cursor
var err error var err error
composed := query.Compose(filters...) composed := query.Compose(filters...)
@ -27,7 +25,7 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongo
return nil, fmt.Errorf("can't create find result: %w", err) return nil, fmt.Errorf("can't create find result: %w", err)
} }
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target} l := &StreamLoader{cur: cursor, ctx: d.Context(), target: target}
return l, nil return l, nil
} }

@ -4,14 +4,13 @@ import (
"context" "context"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/mongo" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
) )
// StreamLoader is a controller for a database cursor // StreamLoader is a controller for a database cursor
type StreamLoader struct { type StreamLoader struct {
*mongo.Cursor cur *mongox.Cursor
ctx context.Context ctx context.Context
target interface{} target interface{}
} }
@ -19,18 +18,18 @@ type StreamLoader struct {
// DecodeNext loads next documents to a target or returns an error // DecodeNext loads next documents to a target or returns an error
func (l *StreamLoader) DecodeNext() error { func (l *StreamLoader) DecodeNext() error {
hasNext := l.Cursor.Next(l.ctx) hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil { if l.cur.Err() != nil {
return l.Cursor.Err() return l.cur.Err()
} }
if !hasNext { if !hasNext {
return mongo.ErrNoDocuments return mongox.ErrNoDocuments
} }
base.Reset(l.target) base.Reset(l.target)
err := l.Cursor.Decode(l.target) err := l.cur.Decode(l.target)
if err != nil { if err != nil {
return fmt.Errorf("can't decode desult: %w", err) return fmt.Errorf("can't decode desult: %w", err)
} }
@ -43,7 +42,7 @@ func (l *StreamLoader) Decode() error {
base.Reset(l.target) base.Reset(l.target)
err := l.Cursor.Decode(l.target) err := l.cur.Decode(l.target)
if err != nil { if err != nil {
return fmt.Errorf("can't decode desult: %w", err) return fmt.Errorf("can't decode desult: %w", err)
} }
@ -54,20 +53,27 @@ func (l *StreamLoader) Decode() error {
// Next loads next documents but doesn't perform decoding // Next loads next documents but doesn't perform decoding
func (l *StreamLoader) Next() error { func (l *StreamLoader) Next() error {
hasNext := l.Cursor.Next(l.ctx) hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil { if l.cur.Err() != nil {
return l.Cursor.Err() return l.cur.Err()
} }
if !hasNext { if !hasNext {
return mongo.ErrNoDocuments return mongox.ErrNoDocuments
} }
return nil return nil
} }
func (l *StreamLoader) Cursor() *mongox.Cursor {
return l.cur
}
// Close cursor // Close cursor
func (l *StreamLoader) Close() error { func (l *StreamLoader) Close() error {
return l.cur.Close(l.ctx)
}
return l.Cursor.Close(l.ctx) func (l *StreamLoader) Err() error {
return l.cur.Err()
} }

@ -0,0 +1,20 @@
package mongox
import (
"go.mongodb.org/mongo-driver/mongo"
)
// Reexported mongo errors
var (
ErrMissingResumeToken = mongo.ErrMissingResumeToken
ErrNilCursor = mongo.ErrNilCursor
ErrUnacknowledgedWrite = mongo.ErrUnacknowledgedWrite
ErrClientDisconnected = mongo.ErrClientDisconnected
ErrNilDocument = mongo.ErrNilDocument
ErrEmptySlice = mongo.ErrEmptySlice
ErrInvalidIndexValue = mongo.ErrInvalidIndexValue
ErrNonStringIndexName = mongo.ErrNonStringIndexName
ErrMultipleIndexDrop = mongo.ErrMultipleIndexDrop
ErrWrongClient = mongo.ErrWrongClient
ErrNoDocuments = mongo.ErrNoDocuments
)

@ -7,13 +7,20 @@ import (
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
// Reexport basic mongo structs
type (
Cursor = mongo.Cursor
Client = mongo.Client
Collection = mongo.Collection
)
// Database is the mongox database interface // Database is the mongox database interface
type Database interface { type Database interface {
Client() *mongo.Client Client() *Client
Context() context.Context Context() context.Context
Name() string Name() string
New(ctx context.Context) Database New(ctx context.Context) Database
GetCollectionOf(document interface{}) *mongo.Collection GetCollectionOf(document interface{}) *Collection
Count(target interface{}, filters ...interface{}) (int64, error) Count(target interface{}, filters ...interface{}) (int64, error)
DeleteArray(target interface{}) error DeleteArray(target interface{}) error
DeleteOne(target interface{}, filters ...interface{}) error DeleteOne(target interface{}, filters ...interface{}) error
@ -25,6 +32,7 @@ type Database interface {
// StreamLoader is a interface to control database cursor // StreamLoader is a interface to control database cursor
type StreamLoader interface { type StreamLoader interface {
Cursor() *Cursor
DecodeNext() error DecodeNext() error
Decode() error Decode() error
Next() error Next() error

Loading…
Cancel
Save