Emulating Unique Constraints in Google Firestore

Compared to most popular databases, Google Firestore is very minimalistic.

A very important feature that it lacks, is that of unique constraints (unique indexes). This feature is particularly important when we are building a system that allows users to pick a username. If we don’t enforce uniqueness we could end with two users with the same username, which is not what we want.

Using Document ID and transactions

If we are able to use the username as the user id, then things are a little easier. We can follow these steps to create a new user (wrapped in a transaction):

  • Get user by ID (username)
  • If it exists, user is taken, return an error
  • If it doesn’t exist, add it

Before we proceed with this solution, there are a few things that are important to know about transactions in firestore:

Read More

Running Google Firestore locally

In a previous article, we started playing with Google Firestore. In this article we are going to learn how we can test our applications without the need to talk to Google Cloud.

Note that the local version of Google Firestore is intended for testing only and shouldn’t be used for production systems. It doesn’t provide the reliability or scalability features that the real Firestore does.

Firebase emulator suite

Google provides this suite to help developers test applications without having to use production data or incur cost. The suite doesn’t only emulate the database, but also cloud functions and real-time functionality, to name a couple. In this article we’re only going to focus on the Firestore database.

Read More

Introduction to Google Firestore

Firestore is Google’s serverless document database offering.

What does serverless mean?

Serverless means that we don’t have to worry about managing the servers on which the database runs. All the management is done automatically by Google. The database will scale up or down depending on the demand.

What’s a document database?

A document database is a non-relational database that stores data in a semi-structured format. A common format used by many document databases is JSON.

In a relational database, we usually create tables with a defined structure (columns and types). In a document database, it’s not necessary to specify the different columns.

Read More

Identity and Access Management (IAM) with Google Cloud

In this post we’re going to learn how to use Google Cloud IAM (Identity and Access Management) to limit who can manage resources in a Google Cloud account.

If you are interested in AWS IAM, you can check my Identity and Access Management with AWS article.

Concepts

  • Member - An entity that needs to perform an action on a resource. An end user or a service are examples of members
  • Identity - Another name for Member
  • Resource - A resource is pretty much anything that can be managed in GCP. A compute engine instance or a cloud storage bucket are examples of resources
  • Permission - Allows or denys access to resources. For example: create a storage bucket
  • Role - A collection of permissions. Roles can be granted to members
  • Policy - Defines who (member) can perform which actions (permissions) on which resources
Read More

Monetizing a Jekyll blog with Adsense

I’ve been writing this blog for a while and I recently thought it would be nice if it could help pay for the servers I use for my other projects. At the time of this writing, this blog gets around 25,000 views per month, which is not much, but might be enough to pay for a couple of virtual machines (Hopefully. I’ll know more after I have ads running for some time).

Since this blog is built with Jekyll, I’m going to show how to add Adsense to similar blogs.

Creating a site in Adsense

Before we can start adding ads to our site, we need to tell Google that the site is ours. To do that we need click Add site on the Sites section:

Adsense add site

We will then get a code that we should put in the <head> of our website. The code looks something like this:

1
<script data-ad-client="ca-pub-12345678987654321" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
Read More

Introduction to Google Cloud Functions

Cloud Functions are Google’s offering for serverless architecture (similar to AWS lambdas).

What is serverless?

Before we look into how to use Cloud Functions, we should understand some things about it.

Code needs servers to run, so serverless doesn’t mean there are no servers, it means that we don’t need to manage those servers ourselves.

In a usual server based architecture, we might create a service and deploy it to a machine. This service will be running in the machine all the time waiting for requests. This has the disadvantage that even if there are no requests, the machine would need to be up, and incurring cost.

On the other hand, if we use Cloud Functions, we write a service and register it with Google. Google will then listen to the endpoint this service cares about and will only start it when there are requests. If it detects that there haven’t been requests for some time, it will stop the service again.

Read More

Introduction to Golang modules

Modules are the new way of doing dependency management in Golang.

A module is a collection of packages that are distributed together (e.g. a single binary). A module contains a go.mod file at its root.

With modules, it is not necessary to have our code in $GOPATH/src anymore, so we can create a project anywhere.

Let’s start a new project:

1
mkdir ~/project

And make it a module:

1
2
cd ~/project
go mod init mymodule/hello
Read More

Introduction to Google Cloud CLI

A few months ago, I wrote an article about AWS CLI. Today I’m going to explore gcloud, Google Cloud’s CLI.

Installation

The gcloud CLI requires python 3.5 or later. Let’s verify our version will work:

1
python3 --version

If everything is good, we can download the cli:

1
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-309.0.0-linux-x86_64.tar.gz

Extract it:

1
tar -xvf google-cloud-sdk-309.0.0-linux-x86_64.tar.gz
Read More

Java Lambdas

Sometimes when writing software, it is useful to pass a function as an argument to another function. A common example for this is a generic filter function. In pseudo code, it would look something like this:

1
2
3
4
5
6
7
8
9
10
filter(persons, filter_function) {
  filtered = [];
  for (person in persons) {
    if (filter_function(person)) {
      filtered.push(person);
    }
  }

  return filtered;
}
Read More

Java Map::entryset

The entryset method of a Java Map is used to provide a Set “view” of the Map. Since Map is not iterable, this method provides a way to iterate over the key-value pairs. Let’s look at it in action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.HashMap;
import java.util.Set;

class Streams {
  public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("hello", "world");
    map.put("hola", "mundo");

    Set<HashMap.Entry<String, String>> set = map.entrySet();
    for (HashMap.Entry<String, String> entry : set) {
      System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
    }
  }
}

The output of this program is:

1
2
Key: hello Value: world
Key: hola Value: mundo
Read More