func ExampleCommit_Key() { ctx := context.Background() client, err := datastore.NewClient(ctx, "") if err != nil { // TODO: Handle error. } var pk1, pk2 *datastore.PendingKey // Create two posts in a single transaction. commit, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error { var err error pk1, err = tx.Put(datastore.IncompleteKey("Post", nil), &Post{Title: "Post 1", PublishedAt: time.Now()}) if err != nil { return err } pk2, err = tx.Put(datastore.IncompleteKey("Post", nil), &Post{Title: "Post 2", PublishedAt: time.Now()}) if err != nil { return err } return nil }) if err != nil { // TODO: Handle error. } // Now pk1, pk2 are valid PendingKeys. Let's convert them into real keys // using the Commit object. k1 := commit.Key(pk1) k2 := commit.Key(pk2) fmt.Println(k1, k2) }
func ExampleClient_PutMulti() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START batch_upsert] tasks := []*Task{ { Category: "Personal", Done: false, Priority: 4, Description: "Learn Cloud Datastore", }, { Category: "Personal", Done: false, Priority: 5, Description: "Integrate Cloud Datastore", }, } keys := []*datastore.Key{ datastore.IncompleteKey("Task", nil), datastore.IncompleteKey("Task", nil), } keys, err := client.PutMulti(ctx, keys, tasks) // [END batch_upsert] _ = err // Make sure you check err. _ = keys // keys now has the complete keys for the newly stored tasks. }
func ExampleClient_Put_flatten() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { log.Fatal(err) } type Animal struct { Name string Type string Breed string } type Human struct { Name string Height int Pet Animal `datastore:",flatten"` } newKey := datastore.IncompleteKey("Human", nil) _, err = client.Put(ctx, newKey, &Human{ Name: "Susan", Height: 67, Pet: Animal{ Name: "Fluffy", Type: "Cat", Breed: "Sphynx", }, }) if err != nil { log.Fatal(err) } }
func ExampleClient_Put() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } type Article struct { Title string Description string Body string `datastore:",noindex"` Author *datastore.Key PublishedAt time.Time } newKey := datastore.IncompleteKey("Article", nil) _, err = client.Put(ctx, newKey, &Article{ Title: "The title of the article", Description: "The description of the article...", Body: "...", Author: datastore.NameKey("Author", "jbd", nil), PublishedAt: time.Now(), }) if err != nil { // TODO: Handle error. } }
// AddTask adds a task with the given description to the datastore, // returning the key of the newly created entity. func AddTask(ctx context.Context, client *datastore.Client, desc string) (*datastore.Key, error) { task := &Task{ Desc: desc, Created: time.Now(), } key := datastore.IncompleteKey("Task", nil) return client.Put(ctx, key, task) }
// AddBook saves a given book, assigning it a new ID. func (db *datastoreDB) AddBook(b *Book) (id int64, err error) { ctx := context.Background() k := datastore.IncompleteKey("Book", nil) k, err = db.client.Put(ctx, k, b) if err != nil { return 0, fmt.Errorf("datastoredb: could not put Book: %v", err) } return k.ID, nil }
func recordVisit(ctx context.Context, now time.Time, userIP string) error { v := &visit{ Timestamp: now, UserIP: userIP, } k := datastore.IncompleteKey("Visit", nil) _, err := datastoreClient.Put(ctx, k, v) return err }
func ExampleClient_Put_upsert() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") task := &Task{} // Populated with appropriate data. key := datastore.IncompleteKey("Task", nil) // [START upsert] key, err := client.Put(ctx, key, task) // [END upsert] _ = err // Make sure you check err. _ = key // key is the complete key for the newly stored task }
func ExampleClient_AllocateIDs() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { // TODO: Handle error. } var keys []*datastore.Key for i := 0; i < 10; i++ { keys = append(keys, datastore.IncompleteKey("Article", nil)) } keys, err = client.AllocateIDs(ctx, keys) if err != nil { // TODO: Handle error. } _ = keys // TODO: Use keys. }
func ExampleClient_Put() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START entity_with_parent] parentKey := datastore.NameKey("TaskList", "default", nil) key := datastore.IncompleteKey("Task", parentKey) task := Task{ Category: "Personal", Done: false, Priority: 4, Description: "Learn Cloud Datastore", } // A complete key is assigned to the entity when it is Put. var err error key, err = client.Put(ctx, key, &task) // [END entity_with_parent] _ = err // Make sure you check err. }
func ExampleIncompleteKey() { k := datastore.IncompleteKey("Article", nil) _ = k // TODO: Use incomplete key. }
func ExampleNewIncompleteKey() { // [START incomplete_key] taskKey := datastore.IncompleteKey("Task", nil) // [END incomplete_key] _ = taskKey // Use the task key for datastore operations. }