public void Upsert() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: Upsert(Entity[]) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("book"); Entity book1 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Harper Lee", ["title"] = "Tequila Mockingbird", ["publication_date"] = new DateTime(1960, 7, 11, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Southern drama", "Courtroom drama", "Bildungsroman" } }; db.Insert(book1); using (DatastoreTransaction transaction = db.BeginTransaction()) { // Correct the typo in memory book1["title"] = "To Kill a Mockingbird"; Entity book2 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Charlotte Brontë", ["title"] = "Jane Eyre", ["publication_date"] = new DateTime(1847, 10, 16, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Gothic", "Romance", "Bildungsroman" } }; // This adds the entities to a collection of mutations in memory: nothing // is sent to the server. transaction.Upsert(book1, book2); // Without the Commit call, the transaction will automatically // be rolled back. The Commit call performs all the mutations // within the transaction. transaction.Commit(); } // End snippet }
public void UpdateEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; Key key = _fixture.LearnDatastoreKey; // Sample: UpdateEntity DatastoreClient client = DatastoreClient.Create(); ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { Entity entity = transaction.Lookup(key); entity["priority"] = 5; transaction.Update(entity); transaction.Commit(); } // End sample }
private void UpsertLocation(DatastoreTransaction transaction, Location location) { Entity dbLocation = null; if (location.Id > 0) { dbLocation = transaction.Lookup(_keyFactory.CreateKey(location.Id)); } if (dbLocation == null) { dbLocation = new Entity { Key = _keyFactory.CreateIncompleteKey() }; } location.Update(dbLocation); transaction.Upsert(dbLocation); }
public async Task RollbackAsync() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: RollbackAsync(*) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("message"); // Dispose automatically rolls back an uncommitted transaction synchronously. // To roll back asynchronously, bool committed = false; DatastoreTransaction transaction = await db.BeginTransactionAsync(); try { Entity message = new Entity { Key = keyFactory.CreateIncompleteKey(), ["text"] = "Hello", }; // This adds the entity to a collection in memory: nothing // is sent to the server. db.Insert(message); // Attempt to commit the transaction asynchronously. await transaction.CommitAsync(); committed = true; } finally { if (!committed) { // Roll back asynchronously if anything failed. await transaction.RollbackAsync(); } } // End snippet }
public void DeleteEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: Delete(Entity[]) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("message"); Entity message = new Entity { Key = keyFactory.CreateIncompleteKey(), ["text"] = "Hello", }; db.Insert(message); Entity fetchedBeforeDeletion = db.Lookup(message.Key); using (DatastoreTransaction transaction = db.BeginTransaction()) { // This adds the entity key to a collection of mutations in memory: nothing // is sent to the server. Only the key from the entity is used to determine what to delete. // If you already have the key but not the entity, use Delete(Key[]) or // a similar overload. transaction.Delete(message); // Without the Commit call, the transaction will automatically // be rolled back. The Commit call performs all the mutations // within the transaction. transaction.Commit(); } Entity fetchedAfterDeletion = db.Lookup(message.Key); Console.WriteLine($"Entity exists before deletion? {fetchedBeforeDeletion != null}"); Console.WriteLine($"Entity exists after deletion? {fetchedAfterDeletion != null}"); // End snippet Assert.NotNull(fetchedBeforeDeletion); Assert.Null(fetchedAfterDeletion); }
public ActionResult <object> Register([FromBody] User user) { Environment.SetEnvironmentVariable( "GOOGLE_APPLICATION_CREDENTIALS", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fsndigital-cred.json")); DatastoreDb db = DatastoreDb.Create("fsndigital", "Users", DatastoreClient.Create()); string kind = "user"; KeyFactory keyFactory = db.CreateKeyFactory(kind); Key key = keyFactory.CreateKey(user.Email); var task = new Entity { Key = key, ["TypeId"] = user.TypeId, ["UserName"] = user.UserName, ["Email"] = user.Email, ["Password"] = user.Password, }; Entity check = db.Lookup(task.Key); if (check != null) { return(Ok(new { succeeded = false, message = "An account with that email already exists." })); } using (DatastoreTransaction transaction = db.BeginTransaction()) { transaction.Upsert(task); transaction.Commit(); } return(Ok(new { succeeded = true })); }
public void Save(T obj) { DatastoreDb db = DatastoreDb.Create("fantasyfootball-204922", "default"); KeyFactory keyFactory = db.CreateKeyFactory(obj.Kind); Entity entity = new Entity { Key = keyFactory.CreateKey(obj.id), ["updated"] = DateTime.UtcNow, ["text"] = JsonConvert.SerializeObject(obj), ["tags"] = obj.Tags.ToArray() }; using (DatastoreTransaction transaction = db.BeginTransaction()) { transaction.Upsert(entity); CommitResponse commitResponse = transaction.Commit(); Key insertedKey = commitResponse.MutationResults[0].Key; Console.WriteLine($"Inserted key: {insertedKey}"); // The key is also propagated to the entity Console.WriteLine($"Entity key: {entity.Key}"); } }
public void Update() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: Update(Entity[]) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("book"); Entity book = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Harper Lee", ["title"] = "Tequila Mockingbird", ["publication_date"] = new DateTime(1960, 7, 11, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Southern drama", "Courtroom drama", "Bildungsroman" } }; db.Insert(book); using (DatastoreTransaction transaction = db.BeginTransaction()) { // Correct the typo in memory book["title"] = "To Kill a Mockingbird"; // This adds the entity to a collection of mutations in memory: nothing // is sent to the server. transaction.Update(book); // Without the Commit call, the transaction will automatically // be rolled back. The Commit call performs all the mutations // within the transaction. transaction.Commit(); } // End snippet var fetched = db.Lookup(book.Key); Assert.Equal("To Kill a Mockingbird", (string)fetched["title"]); }
public void AddEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Sample: AddEntity DatastoreClient client = DatastoreClient.Create(); KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "book"); Entity book1 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Harper Lee", ["title"] = "To Kill a Mockingbird", ["publication_date"] = new DateTime(1960, 7, 11, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Southern drama", "Courtroom drama", "Bildungsroman" } }; Entity book2 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Charlotte Brontë", ["title"] = "Jane Eyre", ["publication_date"] = new DateTime(1847, 10, 16, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Gothic", "Romance", "Bildungsroman" } }; ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { transaction.Insert(book1, book2); CommitResponse response = transaction.Commit(); IEnumerable <Key> insertedKeys = response.MutationResults.Select(r => r.Key); Console.WriteLine($"Inserted keys: {string.Join(",", insertedKeys)}"); } // End sample }
public void Firebase_Connection() { string path = AppDomain.CurrentDomain.BaseDirectory + @"safe-zone-child-firebase-adminsdk-mq2cq-8ea2101a36.json"; Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path); // Your Google Cloud Platform project ID. string projectId = "safe-zone-child"; // Instantiates a client DatastoreDb db = DatastoreDb.Create(projectId); // The kind for the new entity string kind = "Task"; // The name/ID for the new entity string name = "sampletask1"; KeyFactory keyFactory = db.CreateKeyFactory(kind); // The Cloud Datastore key for the new entity Key key = keyFactory.CreateKey(name); var task = new Entity { Key = key, ["description"] = "8888Buy milk" }; using (DatastoreTransaction transaction = db.BeginTransaction()) { // Saves the task transaction.Upsert(task); transaction.Commit(); Console.WriteLine($"Saved {task.Key.Path[0].Name}: {(string)task["description"]}"); } }
public async Task <long> AddAsync(Customer customer) { var db = _datastoreManager.GetDatastore(); KeyFactory keyFactory = db.CreateKeyFactory(EntityKind); Entity entity = new Entity { Key = keyFactory.CreateIncompleteKey() }; MapCustomerToEntity(customer, entity); using (DatastoreTransaction transaction = await db.BeginTransactionAsync()) { transaction.Insert(entity); CommitResponse commitResponse = await transaction.CommitAsync(); Key insertedKey = commitResponse.MutationResults[0].Key; //TODO - Logging return(insertedKey.Path[0].Id); } }
public IActionResult CreateQuestion(Question ques) { // Instantiates a client DatastoreDb db = DatastoreDb.Create(projectId); // The kind for the new entity string kind = "Questions"; KeyFactory keyFactory = db.CreateKeyFactory(kind); // The Cloud Datastore key for the new entity //Key key = keyFactory.CreateKey(name); var task = new Entity { Key = keyFactory.CreateIncompleteKey(), ["Author"] = ques.Author, ["Quiz"] = ques.Quiz, ["Title"] = ques.Title, ["Answer1"] = ques.Answer1, ["Answer2"] = ques.Answer2, ["Answer3"] = ques.Answer3, ["Answer4"] = ques.Answer4, ["CorrectAnswer"] = ques.CorrectAnswer }; using (DatastoreTransaction transaction = db.BeginTransaction()) { // Saves the task task.Key = db.Insert(task); //transaction.Upsert(task); transaction.Commit(); Console.WriteLine($"Saved {task.Key.Path[0].Name}: {(string)task["Quiz"]}"); } return(RedirectToAction("QuestionList", "Home")); }