Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I interact with entities in Datastore? #568

Closed
jgeewax opened this issue May 8, 2015 · 8 comments
Closed

How do I interact with entities in Datastore? #568

jgeewax opened this issue May 8, 2015 · 8 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.

Comments

@jgeewax
Copy link
Contributor

jgeewax commented May 8, 2015

dataset.save() seems to let me put some data, but how do I then get a hold of the persisted entity? Do we have documentation for this that I'm just missing?

@jgeewax jgeewax added type: question Request for information or clarification. Not an issue. docs api: datastore Issues related to the Datastore API. labels May 8, 2015
@jgeewax jgeewax added this to the Datastore Future milestone May 8, 2015
@ryanseys
Copy link
Contributor

Yeah the entity is an object with key and data properties. When you run dataset#save you're actually just passing in an entity. When you dataset#get, datastore returns an array of entities that match your given query. We should perhaps create an Entity object with handy methods, as was discussed as potential in #508. In the meantime, I think documenting the entities returned from dataset#get a little better seems like a reasonable compromise. What do you think @jgeewax ?

@jgeewax
Copy link
Contributor Author

jgeewax commented May 11, 2015

I think an Entity class might be worthwhile... Can we write up some sample code of what this would look like and how we'd use it?

(Here's the Python documentation for this: http://googlecloudplatform.github.io/gcloud-python/latest/datastore-entities.html)

@stephenplusplus
Copy link
Contributor

var entity = Entity(key: Key | String | Array, data: Object)

key
  • Required
  • Type: Key, String, Array

A pass-through to dataset.key if not already an instance of Key.

data
  • Required
  • Type: Object

entity.getKey()

  • Returns: Key

entity.getShouldExcludeFromIndexes();

  • Returns: Boolean

entity.shouldExcludeFromIndexes(Boolean);

Marks the entity as excluded from indexes.

Saving data

Old
var entity = {
  key: dataset.key('Company'),
  data: {
    HQ: 'Dallas, TX'
  },
  excludeFromIndexes: true
};

dataset.save(entity, callback);
New
var company = dataset.entity('Company', {
  HQ: 'Dallas, TX'
});
company.shouldExcludeFromIndexes();

dataset.save(entity, callback);

Getting data

Old
var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  // entity.data.HQ === 'Dallas, TX';
});
New
var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  // entity.HQ === 'Dallas, TX';
});

Getting the key after an operation

Old
var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  // entity.key === companyKey;
});
New
var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  // entity.getKey() === companyKey;
});

Notes

We can restrict users from creating Entity types, and instead only we would create and return them from the API calls. Currently, a user can sufficiently start any API call from just a Key instance. The only place Entity would be useful for a user to create is before a save, as our API now is a bit weird. But the weirdness of that might not be as weird as allowing users to create Entities and wonder:

  • "Does it have to be a complete key, or can I give it an incomplete key?"
  • "Do I have to set the data now?"
  • "Can I do a query with an entity? Or Key?"

Hope I didn't get the vision wrong!

@jgeewax
Copy link
Contributor Author

jgeewax commented Jun 4, 2015

What about the "get and modify" process. Would that look like....

var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  entity.HQ = 'Houston, TX';
  dataset.save(entity);
});

?

@stephenplusplus
Copy link
Contributor

Yes, that would work. Currently we have:

var companyKey = dataset.key(['Company', 123]);
dataset.get(companyKey, function(err, entity) {
  entity.data.HQ = 'Houston, TX';
  dataset.save(entity, callback);
});

I'm starting to wonder if an Entity type is a better solution than making sure we show more complete workflows in our Examples.

@stephenplusplus
Copy link
Contributor

Anyone else have thoughts here? // @callmehiphop?

@callmehiphop
Copy link
Contributor

Looking at the old vs. new code samples, I don't really see any benefits in creating an Entity class. It sounds to me like our problem is that we need more documentation around interacting with entities. An Entity class could fix that, but I think it's the wrong way to go about it.

@stephenplusplus
Copy link
Contributor

I agree. I like using plain JS objects where possible, and this seems like a logical place. I think #574 will go a long way to answer the question we repeatedly hear: "How do I get the generated ID?"

Regarding entities, users seem to be grasping how it works to store, get, and update. But, it couldn't hurt to add an example here: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.25.0/datastore/dataset?method=get

