Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
afcc89bf9e
|
|||
1672e8f8fd
|
|||
398ed56d32
|
@@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.304"
|
||||
const GoextVersion = "0.0.307"
|
||||
|
||||
const GoextVersionTimestamp = "2023-11-09T09:26:46+0100"
|
||||
const GoextVersionTimestamp = "2023-11-09T10:00:01+0100"
|
||||
|
@@ -1,29 +1,28 @@
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
||||
)
|
||||
|
||||
type Filter interface {
|
||||
FilterQuery() mongo.Pipeline
|
||||
Pagination() (string, ct.SortDirection)
|
||||
Sort() bson.D
|
||||
}
|
||||
|
||||
type dynamicFilter struct {
|
||||
pipeline mongo.Pipeline
|
||||
sortField string
|
||||
sortDir ct.SortDirection
|
||||
sort bson.D
|
||||
}
|
||||
|
||||
func (d dynamicFilter) FilterQuery() mongo.Pipeline {
|
||||
return d.pipeline
|
||||
}
|
||||
|
||||
func (d dynamicFilter) Pagination() (string, ct.SortDirection) {
|
||||
return d.sortField, d.sortDir
|
||||
func (d dynamicFilter) Sort() bson.D {
|
||||
return d.sort
|
||||
}
|
||||
|
||||
func CreateFilter(pipeline mongo.Pipeline, sortField string, sortdir ct.SortDirection) Filter {
|
||||
return dynamicFilter{pipeline: pipeline, sortField: sortField, sortDir: sortdir}
|
||||
func CreateFilter(pipeline mongo.Pipeline, sort bson.D) Filter {
|
||||
return dynamicFilter{pipeline: pipeline, sort: sort}
|
||||
}
|
||||
|
@@ -21,6 +21,20 @@ func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, erro
|
||||
return *r, nil
|
||||
}
|
||||
|
||||
// InsertOneUnchecked behaves the same as InsertOne, but allows arbitrary data to be inserted (valueIn is any instead of TData)
|
||||
func (c *Coll[TData]) InsertOneUnchecked(ctx context.Context, valueIn any) (TData, error) {
|
||||
insRes, err := c.coll.InsertOne(ctx, valueIn)
|
||||
if err != nil {
|
||||
return *new(TData), exerr.Wrap(err, "mongo-query[insert-one] failed").Str("collection", c.Name()).Build()
|
||||
}
|
||||
|
||||
r, err := c.findOneInternal(ctx, bson.M{"_id": insRes.InsertedID}, false)
|
||||
if err != nil {
|
||||
return *new(TData), exerr.Wrap(err, "mongo-query[insert-one] failed").Str("collection", c.Name()).Build()
|
||||
}
|
||||
return *r, nil
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) InsertMany(ctx context.Context, valueIn []TData) (*mongo.InsertManyResult, error) {
|
||||
insRes, err := c.coll.InsertMany(ctx, langext.ArrayToInterface(valueIn))
|
||||
if err != nil {
|
||||
@@ -29,3 +43,13 @@ func (c *Coll[TData]) InsertMany(ctx context.Context, valueIn []TData) (*mongo.I
|
||||
|
||||
return insRes, nil
|
||||
}
|
||||
|
||||
// InsertManyUnchecked behaves the same as InsertOne, but allows arbitrary data to be inserted (valueIn is []any instead of []TData)
|
||||
func (c *Coll[TData]) InsertManyUnchecked(ctx context.Context, valueIn []any) (*mongo.InsertManyResult, error) {
|
||||
insRes, err := c.coll.InsertMany(ctx, langext.ArrayToInterface(valueIn))
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "mongo-query[insert-many] failed").Int("len(valueIn)", len(valueIn)).Str("collection", c.Name()).Build()
|
||||
}
|
||||
|
||||
return insRes, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user