-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongorm.go
55 lines (44 loc) · 1.56 KB
/
mongorm.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
package mongorm
import (
"github.com/mmycin/mongorm/client"
"github.com/mmycin/mongorm/operations"
"go.mongodb.org/mongo-driver/mongo"
)
// Initialize sets up the MongoDB client and database
func Initialize(uri, dbName string) (*mongo.Client, error) {
config := client.MongoConfig{
URI: uri,
DBName: dbName,
}
return client.Initialize(config)
}
// CreateOne inserts a single document.
func CreateOne(collectionName string, doc interface{}) error {
return operations.CreateOne(client.Database, collectionName, doc)
}
// ReadOne retrieves a single document.
func ReadOne(collectionName string, filter interface{}, result interface{}) error {
return operations.ReadOne(client.Database, collectionName, filter, result)
}
// ReadAll retrieves multiple documents.
func ReadAll(collectionName string, results interface{}) error {
return operations.ReadAll(client.Database, collectionName, results)
}
// Update modifies an existing document.
func Update(collectionName string, filter interface{}, update interface{}) error {
return operations.Update(client.Database, collectionName, filter, update)
}
// DeleteOne removes a single document.
func DeleteOne( collectionName string, filter interface{}) error {
return operations.DeleteOne(client.Database, collectionName, filter)
}
// DeleteAll removes multiple documents.
func DeleteAll(collectionName string, filter interface{}) error {
return operations.DeleteAll(client.Database, collectionName, filter)
}
// HandleError panics if an error is encountered.
func HandleError(err error) {
if err != nil {
panic(err)
}
}