@stephenplusplus stephenplusplus self-assigned this Nov 23, 2015
sofisl pushed a commit that referenced this issue Sep 15, 2022
* chore(main): release 3.0.0

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
sofisl pushed a commit that referenced this issue Oct 12, 2022
* test: use fully qualified request type name in tests

PiperOrigin-RevId: 475685359

Source-Link: googleapis/googleapis@7a12973

Source-Link: googleapis/googleapis-gen@370c729
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMzcwYzcyOWUyYmEwNjJhMTY3NDQ5YzI3ODgyYmE1ZjM3OWM1YzM0ZCJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
sofisl pushed a commit that referenced this issue Oct 13, 2022
* test: use fully qualified request type name in tests

PiperOrigin-RevId: 475685359

Source-Link: googleapis/googleapis@7a12973

Source-Link: googleapis/googleapis-gen@370c729
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMzcwYzcyOWUyYmEwNjJhMTY3NDQ5YzI3ODgyYmE1ZjM3OWM1YzM0ZCJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
sofisl pushed a commit that referenced this issue Nov 10, 2022
PiperOrigin-RevId: 417752787
Source-Link: googleapis/googleapis@3e58417
Source-Link: googleapis/googleapis-gen@e9ad9d6
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTlhZDlkNjM4NjAwNjM4YTAwYjA3MmZmYThkNDE4YTc4OThmMmQ0NiJ9
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
sofisl pushed a commit that referenced this issue Nov 10, 2022
chore: relocate owl bot post processor
sofisl pushed a commit that referenced this issue Nov 11, 2022
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 474338479

Source-Link: googleapis/googleapis@d5d35e0

Source-Link: googleapis/googleapis-gen@efcd3f9
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9
sofisl pushed a commit that referenced this issue Nov 11, 2022
🤖 I have created a release *beep* *boop*
---


## [4.2.0](googleapis/nodejs-cloud-container@v4.1.3...v4.2.0) (2022-09-22)


### Features

* Added High Throughput Logging API for Google Kubernetes Engine ([#571](googleapis/nodejs-cloud-container#571)) ([588b19a](googleapis/nodejs-cloud-container@588b19a))


### Bug Fixes

* Preserve default values in x-goog-request-params header ([#568](googleapis/nodejs-cloud-container#568)) ([575531f](googleapis/nodejs-cloud-container@575531f))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
sofisl pushed a commit that referenced this issue Nov 11, 2022
🤖 I have created a release *beep* *boop*
---


## [3.0.0](googleapis/nodejs-iot@v2.5.1...v3.0.0) (2022-06-20)


### ⚠ BREAKING CHANGES

* update library to use Node 12 (#566)

### Features

* AuditConfig for IAM v1 ([#552](googleapis/nodejs-iot#552)) ([1b1a3d2](googleapis/nodejs-iot@1b1a3d2))


### Bug Fixes

* **deps:** update dependency @google-cloud/pubsub to v3 ([#569](googleapis/nodejs-iot#569)) ([77a2005](googleapis/nodejs-iot@77a2005))
* **deps:** update dependency @google-cloud/storage to v6 ([#568](googleapis/nodejs-iot#568)) ([b15efb4](googleapis/nodejs-iot@b15efb4))


### Build System

* update library to use Node 12 ([#566](googleapis/nodejs-iot#566)) ([1efe654](googleapis/nodejs-iot@1efe654))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
sofisl pushed a commit that referenced this issue Jan 10, 2023
This PR was generated using Autosynth. 🌈

Synth log will be available here:
https://source.cloud.google.com/results/invocations/39538a75-dd2e-4f49-b972-60368d19d19d/targets

- [ ] To automatically regenerate this PR, check this box.
sofisl pushed a commit that referenced this issue Jan 17, 2023
* feat!: drop node8 support, support for async iterators

BREAKING CHANGE: The library now supports Node.js v10+. The last version to support Node.js v8 is tagged legacy-8 on NPM.

New feature: methods with pagination now support async iteration.

* fix tests & linter

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Co-authored-by: Xiaozhen Liu <xiaozhenliu@google.com>
sofisl pushed a commit that referenced this issue Jan 24, 2023
sofisl pushed a commit that referenced this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants