This commit is contained in:
Mike Schwörer 2023-06-06 21:24:13 +02:00
parent b02e1d2e85
commit 6622c9003d
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -10,6 +10,8 @@ import (
"reflect"
)
type EntityID = any
type fullTypeRef[TData any] struct {
IsPointer bool
Kind reflect.Kind
@ -40,7 +42,7 @@ func (c *Coll[TData]) Drop(ctx context.Context) error {
return c.coll.Drop(ctx)
}
func (c *Coll[TData]) FindOne(ctx context.Context, filter any) (TData, error) {
func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) {
var res TData
err := c.coll.FindOne(ctx, filter).Decode(&res)
@ -51,7 +53,7 @@ func (c *Coll[TData]) FindOne(ctx context.Context, filter any) (TData, error) {
return res, nil
}
func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter any) (*TData, error) {
func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) {
var res TData
err := c.coll.FindOne(ctx, filter).Decode(&res)
@ -65,7 +67,7 @@ func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter any) (*TData, error
return &res, nil
}
func (c *Coll[TData]) FindOneByID(ctx context.Context, id any) (TData, error) {
func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) {
var res TData
err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res)
@ -76,7 +78,7 @@ func (c *Coll[TData]) FindOneByID(ctx context.Context, id any) (TData, error) {
return res, nil
}
func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id any) (*TData, error) {
func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) {
var res TData
err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res)
@ -90,7 +92,7 @@ func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id any) (*TData, error
return &res, nil
}
func (c *Coll[TData]) Find(ctx context.Context, filter any, opts ...*options.FindOptions) ([]TData, error) {
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
cursor, err := c.coll.Find(ctx, filter, opts...)
if err != nil {
return nil, err
@ -120,7 +122,7 @@ func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, op
return res, nil
}
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id any, value TData) error {
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value)
if err != nil {
return err
@ -129,7 +131,7 @@ func (c *Coll[TData]) ReplaceOne(ctx context.Context, id any, value TData) error
return nil
}
func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery any, updateQuery any) error {
func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery bson.M, updateQuery bson.M) error {
_, err := c.coll.UpdateOne(ctx, filterQuery, updateQuery)
if err != nil {
return err
@ -138,7 +140,7 @@ func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery any, updateQuer
return nil
}
func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id any, updateQuery any) error {
func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuery bson.M) error {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery)
if err != nil {
return err
@ -147,7 +149,7 @@ func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id any, updateQuery any
return nil
}
func (c *Coll[TData]) UpdateOneAndReturn(ctx context.Context, id any, updateQuery any) (TData, error) {
func (c *Coll[TData]) UpdateOneAndReturn(ctx context.Context, id EntityID, updateQuery bson.M) (TData, error) {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery)
if err != nil {
return *new(TData), err
@ -163,7 +165,7 @@ func (c *Coll[TData]) UpdateOneAndReturn(ctx context.Context, id any, updateQuer
return res, nil
}
func (c *Coll[TData]) DeleteOne(ctx context.Context, id any) error {
func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error {
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
if err != nil {
return err
@ -172,7 +174,7 @@ func (c *Coll[TData]) DeleteOne(ctx context.Context, id any) error {
return nil
}
func (c *Coll[TData]) DeleteMany(ctx context.Context, filterQuery any) (*mongo.DeleteResult, error) {
func (c *Coll[TData]) DeleteMany(ctx context.Context, filterQuery bson.M) (*mongo.DeleteResult, error) {
res, err := c.coll.DeleteMany(ctx, filterQuery)
if err != nil {
return nil, err