// Load a datastore value into a model and add the key func GetModel(ctx appengine.Context, key *datastore.Key, model models.Model) error { err := datastore.Get(ctx, key, model) if err == nil { model.SetKey(key) } return err }
// Call the parent Next and set the model's key func (iterator *ModelIterator) Next(destModel models.Model) (*datastore.Key, error) { modelKey, err := iterator.Iterator.Next(destModel) if err == nil { destModel.SetKey(modelKey) } return modelKey, err }
// Load multiple datastore values into a slice of models func GetModels(ctx appengine.Context, keys []*datastore.Key, modelSlice []models.Model) error { err := datastore.GetMulti(ctx, keys, modelSlice) var ( index int model models.Model ) for index, model = range modelSlice { model.SetKey(keys[index]) } return err }
// Store a Model, and call SetKey after generating its key (if it doesn't // have one already). func PutModel(ctx appengine.Context, model models.Model) error { modelKey, err := model.Key() if err == models.NoKeyLoaded { modelKey = datastore.NewIncompleteKey(ctx, model.Kind(), nil) } else if err != nil { panic(err) } key, err := datastore.Put(ctx, modelKey, model) if err == nil { model.SetKey(key) } return err }