rest-layer-datastore: github.com/ajcrowe/rest-layer-datastore Index | Examples | Files

package datastore

import "github.com/ajcrowe/rest-layer-datastore"

Code:

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)
    }
}

Index

Examples

Package Files

datastore.go lookup.go

func NewClient Uses

func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*datastore.Client, error)

Wrap datastore.NewClient to avoid user having to import this

type Entity Uses

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 Uses

func (e *Entity) Load(ps []datastore.Property) error

Load implements the PropertyLoadSaver interface to process our dynamic payload data see https://godoc.org/cloud.google.com/go/datastore#hdr-The_PropertyLoadSaver_Interface

func (*Entity) Save Uses

func (e *Entity) Save() ([]datastore.Property, error)

Save 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 Uses

type Handler struct {
    // contains filtered or unexported fields
}

Handler handles resource storage in Google Datastore.

func NewHandler Uses

func NewHandler(client *datastore.Client, namespace, entity string) *Handler

NewHandler creates a new Google Datastore handler

func (*Handler) Clear Uses

func (d *Handler) Clear(ctx context.Context, lookup *resource.Lookup) (int, error)

Clear clears all entities matching the lookup from the Datastore

func (*Handler) Delete Uses

func (d *Handler) Delete(ctx context.Context, item *resource.Item) error

Delete deletes an item from the datastore

func (*Handler) Find Uses

func (d *Handler) Find(ctx context.Context, lookup *resource.Lookup, offset, limit int) (*resource.ItemList, error)

Find entities matching the provided lookup from the Datastore

func (*Handler) Insert Uses

func (d *Handler) Insert(ctx context.Context, items []*resource.Item) error

Insert inserts new entities

func (*Handler) SetNoIndexProperties Uses

func (d *Handler) SetNoIndexProperties(props []string) *Handler

SetNoIndexProps sets the handlers properties which should have noindex set.

func (*Handler) Update Uses

func (d *Handler) Update(ctx context.Context, item *resource.Item, original *resource.Item) error

Update replace an entity by a new one in the Datastore

Package datastore imports 9 packages (graph). Updated 2017-07-14. Refresh now. Tools for package owners.