upgrade mongodb driver to 2.0 (may revert)
This commit is contained in:
parent
64b9d1c241
commit
8c559d5926
@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/fatih/structtag"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
13
go.mod
13
go.mod
@ -4,20 +4,25 @@ go 1.23
|
||||
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0
|
||||
go.mongodb.org/mongo-driver v1.16.1
|
||||
golang.org/x/net v0.29.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-loremipsum/loremipsum v1.1.3
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
golang.org/x/crypto v0.27.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
|
27
go.sum
27
go.sum
@ -10,8 +10,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
@ -20,31 +20,24 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver v1.16.1 h1:rIVLL3q0IHM39dvE+z2ulZLp9ENZKThVfuvN/IiN4l8=
|
||||
go.mongodb.org/mongo-driver v1.16.1/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
|
||||
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2 h1:PRtbRKwblE8ZfI8qOhofcjn9y8CmKZI7trS5vDMeJX0=
|
||||
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2/go.mod h1:UGLb3ZgEzaY0cCbJpH9UFt9B6gEXiTPzsnJS38nBeoU=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
|
||||
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@ -57,11 +50,13 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
26
gridfs.go
26
gridfs.go
@ -6,11 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fatih/structtag"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/gridfs"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"html/template"
|
||||
"io"
|
||||
"reflect"
|
||||
@ -18,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
type GridFSFile struct {
|
||||
ID primitive.ObjectID `bson:"_id"`
|
||||
ID bson.ObjectID `bson:"_id"`
|
||||
Name string `bson:"filename"`
|
||||
Length int `bson:"length"`
|
||||
}
|
||||
@ -32,8 +30,8 @@ func parseFmt(format string, value any) string {
|
||||
return w.String()
|
||||
}
|
||||
|
||||
func bucket(gfsRef gridFSReference) *gridfs.Bucket {
|
||||
b, _ := gridfs.NewBucket(DB, options.GridFSBucket().SetName(gfsRef.BucketName))
|
||||
func bucket(gfsRef gridFSReference) *mongo.GridFSBucket {
|
||||
b := DB.GridFSBucket(options.GridFSBucket().SetName(gfsRef.BucketName))
|
||||
return b
|
||||
}
|
||||
|
||||
@ -83,14 +81,14 @@ func gridFsLoad(val any, g gridFSReference, field string) any {
|
||||
default:
|
||||
b := bucket(g)
|
||||
var found GridFSFile
|
||||
cursor, err := b.Find(bson.M{"filename": parseFmt(g.FilenameFmt, val)})
|
||||
cursor, err := b.Find(context.TODO(), bson.M{"filename": parseFmt(g.FilenameFmt, val)})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
cursor.Next(context.TODO())
|
||||
_ = cursor.Decode(&found)
|
||||
bb := bytes.NewBuffer(nil)
|
||||
_, err = b.DownloadToStream(found.ID, bb)
|
||||
_, err = b.DownloadToStream(context.TODO(), found.ID, bb)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@ -132,15 +130,15 @@ func gridFsSave(val any, imodel Model) error {
|
||||
break
|
||||
}
|
||||
}
|
||||
var inner = func(b *gridfs.Bucket, it reflect.Value) error {
|
||||
var inner = func(b *mongo.GridFSBucket, it reflect.Value) error {
|
||||
filename := parseFmt(gfsRef.FilenameFmt, it.Interface())
|
||||
contents := GridFSFile{}
|
||||
curs, err2 := b.Find(bson.M{"filename": filename})
|
||||
curs, err2 := b.Find(context.TODO(), bson.M{"filename": filename})
|
||||
|
||||
if !errors.Is(err2, mongo.ErrNoDocuments) {
|
||||
_ = curs.Decode(&contents)
|
||||
if !reflect.ValueOf(contents).IsZero() {
|
||||
_ = b.Delete(contents.ID)
|
||||
_ = b.Delete(context.TODO(), contents.ID)
|
||||
}
|
||||
}
|
||||
c := it.Field(gfsRef.Idx)
|
||||
@ -153,7 +151,7 @@ func gridFsSave(val any, imodel Model) error {
|
||||
} else {
|
||||
return fmt.Errorf("gridfs loader type '%s' not supported", c.Type().String())
|
||||
}
|
||||
_, err = b.UploadFromStream(filename, rdr)
|
||||
_, err = b.UploadFromStream(context.TODO(), filename, rdr)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,8 @@ import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
const COUNTER_COL = "@counters"
|
||||
@ -20,7 +19,7 @@ func getLastInColl(cname string, id interface{}) interface{} {
|
||||
var opts = options.FindOne()
|
||||
|
||||
switch id.(type) {
|
||||
case int, int64, int32, uint, uint32, uint64, primitive.ObjectID:
|
||||
case int, int64, int32, uint, uint32, uint64, bson.ObjectID:
|
||||
opts.SetSort(bson.M{"_id": -1})
|
||||
case string:
|
||||
opts.SetSort(bson.M{"createdAt": -1})
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
var optionKeywords = [...]string{"unique", "sparse", "background", "dropdups"}
|
||||
@ -138,8 +138,8 @@ func buildIndex(i InternalIndex) *mongo.IndexModel {
|
||||
switch o {
|
||||
case "unique":
|
||||
idx.Options.SetUnique(true)
|
||||
case "background":
|
||||
idx.Options.SetBackground(true)
|
||||
//case "background":
|
||||
// idx.Options.SetBackground(true)
|
||||
case "sparse":
|
||||
idx.Options.SetSparse(true)
|
||||
}
|
||||
|
42
model.go
42
model.go
@ -3,9 +3,9 @@ package orm
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
@ -36,11 +36,11 @@ type HasID interface {
|
||||
type HasIDSlice []HasID
|
||||
|
||||
type IModel interface {
|
||||
FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
|
||||
Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
|
||||
FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error)
|
||||
Find(query interface{}, opts *options.FindOptionsBuilder) (*Query, error)
|
||||
FindByID(id interface{}) (*Query, error)
|
||||
FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
|
||||
FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error)
|
||||
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
||||
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
||||
|
||||
getColl() *mongo.Collection
|
||||
getIdxs() []*mongo.IndexModel
|
||||
@ -87,15 +87,19 @@ func (m *Model) getParsedIdxs() map[string][]InternalIndex {
|
||||
}
|
||||
|
||||
// FindRaw - find documents satisfying `query` and return a plain mongo cursor.
|
||||
func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
|
||||
func (m *Model) FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error) {
|
||||
coll := m.getColl()
|
||||
cursor, err := coll.Find(context.TODO(), query, opts...)
|
||||
var fo options.FindOptions
|
||||
for _, setter := range opts.Opts {
|
||||
_ = setter(&fo)
|
||||
}
|
||||
cursor, err := coll.Find(context.TODO(), query, opts)
|
||||
return cursor, err
|
||||
}
|
||||
|
||||
// Find - find all documents satisfying `query`.
|
||||
// returns a pointer to a Query for further chaining.
|
||||
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error) {
|
||||
func (m *Model) Find(query interface{}, opts *options.FindOptionsBuilder) (*Query, error) {
|
||||
qqn := ModelRegistry.new_(m.typeName)
|
||||
qqt := reflect.SliceOf(reflect.TypeOf(qqn))
|
||||
qqv := reflect.New(qqt)
|
||||
@ -106,7 +110,7 @@ func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, e
|
||||
doc: qqv.Interface(),
|
||||
op: OP_FIND_ALL,
|
||||
}
|
||||
q, err := m.FindRaw(query, opts...)
|
||||
q, err := m.FindRaw(query, opts)
|
||||
idoc := (*DocumentSlice)(unsafe.Pointer(qqv.Elem().UnsafeAddr()))
|
||||
if err == nil {
|
||||
rawRes := bson.A{}
|
||||
@ -127,17 +131,13 @@ func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, e
|
||||
|
||||
// FindPaged - Wrapper around FindAll with the Skip and Limit options populated.
|
||||
// returns a pointer to a Query for further chaining.
|
||||
func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error) {
|
||||
func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts *options.FindOptionsBuilder) (*Query, error) {
|
||||
skipAmt := perPage * (page - 1)
|
||||
if skipAmt < 0 {
|
||||
skipAmt = 0
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
opts[0].SetSkip(skipAmt).SetLimit(perPage)
|
||||
} else {
|
||||
opts = append(opts, options.Find().SetSkip(skipAmt).SetLimit(perPage))
|
||||
}
|
||||
q, err := m.Find(query, opts...)
|
||||
opts.SetSkip(skipAmt).SetLimit(perPage)
|
||||
q, err := m.Find(query, opts)
|
||||
q.op = OP_FIND_PAGED
|
||||
return q, err
|
||||
}
|
||||
@ -145,14 +145,14 @@ func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...
|
||||
// FindByID - find a single document by its _id field.
|
||||
// Wrapper around FindOne with an ID query as its first argument
|
||||
func (m *Model) FindByID(id interface{}) (*Query, error) {
|
||||
return m.FindOne(bson.D{{"_id", id}})
|
||||
return m.FindOne(bson.D{{"_id", id}}, nil)
|
||||
}
|
||||
|
||||
// FindOne - find a single document satisfying `query`.
|
||||
// returns a pointer to a Query for further chaining.
|
||||
func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) {
|
||||
func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error) {
|
||||
coll := m.getColl()
|
||||
rip := coll.FindOne(context.TODO(), query, options...)
|
||||
rip := coll.FindOne(context.TODO(), query, options)
|
||||
raw := bson.M{}
|
||||
err := rip.Decode(&raw)
|
||||
if err != nil {
|
||||
|
@ -2,8 +2,8 @@ package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
9
query.go
9
query.go
@ -5,9 +5,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/fatih/structtag"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
@ -471,8 +470,8 @@ func handleAnon(raw interface{}, rtype reflect.Type, rval reflect.Value) reflect
|
||||
amap, ok := raw.(bson.M)
|
||||
if ok {
|
||||
fval := amap[btag.Name]
|
||||
if reflect.TypeOf(fval) == reflect.TypeFor[primitive.DateTime]() {
|
||||
fval = time.UnixMilli(int64(fval.(primitive.DateTime)))
|
||||
if reflect.TypeOf(fval) == reflect.TypeFor[bson.DateTime]() {
|
||||
fval = time.UnixMilli(int64(fval.(bson.DateTime)))
|
||||
}
|
||||
if valueField.Kind() == reflect.Pointer {
|
||||
valueField.Elem().Set(reflect.ValueOf(fval))
|
||||
|
11
registry.go
11
registry.go
@ -8,10 +8,9 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
@ -261,7 +260,7 @@ func (r TModelRegistry) Model(mdl ...any) {
|
||||
panic(fmt.Sprintf("you MUST implement the HasID interface!!! skipping...\n"))
|
||||
}
|
||||
switch (id).Id().(type) {
|
||||
case int, int64, int32, string, primitive.ObjectID, uint, uint32, uint64:
|
||||
case int, int64, int32, string, bson.ObjectID, uint, uint32, uint64:
|
||||
break
|
||||
default:
|
||||
log.Printf("invalid ID type specified!!! skipping...\n")
|
||||
@ -357,7 +356,7 @@ func innerWatch(coll *mongo.Collection) {
|
||||
}
|
||||
|
||||
func Connect(uri string, dbName string) {
|
||||
cli, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
||||
cli, err := mongo.Connect(options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
log.Fatal("failed to open database")
|
||||
}
|
||||
|
13
testing.go
13
testing.go
@ -9,14 +9,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-loremipsum/loremipsum"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
type chapter struct {
|
||||
ID primitive.ObjectID `bson:"_id" json:"_id"`
|
||||
ID bson.ObjectID `bson:"_id" json:"_id"`
|
||||
Title string `bson:"chapterTitle" json:"chapterTitle" form:"chapterTitle"`
|
||||
ChapterID int `bson:"id" json:"chapterID" autoinc:"chapters"`
|
||||
Index int `bson:"index" json:"index" form:"index"`
|
||||
@ -134,7 +133,7 @@ func genChaps(single bool) []chapter {
|
||||
for i := 0; i < ceil; i++ {
|
||||
spf := fmt.Sprintf("%d.md", i+1)
|
||||
ret = append(ret, chapter{
|
||||
ID: primitive.NewObjectID(),
|
||||
ID: bson.NewObjectID(),
|
||||
Title: fmt.Sprintf("-%d-", i+1),
|
||||
Index: i + 1,
|
||||
Words: 50,
|
||||
@ -188,7 +187,7 @@ func iti_blank() story {
|
||||
func initTest() {
|
||||
uri := "mongodb://127.0.0.1:27017"
|
||||
db := "rockfic_ormTest"
|
||||
ic, _ := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
||||
ic, _ := mongo.Connect(options.Client().ApplyURI(uri))
|
||||
ic.Database(db).Drop(context.TODO())
|
||||
colls, _ := ic.Database(db).ListCollectionNames(context.TODO(), bson.M{})
|
||||
if len(colls) < 1 {
|
||||
|
6
util.go
6
util.go
@ -2,7 +2,7 @@ package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -130,8 +130,8 @@ func incrementInterface(t interface{}) interface{} {
|
||||
t = pt + 1
|
||||
case string:
|
||||
t = NextStringID()
|
||||
case primitive.ObjectID:
|
||||
t = primitive.NewObjectID()
|
||||
case bson.ObjectID:
|
||||
t = bson.NewObjectID()
|
||||
default:
|
||||
panic(ErrUnsupportedID)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user