aedatastore

package
Version: v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 11 Imported by: 7

Documentation

Overview

Package aedatastore provides AppEngine Datastore implementation of datastore.Client. This package wrapping google.golang.org/appengine/datastore package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context, opts ...w.ClientOption) (w.Client, error)

FromContext make new Client by specified context.

Example
package main

import (
	"fmt"

	"go.mercari.io/datastore/aedatastore"
	"google.golang.org/appengine"
	"google.golang.org/appengine/aetest"
)

func main() {
	inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true, SuppressDevAppServerLog: true})
	if err != nil {
		panic(err)
	}
	defer inst.Close()
	r, err := inst.NewRequest("GET", "/", nil)
	if err != nil {
		panic(err)
	}
	ctx := appengine.NewContext(r)

	client, err := aedatastore.FromContext(
		ctx,
	)
	if err != nil {
		panic(err)
	}
	defer client.Close()

	type Data struct {
		Name string
	}

	key := client.IncompleteKey("Data", nil)
	entity := &Data{Name: "mercari"}
	key, err = client.Put(ctx, key, entity)
	if err != nil {
		panic(err)
	}

	entity = &Data{}
	err = client.Get(ctx, key, entity)
	if err != nil {
		panic(err)
	}

	fmt.Println(entity.Name)
}
Output:

mercari

func IsAEDatastoreClient

func IsAEDatastoreClient(client w.Client) bool

IsAEDatastoreClient returns check result that client is this package's client or not.

func TransactionContext

func TransactionContext(tx w.Transaction) context.Context

TransactionContext returns context that is under the AppEngine Datastore's transaction.

Example
package main

import (
	"context"
	"net/url"

	"go.mercari.io/datastore/aedatastore"
	"google.golang.org/appengine"
	"google.golang.org/appengine/aetest"
	"google.golang.org/appengine/taskqueue"
)

func appengineContext() (ctx context.Context, cancelFn func() error) {
	inst, err := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true, SuppressDevAppServerLog: true})
	if err != nil {
		panic(err)
	}
	cancelFn = inst.Close
	r, err := inst.NewRequest("GET", "/", nil)
	if err != nil {
		panic(err)
	}
	ctx = appengine.NewContext(r)

	return
}

func main() {
	ctx, cancelFn := appengineContext()
	go cancelFn()

	client, err := aedatastore.FromContext(ctx)
	if err != nil {
		panic(err)
	}
	defer client.Close()

	tx, err := client.NewTransaction(ctx)
	if err != nil {
		panic(err)
	}
	go tx.Commit()

	txCtx := aedatastore.TransactionContext(tx)

	// join task to Transaction!
	task := taskqueue.NewPOSTTask("/foobar", url.Values{})
	_, err = taskqueue.Add(txCtx, task, "")
	if err != nil {
		panic(err)
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL