|
| 1 | +# @google-cloud/spanner ([Alpha][versioning]) |
| 2 | +> Cloud Spanner Client Library for Node.js |
| 3 | +
|
| 4 | +*Looking for more Google APIs than just Cloud Spanner? You might want to check out [`google-cloud`][google-cloud].* |
| 5 | + |
| 6 | +- [API Documentation][gcloud-spanner-docs] |
| 7 | +- [Official Documentation][cloud-spanner-docs] |
| 8 | + |
| 9 | + |
| 10 | +```sh |
| 11 | +$ npm install --save @google-cloud/spanner |
| 12 | +``` |
| 13 | +```js |
| 14 | +var spanner = require('@google-cloud/spanner')({ |
| 15 | + projectId: 'grape-spaceship-123', |
| 16 | + keyFilename: '/path/to/keyfile.json' |
| 17 | +}); |
| 18 | + |
| 19 | +var instance = spanner.instance('my-instance'); |
| 20 | +var database = instance.database('my-database'); |
| 21 | + |
| 22 | +// Create a table. |
| 23 | +var schema = |
| 24 | + 'CREATE TABLE Singers (' + |
| 25 | + ' SingerId INT64 NOT NULL,' + |
| 26 | + ' FirstName STRING(1024),' + |
| 27 | + ' LastName STRING(1024),' + |
| 28 | + ' SingerInfo BYTES(MAX),' + |
| 29 | + ') PRIMARY KEY(SingerId)'; |
| 30 | + |
| 31 | +database.createTable(schema, function(err, table, operation) { |
| 32 | + if (err) { |
| 33 | + // Error handling omitted. |
| 34 | + } |
| 35 | + |
| 36 | + operation |
| 37 | + .on('error', function(err) {}) |
| 38 | + .on('complete', function() { |
| 39 | + // Table created successfully. |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +// Insert data into the table. |
| 44 | +var table = database.table('Singers'); |
| 45 | + |
| 46 | +table.insert({ |
| 47 | + SingerId: 10, |
| 48 | + FirstName: 'Eddie', |
| 49 | + LastName: 'Wilson' |
| 50 | +}, function(err) { |
| 51 | + if (!err) { |
| 52 | + // Row inserted successfully. |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +// Run a query as a readable object stream. |
| 57 | +database.runStream('SELECT * FROM Singers') |
| 58 | + .on('error', function(err) {}) |
| 59 | + .on('data', function(row) { |
| 60 | + // row.toJSON() = { |
| 61 | + // SingerId: 10, |
| 62 | + // FirstName: 'Eddie', |
| 63 | + // LastName: 'Wilson' |
| 64 | + // } |
| 65 | + } |
| 66 | + }) |
| 67 | + .on('end', function() { |
| 68 | + // All results retrieved. |
| 69 | + }); |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +## Authentication |
| 74 | + |
| 75 | +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. |
| 76 | + |
| 77 | +### On Google Cloud Platform |
| 78 | + |
| 79 | +If you are running this client on Google Cloud Platform, 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. |
| 80 | + |
| 81 | +``` js |
| 82 | +var spanner = require('@google-cloud/spanner')(); |
| 83 | +// ...you're good to go! |
| 84 | +``` |
| 85 | + |
| 86 | +### Elsewhere |
| 87 | + |
| 88 | +If you are not running this client on Google Cloud Platform, you need a Google Developers service account. To create a service account: |
| 89 | + |
| 90 | +1. Visit the [Google Developers Console][dev-console]. |
| 91 | +2. Create a new project or click on an existing project. |
| 92 | +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): |
| 93 | + * Cloud Spanner API |
| 94 | +4. Navigate to **APIs & auth** > **Credentials** and then: |
| 95 | + * If you want to use a new service account key, click on **Create credentials** and select **Service account key**. After the account key is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. |
| 96 | + * If you want to generate a new service account key for an existing service account, click on **Generate new JSON key** and download the JSON key file. |
| 97 | + |
| 98 | +``` js |
| 99 | +var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' |
| 100 | + |
| 101 | +var spanner = require('@google-cloud/spanner')({ |
| 102 | + projectId: projectId, |
| 103 | + |
| 104 | + // The path to your key file: |
| 105 | + keyFilename: '/path/to/keyfile.json' |
| 106 | + |
| 107 | + // Or the contents of the key file: |
| 108 | + credentials: require('./path/to/keyfile.json') |
| 109 | +}); |
| 110 | + |
| 111 | +// ...you're good to go! |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +[versioning]: https://github.com/GoogleCloudPlatform/google-cloud-node#versioning |
| 116 | +[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/ |
| 117 | +[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using |
| 118 | +[dev-console]: https://console.developers.google.com/project |
| 119 | +[gcloud-spanner-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/spanner |
| 120 | +[cloud-spanner-docs]: https://cloud.google.com/spanner |
0 commit comments