Entities, Properties, and Keys
Data objects in the App Engine Datastore are known as entities . An entity has one or more named properties , each of which can have one or more values. Entities of the same kind need not have the same properties, and an entity's values for a given property need not all be of the same data type. (If necessary, an application can establish and enforce such restrictions in its own data model.)
The Datastore supports a variety of data types for property values . These include, among others:
- Integers
- Floating-point numbers
- Strings
- Dates
- Binary data
Each entity in the Datastore has a key that uniquely identifies it. The key consists of the following components:
- The namespace of the entity, which allows for multitenancy
- The kind of the entity, which categorizes it for the purpose of Datastore queries
-
An
identifier
for the individual entity, which can be either
- a key name string
- an integer numeric ID
- An optional ancestor path locating the entity within the Datastore hierarchy
An application has access only to entities it has created itself; it can't access data belonging to other applications. It can fetch an individual entity from the Datastore using the entity's key, or it can retrieve one or more entities by issuing a query based on the entities' keys or property values.
The Go App Engine SDK includes a package for representing Datastore entities as Go structs, and for storing and retrieving them in the Datastore.
The Datastore itself does not enforce any restrictions on the structure of entities, such as whether a given property has a value of a particular type; this task is left to the application.
Contents
- Kinds and identifiers
- Ancestor paths
- Transactions and entity groups
- Properties and value types
- Working with entities
- Understanding write costs
Kinds and identifiers
Each Datastore entity is of a particular
kind,
which categorizes the entity for the purpose of queries: for instance, a human resources application might represent each employee at a company with an entity of kind
Employee
. In the Go Datastore API, you specify an entity's kind when you create a
datastore.Key
. All kind names that begin with two underscores (
__
) are reserved and may not be used.
The following example creates an entity of kind
Employee
, populates its property values, and saves it to the Datastore:
import (
"time"
"appengine/datastore"
)
type Employee struct {
FirstName string
LastName string
HireDate time.Time
AttendedHRTraining bool
}
func f(...) {
// ...
employee := &Employee{
FirstName: "Antonio",
LastName: "Salieri",
HireDate: time.Now()
}
employee.AttendedHRTraining = true
key := datastore.NewIncompleteKey(c, "Employee", nil)
_, err := datastore.Put(c, key, employee)
// ...
}
The
Employee
type declares four fields for the data model:
FirstName
,
LastName
,
HireDate
, and
AttendedHRTraining
.
In addition to a kind, each entity has an identifier , assigned when the entity is created. Because it is part of the entity's key, the identifier is associated permanently with the entity and cannot be changed. It can be assigned in either of two ways:
- Your application can specify its own key name string for the entity.
- You can have the Datastore automatically assign the entity an integer numeric ID .
To assign an entity a key name, provide a non-empty
stringID
argument to
datastore.NewKey
:
// Create a key with a key name "asalieri".
key := datastore.NewKey(
c, // appengine.Context
"Employee", // Kind
"asalieri", // String ID; empty means no string ID
0, // Integer ID; if 0, generate automatically. Ignored if string ID specified.
nil // Parent Key; nil means no parent
)
To have the Datastore assign a numeric ID automatically, use an empty
stringID
argument:
// Create a key such as Employee:8261.
key := datastore.NewKey(c, "Employee", "", 0, nil)
// This is equivalent:
key := datastore.NewIncompleteKey(c, "Employee", nil)
Assigning identifiers
The Datastore can be configured to generate auto IDs using two different auto id policies :-
The
default
policy generates a random sequence of IDs that are approximately uniformly distributed. Each ID can be up to 16 decimal digits long. -
The
legacy
policy creates a sequence of non-consecutive smaller integer IDs.