Add mongo client interfaces

v2
Nikita Tokarchuk 4 years ago
parent e681af6dea
commit c5fab32e49
  1. 43
      mongox/common.go
  2. 4
      mongox/common/common.go
  3. 2
      mongox/common/count.go
  4. 2
      mongox/common/deletearray.go
  5. 2
      mongox/common/deleteone.go
  6. 2
      mongox/common/loadarray.go
  7. 2
      mongox/common/loadone.go
  8. 2
      mongox/common/loadstream.go
  9. 2
      mongox/common/saveone.go
  10. 11
      mongox/database/database.go
  11. 99
      mongox/mongox.go

@ -1,43 +0,0 @@
package mongox
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
// Saver is an interface for documents that can be saved
type Saver interface {
Save(db *Database) error
}
// Deleter is an interface for documents that can be deleted
type Deleter interface {
Delete(db *Database) error
}
// Loader is an interface for documents that can be loaded
type Loader interface {
Load(db *Database, filters ...interface{}) error
}
// Resetter is an interface for documenta that can be resetted
type Resetter interface {
Reset()
}
// BaseObjectID is an interface for documents that have objectId type for the _id field
type BaseObjectID interface {
GetID() primitive.ObjectID
SetID(id primitive.ObjectID)
}
// BaseString is an interface for documents that have string type for the _id field
type BaseString interface {
GetID() string
SetID(id string)
}
// BaseObject is an interface for documents that have object type for the _id field
type BaseObject interface {
GetID() primitive.D
SetID(id primitive.D)
}

@ -14,7 +14,7 @@ import (
"github.com/mainnika/mongox-go-driver/mongox/query"
)
func createSimpleLoad(db *mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
func createSimpleLoad(db mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
collection := db.GetCollectionOf(target)
opts := options.Find()
@ -26,7 +26,7 @@ func createSimpleLoad(db *mongox.Database, target interface{}, composed *query.Q
return collection.Find(db.Context(), composed.M(), opts)
}
func createAggregateLoad(db *mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
func createAggregateLoad(db mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
collection := db.GetCollectionOf(target)
opts := options.Aggregate()

@ -11,7 +11,7 @@ import (
// Count function counts documents in the database by query
// target is used only to get collection by tag so it'd be better to use nil ptr here
func Count(db *mongox.Database, target interface{}, filters ...interface{}) (int64, error) {
func Count(db mongox.Database, target interface{}, filters ...interface{}) (int64, error) {
collection := db.GetCollectionOf(target)
opts := options.Count()

@ -12,7 +12,7 @@ import (
)
// DeleteArray removes documents list from a database by their ids
func DeleteArray(db *mongox.Database, target interface{}) error {
func DeleteArray(db mongox.Database, target interface{}) error {
targetV := reflect.ValueOf(target)
targetT := targetV.Type()

@ -14,7 +14,7 @@ import (
)
// DeleteOne removes a document from a database and then returns it into target
func DeleteOne(db *mongox.Database, target interface{}, filters ...interface{}) error {
func DeleteOne(db mongox.Database, target interface{}, filters ...interface{}) error {
collection := db.GetCollectionOf(target)
opts := &options.FindOneAndDeleteOptions{}

@ -12,7 +12,7 @@ import (
)
// LoadArray loads an array of documents from the database by query
func LoadArray(db *mongox.Database, target interface{}, filters ...interface{}) error {
func LoadArray(db mongox.Database, target interface{}, filters ...interface{}) error {
targetV := reflect.ValueOf(target)
targetT := targetV.Type()

@ -10,7 +10,7 @@ import (
)
// LoadOne function loads a first single target document by a query
func LoadOne(db *mongox.Database, target interface{}, filters ...interface{}) error {
func LoadOne(db mongox.Database, target interface{}, filters ...interface{}) error {
composed := query.Compose(append(filters, query.Limit(1))...)
hasPreloader, _ := composed.Preloader()

@ -55,7 +55,7 @@ func (l *StreamLoader) Close() error {
}
// LoadStream function loads documents one by one into a target channel
func LoadStream(db *mongox.Database, target interface{}, filters ...interface{}) (*StreamLoader, error) {
func LoadStream(db mongox.Database, target interface{}, filters ...interface{}) (*StreamLoader, error) {
var cursor *mongo.Cursor
var err error

@ -14,7 +14,7 @@ import (
)
// SaveOne saves a single source document to the database
func SaveOne(db *mongox.Database, source interface{}) error {
func SaveOne(db mongox.Database, source interface{}) error {
collection := db.GetCollectionOf(source)
opts := options.FindOneAndReplace()

@ -1,4 +1,4 @@
package mongox
package database
import (
"context"
@ -6,6 +6,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/mongox"
"github.com/mainnika/mongox-go-driver/mongox/errors"
)
@ -17,7 +18,7 @@ type Database struct {
}
// NewDatabase function creates new database instance with mongo client and empty context
func NewDatabase(client *mongo.Client, dbname string) *Database {
func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
db := &Database{}
db.client = client
@ -27,7 +28,7 @@ func NewDatabase(client *mongo.Client, dbname string) *Database {
}
// Client function returns a mongo client
func (d *Database) Client() *mongo.Client {
func (d *Database) Client() mongox.MongoClient {
return d.client
}
@ -42,7 +43,7 @@ func (d *Database) Name() string {
}
// New function creates new database context with same client
func (d *Database) New(ctx context.Context) *Database {
func (d *Database) New(ctx context.Context) mongox.Database {
if ctx != nil {
ctx = context.Background()
@ -61,7 +62,7 @@ func (d *Database) New(ctx context.Context) *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.MongoCollection {
el := reflect.TypeOf(document).Elem()
numField := el.NumField()

@ -0,0 +1,99 @@
package mongox
import (
"context"
"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/readpref"
)
// Database is the mongox database interface
type Database interface {
Client() MongoClient
Context() context.Context
Name() string
New(ctx context.Context) Database
GetCollectionOf(document interface{}) MongoCollection
}
// MongoClient is the mongo client interface
type MongoClient interface {
Connect(ctx context.Context) error
Disconnect(ctx context.Context) error
Ping(ctx context.Context, rp *readpref.ReadPref) error
StartSession(opts ...*options.SessionOptions) (mongo.Session, error)
Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
ListDatabases(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (mongo.ListDatabasesResult, error)
ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) ([]string, error)
UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error
UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(mongo.SessionContext) error) error
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
NumberSessionsInProgress() int
}
// MongoCollection is the mongo collection interface
type MongoCollection interface {
Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
Name() string
Database() *mongo.Database
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
Indexes() mongo.IndexView
Drop(ctx context.Context) error
}
// Saver is an interface for documents that can be saved
type Saver interface {
Save(db Database) error
}
// Deleter is an interface for documents that can be deleted
type Deleter interface {
Delete(db Database) error
}
// Loader is an interface for documents that can be loaded
type Loader interface {
Load(db Database, filters ...interface{}) error
}
// Resetter is an interface for documenta that can be resetted
type Resetter interface {
Reset()
}
// BaseObjectID is an interface for documents that have objectId type for the _id field
type BaseObjectID interface {
GetID() primitive.ObjectID
SetID(id primitive.ObjectID)
}
// BaseString is an interface for documents that have string type for the _id field
type BaseString interface {
GetID() string
SetID(id string)
}
// BaseObject is an interface for documents that have object type for the _id field
type BaseObject interface {
GetID() primitive.D
SetID(id primitive.D)
}
Loading…
Cancel
Save