https://github.com/rs/rest-layer-mongo
REST Layer MongoDB resource storage handler
https://github.com/rs/rest-layer-mongo
Last synced: about 1 year ago
JSON representation
REST Layer MongoDB resource storage handler
- Host: GitHub
- URL: https://github.com/rs/rest-layer-mongo
- Owner: rs
- License: mit
- Created: 2015-08-11T01:04:37.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-10-04T21:23:47.000Z (over 4 years ago)
- Last Synced: 2025-03-28T21:11:29.225Z (about 1 year ago)
- Language: Go
- Size: 51.8 KB
- Stars: 18
- Watchers: 7
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# REST Layer MongoDB Backend
[](https://godoc.org/github.com/rs/rest-layer-mongo) [](https://raw.githubusercontent.com/rs/rest-layer-mongo/master/LICENSE) [](https://travis-ci.org/rs/rest-layer-mongo)
This [REST Layer](https://github.com/rs/rest-layer) resource storage backend stores data in a MongoDB cluster using [mgo](https://godoc.org/labix.org/v2/mgo).
## Usage
```go
import "github.com/rs/rest-layer-mongo"
```
Create a mgo master session:
```go
session, err := mgo.Dial(url)
```
Create a resource storage handler with a given DB/collection:
```go
s := mongo.NewHandler(session, "the_db", "the_collection")
```
Use this handler with a resource:
```go
index.Bind("foo", foo, s, resource.DefaultConf)
```
You may want to create a many mongo handlers as you have resources as long as you want each resources in a different collection. You can share the same `mgo` session across all you handlers.
### Object ID
This package also provides a REST Layer [schema.Validator](https://godoc.org/github.com/rs/rest-layer/schema#Validator) for MongoDB ObjectIDs. This validator ensures proper binary serialization of the Object ID in the database for space efficiency.
You may reference this validator using [mongo.ObjectID](https://godoc.org/github.com/rs/rest-layer-mongo#ObjectID) as [schema.Field](https://godoc.org/github.com/rs/rest-layer/schema#Field).
A `mongo.NewObjectID` field hook and `mongo.ObjectIDField` helper are also provided.