Compare commits
10 Commits
9d791b1e3d
...
d66f9dd4aa
Author | SHA1 | Date | |
---|---|---|---|
d66f9dd4aa | |||
cd72703068 | |||
e6a83d12e3 | |||
7fc4ed46c8 | |||
866a459855 | |||
8c559d5926 | |||
64b9d1c241 | |||
53e23bb52a | |||
15d032459a | |||
f9aa34ef12 |
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@ -1,5 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GitSharedSettings">
|
||||||
|
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="synchronizeBranchProtectionRules" value="false" />
|
||||||
|
</component>
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
|
365
README.md
Normal file
365
README.md
Normal file
@ -0,0 +1,365 @@
|
|||||||
|
# diamond
|
||||||
|
|
||||||
|
### a golang ORM for mongodb that rocks 🎸~♬
|
||||||
|
|
||||||
|
# usage
|
||||||
|
|
||||||
|
## installation
|
||||||
|
|
||||||
|
run the following command in your terminal...
|
||||||
|
|
||||||
|
```shell
|
||||||
|
go get rockfic.com/orm
|
||||||
|
```
|
||||||
|
|
||||||
|
...and import the package at the top of your file(s) like so:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Connect to the database
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
orm.Connect("mongodb://127.0.0.1", "your_database")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
this will create a connection and store it in the `DB` global variable. This global variable is used internally to
|
||||||
|
interact with the underlying database.
|
||||||
|
|
||||||
|
if you need to <sub><sub>~~why?~~</sub></sub>, you may also access the mongoDB client directly via the `orm.Client`
|
||||||
|
variable.
|
||||||
|
|
||||||
|
## Create a Model
|
||||||
|
|
||||||
|
to create a new model, you need to define a struct like this:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
orm.Document `bson:",inline" coll:"collection"`
|
||||||
|
ID int64 `bson:"_id"`
|
||||||
|
Username string `bson:"username"`
|
||||||
|
Email string `bson:"email"`
|
||||||
|
Friends []User `bson:"friends"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
this on its own is useless. to actually do anything useful with it, you need to *register* this struct as a model:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
orm.ModelRegistry.Model(User{})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
you can also pass multiple arguments to `orm.Model`, so long as they embed the `Document` struct.
|
||||||
|
|
||||||
|
you can access the newly created model like this:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
userModel := orm.ModelRegistry.Get("User")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documents
|
||||||
|
|
||||||
|
any struct can be used as a document, so long as it embeds the `Document` struct. the `Document` struct is special, in
|
||||||
|
that it turns any structs which embed it into `IDocument` implementors.
|
||||||
|
`Document` should be embedded with a `bson:",inline" tag, otherwise you will end up with something like this in the
|
||||||
|
database:
|
||||||
|
|
||||||
|
```bson
|
||||||
|
{
|
||||||
|
"document": {
|
||||||
|
"createdAt": ISODate('2001-09-11T05:37:18.742Z'),
|
||||||
|
"updatedAt": ISODate('2001-09-11T05:37:18.742Z')
|
||||||
|
},
|
||||||
|
_id: 1,
|
||||||
|
username: "foobar",
|
||||||
|
email: "test@testi.ng",
|
||||||
|
friends: []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
a `coll` or `collection` tag is also required, to assign the model to a mongodb collection. this tag is only valid on
|
||||||
|
the embedded `Document` field, and each document can only have one collection associated with it.
|
||||||
|
<sub>obviously.</sub>
|
||||||
|
<sub><sub>i mean seriously, who'd want to store one thing in two places?</sub></sub>
|
||||||
|
|
||||||
|
the recommended way to create a new document instance is via the `orm.Create` method:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
user := orm.Create(User{}).(*User)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
similarly, to create a slice of documents, call `orm.CreateSlice`:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
users := orm.CreateSlice[User](User{})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
lastly, let's implement the `HasID` interface on our document:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func (u *User) GetId() any {
|
||||||
|
return u.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) SetId(id any) {
|
||||||
|
u.ID = id.(int64)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### but why do i need to implement this? :(
|
||||||
|
|
||||||
|
other ORMs rudely assume that you want your `_id`s to be mongodb ObjectIDs, which are **fucking ugly**, but also, more
|
||||||
|
importantly, *may not match your existing schema.*
|
||||||
|
by doing things this way, you can set your _id to be any of the following types:
|
||||||
|
|
||||||
|
- int
|
||||||
|
- int32
|
||||||
|
- int64
|
||||||
|
- uint
|
||||||
|
- uint32
|
||||||
|
- uint64
|
||||||
|
- string
|
||||||
|
- ObjectId
|
||||||
|
|
||||||
|
<sub>(if you hate yourself that much)</sub>
|
||||||
|
|
||||||
|
## finding/querying
|
||||||
|
|
||||||
|
`Find`, `FindOne`, `FindByID`, and `FindPaged` all return an `*orm.Query` object.
|
||||||
|
to get the underlying value, simply call `.Exec()`, passing to it a pointer to the variable in which you wish to store
|
||||||
|
the results.
|
||||||
|
|
||||||
|
### `Find` example
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"rockfic.com/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
userModel := orm.ModelRegistry.Get("User")
|
||||||
|
if userModel != nil {
|
||||||
|
res := orm.CreateSlice(User{})
|
||||||
|
jq, _ := userModel.Find(bson.M{"username": "foobar"})
|
||||||
|
q.Exec(&res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## saving/updating
|
||||||
|
|
||||||
|
to save a document, simply call the `Save()` method. if the document doesn't exist, the ORM will create it, otherwise it
|
||||||
|
replaces the existing one.
|
||||||
|
|
||||||
|
## referencing other documents
|
||||||
|
|
||||||
|
it's possible to store references to other documents/structs, or slices to other documents. given the `User` struct we
|
||||||
|
defined above, which contains a slice (`Friends`) referencing other `User`s, we could change the `Friends` field in the
|
||||||
|
`User` type to be populateable, like so:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
type User struct {
|
||||||
|
orm.Document `bson:",inline" coll:"collection"`
|
||||||
|
ID int64 `bson:"_id"`
|
||||||
|
Username string `bson:"username"`
|
||||||
|
Email string `bson:"email"`
|
||||||
|
- Friends []User `bson:"friends"`
|
||||||
|
+ Friends []User `bson:"friends" ref:"User"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
assuming we've filled a user's `Friends` with a few friends...
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import "rockfic.com/orm"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
user := orm.Create(User{}).(*User)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
friend := orm.Create(User{ID: int64(i + 2)}).(*User)
|
||||||
|
user.Friends = append(user.Friends, friend)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
...in the database, `friends` will be stored like this:
|
||||||
|
|
||||||
|
```json5
|
||||||
|
{
|
||||||
|
// ~ snip ~ //
|
||||||
|
friends: [
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
11
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
after retrieving a `User` from the database, you can load their `Friends` and their info by using the `Populate`function
|
||||||
|
on the returned `Query` pointer, like in the following example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package tutorial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"rockfic.com/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* ~ snip ~ */
|
||||||
|
query, err := userModel.FindOne(bson.M{"_id": 1})
|
||||||
|
if err == nil {
|
||||||
|
result := orm.Create(User{}).(*User)
|
||||||
|
query.Populate("Friends").Exec(result)
|
||||||
|
fmt.Printf("%+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Models vs Documents: what's the difference?
|
||||||
|
|
||||||
|
a **`Model`** is a static type that contains information about a given Document-like type. a **`Document`**, on the
|
||||||
|
other hand, is a full-fledged instances with its own set of methods.
|
||||||
|
if you come from an object-oriented programming background, you can envision the relationship between a `Model` and
|
||||||
|
`Document` something like this (using Java as an example):
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Document {
|
||||||
|
public void Pull(field String, elements ...Document) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
public void Append(field String, elements ...Document) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
public void Save() {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
public void Delete() {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
|
||||||
|
// model methods //
|
||||||
|
public static Document FindOne(HashMap<String, Object> query, MongoOptions options) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
public static ArrayList<Document> FindAll(HashMap<String, Object> query, MongoOptions options) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Struct Tags
|
||||||
|
|
||||||
|
the following tags are recognized by the ORM:
|
||||||
|
|
||||||
|
### `ref`
|
||||||
|
|
||||||
|
the syntax of the `ref` tag is:
|
||||||
|
|
||||||
|
```
|
||||||
|
`ref:"struct_name"`
|
||||||
|
```
|
||||||
|
|
||||||
|
where `struct_name` is the name of the struct you're referencing, as shown
|
||||||
|
in [Referencing other documents](#referencing-other-documents).
|
||||||
|
|
||||||
|
### `gridfs`
|
||||||
|
|
||||||
|
you can load files stored in [gridfs](https://www.mongodb.com/docs/manual/core/gridfs/) directly into your structs using
|
||||||
|
this tag, which has the following syntax:
|
||||||
|
|
||||||
|
```
|
||||||
|
`gridfs:"bucket_name,file_fmt"
|
||||||
|
```
|
||||||
|
|
||||||
|
where:
|
||||||
|
|
||||||
|
- `bucket_name` is the name of the gridfs bucket
|
||||||
|
- `file_fmt` is a valid [go template string](https://pkg.go.dev/text/template) that resolves to the unique file name.
|
||||||
|
all exported methods and fields in the surrounding struct can be referenced in this template.
|
||||||
|
|
||||||
|
currently, the supported field types for the `gridfs` tag are:
|
||||||
|
|
||||||
|
- `string`
|
||||||
|
- `[]byte`
|
||||||
|
|
||||||
|
### `idx`/`index`
|
||||||
|
|
||||||
|
indexes can be defined using this tag on the field you want to create the index for. the syntax is as follows:
|
||||||
|
|
||||||
|
`idx:{field || field1,field2,...},keyword1,keyword2,...`
|
||||||
|
|
||||||
|
supported keywords are `unique` and `sparse`.
|
||||||
|
`background` is technically allowed, but it's deprecated, and so will essentially be a no-op on mongodb 4.2+.
|
||||||
|
|
||||||
|
## acknowledgements
|
||||||
|
|
||||||
|
- [goonode/mogo](https://github.com/goonode/mogo), the project which largely inspired this one
|
||||||
|
|
||||||
|
# Further reading
|
||||||
|
|
||||||
|
see the [godocs](https://git.tablet.sh/tablet/diamond-orm/src/branch/main/godoc.html) for more details :)
|
@ -10,7 +10,7 @@ type Document struct {
|
|||||||
Created time.Time `bson:"createdAt" json:"createdAt"`
|
Created time.Time `bson:"createdAt" json:"createdAt"`
|
||||||
// Modified time. updated/added automatically.
|
// Modified time. updated/added automatically.
|
||||||
Modified time.Time `bson:"updatedAt" json:"updatedAt"`
|
Modified time.Time `bson:"updatedAt" json:"updatedAt"`
|
||||||
model *Model
|
model *Model `bson:"-"`
|
||||||
exists bool `bson:"-"`
|
exists bool `bson:"-"`
|
||||||
self any `bson:"-"`
|
self any `bson:"-"`
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/structtag"
|
"github.com/fatih/structtag"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -39,21 +39,6 @@ func serializeIDs(input interface{}) interface{} {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*var itagged interface{}
|
|
||||||
if reflect.ValueOf(itagged).Kind() != reflect.Pointer {
|
|
||||||
itagged = incrementTagged(&input)
|
|
||||||
} else {
|
|
||||||
itagged = incrementTagged(input)
|
|
||||||
}
|
|
||||||
taggedVal := reflect.ValueOf(reflect.ValueOf(itagged).Interface()).Elem()
|
|
||||||
if vp.Kind() == reflect.Ptr {
|
|
||||||
tmp := reflect.ValueOf(taggedVal.Interface())
|
|
||||||
if tmp.Kind() == reflect.Pointer {
|
|
||||||
vp.Elem().Set(tmp.Elem())
|
|
||||||
} else {
|
|
||||||
vp.Elem().Set(tmp)
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
switch vp.Elem().Kind() {
|
switch vp.Elem().Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
ret0 := bson.M{}
|
ret0 := bson.M{}
|
||||||
@ -72,10 +57,6 @@ func serializeIDs(input interface{}) interface{} {
|
|||||||
bson.Unmarshal(marsh, &unmarsh)
|
bson.Unmarshal(marsh, &unmarsh)
|
||||||
for k, v := range unmarsh {
|
for k, v := range unmarsh {
|
||||||
ret0[k] = v
|
ret0[k] = v
|
||||||
/*if t, ok := v.(primitive.DateTime); ok {
|
|
||||||
ret0
|
|
||||||
} else {
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, terr := tag.Get("ref")
|
_, terr := tag.Get("ref")
|
||||||
@ -92,8 +73,6 @@ func serializeIDs(input interface{}) interface{} {
|
|||||||
rarr = append(rarr, getID(fv.Index(j).Interface()))
|
rarr = append(rarr, getID(fv.Index(j).Interface()))
|
||||||
}
|
}
|
||||||
ret0[bbson.Name] = rarr
|
ret0[bbson.Name] = rarr
|
||||||
/*ret0[bbson.Name] = serializeIDs(fv.Interface())
|
|
||||||
break*/
|
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
panic(fmt.Sprintf("referenced model slice at '%s.%s' does not implement HasID", nameOf(input), ft.Name))
|
panic(fmt.Sprintf("referenced model slice at '%s.%s' does not implement HasID", nameOf(input), ft.Name))
|
||||||
} else {
|
} else {
|
||||||
|
13
go.mod
13
go.mod
@ -4,20 +4,25 @@ go 1.23
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fatih/structtag v1.2.0
|
github.com/fatih/structtag v1.2.0
|
||||||
go.mongodb.org/mongo-driver v1.16.1
|
|
||||||
golang.org/x/net v0.29.0
|
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 (
|
require (
|
||||||
github.com/go-loremipsum/loremipsum v1.1.3
|
github.com/go-loremipsum/loremipsum v1.1.3
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/klauspost/compress v1.13.6 // indirect
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
github.com/stretchr/testify v1.9.0
|
||||||
github.com/stretchr/testify v1.9.0 // indirect
|
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||||
github.com/xdg-go/scram v1.1.2 // indirect
|
github.com/xdg-go/scram v1.1.2 // indirect
|
||||||
github.com/xdg-go/stringprep v1.0.4 // 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/crypto v0.27.0 // indirect
|
||||||
golang.org/x/sync v0.8.0 // indirect
|
golang.org/x/sync v0.8.0 // indirect
|
||||||
golang.org/x/text v0.18.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/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 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
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 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
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=
|
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/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 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
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-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
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=
|
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/v2 v2.0.0-beta2 h1:PRtbRKwblE8ZfI8qOhofcjn9y8CmKZI7trS5vDMeJX0=
|
||||||
go.mongodb.org/mongo-driver v1.16.1/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
|
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-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.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 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
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.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-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-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.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 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
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-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.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
|
||||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
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.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.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
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 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
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-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.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/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=
|
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=
|
||||||
|
1229
godoc.html
Normal file
1229
godoc.html
Normal file
File diff suppressed because one or more lines are too long
32
gridfs.go
32
gridfs.go
@ -6,11 +6,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/structtag"
|
"github.com/fatih/structtag"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/gridfs"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -18,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GridFSFile struct {
|
type GridFSFile struct {
|
||||||
ID primitive.ObjectID `bson:"_id"`
|
ID bson.ObjectID `bson:"_id"`
|
||||||
Name string `bson:"filename"`
|
Name string `bson:"filename"`
|
||||||
Length int `bson:"length"`
|
Length int `bson:"length"`
|
||||||
}
|
}
|
||||||
@ -32,12 +30,12 @@ func parseFmt(format string, value any) string {
|
|||||||
return w.String()
|
return w.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func bucket(gfsRef GridFSReference) *gridfs.Bucket {
|
func bucket(gfsRef gridFSReference) *mongo.GridFSBucket {
|
||||||
b, _ := gridfs.NewBucket(DB, options.GridFSBucket().SetName(gfsRef.BucketName))
|
b := DB.GridFSBucket(options.GridFSBucket().SetName(gfsRef.BucketName))
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func gridFsLoad(val any, g GridFSReference, field string) any {
|
func gridFsLoad(val any, g gridFSReference, field string) any {
|
||||||
doc := reflect.ValueOf(val)
|
doc := reflect.ValueOf(val)
|
||||||
rdoc := reflect.ValueOf(val)
|
rdoc := reflect.ValueOf(val)
|
||||||
if doc.Kind() != reflect.Pointer {
|
if doc.Kind() != reflect.Pointer {
|
||||||
@ -83,14 +81,14 @@ func gridFsLoad(val any, g GridFSReference, field string) any {
|
|||||||
default:
|
default:
|
||||||
b := bucket(g)
|
b := bucket(g)
|
||||||
var found GridFSFile
|
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 {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cursor.Next(context.TODO())
|
cursor.Next(context.TODO())
|
||||||
_ = cursor.Decode(&found)
|
_ = cursor.Decode(&found)
|
||||||
bb := bytes.NewBuffer(nil)
|
bb := bytes.NewBuffer(nil)
|
||||||
_, err = b.DownloadToStream(found.ID, bb)
|
_, err = b.DownloadToStream(context.TODO(), found.ID, bb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -125,22 +123,22 @@ func gridFsSave(val any, imodel Model) error {
|
|||||||
}
|
}
|
||||||
_, err := structtag.Parse(string(ft.Tag))
|
_, err := structtag.Parse(string(ft.Tag))
|
||||||
panik(err)
|
panik(err)
|
||||||
var gfsRef *GridFSReference
|
var gfsRef *gridFSReference
|
||||||
for kk, vv := range imodel.GridFSReferences {
|
for kk, vv := range imodel.gridFSReferences {
|
||||||
if strings.HasPrefix(kk, ft.Name) {
|
if strings.HasPrefix(kk, ft.Name) {
|
||||||
gfsRef = &vv
|
gfsRef = &vv
|
||||||
break
|
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())
|
filename := parseFmt(gfsRef.FilenameFmt, it.Interface())
|
||||||
contents := GridFSFile{}
|
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) {
|
if !errors.Is(err2, mongo.ErrNoDocuments) {
|
||||||
_ = curs.Decode(&contents)
|
_ = curs.Decode(&contents)
|
||||||
if !reflect.ValueOf(contents).IsZero() {
|
if !reflect.ValueOf(contents).IsZero() {
|
||||||
_ = b.Delete(contents.ID)
|
_ = b.Delete(context.TODO(), contents.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c := it.Field(gfsRef.Idx)
|
c := it.Field(gfsRef.Idx)
|
||||||
@ -153,7 +151,7 @@ func gridFsSave(val any, imodel Model) error {
|
|||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("gridfs loader type '%s' not supported", c.Type().String())
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const COUNTER_COL = "@counters"
|
const COUNTER_COL = "@counters"
|
||||||
@ -20,7 +19,7 @@ func getLastInColl(cname string, id interface{}) interface{} {
|
|||||||
var opts = options.FindOne()
|
var opts = options.FindOne()
|
||||||
|
|
||||||
switch id.(type) {
|
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})
|
opts.SetSort(bson.M{"_id": -1})
|
||||||
case string:
|
case string:
|
||||||
opts.SetSort(bson.M{"createdAt": -1})
|
opts.SetSort(bson.M{"createdAt": -1})
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var optionKeywords = [...]string{"unique", "sparse", "background", "dropdups"}
|
var optionKeywords = [...]string{"unique", "sparse", "background", "dropdups"}
|
||||||
@ -138,8 +138,8 @@ func buildIndex(i InternalIndex) *mongo.IndexModel {
|
|||||||
switch o {
|
switch o {
|
||||||
case "unique":
|
case "unique":
|
||||||
idx.Options.SetUnique(true)
|
idx.Options.SetUnique(true)
|
||||||
case "background":
|
//case "background":
|
||||||
idx.Options.SetBackground(true)
|
// idx.Options.SetBackground(true)
|
||||||
case "sparse":
|
case "sparse":
|
||||||
idx.Options.SetSparse(true)
|
idx.Options.SetSparse(true)
|
||||||
}
|
}
|
||||||
|
77
model.go
77
model.go
@ -3,22 +3,23 @@ package orm
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"reflect"
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model - "base" struct for all queryable models
|
// Model - type which contains "static" methods like
|
||||||
|
// Find, FindOne, etc.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
typeName string `bson:"-"`
|
|
||||||
Idx int
|
|
||||||
Type reflect.Type
|
|
||||||
Collection string
|
|
||||||
References map[string]Reference
|
|
||||||
Indexes map[string][]InternalIndex
|
Indexes map[string][]InternalIndex
|
||||||
GridFSReferences map[string]GridFSReference
|
Type reflect.Type
|
||||||
|
collection string
|
||||||
|
gridFSReferences map[string]gridFSReference
|
||||||
|
idx int
|
||||||
|
references map[string]Reference
|
||||||
|
typeName string `bson:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasID is a simple interface that you must implement
|
// HasID is a simple interface that you must implement
|
||||||
@ -35,11 +36,11 @@ type HasID interface {
|
|||||||
type HasIDSlice []HasID
|
type HasIDSlice []HasID
|
||||||
|
|
||||||
type IModel interface {
|
type IModel interface {
|
||||||
FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
|
FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error)
|
||||||
Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
|
Find(query interface{}, opts *options.FindOptionsBuilder) (*Query, error)
|
||||||
FindByID(id interface{}) (*Query, error)
|
FindByID(id interface{}) (*Query, error)
|
||||||
FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
|
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
||||||
FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error)
|
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
||||||
|
|
||||||
getColl() *mongo.Collection
|
getColl() *mongo.Collection
|
||||||
getIdxs() []*mongo.IndexModel
|
getIdxs() []*mongo.IndexModel
|
||||||
@ -61,7 +62,7 @@ func (m *Model) getColl() *mongo.Collection {
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
||||||
}
|
}
|
||||||
return DB.Collection(ri.Collection)
|
return DB.Collection(ri.collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) getIdxs() []*mongo.IndexModel {
|
func (m *Model) getIdxs() []*mongo.IndexModel {
|
||||||
@ -86,15 +87,19 @@ func (m *Model) getParsedIdxs() map[string][]InternalIndex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindRaw - find documents satisfying `query` and return a plain mongo cursor.
|
// 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()
|
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
|
return cursor, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find - find all documents satisfying `query`.
|
// Find - find all documents satisfying `query`.
|
||||||
// returns a pointer to a Query for further chaining.
|
// 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)
|
qqn := ModelRegistry.new_(m.typeName)
|
||||||
qqt := reflect.SliceOf(reflect.TypeOf(qqn))
|
qqt := reflect.SliceOf(reflect.TypeOf(qqn))
|
||||||
qqv := reflect.New(qqt)
|
qqv := reflect.New(qqt)
|
||||||
@ -105,7 +110,7 @@ func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, e
|
|||||||
doc: qqv.Interface(),
|
doc: qqv.Interface(),
|
||||||
op: OP_FIND_ALL,
|
op: OP_FIND_ALL,
|
||||||
}
|
}
|
||||||
q, err := m.FindRaw(query, opts...)
|
q, err := m.FindRaw(query, opts)
|
||||||
idoc := (*DocumentSlice)(unsafe.Pointer(qqv.Elem().UnsafeAddr()))
|
idoc := (*DocumentSlice)(unsafe.Pointer(qqv.Elem().UnsafeAddr()))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
rawRes := bson.A{}
|
rawRes := bson.A{}
|
||||||
@ -113,12 +118,16 @@ func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, e
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
idoc.setExists(true)
|
idoc.setExists(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
qq.rawDoc = rawRes
|
qq.rawDoc = rawRes
|
||||||
err = q.All(context.TODO(), &qq.doc)
|
err = q.All(context.TODO(), &qq.doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
qq.reOrganize()
|
qq.reOrganize()
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
for _, doc := range *idoc {
|
||||||
|
doc.setModel(*m)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return qq, err
|
return qq, err
|
||||||
@ -126,17 +135,13 @@ func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, e
|
|||||||
|
|
||||||
// FindPaged - Wrapper around FindAll with the Skip and Limit options populated.
|
// FindPaged - Wrapper around FindAll with the Skip and Limit options populated.
|
||||||
// returns a pointer to a Query for further chaining.
|
// 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)
|
skipAmt := perPage * (page - 1)
|
||||||
if skipAmt < 0 {
|
if skipAmt < 0 {
|
||||||
skipAmt = 0
|
skipAmt = 0
|
||||||
}
|
}
|
||||||
if len(opts) > 0 {
|
opts.SetSkip(skipAmt).SetLimit(perPage)
|
||||||
opts[0].SetSkip(skipAmt).SetLimit(perPage)
|
q, err := m.Find(query, opts)
|
||||||
} else {
|
|
||||||
opts = append(opts, options.Find().SetSkip(skipAmt).SetLimit(perPage))
|
|
||||||
}
|
|
||||||
q, err := m.Find(query, opts...)
|
|
||||||
q.op = OP_FIND_PAGED
|
q.op = OP_FIND_PAGED
|
||||||
return q, err
|
return q, err
|
||||||
}
|
}
|
||||||
@ -144,14 +149,14 @@ func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...
|
|||||||
// FindByID - find a single document by its _id field.
|
// FindByID - find a single document by its _id field.
|
||||||
// Wrapper around FindOne with an ID query as its first argument
|
// Wrapper around FindOne with an ID query as its first argument
|
||||||
func (m *Model) FindByID(id interface{}) (*Query, error) {
|
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`.
|
// FindOne - find a single document satisfying `query`.
|
||||||
// returns a pointer to a Query for further chaining.
|
// 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()
|
coll := m.getColl()
|
||||||
rip := coll.FindOne(context.TODO(), query, options...)
|
rip := coll.FindOne(context.TODO(), query, options)
|
||||||
raw := bson.M{}
|
raw := bson.M{}
|
||||||
err := rip.Decode(&raw)
|
err := rip.Decode(&raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -159,9 +164,6 @@ func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (
|
|||||||
}
|
}
|
||||||
qqn := ModelRegistry.new_(m.typeName)
|
qqn := ModelRegistry.new_(m.typeName)
|
||||||
idoc, ok := qqn.(IDocument)
|
idoc, ok := qqn.(IDocument)
|
||||||
if ok {
|
|
||||||
idoc.setExists(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
qq := &Query{
|
qq := &Query{
|
||||||
collection: m.getColl(),
|
collection: m.getColl(),
|
||||||
@ -176,6 +178,10 @@ func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (
|
|||||||
qq.reOrganize()
|
qq.reOrganize()
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
if ok {
|
||||||
|
idoc.setExists(true)
|
||||||
|
idoc.setModel(*m)
|
||||||
|
}
|
||||||
idoc.setSelf(idoc)
|
idoc.setSelf(idoc)
|
||||||
return qq, err
|
return qq, err
|
||||||
}
|
}
|
||||||
@ -224,6 +230,9 @@ func Create(d any) any {
|
|||||||
return what
|
return what
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateSlice - convenience method which creates a new slice
|
||||||
|
// of type *T (where T is a type which embeds Document) and
|
||||||
|
// returns it
|
||||||
func CreateSlice[T any](d T) []*T {
|
func CreateSlice[T any](d T) []*T {
|
||||||
r, _, _ := createBase(d)
|
r, _, _ := createBase(d)
|
||||||
rtype := r.Type()
|
rtype := r.Type()
|
||||||
@ -232,7 +241,3 @@ func CreateSlice[T any](d T) []*T {
|
|||||||
newItem.Elem().Set(reflect.MakeSlice(rslice, 0, 0))
|
newItem.Elem().Set(reflect.MakeSlice(rslice, 0, 0))
|
||||||
return newItem.Elem().Interface().([]*T)
|
return newItem.Elem().Interface().([]*T)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) PrintMe() {
|
|
||||||
fmt.Printf("My name is %s !\n", nameOf(m))
|
|
||||||
}
|
|
||||||
|
@ -2,8 +2,8 @@ package orm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
34
query.go
34
query.go
@ -5,9 +5,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/structtag"
|
"github.com/fatih/structtag"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@ -127,7 +126,7 @@ func populate(r Reference, rcoll string, rawDoc interface{}, d string, src inter
|
|||||||
tag, err := structtag.Parse(string(ff.Tag))
|
tag, err := structtag.Parse(string(ff.Tag))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
val, err2 := tag.Get("bson")
|
val, err2 := tag.Get("bson")
|
||||||
if err2 == nil {
|
if err2 == nil && val.Name != "-" {
|
||||||
fttt := ff.Type
|
fttt := ff.Type
|
||||||
if fttt.Kind() == reflect.Pointer || fttt.Kind() == reflect.Slice {
|
if fttt.Kind() == reflect.Pointer || fttt.Kind() == reflect.Slice {
|
||||||
fttt = fttt.Elem()
|
fttt = fttt.Elem()
|
||||||
@ -176,6 +175,7 @@ func populate(r Reference, rcoll string, rawDoc interface{}, d string, src inter
|
|||||||
src = t
|
src = t
|
||||||
toReturn = src
|
toReturn = src
|
||||||
}
|
}
|
||||||
|
t.setExists(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if toReturn == nil {
|
if toReturn == nil {
|
||||||
@ -199,9 +199,9 @@ func (q *Query) LoadFile(fields ...string) *Query {
|
|||||||
_, cm, _ := ModelRegistry.HasByName(q.model.typeName)
|
_, cm, _ := ModelRegistry.HasByName(q.model.typeName)
|
||||||
if cm != nil {
|
if cm != nil {
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
var r GridFSReference
|
var r gridFSReference
|
||||||
hasAnnotated := false
|
hasAnnotated := false
|
||||||
for k2, v := range cm.GridFSReferences {
|
for k2, v := range cm.gridFSReferences {
|
||||||
if strings.HasPrefix(k2, field) {
|
if strings.HasPrefix(k2, field) {
|
||||||
r = v
|
r = v
|
||||||
hasAnnotated = true
|
hasAnnotated = true
|
||||||
@ -227,14 +227,14 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
|
|
||||||
var r Reference
|
var r Reference
|
||||||
|
|
||||||
for k2, v := range cm.References {
|
for k2, v := range cm.references {
|
||||||
if strings.HasPrefix(k2, field) {
|
if strings.HasPrefix(k2, field) {
|
||||||
r = v
|
r = v
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Exists {
|
if r.exists {
|
||||||
// get self
|
// get self
|
||||||
// get ptr
|
// get ptr
|
||||||
// find
|
// find
|
||||||
@ -263,7 +263,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
ref = ref.Elem()
|
ref = ref.Elem()
|
||||||
}
|
}
|
||||||
src := ref.Index(i).Interface()
|
src := ref.Index(i).Interface()
|
||||||
inter := populate(r, refColl.Collection, val2, field, src)
|
inter := populate(r, refColl.collection, val2, field, src)
|
||||||
if reflect.ValueOf(inter).Kind() == reflect.Pointer {
|
if reflect.ValueOf(inter).Kind() == reflect.Pointer {
|
||||||
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
|
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
|
||||||
} else {
|
} else {
|
||||||
@ -272,7 +272,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
}
|
}
|
||||||
tmp1 = slic.Interface()
|
tmp1 = slic.Interface()
|
||||||
} else {
|
} else {
|
||||||
tmp1 = populate(r, refColl.Collection, rawDoc, field, reflect.ValueOf(q.doc).Interface())
|
tmp1 = populate(r, refColl.collection, rawDoc, field, reflect.ValueOf(q.doc).Interface())
|
||||||
}
|
}
|
||||||
q.doc = tmp1
|
q.doc = tmp1
|
||||||
}
|
}
|
||||||
@ -290,21 +290,11 @@ func (q *Query) reOrganize() {
|
|||||||
slic := reflect.New(reflect.SliceOf(typ))
|
slic := reflect.New(reflect.SliceOf(typ))
|
||||||
for _, v2 := range arr {
|
for _, v2 := range arr {
|
||||||
inter := reflect.ValueOf(rerere(v2, typ))
|
inter := reflect.ValueOf(rerere(v2, typ))
|
||||||
/*if inter.Kind() == reflect.Pointer {
|
|
||||||
inter = inter.Elem()
|
|
||||||
}*/
|
|
||||||
slic.Elem().Set(reflect.Append(slic.Elem(), inter))
|
slic.Elem().Set(reflect.Append(slic.Elem(), inter))
|
||||||
}
|
}
|
||||||
trvo = slic.Elem()
|
trvo = slic.Elem()
|
||||||
} else {
|
} else {
|
||||||
trvo = reflect.ValueOf(rerere(q.rawDoc, reflect.TypeOf(q.doc)))
|
trvo = reflect.ValueOf(rerere(q.rawDoc, reflect.TypeOf(q.doc)))
|
||||||
/*for {
|
|
||||||
if trvo.Kind() == reflect.Pointer {
|
|
||||||
trvo = trvo.Elem()
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resV := reflect.ValueOf(q.doc)
|
resV := reflect.ValueOf(q.doc)
|
||||||
@ -481,8 +471,8 @@ func handleAnon(raw interface{}, rtype reflect.Type, rval reflect.Value) reflect
|
|||||||
amap, ok := raw.(bson.M)
|
amap, ok := raw.(bson.M)
|
||||||
if ok {
|
if ok {
|
||||||
fval := amap[btag.Name]
|
fval := amap[btag.Name]
|
||||||
if reflect.TypeOf(fval) == reflect.TypeFor[primitive.DateTime]() {
|
if reflect.TypeOf(fval) == reflect.TypeFor[bson.DateTime]() {
|
||||||
fval = time.UnixMilli(int64(fval.(primitive.DateTime)))
|
fval = time.UnixMilli(int64(fval.(bson.DateTime)))
|
||||||
}
|
}
|
||||||
if valueField.Kind() == reflect.Pointer {
|
if valueField.Kind() == reflect.Pointer {
|
||||||
valueField.Elem().Set(reflect.ValueOf(fval))
|
valueField.Elem().Set(reflect.ValueOf(fval))
|
||||||
|
114
registry.go
114
registry.go
@ -5,14 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/fatih/structtag"
|
"github.com/fatih/structtag"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,10 +28,10 @@ type Reference struct {
|
|||||||
// field kind (struct, slice, ...)
|
// field kind (struct, slice, ...)
|
||||||
Kind reflect.Kind
|
Kind reflect.Kind
|
||||||
|
|
||||||
Exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GridFSReference struct {
|
type gridFSReference struct {
|
||||||
BucketName string
|
BucketName string
|
||||||
FilenameFmt string
|
FilenameFmt string
|
||||||
LoadType reflect.Type
|
LoadType reflect.Type
|
||||||
@ -45,7 +43,7 @@ type TModelRegistry map[string]*Model
|
|||||||
// ModelRegistry - the ModelRegistry stores a map containing
|
// ModelRegistry - the ModelRegistry stores a map containing
|
||||||
// pointers to Model instances, keyed by an associated
|
// pointers to Model instances, keyed by an associated
|
||||||
// model name
|
// model name
|
||||||
var ModelRegistry = make(TModelRegistry, 0)
|
var ModelRegistry = make(TModelRegistry)
|
||||||
|
|
||||||
// DB - The mongodb database handle
|
// DB - The mongodb database handle
|
||||||
var DB *mongo.Database
|
var DB *mongo.Database
|
||||||
@ -59,27 +57,7 @@ var NextStringID func() string
|
|||||||
|
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
func getRawTypeFromTag(tagOpt string, slice bool) reflect.Type {
|
func makeGfsRef(tag *structtag.Tag, idx int) gridFSReference {
|
||||||
var t reflect.Type
|
|
||||||
switch strings.ToLower(tagOpt) {
|
|
||||||
case "int":
|
|
||||||
var v int64 = 0
|
|
||||||
t = reflect.TypeOf(v)
|
|
||||||
case "uint":
|
|
||||||
var v uint = 0
|
|
||||||
t = reflect.TypeOf(v)
|
|
||||||
case "string":
|
|
||||||
var v = "0"
|
|
||||||
t = reflect.TypeOf(v)
|
|
||||||
|
|
||||||
}
|
|
||||||
if slice {
|
|
||||||
return reflect.SliceOf(t)
|
|
||||||
}
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
|
||||||
opts := tag.Options
|
opts := tag.Options
|
||||||
var ffmt string
|
var ffmt string
|
||||||
if len(opts) < 1 {
|
if len(opts) < 1 {
|
||||||
@ -101,7 +79,7 @@ func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return GridFSReference{
|
return gridFSReference{
|
||||||
FilenameFmt: ffmt,
|
FilenameFmt: ffmt,
|
||||||
BucketName: tag.Name,
|
BucketName: tag.Name,
|
||||||
LoadType: typ,
|
LoadType: typ,
|
||||||
@ -117,7 +95,7 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
|||||||
Model: modelName,
|
Model: modelName,
|
||||||
HydratedType: ht,
|
HydratedType: ht,
|
||||||
Kind: ht.Kind(),
|
Kind: ht.Kind(),
|
||||||
Exists: true,
|
exists: true,
|
||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,17 +105,17 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
|||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
HydratedType: ht,
|
HydratedType: ht,
|
||||||
Kind: ht.Kind(),
|
Kind: ht.Kind(),
|
||||||
Exists: true,
|
exists: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("model name was empty")
|
panic("model name was empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map[string]Reference, map[string]GridFSReference, string) {
|
func parseTags(t reflect.Type, v reflect.Value, lastParsed string, eqCount int) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) {
|
||||||
coll := ""
|
coll := ""
|
||||||
refs := make(map[string]Reference)
|
refs := make(map[string]Reference)
|
||||||
idcs := make(map[string][]InternalIndex)
|
idcs := make(map[string][]InternalIndex)
|
||||||
gfsRefs := make(map[string]GridFSReference)
|
gfsRefs := make(map[string]gridFSReference)
|
||||||
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
sft := t.Field(i)
|
sft := t.Field(i)
|
||||||
@ -152,9 +130,15 @@ func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map
|
|||||||
switch ft.Kind() {
|
switch ft.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
ft = ft.Elem()
|
ft = ft.Elem()
|
||||||
if _, ok := tags.Get("ref"); ok != nil {
|
count := eqCount
|
||||||
|
if lastParsed != ft.String() {
|
||||||
|
count = 0
|
||||||
|
} else {
|
||||||
|
count = count + 1
|
||||||
|
}
|
||||||
|
if /*_, ok := tags.Get("ref"); ok != nil && */ count < 3 {
|
||||||
if ft.Kind() == reflect.Struct {
|
if ft.Kind() == reflect.Struct {
|
||||||
ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem())
|
ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem(), ft.String(), count)
|
||||||
for k, vv := range ii2 {
|
for k, vv := range ii2 {
|
||||||
idcs[sft.Name+"."+k] = vv
|
idcs[sft.Name+"."+k] = vv
|
||||||
}
|
}
|
||||||
@ -183,10 +167,6 @@ func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if refTag, ok := tags.Get("ref"); ok == nil {
|
if refTag, ok := tags.Get("ref"); ok == nil {
|
||||||
// ref:"ModelName,refType"
|
|
||||||
/* if len(refTag.Options) < 1 {
|
|
||||||
panic("no raw type provided for ref")
|
|
||||||
} */
|
|
||||||
sname := sft.Name + "@" + refTag.Name
|
sname := sft.Name + "@" + refTag.Name
|
||||||
refs[sname] = makeRef(i, refTag.Name, sft.Name, sft.Type)
|
refs[sname] = makeRef(i, refTag.Name, sft.Name, sft.Type)
|
||||||
}
|
}
|
||||||
@ -239,7 +219,7 @@ func (r TModelRegistry) HasByName(n string) (string, *Model, bool) {
|
|||||||
// Index returns the index at which the Document struct is embedded
|
// Index returns the index at which the Document struct is embedded
|
||||||
func (r TModelRegistry) Index(n string) int {
|
func (r TModelRegistry) Index(n string) int {
|
||||||
if v, ok := ModelRegistry[n]; ok {
|
if v, ok := ModelRegistry[n]; ok {
|
||||||
return v.Idx
|
return v.idx
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
@ -247,7 +227,7 @@ func (r TModelRegistry) Index(n string) int {
|
|||||||
func (r TModelRegistry) new_(n string) interface{} {
|
func (r TModelRegistry) new_(n string) interface{} {
|
||||||
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
||||||
v := reflect.New(m.Type)
|
v := reflect.New(m.Type)
|
||||||
df := v.Elem().Field(m.Idx)
|
df := v.Elem().Field(m.idx)
|
||||||
do := reflect.New(df.Type())
|
do := reflect.New(df.Type())
|
||||||
d := do.Interface().(IDocument)
|
d := do.Interface().(IDocument)
|
||||||
//d := df.Interface().(IDocument)
|
//d := df.Interface().(IDocument)
|
||||||
@ -259,6 +239,14 @@ func (r TModelRegistry) new_(n string) interface{} {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r TModelRegistry) Get(name string) *Model {
|
||||||
|
model, ok := r[name]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
// Model registers models in the ModelRegistry, where
|
// Model registers models in the ModelRegistry, where
|
||||||
// they can be accessed via a model's struct name
|
// they can be accessed via a model's struct name
|
||||||
func (r TModelRegistry) Model(mdl ...any) {
|
func (r TModelRegistry) Model(mdl ...any) {
|
||||||
@ -278,7 +266,7 @@ func (r TModelRegistry) Model(mdl ...any) {
|
|||||||
panic(fmt.Sprintf("you MUST implement the HasID interface!!! skipping...\n"))
|
panic(fmt.Sprintf("you MUST implement the HasID interface!!! skipping...\n"))
|
||||||
}
|
}
|
||||||
switch (id).Id().(type) {
|
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
|
break
|
||||||
default:
|
default:
|
||||||
log.Printf("invalid ID type specified!!! skipping...\n")
|
log.Printf("invalid ID type specified!!! skipping...\n")
|
||||||
@ -303,31 +291,31 @@ func (r TModelRegistry) Model(mdl ...any) {
|
|||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
panic("A model must embed the Document struct!")
|
panic("A model must embed the Document struct!")
|
||||||
}
|
}
|
||||||
inds, refs, gfs, coll := parseTags(t, v)
|
inds, refs, gfs, coll := parseTags(t, v, "", 0)
|
||||||
if coll == "" {
|
if coll == "" {
|
||||||
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
||||||
}
|
}
|
||||||
ModelRegistry[n] = &Model{
|
ModelRegistry[n] = &Model{
|
||||||
Idx: idx,
|
idx: idx,
|
||||||
Type: t,
|
Type: t,
|
||||||
Collection: coll,
|
collection: coll,
|
||||||
Indexes: inds,
|
Indexes: inds,
|
||||||
References: refs,
|
references: refs,
|
||||||
GridFSReferences: gfs,
|
gridFSReferences: gfs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, v := range ModelRegistry {
|
for k, v := range ModelRegistry {
|
||||||
for k2, v2 := range v.References {
|
for k2, v2 := range v.references {
|
||||||
if !v2.Exists {
|
if !v2.exists {
|
||||||
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
||||||
tmp := ModelRegistry[k].References[k2]
|
tmp := ModelRegistry[k].references[k2]
|
||||||
ModelRegistry[k].References[k2] = Reference{
|
ModelRegistry[k].references[k2] = Reference{
|
||||||
Model: k,
|
Model: k,
|
||||||
Idx: tmp.Idx,
|
Idx: tmp.Idx,
|
||||||
FieldName: tmp.FieldName,
|
FieldName: tmp.FieldName,
|
||||||
Kind: tmp.Kind,
|
Kind: tmp.Kind,
|
||||||
HydratedType: tmp.HydratedType,
|
HydratedType: tmp.HydratedType,
|
||||||
Exists: true,
|
exists: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -360,7 +348,23 @@ func innerWatch(coll *mongo.Collection) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var uid = data["documentKey"].(bson.M)["_id"]
|
var uid any
|
||||||
|
|
||||||
|
docKey := data["documentKey"]
|
||||||
|
|
||||||
|
switch docKey.(type) {
|
||||||
|
case bson.M:
|
||||||
|
uid = docKey.(bson.M)["_id"]
|
||||||
|
case bson.D:
|
||||||
|
for _, vv := range docKey.(bson.D) {
|
||||||
|
if vv.Key == "_id" {
|
||||||
|
uid = vv.Value
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//var uid = data["documentKey"].(bson.M)["_id"]
|
||||||
|
|
||||||
if data["operationType"] == "insert" {
|
if data["operationType"] == "insert" {
|
||||||
counterColl := DB.Collection(COUNTER_COL)
|
counterColl := DB.Collection(COUNTER_COL)
|
||||||
@ -374,7 +378,7 @@ func innerWatch(coll *mongo.Collection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Connect(uri string, dbName string) {
|
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 {
|
if err != nil {
|
||||||
log.Fatal("failed to open database")
|
log.Fatal("failed to open database")
|
||||||
}
|
}
|
||||||
|
19
testing.go
19
testing.go
@ -9,14 +9,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-loremipsum/loremipsum"
|
"github.com/go-loremipsum/loremipsum"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type chapter struct {
|
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"`
|
Title string `bson:"chapterTitle" json:"chapterTitle" form:"chapterTitle"`
|
||||||
ChapterID int `bson:"id" json:"chapterID" autoinc:"chapters"`
|
ChapterID int `bson:"id" json:"chapterID" autoinc:"chapters"`
|
||||||
Index int `bson:"index" json:"index" form:"index"`
|
Index int `bson:"index" json:"index" form:"index"`
|
||||||
@ -134,9 +133,9 @@ func genChaps(single bool) []chapter {
|
|||||||
for i := 0; i < ceil; i++ {
|
for i := 0; i < ceil; i++ {
|
||||||
spf := fmt.Sprintf("%d.md", i+1)
|
spf := fmt.Sprintf("%d.md", i+1)
|
||||||
ret = append(ret, chapter{
|
ret = append(ret, chapter{
|
||||||
ID: primitive.NewObjectID(),
|
ID: bson.NewObjectID(),
|
||||||
Title: fmt.Sprintf("-%d-", i+1),
|
Title: fmt.Sprintf("-%d-", i+1),
|
||||||
Index: int(i + 1),
|
Index: i + 1,
|
||||||
Words: 50,
|
Words: 50,
|
||||||
Notes: "notenotenote !!!",
|
Notes: "notenotenote !!!",
|
||||||
Genre: []string{"Slash"},
|
Genre: []string{"Slash"},
|
||||||
@ -188,7 +187,7 @@ func iti_blank() story {
|
|||||||
func initTest() {
|
func initTest() {
|
||||||
uri := "mongodb://127.0.0.1:27017"
|
uri := "mongodb://127.0.0.1:27017"
|
||||||
db := "rockfic_ormTest"
|
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())
|
ic.Database(db).Drop(context.TODO())
|
||||||
colls, _ := ic.Database(db).ListCollectionNames(context.TODO(), bson.M{})
|
colls, _ := ic.Database(db).ListCollectionNames(context.TODO(), bson.M{})
|
||||||
if len(colls) < 1 {
|
if len(colls) < 1 {
|
||||||
@ -202,10 +201,6 @@ func initTest() {
|
|||||||
author.ID = 696969
|
author.ID = 696969
|
||||||
ModelRegistry.Model(band{}, user{}, story{})
|
ModelRegistry.Model(band{}, user{}, story{})
|
||||||
}
|
}
|
||||||
func after() {
|
|
||||||
err := DBClient.Disconnect(context.TODO())
|
|
||||||
panik(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var metallica = band{
|
var metallica = band{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
|
29
util.go
29
util.go
@ -2,7 +2,7 @@ package orm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -64,7 +64,7 @@ func getNested(field string, aValue reflect.Value) (*reflect.Type, *reflect.Valu
|
|||||||
}
|
}
|
||||||
aft := value.Type()
|
aft := value.Type()
|
||||||
dots := strings.Split(field, ".")
|
dots := strings.Split(field, ".")
|
||||||
if value.Kind() != reflect.Struct /*&& arrRegex.FindString(dots[0]) == ""*/ {
|
if value.Kind() != reflect.Struct {
|
||||||
if value.Kind() == reflect.Slice {
|
if value.Kind() == reflect.Slice {
|
||||||
st := reflect.MakeSlice(value.Type().Elem(), 0, 0)
|
st := reflect.MakeSlice(value.Type().Elem(), 0, 0)
|
||||||
for i := 0; i < value.Len(); i++ {
|
for i := 0; i < value.Len(); i++ {
|
||||||
@ -85,8 +85,6 @@ func getNested(field string, aValue reflect.Value) (*reflect.Type, *reflect.Valu
|
|||||||
} else {
|
} else {
|
||||||
return &aft, &value, nil
|
return &aft, &value, nil
|
||||||
}
|
}
|
||||||
/*ft := value.Type()
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
ref := value
|
ref := value
|
||||||
if ref.Kind() == reflect.Pointer {
|
if ref.Kind() == reflect.Pointer {
|
||||||
@ -96,7 +94,7 @@ func getNested(field string, aValue reflect.Value) (*reflect.Type, *reflect.Valu
|
|||||||
if arrRegex.FindString(dots[0]) != "" && fv.Kind() == reflect.Slice {
|
if arrRegex.FindString(dots[0]) != "" && fv.Kind() == reflect.Slice {
|
||||||
matches := arrRegex.FindStringSubmatch(dots[0])
|
matches := arrRegex.FindStringSubmatch(dots[0])
|
||||||
ridx, _ := strconv.Atoi(matches[0])
|
ridx, _ := strconv.Atoi(matches[0])
|
||||||
idx := int(ridx)
|
idx := ridx
|
||||||
fv = fv.Index(idx)
|
fv = fv.Index(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,8 +130,8 @@ func incrementInterface(t interface{}) interface{} {
|
|||||||
t = pt + 1
|
t = pt + 1
|
||||||
case string:
|
case string:
|
||||||
t = NextStringID()
|
t = NextStringID()
|
||||||
case primitive.ObjectID:
|
case bson.ObjectID:
|
||||||
t = primitive.NewObjectID()
|
t = bson.NewObjectID()
|
||||||
default:
|
default:
|
||||||
panic(ErrUnsupportedID)
|
panic(ErrUnsupportedID)
|
||||||
}
|
}
|
||||||
@ -194,20 +192,3 @@ func normalizeSliceToDocumentSlice(in any) *DocumentSlice {
|
|||||||
}
|
}
|
||||||
return &ret
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func normalizeDocSlice(iput reflect.Value) reflect.Value {
|
|
||||||
idslice, idsliceOk := iput.Interface().(IDocumentSlice)
|
|
||||||
dslice, dsliceOk := resV.Interface().(DocumentSlice)
|
|
||||||
switch {
|
|
||||||
case idsliceOk:
|
|
||||||
|
|
||||||
case dsliceOk:
|
|
||||||
default:
|
|
||||||
return iput
|
|
||||||
}
|
|
||||||
//if {
|
|
||||||
// resV.Set(trvo.Elem())
|
|
||||||
//} else {
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
}*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user