diff --git a/persist.go b/persist.go index 2e91d43..e76b630 100644 --- a/persist.go +++ b/persist.go @@ -100,6 +100,7 @@ func Init(props *InitProps) { BuildInsertOne(col, report) BuildFindOne(col, report) BuildFind(col, report) + BuildFindOffset(col, report) BuildGetOne(col, report) BuildDeleteOne(col, report) diff --git a/persist_find_offset.go b/persist_find_offset.go new file mode 100644 index 0000000..5261fdd --- /dev/null +++ b/persist_find_offset.go @@ -0,0 +1,62 @@ +package persist + +import ( + "fmt" + "context" + "reflect" + + "go.mongodb.org/mongo-driver/v2/bson" + + "git.gsuntres.com/general/commons" + "git.gsuntres.com/general/mongo" +) + +func BuildFindOffset(col map[string]any, report *InitReport) { + name := col["_name"].(string) + plural := col["plural"].(string) + + // prepare input arguments and return results + in := []reflect.Type{ + reflect.TypeOf((*context.Context)(nil)).Elem(), + reflect.TypeOf((*map[string]any)(nil)).Elem(), + reflect.TypeOf((*int64)(nil)).Elem(), + reflect.TypeOf((*int64)(nil)).Elem(), + } + out := []reflect.Type{ + reflect.TypeOf(bson.M{}), + reflect.TypeOf((*error)(nil)).Elem(), + } + + // create function signature + variadic := false + funcType := reflect.FuncOf(in, out, variadic) + funcName := fmt.Sprintf("Find%sOffset", commons.StringTitle(plural)) + fields = append(fields, reflect.StructField{ + Name: funcName, + Type: funcType, + }) + + isSystem := false + if v, ok := col["system"]; ok { + isSystem = v.(bool) + } + + report.AddField(funcName) + + mc := mongo.GetMongoClient() + + // we defer function's implementation until we create the actual struct + deferedFuncs[funcName] = func(ctx context.Context, filter map[string]any, offset int64, limit int64) (bson.M, error) { + db := "__undefined__" + if isSystem { + db = sysDb + } else { + account := ctx.Value("account").(string) + if account != "" { + db = mc.GetName(account) + } + } + + return mc.FindOffset(ctx, db, name, filter, offset, limit) + } +}