Created
July 26, 2014 06:51
-
-
Save ThomasLocke/b6104c4955d12cfe9038 to your computer and use it in GitHub Desktop.
Google Cloud Datastore Dart example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'configuration.dart'; | |
import 'package:google_oauth2_client/google_oauth2_console.dart'; | |
import 'package:google_datastore_v1beta2_api/datastore_v1beta2_api_client.dart' as client; | |
import 'package:google_datastore_v1beta2_api/datastore_v1beta2_api_console.dart' as console; | |
/** | |
* Search for args[0] ID in datastore. Add new entity if search fails to locate | |
* a matching entity. | |
*/ | |
void main(List<String> args) { | |
if(args.length == 0) {args = [''];} | |
try { | |
final console.Datastore datastore = getDatastore(); | |
String id = args[0].isEmpty ? '1' : args[0]; | |
String txnHandle; | |
List<client.Key> keys = [buildKey('person', id)]; | |
datastore.datasets.beginTransaction(txnRequest(), Config.projectId) | |
.then((client.BeginTransactionResponse txnResponse) { | |
txnHandle = txnResponse.transaction; | |
return datastore.datasets.lookup(lookupRequest(txnHandle, keys), Config.projectId); | |
}) | |
.then((client.LookupResponse response) { | |
response.found.forEach((client.EntityResult result) { | |
print('Found: ${JSON.encode(result.entity.properties)}'); | |
}); | |
if (response.found.isEmpty) { | |
final String timestamp = new DateTime.now().toUtc().toIso8601String(); | |
final Map<String, client.Property> person = | |
{'name' : new client.Property.fromJson({'stringValue' : 'Thomas Løcke'}), | |
'website' : new client.Property.fromJson({'stringValue' : 'http://google.com/+ThomasLøcke'}), | |
'timestamp' : new client.Property.fromJson({'dateTimeValue' : timestamp})}; | |
final client.Entity entity = new client.Entity.fromJson({}) | |
..key = new client.Key.fromJson({'path' : [{'kind' : 'person'}]}) | |
..properties = person; | |
final client.CommitRequest request = new client.CommitRequest.fromJson({}) | |
..transaction = txnHandle | |
..mutation = new client.Mutation.fromJson({}) | |
..mutation.insertAutoId = [entity]; | |
datastore.datasets.commit(request, Config.projectId) | |
.then((client.CommitResponse response) { | |
id = response.mutationResult.insertAutoIdKeys.first.toJson()['path'][0]['id']; | |
print('Inserted new entity with id ${id}'); | |
}) | |
.whenComplete(() { | |
datastore.datasets.lookup(lookupRequest('', [buildKey('person', id)]), Config.projectId) | |
.then((client.LookupResponse response) { | |
response.found.forEach((client.EntityResult result) { | |
print('Found new entity: ${JSON.encode(result.entity.properties)}'); | |
}); | |
}); | |
}); | |
} | |
}).catchError((error) => print('Datastore interaction failed with ${error}')); | |
} catch(error) { | |
print('Script failed with ${error}'); | |
} | |
} | |
client.Key buildKey(String kind, String id) => new client.Key.fromJson({'path' : [{'id' : '${id}', 'kind' : '${kind}'}]}); | |
console.Datastore getDatastore() { | |
final ComputeOAuth2Console oauthClient = | |
new ComputeOAuth2Console(Config.projectNumber, | |
privateKey : Config.privateKey, | |
iss : Config.serviceEmail, | |
scopes : Config.scopes); | |
return new console.Datastore(oauthClient) | |
..makeAuthRequests = true; | |
} | |
client.LookupRequest lookupRequest(String txnHandle, List<client.Key> keys) { | |
final client.LookupRequest lookupRequest = new client.LookupRequest.fromJson({}) | |
..keys = keys; | |
if(txnHandle.isNotEmpty) { | |
lookupRequest.readOptions = new client.ReadOptions.fromJson({'transaction' : txnHandle}); | |
} | |
return lookupRequest; | |
} | |
client.BeginTransactionRequest txnRequest() => new client.BeginTransactionRequest.fromJson({}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment