diff --git a/find_cursor.go b/cursor.go similarity index 100% rename from find_cursor.go rename to cursor.go diff --git a/find_cursor_test.go b/cursor_test.go similarity index 100% rename from find_cursor_test.go rename to cursor_test.go diff --git a/filter.go b/filter.go index c076426..949f323 100644 --- a/filter.go +++ b/filter.go @@ -13,6 +13,7 @@ type Filter struct { } func makeFilter(name string, value any) *Filter { + log.Printf("FILTER: %#v", value) var op string var v string diff --git a/find.go b/find.go index a52917d..639740d 100644 --- a/find.go +++ b/find.go @@ -7,11 +7,27 @@ import ( "git.gsuntres.com/general/commons" ) +type FindOptions struct { + Offset int64 `json:"offset"` + Limit int64 `json:"limit"` + Alias string `json:"alias"` +} + // Find is used to fetch the first page of data. -func (c *MongoClient) Find(ctx context.Context, database, name string, filter bson.M, limit int64) (bson.M, error) { +func (c *MongoClient) Find(ctx context.Context, database, name string, filter bson.M, opts *FindOptions) (bson.M, error) { + + var limit int64 + + if opts != nil { + limit = opts.Limit + } // 1. Prepare to query. - collection := c.GetCollection(database, name) + finalName := name + if opts != nil && commons.StringIsNotBlank(opts.Alias) { + finalName = opts.Alias + } + collection := c.GetCollection(database, finalName) pageSize := max(limit, c.Limit) @@ -44,8 +60,8 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs var totalValue any if len(metadata) != 0 { - metadataRoot := metadata[0].(bson.M) - totalValue = metadataRoot["total"] + metadataRoot := metadata[0].(bson.D) + totalValue, _ = commons.BsonDGetAny(metadataRoot, "total") } var total int64 @@ -84,9 +100,20 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs return out, nil } -func (c *MongoClient) FindOffset(ctx context.Context, database, name string, filter bson.M, offset, limit int64) (bson.M, error) { +func (c *MongoClient) FindOffset(ctx context.Context, database, name string, filter bson.M, opts *FindOptions) (bson.M, error) { + var offset int64 + var limit int64 + + if opts != nil { + limit = opts.Limit + } + // 1. Prepare to query. - collection := c.GetCollection(database, name) + finalName := name + if opts != nil && commons.StringIsNotBlank(opts.Alias) { + finalName = opts.Alias + } + collection := c.GetCollection(database, finalName) finalLimit := max(limit, c.Limit)