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 (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"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()
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
if err == mongo.ErrNoDocuments {
if err == mongox.ErrNoDocuments {
return 0, err
}
if err != nil {

@ -8,7 +8,6 @@ import (
"strings"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox"
@ -17,13 +16,13 @@ import (
// Database handler
type Database struct {
client *mongo.Client
client *mongox.Client
dbname string
ctx context.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.client = client
@ -33,7 +32,7 @@ func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
}
// Client function returns a mongo client
func (d *Database) Client() *mongo.Client {
func (d *Database) Client() *mongox.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"`
// ...
// 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()
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))
}
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)
opts := options.Find()
@ -98,7 +97,7 @@ func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (
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)
opts := options.Aggregate()

@ -5,9 +5,9 @@ import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"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/query"
)
@ -38,7 +38,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
}
err := result.Decode(target)
if err == mongo.ErrNoDocuments {
if err == mongox.ErrNoDocuments {
return err
}
if err != nil {

@ -4,8 +4,7 @@ import (
"fmt"
"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/query"
)
@ -36,7 +35,7 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
zeroElem := reflect.Zero(targetSliceElemT)
hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor
var result *mongox.Cursor
var err error
if hasPreloader {

@ -3,8 +3,7 @@ package database
import (
"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/query"
)
@ -15,7 +14,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
composed := query.Compose(append(filters, query.Limit(1))...)
hasPreloader, _ := composed.Preloader()
var result *mongo.Cursor
var result *mongox.Cursor
var err error
if hasPreloader {
@ -32,7 +31,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
return err
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
base.Reset(target)

@ -3,8 +3,6 @@ package database
import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"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
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
var cursor *mongo.Cursor
var cursor *mongox.Cursor
var err error
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)
}
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
l := &StreamLoader{cur: cursor, ctx: d.Context(), target: target}
return l, nil
}

@ -4,14 +4,13 @@ import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
)
// StreamLoader is a controller for a database cursor
type StreamLoader struct {
*mongo.Cursor
cur *mongox.Cursor
ctx context.Context
target interface{}
}
@ -19,18 +18,18 @@ type StreamLoader struct {
// DecodeNext loads next documents to a target or returns an error
func (l *StreamLoader) DecodeNext() error {
hasNext := l.Cursor.Next(l.ctx)
hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
if l.cur.Err() != nil {
return l.cur.Err()
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
base.Reset(l.target)
err := l.Cursor.Decode(l.target)
err := l.cur.Decode(l.target)
if err != nil {
return fmt.Errorf("can't decode desult: %w", err)
}
@ -43,7 +42,7 @@ func (l *StreamLoader) Decode() error {
base.Reset(l.target)
err := l.Cursor.Decode(l.target)
err := l.cur.Decode(l.target)
if err != nil {
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
func (l *StreamLoader) Next() error {
hasNext := l.Cursor.Next(l.ctx)
hasNext := l.cur.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
if l.cur.Err() != nil {
return l.cur.Err()
}
if !hasNext {
return mongo.ErrNoDocuments
return mongox.ErrNoDocuments
}
return nil
}
func (l *StreamLoader) Cursor() *mongox.Cursor {
return l.cur
}
// Close cursor
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"
)
// Reexport basic mongo structs
type (
Cursor = mongo.Cursor
Client = mongo.Client
Collection = mongo.Collection
)
// Database is the mongox database interface
type Database interface {
Client() *mongo.Client
Client() *Client
Context() context.Context
Name() string
New(ctx context.Context) Database
GetCollectionOf(document interface{}) *mongo.Collection
GetCollectionOf(document interface{}) *Collection
Count(target interface{}, filters ...interface{}) (int64, error)
DeleteArray(target interface{}) error
DeleteOne(target interface{}, filters ...interface{}) error
@ -25,6 +32,7 @@ type Database interface {
// StreamLoader is a interface to control database cursor
type StreamLoader interface {
Cursor() *Cursor
DecodeNext() error
Decode() error
Next() error

Loading…
Cancel
Save