-
Notifications
You must be signed in to change notification settings - Fork 1
/
orm_simple.go
174 lines (141 loc) · 3.29 KB
/
orm_simple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package mongo
package mongo
import (
"context"
"fmt"
)
type tb1 struct {
ID ObjectID `bson:"_id" json:"id"`
Txt string `bson:"txt" json:"txt"`
Ref *Foreign[tb2] `bson:"ref" json:"ref" ref:"def"`
Ref2 ForeignList[tb3] `bson:"ref2" json:"ref2" ref:"match"`
}
type tb2 struct {
ID string `bson:"_id" json:"id"`
Name string `bson:"name" json:"name"`
Ref *Foreign[tb3] `bson:"ref" json:"ref" ref:"def"`
}
type tb3 struct {
ID string `bson:"_id" json:"id"`
Txt string `bson:"txt" json:"txt"`
}
func simpleOrm() {
opts := OptionsFromURI("mongodb://localhost:27017")
client, err := NewClient(context.Background(), opts)
if err != nil {
fmt.Println("get mongotool client error")
panic(err)
}
ref := NewReference()
ref.AddTableDef("test1", tb1{})
ref.AddTableDef("test2", tb2{})
ref.AddTableDef("test3", tb3{})
ref.BuildRefs()
dbObj := client.Database("test_db")
q := NewORMByDB(context.Background(), dbObj, "test1", ref)
exist, err := q.Exist()
if err != nil {
panic(err)
}
fmt.Println(exist, err.Error())
q.Query("txt", "1", "ref", RefWhere{
"name": "test",
"ref": RefWhere{
"txt": "1",
},
}, "$or", Where{
"txt": "1",
"ref2": RefWhere{
"txt": "1",
},
})
//q.Wheres(map[string]interface{}{
// "txt": "123",
//})
var ret tb1
// var ret tb1
err = q.ToData(&ret)
if err != nil {
panic(err)
}
fmt.Println("ret: ", ret)
if ret.ID.IsZero() {
return
}
var dt map[string]interface{}
err = ret.Ref.ToData(client.ctx, dbObj, &dt)
if err != nil {
panic(err)
}
fmt.Println(dt)
dt2, err := ret.Ref.GetData(context.Background(), q.Database())
if err != nil {
panic(err)
}
fmt.Println(dt2)
dt3, err := ret.Ref2.GetData(context.Background(), q.Database())
if err != nil {
panic(err)
}
fmt.Println("dt3: ", dt3)
}
func simpleRef() {
ref := NewReference()
ref.AddTableDef("test1", tb1{})
ref.AddTableDef("test2", tb2{})
ref.AddTableDef("test3", tb3{})
ref.BuildRefs()
ret := tb1{}
ret.ID = NewObjectID()
data, err := ToRefData(ref, &ret)
fmt.Println(data, err)
p := []*tb1{&ret}
r, err := ToRefListData[tb1](ref, p)
fmt.Println(r, err)
}
func simpleMongoOrmQueryInsertOne() {
opts := OptionsFromURI("mongodb://localhost:27017")
client, err := NewClient(context.Background(), opts)
if err != nil {
fmt.Println("get mongotool client error")
panic(err)
}
ref := NewReference()
ref.AddTableDef("test1", tb1{})
ref.AddTableDef("test2", tb2{})
ref.AddTableDef("test3", tb3{})
ref.BuildRefs()
dbObj := client.Database("test_db")
q := &ORM{
db: dbObj,
tableName: "test1",
}
id := ObjectID2String(NewObjectID())
fmt.Println(id)
one, err := q.InsertOne(map[string]interface{}{
"_id": id,
"test": 123,
})
if err != nil {
_ = fmt.Errorf("insert err:%w", err)
}
fmt.Println(one)
}
func simpleProjection() {
opts := OptionsFromURI("mongodb://localhost:27017")
client, err := NewClient(context.Background(), opts)
if err != nil {
fmt.Println("get mongotool client error")
panic(err)
}
dbObj := client.Database("test_db")
q := NewORMByDB(context.Background(), dbObj, "test_arr", NewReference())
q.Query("arr_list.k1", "k").Select("arr_list.k2").Projection("arr_list.$", true)
// var s string
var s []tb1
err = q.ToData(&s)
if err != nil {
panic(err)
}
fmt.Println("ret: ", s)
}