diff --git a/mongox/common.go b/mongox/common.go deleted file mode 100644 index 7bb1b5b..0000000 --- a/mongox/common.go +++ /dev/null @@ -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) -} diff --git a/mongox/common/common.go b/mongox/common/common.go index 0dcd6b0..f681877 100644 --- a/mongox/common/common.go +++ b/mongox/common/common.go @@ -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() diff --git a/mongox/common/count.go b/mongox/common/count.go index b1f6117..4964f7b 100644 --- a/mongox/common/count.go +++ b/mongox/common/count.go @@ -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() diff --git a/mongox/common/deletearray.go b/mongox/common/deletearray.go index 3b5eb8e..f07b2bc 100644 --- a/mongox/common/deletearray.go +++ b/mongox/common/deletearray.go @@ -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() diff --git a/mongox/common/deleteone.go b/mongox/common/deleteone.go index 8d92f9d..0494bcb 100644 --- a/mongox/common/deleteone.go +++ b/mongox/common/deleteone.go @@ -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{} diff --git a/mongox/common/loadarray.go b/mongox/common/loadarray.go index 8aa46e6..26bb5f3 100644 --- a/mongox/common/loadarray.go +++ b/mongox/common/loadarray.go @@ -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() diff --git a/mongox/common/loadone.go b/mongox/common/loadone.go index aac7460..4ddf750 100644 --- a/mongox/common/loadone.go +++ b/mongox/common/loadone.go @@ -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() diff --git a/mongox/common/loadstream.go b/mongox/common/loadstream.go index 5f07839..746a4ae 100644 --- a/mongox/common/loadstream.go +++ b/mongox/common/loadstream.go @@ -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 diff --git a/mongox/common/saveone.go b/mongox/common/saveone.go index 06a4f8a..947ef0b 100644 --- a/mongox/common/saveone.go +++ b/mongox/common/saveone.go @@ -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() diff --git a/mongox/database.go b/mongox/database/database.go similarity index 82% rename from mongox/database.go rename to mongox/database/database.go index 5fc965e..b95bd85 100644 --- a/mongox/database.go +++ b/mongox/database/database.go @@ -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() diff --git a/mongox/mongox.go b/mongox/mongox.go new file mode 100644 index 0000000..4021dc9 --- /dev/null +++ b/mongox/mongox.go @@ -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) +}