Documentation ¶
Overview ¶
Package mongo is a REST Layer resource storage handler for MongoDB using mgo
Example ¶
package main import ( "context" "log" "net/http" "os" "time" "github.com/clarify/rested/resource" "github.com/clarify/rested/rest" "github.com/clarify/rested/schema" mgo "github.com/drdeee/rested-mongo" "github.com/rs/cors" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) var ( user = schema.Schema{ Fields: schema.Fields{ "id": schema.IDField, "created": schema.CreatedField, "updated": schema.UpdatedField, "name": { Required: true, Filterable: true, Sortable: true, Validator: &schema.String{ MaxLen: 150, }, }, }, } // Define a post resource schema post = schema.Schema{ Fields: schema.Fields{ "id": schema.IDField, "created": schema.CreatedField, "updated": schema.UpdatedField, "user": { Required: true, Filterable: true, Validator: &schema.Reference{ Path: "users", }, }, "public": { Filterable: true, Validator: &schema.Bool{}, }, "meta": { Schema: &schema.Schema{ Fields: schema.Fields{ "title": { Required: true, Validator: &schema.String{ MaxLen: 150, }, }, "body": { Validator: &schema.String{ MaxLen: 100000, }, }, }, }, }, }, } ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("GOTEST_MONGODB")+"/exampledb")) db := "test_rest_layer" index := resource.NewIndex() users := index.Bind("users", user, mgo.NewHandler(client, db, "users"), resource.Conf{ AllowedModes: resource.ReadWrite, }) users.Bind("posts", "user", post, mgo.NewHandler(client, db, "posts"), resource.Conf{ AllowedModes: resource.ReadWrite, }) api, err := rest.NewHandler(index) if err != nil { log.Fatalf("Invalid API configuration: %s", err) } http.Handle("/", cors.New(cors.Options{OptionsPassthrough: true}).Handler(api)) log.Print("Serving API on http://localhost:8080") if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } }
Output:
Index ¶
- Variables
- type Handler
- func (m Handler) Clear(ctx context.Context, q *query.Query) (int, error)
- func (m Handler) Count(ctx context.Context, query *query.Query) (int, error)
- func (m Handler) Delete(ctx context.Context, item *resource.Item) error
- func (m Handler) Find(ctx context.Context, q *query.Query) (*resource.ItemList, error)
- func (m Handler) Insert(ctx context.Context, items []*resource.Item) error
- func (m Handler) Update(ctx context.Context, item *resource.Item, original *resource.Item) error
- type ObjectID
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // NewObjectID is a field hook handler that generates a new Mongo ObjectID hex if // value is nil to be used in schema with OnInit. NewObjectID = func(ctx context.Context, value interface{}) interface{} { if value == nil { value = primitive.NewObjectID().Hex() } return value } // ObjectIDField is a common schema field configuration that generate an Object ID // for new item id. ObjectIDField = schema.Field{ Required: true, ReadOnly: true, OnInit: NewObjectID, Filterable: true, Sortable: true, Validator: &ObjectID{}, } )
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler func(ctx context.Context) (*mongo.Collection, error)
Handler handles resource storage in a MongoDB collection.
func NewHandler ¶
NewHandler creates an new mongo handler
func (Handler) Clear ¶
Clear clears all items from the mongo collection matching the query. Note that when q.Window != nil, the current implementation may error if the BSON encoding of all matching IDs according to the q.Window length gets close to the maximum document size in MongDB (usually 16MiB): https://docs.mongodb.com/manual/reference/limits/#bson-documents
Click to show internal directories.
Click to hide internal directories.