Created
April 5, 2020 00:16
-
-
Save wstrange/52c33cfb3c768718af607737ef4345d4 to your computer and use it in GitHub Desktop.
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:io'; | |
import 'dart:async'; | |
import 'package:firedart/generated/google/firestore/v1/common.pb.dart'; | |
import 'package:firedart/generated/google/firestore/v1/document.pb.dart' as fs; | |
import 'package:firedart/generated/google/firestore/v1/firestore.pbgrpc.dart'; | |
import 'package:firedart/generated/google/firestore/v1/query.pb.dart'; | |
import 'package:grpc/grpc.dart'; | |
import 'models.dart'; | |
class FireStoreSvc { | |
FirestoreClient _client; | |
String _projectId; | |
ClientChannel _channel; | |
FireStoreSvc(this._projectId) { | |
String gapp = Platform.environment['GOOGLE_APPLICATION_CREDENTIALS']; | |
var _saJson = File(gapp).readAsStringSync(); | |
var _jwt = JwtServiceAccountAuthenticator(_saJson); | |
_channel = ClientChannel('firestore.googleapis.com'); | |
_client = FirestoreClient(_channel, options: _jwt.toCallOptions); | |
} | |
close() async { | |
await _channel.shutdown(); | |
} | |
// example - list Collections | |
Future<void> listCollectionIds() async { | |
var request = ListCollectionIdsRequest() | |
..parent = 'projects/$_projectId/databases/(default)/documents'; | |
var result = await _client.listCollectionIds(request); | |
print("Got result = $result"); | |
} | |
// example - create docuement under 'test' collection | |
Future<void> createDocument(Map<String, String> m) async { | |
fs.Document doc = fs.Document(); | |
m.forEach((k, v) => doc.fields[k] = encode(v)); | |
var request = CreateDocumentRequest() | |
..parent = 'projects/$_projectId/databases/(default)/documents' | |
..collectionId = 'test' | |
..document = doc; | |
var result = await _client.createDocument(request); | |
print("result = $result"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment