Posts
Updating deeply nested elixir maps
In some cases it is necessary to update a deeply nested attribute in a map. I went searching for a way to do this and came across several approaches, the one that worked for me was this, a combination of Kernel.put_in and Access.key, here I’m using it to update a socket struct nested assigns map.
put_in(socket, Enum.map([:assigns, :beacon_live_data, :form_submitted], &Access.key(&1, %{})), true) The put_in function is pretty easy to understand, it Puts a value in a nested structure via the given path.
Posts
Running Quantum in an Elixir Cluster
In one of the projects that I work on, for the sake of availability and reliability, we are running our Phoenix app in a cluster of 3 servers using libcluster. Initially, it was just running on a single server and I had configured Quantum to run our recurring tasks on a cron like schedule. This worked well, and I had no issues with the setup. When we decided to move to a clustered setup, there was a consideration concerning Quantum.
Posts
Decoding TypeORM-Encrypted Data With Elixir
Today’s challege was to figure out how to decrypt an encrypted field created by the TypeORM plugin typeorm-encrypted. Typeorm-encrypted allows you to automatically encrypt/decrypt a field on save/fetch when using TypeORM. This allows you to encrypt sensitive data while at rest in your database.
The Typeorm-encrypted module saves the encrypted (aes-256-gcm) field as a Base64 encoded string in the database. Our challege is to be able to read that field from our database and decrypt the sensitive data for use in our Elixir application.
Posts
Ecto Preload Nested Associations with joins
Recently I had an issue where I was loading some “models” in ecto, and I needed to preload some of the associations along with the parent record. I had it working with a very simple bit of code.
def get_franchises do Franchise |> preload(merchants: [stores: :shopify]) |> Repo.all() end As you can see from this bit of code, some of the associations were nested, Franchise has many Merchants, Merchants have many stores, and a store can have a Shopify record.
Posts
Heroku Git Transport Feature Retirement
We recently received an email from Heroku telling us that we would need to transition our deployments away from the ssh transport that we’ve been using on Semaphore CI to deploy our Elixir Phoenix app. The Semaphore deployment documentation only covers the ssh transport route. We needed to figure out how to move to the https deployments from our CI server. The instructions in the email were’t too helpful as those steps had already been taken in our project, the Semaphore solution replaces https with ssh for deployments.
Posts
Add Sass to Phoenix 1.6 with Esbuild
If you are starting a new project with The Phoenix Framework version 1.6, you’ll notice there has been a change with regard to asset handling. Chris McCord and The Phoenix Team have made the choice to move away from WebPack and start using Esbuild to deal with the Javascript files. One side effect of this is you lose the ability to use Sass files by default. Have no fear, there’s a way to add Sass back into your project.