17
17
18
18
// [START setup]
19
19
var express = require ( 'express' ) ;
20
- var gcloud = require ( 'gcloud' ) ;
21
20
var crypto = require ( 'crypto' ) ;
22
21
23
22
var app = express ( ) ;
24
23
app . enable ( 'trust proxy' ) ;
25
24
26
- var dataset = gcloud . datastore ( {
27
- // This environment variable is set by app.yaml when running on GAE, but will
28
- // need to be manually set when running locally.
29
- projectId : process . env . GCLOUD_PROJECT
30
- } ) ;
25
+ // By default, the client will authenticate using the service account file
26
+ // specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
27
+ // the project specified by the GCLOUD_PROJECT environment variable. See
28
+ // https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
29
+ // These environment variables are set automatically on Google App Engine
30
+ var Datastore = require ( '@google-cloud/datastore' ) ;
31
+
32
+ // Instantiate a datastore client
33
+ var datastore = Datastore ( ) ;
31
34
// [END setup]
32
35
33
36
// [START insertVisit]
@@ -38,8 +41,8 @@ var dataset = gcloud.datastore({
38
41
* @param {function } callback The callback function.
39
42
*/
40
43
function insertVisit ( visit , callback ) {
41
- dataset . save ( {
42
- key : dataset . key ( 'visit' ) ,
44
+ datastore . save ( {
45
+ key : datastore . key ( 'visit' ) ,
43
46
data : visit
44
47
} , function ( err ) {
45
48
if ( err ) {
@@ -57,11 +60,11 @@ function insertVisit (visit, callback) {
57
60
* @param {function } callback The callback function.
58
61
*/
59
62
function getVisits ( callback ) {
60
- var query = dataset . createQuery ( 'visit' )
63
+ var query = datastore . createQuery ( 'visit' )
61
64
. order ( '-timestamp' )
62
65
. limit ( 10 ) ;
63
66
64
- dataset . runQuery ( query , function ( err , entities ) {
67
+ datastore . runQuery ( query , function ( err , entities ) {
65
68
if ( err ) {
66
69
return callback ( err ) ;
67
70
}
0 commit comments