mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
- Deleted the db module, consolidating database-related functionality into the core/mongo package for better organization and maintainability. - Updated all import paths across the codebase to replace references to the removed db module with core/mongo. - Cleaned up unused code and dependencies, enhancing overall project clarity and reducing complexity. - This refactor improves the structure of the codebase by centralizing database operations and simplifying module management.
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package mongo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type FindResultInterface interface {
|
|
One(val interface{}) (err error)
|
|
All(val interface{}) (err error)
|
|
GetCol() (col *Col)
|
|
GetSingleResult() (res *mongo.SingleResult)
|
|
GetCursor() (cur *mongo.Cursor)
|
|
}
|
|
|
|
type FindResult struct {
|
|
col *Col
|
|
res *mongo.SingleResult
|
|
cur *mongo.Cursor
|
|
err error
|
|
}
|
|
|
|
func (fr *FindResult) One(val interface{}) (err error) {
|
|
if fr.err != nil {
|
|
return fr.err
|
|
}
|
|
if fr.cur != nil {
|
|
if !fr.cur.TryNext(fr.col.ctx) {
|
|
return mongo.ErrNoDocuments
|
|
}
|
|
return fr.cur.Decode(val)
|
|
}
|
|
return fr.res.Decode(val)
|
|
}
|
|
|
|
func (fr *FindResult) All(val interface{}) (err error) {
|
|
if fr.err != nil {
|
|
return fr.err
|
|
}
|
|
var ctx context.Context
|
|
if fr.col == nil {
|
|
ctx = context.Background()
|
|
} else {
|
|
ctx = fr.col.ctx
|
|
}
|
|
if fr.cur == nil {
|
|
return errors.New("no cursor")
|
|
}
|
|
if !fr.cur.TryNext(ctx) {
|
|
return ctx.Err()
|
|
}
|
|
return fr.cur.All(ctx, val)
|
|
}
|
|
|
|
func (fr *FindResult) GetCol() (col *Col) {
|
|
return fr.col
|
|
}
|
|
|
|
func (fr *FindResult) GetSingleResult() (res *mongo.SingleResult) {
|
|
return fr.res
|
|
}
|
|
|
|
func (fr *FindResult) GetCursor() (cur *mongo.Cursor) {
|
|
return fr.cur
|
|
}
|