Keeping Property Work on Track with PMDB
Managing affordable housing properties involves far more than keeping a list of buildings and units. Every property comes with recurring reports, inspections, renewals, financial obligations, important contacts, and years of supporting documents.
When that information is spread across calendars, shared drives, inboxes, and individual staff members, even routine work can become difficult to track. A missed deadline or an unavailable document can create delays for the entire team.
This is one of the problems I built PMDB to solve.
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. The tricky bit is the path.
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. I didn’t want the jobs to run on multiple servers, I wanted to have a single server responsible for handling all the scheduled jobs so as to keep duplicate jobs from running. I looked around and found someone who had written about using Highlander to make sure there was only a single instance of Quantum running on the cluster at a time. I decided to try this out.
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.
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. What I noticed when I started using the returned data was that some of the stores didn’t have a Shopify record. For my purposes, I wasn’t interested in the stores without a Shopify record, so I wanted to exclude them from my query results. In its original form, there were several queries being made to satisfy the ecto results. I struggled with how to include a where clause in the preload statement, until I came across something that led me to my desired result. Here’s the end game, a single SQL query resulting from an ecto statement with a preload that limits the stores to those with a Shopify record. You could further fine tune your query to return a subset of these results if you desired.
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. The real issue was how to authenticate to Heroku now that SSH was off the table.
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.