Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucacasonato/wrap
wrap is an abstraction layer on top of mongodb 🍃 to make it feel like sorta like firestore 🔥
https://github.com/lucacasonato/wrap
go mongodb
Last synced: 3 months ago
JSON representation
wrap is an abstraction layer on top of mongodb 🍃 to make it feel like sorta like firestore 🔥
- Host: GitHub
- URL: https://github.com/lucacasonato/wrap
- Owner: lucacasonato
- License: mit
- Created: 2019-03-07T17:28:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T16:03:59.000Z (about 5 years ago)
- Last Synced: 2024-10-06T10:50:25.032Z (3 months ago)
- Topics: go, mongodb
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 19
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wrap
[![](https://godoc.org/github.com/nathany/looper?status.svg)](https://godoc.org/github.com/lucacasonato/wrap)
Wrap enables the full MongoDB feature set without ever having to touch BSON or other weird libraries. Everything is structured in a way that BSON and MongoDB stay completly hidden.
## install
```bash
go get github.com/lucacasonato/wrap
``````go
import "github.com/lucacasonato/wrap"
```## usage
#### connect
```go
client, err := wrap.Connect("mongodb://localhost:27017", 5*time.Second)
if err != nil {
panic(err)
}
```#### open a database
```go
db := client.Database("production")
```> note: you are only getting a refrence to the database here. you are not actually creating it yet
#### get a collection
```go
users := db.Collection("users")
```> note: you are only getting a refrence to the collection here. you are not actually creating it yet
#### add data
```go
doc, err := users.Add(&User{
Name: "Luca Casonato",
Email: "[email protected]",
FavoriteNumbers: []int{5, 10, 15},
LastEdited: time.Now(),
})
if err != nil {
panic(err)
}
```#### get data
```go
data, err := doc.Get()
if err != nil {
panic(err)
}user := User{}
data.DataTo(&user)
fmt.Println(user.Name)
```#### update data
```go
err = doc.Update(update.Set("email", "[email protected]"), true)
if err != nil {
panic(err)
}
```#### create index
```go
err = users.CreateIndex(map[string]wrap.Index{
"name": wrap.TextIndex,
"email": wrap.AscendingIndex,
})
if err != nil {
panic(err)
}
```#### get filtered data
```go
iterator, err := users.
Where(filter.AND(
filter.TextSearch("luca"),
filter.Equal("email", "[email protected]"),
)).
DocumentIterator()
if err != nil {
panic(err)
}
defer iterator.Close()for iterator.Next() {
user := User{}
err := iterator.DataTo(&user)
if err != nil {
panic(err)
}fmt.Println(user)
}
```#### get structurally modified data (aggregation)
```go
iterator, err = users.All().
Modify(map[string]interface{}{
"email": expressions.Exclude,
}).
AddFields(map[string]interface{}{
"averagefavoritenumber": expressions.MathAvg(expressions.Value("favoritenumbers")),
}).
DocumentIterator()if err != nil {
panic(err)
}
defer iterator.Close()for iterator.Next() {
user := map[string]interface{}{}
err := iterator.DataTo(&user)
if err != nil {
panic(err)
}fmt.Println(user)
}
```#### transactions
```go
err := users.Transaction(func(users *wrap.Collection) error {
now := time.Now()err := users.Document(luca.ID).Update(true, update.Set("lastedited", now))
if err != nil {
return err
}err = users.Document(jaap.ID).Update(true, update.Set("lastedited", now))
if err != nil {
return err
}return nil
})
if err != nil {
err = nil
}
```#### example
A full example can be found in the "example" folder.
## planning
- implement schema filters (im lazy)
- automatic index creation
- more tests## contributing
to build start a mongo server on localhost:27017
run tests with `go test`
1. open an issue about your idea / suggestion / bug
2. wait for response
3. have someone fix it or pr yourself
4. thanks## licence
Copyright (c) 2019 Luca Casonato
This project is licenced under the MIT licence. More details in the LICENCE file.