https://github.com/timakin/gosto
Google Cloud Datastore Client Wrapper in Go, for automation of key attachment.
https://github.com/timakin/gosto
Last synced: 9 months ago
JSON representation
Google Cloud Datastore Client Wrapper in Go, for automation of key attachment.
- Host: GitHub
- URL: https://github.com/timakin/gosto
- Owner: timakin
- Created: 2017-09-05T12:27:29.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-20T14:45:47.000Z (almost 9 years ago)
- Last Synced: 2025-03-24T22:17:09.896Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 44.9 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Gosto
----
Google Cloud Datastore Client Wrapper in Go, for automation of key attachment.
For example, inside of GKE container, you can access to datastore API of you're authorized with Service Account.
## Table of Contents
- [Supported API](#supported-api)
- [Using the library](#using-the-library)
- [Quick start](#quick-start)
- [Get](#get)
- [GetMulti](#get-multi)
- [Delete](#delete)
- [DeleteMulti](#delete-multi)
- [Put](#put)
- [PutMulti](#put-multi)
- [RunInTransaction](#run-in-transaction)
## Supported API
- Get
- GetMulti
- Delete
- DeleteMulti
- Put
- PutMulti
- RunInTransaction
## Using the library
### Quick Start
A client of gosto can be used with `context.Context` and Google Cloud Platform's `project-id`.
```
client, err := gosto.NewGosto(ctx, "project-id")
```
### Get
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
user := &User{
ID: 1,
}
if err = client.Get(user); err != nil {
...
}
```
### GetMulti
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
users := []*User{
&User{
ID: 1,
},
&User{
ID: 2,
},
}
if err = client.GetMulti(users); err != nil {
...
}
```
### Delete
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
user := &User{
ID: 1,
...
}
key := client.Key(user)
if err = client.Delete(key); err != nil {
...
}
```
### DeleteMulti
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
users := []*User{
&User{
ID: 1,
},
&User{
ID: 2,
},
}
var keys []*datastore.Key
for i := range users {
user := users[i]
keys = append(keys, client.Key(user))
}
if err = client.DeleteMulti(keys); err != nil {
...
}
```
### Put
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
user := &User{
Name: "John Doe",
}
if key, err = client.Put(user); err != nil {
...
}
```
### PutMulti
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
users := []*User{
&User{
Name: "John Doe",
},
&User{
Name: "Jane Doe",
},
}
if keys, err = client.PutMulti(users); err != nil {
...
}
```
### RunInTransaction
```
client, err := gosto.NewGosto(ctx, "project-id")
if err != nil {
...
}
if err = client.RunInTransaction(func(tx *datastore.Transaction) error {
user := &User{
Name: "John Doe",
}
uKey := client.Key(user)
if err := tx.Put(uKey, user); err != nil && err != datastore.ErrNoSuchEntity {
return err
}
prof := &UserProfile{
UserID: user.ID,
Content: "HogeFuga",
}
pKey := client.Key(prof)
if _, err := tx.Put(pKey, prof); err != nil {
return err
}
return nil
})
```