You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
mongox-go-driver/mongox/mongox.go

99 lines
4.7 KiB

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)
}