LukeWood

LukeWood

I haven’t seen any examples online, fiddled with the Gcp generated client but had no luck.

Showing Posts 1 to 6

LukeWood

LukeWood OP

Ended up getting the rest api to work decently

rramonfraga

rramonfraga

Hello!

Do you have an example of this implementation. I would like to use a client or ecto adapter for firestore in my service. Thanks!

LukeWood

LukeWood OP

An adapter for Ecto would be sweet - let me see if I can put the code up in a gist.

longpham

longpham

Hi @LukeWood, is it possible to see a code example of your implementation of the firestore REST API ? I want to try and upload a document to a collection. Thanks.

LukeWood

LukeWood OP

I switched to use GoogleApi.Firestore.V1

Some of this code is pretty bad but I don’t actually use it anywhere anymore

defmodule Highscores.Firestore do
  @firestore_url "projects/${PROJECTS}/databases/(default)/documents/highscores/"
  @updateMask %GoogleApi.Firestore.V1.Model.DocumentMask{fieldPaths: ["highscores"]}

  def load_scores(_, nil), do: raise "missing config value: server_name. configure highscores\nconfig :highscores,\n server_name: \"name\""
  def load_scores(conn, server) do
    {:ok, result} = GoogleApi.Firestore.V1.Api.Projects.firestore_projects_databases_documents_get(conn, @firestore_url <> server)
    Map.get(result, :fields) |>
      Map.get("highscores") |>
      Map.get(:arrayValue) |>
      Map.get(:values) |>
      parse_firestore_to_model()
  end

  def sync_scores(conn, server, scores) do
    body = scores |>
      package_to_firestore_types

    {:ok, _response} = GoogleApi.Firestore.V1.Api.Projects.firestore_projects_databases_documents_patch(conn, @firestore_url <> server, updateMask: @updateMask, body: body)
    :ok
  end

  defp package_to_firestore_types(scores) do
    scores = Enum.map(scores, &to_firestore_score/1)
    %{
      fields: %{"highscores" =>
        %GoogleApi.Firestore.V1.Model.Value{
          arrayValue: %GoogleApi.Firestore.V1.Model.ArrayValue{values: scores}
        }
      }
    }
  end

  defp to_firestore_score(%Highscores.Model.Score{name: name, rank: rank, score: score}) do
    %GoogleApi.Firestore.V1.Model.Value{
      mapValue: %GoogleApi.Firestore.V1.Model.MapValue{
        fields: %{
          "name" =>%GoogleApi.Firestore.V1.Model.Value{stringValue: name},
          "score" =>%GoogleApi.Firestore.V1.Model.Value{integerValue: Integer.to_string(score)},
          "rank" =>%GoogleApi.Firestore.V1.Model.Value{integerValue: Integer.to_string(rank)}
        }
      }
    }
  end

  defp parse_firestore_to_model(highscores) do
    Enum.map(highscores, &parse_score/1)
  end

  defp parse_score(entry) do
    fields = entry |>
      Map.get(:mapValue) |>
      Map.get(:fields)

    name = Map.get(fields, "name") |> Map.get(:stringValue)
    {rank, ""} = Map.get(fields, "rank") |> Map.get(:integerValue) |> Integer.parse()
    {score, ""} = Map.get(fields, "score") |> Map.get(:integerValue) |> Integer.parse()

    %Highscores.Model.Score{rank: rank, name: name, score: score}
  end

end
dunyakirkali

dunyakirkali

I’ve managed to use the gRPC API to make a simple connection to it:

https://github.com/dunyakirkali/firestore.ex

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews