Get
Get allows you to retrieve Items by one of their full key paths. If no Item exists at that key path, nothing is returned. Like Put, Get supports batch requests of up to 50 key paths at a time, returning all the items that exist.
In order to Get an Item you must know one of its key paths, which were defined as part of the Item Type’s schema. Each SDK contains some helpers to build key paths.
Clients automatically parse the returned Items into typed objects, though depending on the language you may have different methods for checking which type an Item is.
For this example we’ll use the schema defined in Example: Movies Schema, which declares Movie
as an Item Type with the key path /movies-:id
.
17 collapsed lines
package main
import ( "context" "fmt" "os" "slices" "strconv" "time"
"github.com/google/uuid"
"github.com/StatelyCloud/go-sdk/stately" // This is the code you generated from schema "github.com/StatelyCloud/stately/go-sdk-sample/schema")
func sampleGet( ctx context.Context, client stately.Client, movieID uuid.UUID,) (*schema.Movie, error) { item, err := client.Get( ctx, // Construct the key path for the movie "/movie-"+stately.ToKeyID(movieID[:]), ) if err != nil { return nil, err } if item == nil { // Item not found return nil, nil } if movie, ok := item.(*schema.Movie); ok { return movie, nil } return nil, fmt.Errorf("item is not a Movie")}
item, err := client.WithAllowStale(true).Get( ctx, "/movie-"+stately.ToKeyID(movieID[:]), )
5 collapsed lines
require 'bundler/setup'require_relative 'schema/stately'require 'byebug'
def sample_get(client, movie_id) # Construct the key path for the movie key_path = StatelyDB::KeyPath.with('movie', movie_id) item = client.get(key_path) return itemend item = client.with_allow_stale(true).get(key_path)
9 collapsed lines
from __future__ import annotations
from typing import TYPE_CHECKING
from statelydb import ListToken, SyncChangedItem, SyncDeletedItem, SyncReset, key_path
from .schema import Actor, Change, Character, Client, Movie
async def sample_get(client: Client, movie_id: UUID) -> Movie | None: # Construct the key path for the movie kp = key_path("movie-{id}", id=movie_id) # Tell get that we expect a Movie return await client.get(Movie, kp)
3 collapsed lines
import { createClient, DatabaseClient, Movie } from "./schema/index.js";import { keyPath, ListToken } from "@stately-cloud/client";
async function sampleGet( client: DatabaseClient, movieId: Uint8Array,): Promise<Movie | undefined> { // Construct the key path for the movie const kp = keyPath`/movie-${movieId}`; // Tell get that we expect a Movie const item = await client.get("Movie", kp); return item;}
# There are no key path helpers for shell, so you need to# manually base64-encode the UUID's bytesstately item get \ --store-id <store-id-goes-here> \ --item-key '/movie-2hC3sMFFSlelJlFf9hRD9g'