Documentation
¶
Overview ¶
Example ¶
package main
import (
"log"
"net/http"
gds "cloud.google.com/go/datastore"
"github.com/ajcrowe/rest-layer-datastore"
"github.com/rs/cors"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/rest"
"github.com/rs/rest-layer/schema"
"google.golang.org/appengine/aetest"
)
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, done, err := aetest.NewContext()
if err != nil {
log.Fatal(err)
}
defer done()
client, _ := gds.NewClient(ctx, "test-project")
index := resource.NewIndex()
users := index.Bind("users", user, datastore.NewHandler(client, "default", "users"), resource.Conf{
AllowedModes: resource.ReadWrite,
})
users.Bind("posts", "user", post, datastore.NewHandler(client, "default", "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 ¶
- func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*datastore.Client, error)
- type Entity
- type Handler
- func (d *Handler) Clear(ctx context.Context, q *query.Query) (int, error)
- func (d *Handler) Delete(ctx context.Context, item *resource.Item) error
- func (d *Handler) Find(ctx context.Context, q *query.Query) (*resource.ItemList, error)
- func (d *Handler) Insert(ctx context.Context, items []*resource.Item) error
- func (d *Handler) SetNoIndexProperties(props []string) *Handler
- func (d *Handler) Update(ctx context.Context, item *resource.Item, original *resource.Item) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Entity ¶
type Entity struct {
ID string
ETag string
Updated time.Time
Payload map[string]interface{}
NoIndexProps map[string]bool
}
Entity Is a representation of a Google Datastore entity
func (*Entity) Load ¶
Load implements the PropertyLoadSaver interface to process our dynamic payload data see https://godoc.org/cloud.google.com/go/datastore#hdr-The_PropertyLoadSaver_Interface
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles resource storage in Google Datastore.
func NewHandler ¶
NewHandler creates a new Google Datastore handler
func (*Handler) SetNoIndexProperties ¶
SetNoIndexProperties sets the handlers properties which should have noindex set.
Click to show internal directories.
Click to hide internal directories.