|
| 1 | +# @google-cloud/datastore |
| 2 | +> Google Cloud Datastore Client Library for Node.js |
| 3 | +
|
| 4 | +*Looking for more Google APIs than just Datastore? You might want to check out [`google-cloud`][google-cloud].* |
| 5 | + |
| 6 | +- [API Documentation][gcloud-datastore-docs] |
| 7 | +- [Official Documentation][cloud-datastore-docs] |
| 8 | + |
| 9 | +*Follow the [activation instructions][cloud-datastore-activation] to use the Google Cloud Datastore API with your project.* |
| 10 | + |
| 11 | +```sh |
| 12 | +$ npm install --save @google-cloud/datastore |
| 13 | +``` |
| 14 | +```js |
| 15 | +var datastore = require('@google-cloud/datastore')({ |
| 16 | + projectId: 'grape-spaceship-123', |
| 17 | + keyFilename: '/path/to/keyfile.json' |
| 18 | +}); |
| 19 | + |
| 20 | +var key = datastore.key(['Product', 'Computer']); |
| 21 | + |
| 22 | +datastore.get(key, function(err, entity) { |
| 23 | + console.log(err || entity); |
| 24 | +}); |
| 25 | + |
| 26 | +// Save data to Datastore. |
| 27 | +var blogPostData = { |
| 28 | + title: 'How to make the perfect homemade pasta', |
| 29 | + author: 'Andrew Chilton', |
| 30 | + isDraft: true |
| 31 | +}; |
| 32 | + |
| 33 | +var blogPostKey = datastore.key('BlogPost'); |
| 34 | + |
| 35 | +datastore.save({ |
| 36 | + key: blogPostKey, |
| 37 | + data: blogPostData |
| 38 | +}, function(err) { |
| 39 | + // `blogPostKey` has been updated with an ID so you can do more operations |
| 40 | + // with it, such as an update. |
| 41 | + blogPostData.isDraft = false; |
| 42 | + |
| 43 | + datastore.save({ |
| 44 | + key: blogPostKey, |
| 45 | + data: blogPostData |
| 46 | + }, function(err) { |
| 47 | + if (!err) { |
| 48 | + // The blog post is now published! |
| 49 | + } |
| 50 | + }); |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +## Authentication |
| 56 | + |
| 57 | +It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services. |
| 58 | + |
| 59 | +### On Google Compute Engine |
| 60 | + |
| 61 | +If you are running this client on Google Compute Engine, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access. |
| 62 | + |
| 63 | +``` js |
| 64 | +// Authenticating on a global basis. |
| 65 | +var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' |
| 66 | + |
| 67 | +var datastore = require('@google-cloud/datastore')({ |
| 68 | + projectId: projectId |
| 69 | +}); |
| 70 | + |
| 71 | +// ...you're good to go! |
| 72 | +``` |
| 73 | + |
| 74 | +### Elsewhere |
| 75 | + |
| 76 | +If you are not running this client on Google Compute Engine, you need a Google Developers service account. To create a service account: |
| 77 | + |
| 78 | +1. Visit the [Google Developers Console][dev-console]. |
| 79 | +2. Create a new project or click on an existing project. |
| 80 | +3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services): |
| 81 | + * Google Cloud Datastore API |
| 82 | +4. Navigate to **APIs & auth** > **Credentials** and then: |
| 83 | + * If you want to use a new service account, click on **Create new Client ID** and select **Service account**. After the account is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. |
| 84 | + * If you want to generate a new key for an existing service account, click on **Generate new JSON key** and download the JSON key file. |
| 85 | + |
| 86 | +``` js |
| 87 | +var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' |
| 88 | + |
| 89 | +var datastore = require('@google-cloud/datastore')({ |
| 90 | + projectId: projectId, |
| 91 | + |
| 92 | + // The path to your key file: |
| 93 | + keyFilename: '/path/to/keyfile.json' |
| 94 | + |
| 95 | + // Or the contents of the key file: |
| 96 | + credentials: require('./path/to/keyfile.json') |
| 97 | +}); |
| 98 | + |
| 99 | +// ...you're good to go! |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | +[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node |
| 104 | +[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using |
| 105 | +[dev-console]: https://console.developers.google.com/project |
| 106 | +[gcloud-datastore-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore |
| 107 | +[cloud-datastore-docs]: https://cloud.google.com/datastore/docs |
| 108 | +[cloud-datastore-activation]: https://cloud.google.com/datastore/docs/activate |
0 commit comments