Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhutter/mr
Go library for interaction with MongoDB
https://github.com/mhutter/mr
go golang mgo mongodb odm
Last synced: 25 days ago
JSON representation
Go library for interaction with MongoDB
- Host: GitHub
- URL: https://github.com/mhutter/mr
- Owner: mhutter
- License: mit
- Created: 2018-06-24T00:10:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-11T14:33:38.000Z (over 6 years ago)
- Last Synced: 2024-11-17T04:07:55.365Z (about 1 month ago)
- Topics: go, golang, mgo, mongodb, odm
- Language: Go
- Homepage: https://gowalker.org/github.com/mhutter/mr
- Size: 30.3 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MongoRepo
[![GoDoc](https://godoc.org/github.com/mhutter/mr?status.svg)](https://gowalker.org/github.com/mhutter/mr)
[![Build Status](https://travis-ci.com/mhutter/mr.svg?branch=master)](https://travis-ci.com/mhutter/mr)Library to ease interaction with MongoDB.
This package is an opinionated abstraction for [mgo][].
To get started, connect to MongoDB (uses `$MONGODB_URI` from environment if set, or `localhost`):
```go
repo := mr.MustAutoconnect("db-name")
```Include the "Base" type in your data types:
```go
type User struct {
mr.Base `json:",inline" bson:",inline"`Name string `bson:"name" json:"name"`
Email string `bson:"email" json:"email"`
PasswordHash []byte `bson:"password_hash" json:"-"`
}
```Doing so will add the following fields to your type:
* ID: ObjectId of the document in the DB
* CreatedAt: Timestamp of first insertion into DB
* UpdatedAt: Timestamp of the last update operationsThe "MongoCollection" type implements the basic CRUD operations defined in the "Colleciton" interface. IDs ,must be passed as strings, and objects are usually types that include the "Base" type (see above).
```go
user := User{
Name: "John",
}
err := repo.Insert(&user)var users User
err := repo.FindAll(&users)
```> [Manuel Hutter](https://hutter.io) -
> GitHub [@mhutter](https://github.com/mhutter) -
> Twitter [@dratir](https://twitter.com/dratir)[mgo]: https://github.com/globalsign/mgo