<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elias Martinez on Medium]]></title>
        <description><![CDATA[Stories by Elias Martinez on Medium]]></description>
        <link>https://medium.com/@eliasmsedano?source=rss-726697805cc1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jjyAw9bL6TCuS7thDqaLlA.jpeg</url>
            <title>Stories by Elias Martinez on Medium</title>
            <link>https://medium.com/@eliasmsedano?source=rss-726697805cc1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 09:42:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eliasmsedano/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Avoiding Flyway Migration Collisions While Planning Future Releases]]></title>
            <link>https://eliasmsedano.medium.com/avoiding-flyway-migration-collisions-while-planning-future-releases-268cd1731a79?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/268cd1731a79</guid>
            <category><![CDATA[framework]]></category>
            <category><![CDATA[flyway]]></category>
            <category><![CDATA[database-migration]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Fri, 09 Oct 2026 16:18:56 GMT</pubDate>
            <atom:updated>2026-10-09T16:18:56.900Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TwCRLb4Nx3D31o0m6zvmUg.jpeg" /></figure><p><strong>TL;DR:</strong> Use a timestamp as the Flyway migration version and append the target release date to the filename:</p><pre>V&lt;YYMMDDHHMMSS&gt;__&lt;Description&gt;__v&lt;YY.MM.DD&gt;.sql</pre><p>The timestamp reduces collisions between developers and determines Flyway execution order. The target-date suffix lets the Azure DevOps pipeline expose only migrations eligible for the selected release. Using migration target &lt;= pipeline target also lets environments catch up on older pending migrations, while Flyway’s schema history prevents successfully applied migrations from running again.</p><p><strong>Timestamp controls Flyway. Target date controls release eligibility. Team planning controls dependencies.</strong></p><h3>A timestamp-based naming strategy that separates migration uniqueness from release eligibility</h3><p>Developers often need to prepare database changes for future releases while the current release is still in progress.</p><p>This creates two related but distinct challenges:</p><p>1. Multiple developers may create Flyway migrations at the same time and accidentally use the same version.</p><p>2. A migration may be ready and committed to source control but should not execute until a future release.</p><p>A practical solution is to separate <strong>migration identity</strong> from <strong>release eligibility</strong>.</p><p>The proposed convention is:</p><pre>V&lt;YYMMDDHHMMSS&gt;__&lt;Description&gt;__v&lt;YY.MM.DD&gt;.sql</pre><p>For example:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql<br>V261006104322__CreateCustomerIndex__v26.10.06.sql<br>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><p>This allows developers to commit migrations for current and future releases into the same source branch without coordinating sequential Flyway version numbers.</p><p>The central design principle is:</p><blockquote>The timestamp controls Flyway. The target date controls release eligibility. Team planning controls dependencies.</blockquote><h3>The problem with sequential Flyway versions</h3><p>A simple Flyway project might use migrations such as:</p><pre>V1__CreateCustomerTable.sql<br>V2__CreateCustomerIndex.sql<br>V3__CreateOrderTable.sql</pre><p>This is easy to manage when one developer creates the migrations sequentially.</p><p>The problem appears when several developers work in parallel.</p><p>For example, two developers may independently determine that the next migration should be:</p><pre>V14__SomeDatabaseChange.sql</pre><p>Their branches work correctly in isolation, but a collision appears when both changes are merged.</p><p>Developers must then:</p><ul><li>Check the latest migration version before creating a file.</li><li>Coordinate version numbers with other developers.</li><li>Rename migrations when a merge collision occurs.</li><li>Confirm whether the renamed migration has already executed in an environment.</li></ul><p>The problem becomes more complicated when developers are preparing work for different releases.</p><p>A developer may complete a database change for the October 16 release while the team is still preparing the October 6 release. The migration should be committed early, but it must not execute on October 6.</p><h3>The problem has two separate dimensions</h3><p>Migration identity and release scheduling are related, but they are not the same concern.</p><h3>Developer collision</h3><p>Multiple developers can independently select the same next Flyway migration number.</p><p>The Flyway migration version must therefore be unique without requiring developers to coordinate a shared sequence.</p><h3>Future-release work</h3><p>A migration may be complete and committed to source control but not eligible for the current deployment.</p><p>The pipeline must be able to distinguish between:</p><ul><li>Migrations intended for the current release.</li><li>Migrations intended for a future release.</li><li>Older migrations that an environment may still need to apply.</li></ul><p>Trying to represent both concerns with one sequential Flyway version creates unnecessary coordination.</p><h3>The proposed naming convention</h3><p>The solution is to give each concern its own field in the migration filename:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql</pre><p>The filename contains three important parts.</p><h3>1. Timestamp: Flyway version</h3><pre>V261006104321</pre><p>The timestamp follows this format:</p><pre>YYMMDDHHMMSS</pre><p>In this example:</p><pre>26 10 06 10 43 21</pre><p>represents:</p><pre>October 6, 2026 at 10:43:21</pre><p>Flyway interprets the timestamp as the migration version.</p><p>This provides:</p><ul><li>A sortable migration version.</li><li>A highly collision-resistant identifier.</li><li>Natural ordering based on creation time.</li><li>No requirement to find the latest sequential migration number.</li><li>Less coordination between developers working in parallel.</li></ul><p>Timestamp versions greatly reduce collision risk, although the pipeline should still detect the rare case where two files use the same timestamp.</p><h4><strong>2. Description: human-readable context</strong></h4><pre>CreateCustomerTable</pre><p>The description explains the purpose of the migration.</p><p>It is also visible in the Flyway schema history, making it easier to understand what each migration changed.</p><h3>3. Target date: release eligibility</h3><pre>v26.10.06</pre><p>The target date follows this format:</p><pre>vYY.MM.DD</pre><p>This value represents the release for which the migration is intended.</p><p>The v prefix is intentional. In this process, the version represents a target release date.</p><p>The target date is not the Flyway migration version. It is release metadata interpreted by the delivery pipeline.</p><h3>One filename, two independent controls</h3><p>Consider this filename:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql</pre><p>The responsibilities are divided as follows:</p><ul><li>V261006104321 provides Flyway uniqueness and execution order.</li><li>CreateCustomerTable provides a readable description.</li><li>v26.10.06 determines release eligibility.</li></ul><p>This separation allows each mechanism to focus on one concern:</p><blockquote><strong><em>Timestamp:</em></strong><em> Flyway uniqueness and ordering<br> </em><strong><em>Target version:</em></strong><em> Release eligibility<br> </em><strong><em>Team planning:</em></strong><em> Feature and dependency coordination</em></blockquote><h3>Why Flyway alone does not perform this filtering</h3><p>Flyway reads a versioned migration using the following general structure:</p><pre>V&lt;Version&gt;__&lt;Description&gt;.sql</pre><p>With the proposed convention:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql</pre><p>Flyway sees:</p><pre>Version: 261006104321<br>Description: CreateCustomerTable__v26.10.06</pre><p>The target-release suffix is technically part of the Flyway description.</p><p>Therefore, configuring Flyway with:</p><pre>flyway -target=26.10.06 migrate</pre><p>would not produce the intended result.</p><p>Flyway would compare 26.10.06 with the actual Flyway migration versions:</p><pre>261006104321<br>261006104322<br>261006104323</pre><p>It would not extract or interpret the v26.10.06 suffix.</p><p>The target-release filtering must happen before Flyway scans its migration location.</p><h3>The release workflow</h3><p>The complete workflow consists of five steps.</p><h3>Step 1: Commit</h3><p>Developers commit migrations for current and future releases into the same repository and source branch.</p><p>For example:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql<br>V261006104322__CreateCustomerIndex__v26.10.06.sql<br>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><p>The future migration can safely remain in source control while the October 6 release is prepared.</p><h3>Step 2: Set the release target</h3><p>The Azure DevOps pipeline supplies a target release variable:</p><pre>variables:<br>  TARGET_RELEASE_VERSION: &#39;26.10.06&#39;</pre><p>The value does not include the v prefix because the script already understands that it is parsing the release suffix.</p><h3>Step 3: Validate</h3><p>Before copying any migrations, the pipeline validates:</p><ul><li>The complete filename convention.</li><li>The Flyway timestamp format.</li><li>The target-release date format.</li><li>Duplicate Flyway timestamp versions.</li><li>Invalid or malformed filenames.</li></ul><p>The pipeline should fail instead of silently ignoring an invalid migration.</p><h3>Step 4: Filter</h3><p>The pipeline selects migrations using this rule:</p><pre>migration target date &lt;= pipeline target date</pre><p>Eligible migrations are copied into a temporary staging directory.</p><p>Future migrations remain in source control but are not made visible to Flyway.</p><h3>Step 5: Migrate</h3><p>Flyway scans only the staging directory.</p><p>Flyway continues handling:</p><ul><li>Migration discovery.</li><li>Version ordering.</li><li>Checksum validation.</li><li>Schema history.</li><li>Pending migration detection.</li><li>Exactly-once execution of versioned migrations.</li></ul><h3>Cumulative target-release filtering</h3><p>The recommended eligibility rule is:</p><pre>$migrationRelease -le $targetRelease</pre><p>This performs cumulative selection.</p><p>For example, consider these migrations:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql<br>V261006104322__CreateCustomerIndex__v26.10.06.sql<br>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><h3>Pipeline target: v26.10.06</h3><p>The pipeline includes:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql<br>V261006104322__CreateCustomerIndex__v26.10.06.sql</pre><p>The pipeline excludes:</p><pre>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><h3>Pipeline target: v26.10.16</h3><p>The pipeline includes:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql<br>V261006104322__CreateCustomerIndex__v26.10.06.sql<br>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><p>The October 6 migrations are visible to Flyway again, but Flyway does not execute them again if they are already recorded as successful in its schema history table.</p><p>Only pending migrations are executed.</p><h3>Why use &lt;= instead of an exact match?</h3><p>An exact comparison would look like this:</p><pre>$migrationRelease -eq $targetRelease</pre><p>That would expose only migrations assigned to the selected release.</p><p>However, exact matching becomes a problem if an environment skips a release.</p><p>For example, suppose an environment moves directly from:</p><pre>v26.10.06</pre><p>to:</p><pre>v26.10.20</pre><p>If the pipeline includes only exact v26.10.20 migrations, it may omit pending migrations from:</p><pre>v26.10.10<br>v26.10.16</pre><p>Using a cumulative comparison:</p><pre>$migrationRelease -le $targetRelease</pre><p>allows the environment to catch up.</p><p>Flyway safely ignores migrations it previously applied and executes only eligible migrations that are still pending.</p><h3>PowerShell migration filter</h3><p>The following script validates the migration names, detects duplicate timestamp versions, and copies eligible migrations into a Flyway staging directory.</p><pre>$ErrorActionPreference = &quot;Stop&quot;<br>$sourceFolder = &quot;$(Build.SourcesDirectory)/migrations&quot;<br>$outputFolder = &quot;$(Build.ArtifactStagingDirectory)/flyway-migrations&quot;<br>$targetText = &quot;$(TARGET_RELEASE_VERSION)&quot;<br>$culture = [System.Globalization.CultureInfo]::InvariantCulture<br>$target = :ParseExact(<br>  $targetText,<br>  &quot;yy.MM.dd&quot;,<br>  $culture<br>)<br>Remove-Item `<br>  -Path $outputFolder `<br>  -Recurse `<br>  -Force `<br>  -ErrorAction SilentlyContinue<br>New-Item `<br>  -ItemType Directory `<br>  -Path $outputFolder `<br>  -Force |<br>  Out-Null<br>$files = Get-ChildItem `<br>  -Path $sourceFolder `<br>  -Filter &quot;V*.sql&quot; `<br>  -File</pre><h3>Detect duplicate Flyway timestamp versions</h3><p>A timestamp significantly reduces the likelihood of collisions, but it does not mathematically guarantee uniqueness.</p><p>Two developers could generate migrations during the same second.</p><p>The pipeline should detect that condition:</p><pre>$duplicates = $files |<br>    ForEach-Object {<br>        if ($_.Name -match &#39;^V(?&lt;version&gt;\d{12})__&#39;) {<br>            [PSCustomObject]@{<br>                Version = $Matches[&quot;version&quot;]<br>                File = $_.Name<br>            }<br>        }<br>    } |<br>    Group-Object Version |<br>    Where-Object Count -gt 1<br><br>if ($duplicates) {<br>    $details = $duplicates |<br>        ForEach-Object {<br>            &quot;$($_.Name): $($_.Group.File -join &#39;, &#39;)&quot;<br>        }<br><br>    throw &quot;Duplicate Flyway versions detected: $($details -join &#39;; &#39;)&quot;<br>}</pre><h3>Validate and filter migrations</h3><p>The expected filename structure is:</p><pre>V&lt;12-digit timestamp&gt;__&lt;description&gt;__v&lt;YY.MM.DD&gt;.sql</pre><p>The validation and filtering logic is:</p><pre>$files |<br>    Sort-Object Name |<br>    ForEach-Object {<br>        $fileName = $_.Name<br><br>        $pattern = &#39;^V(?&lt;timestamp&gt;\d{12})__(?&lt;description&gt;.+)__v(?&lt;release&gt;\d{2}\.\d{2}\.\d{2})\.sql$&#39;<br><br>        if ($fileName -notmatch $pattern) {<br>            throw &quot;Invalid migration filename: $fileName&quot;<br>        }<br><br>        $timestampText = $Matches[&quot;timestamp&quot;]<br>        $releaseText = $Matches[&quot;release&quot;]<br><br>        # Validate the Flyway timestamp as a real date and time.<br>        ParseExact(<br>            $timestampText,<br>            &quot;yyMMddHHmmss&quot;,<br>            $culture<br>        )<br><br>        # Validate the target version as a real release date.<br>        $migrationRelease = :ParseExact(<br>            $releaseText,<br>            &quot;yy.MM.dd&quot;,<br>            $culture<br>        )<br><br>        if ($migrationRelease -le $target) {<br>            Copy-Item `<br>                -Path $_.FullName `<br>                -Destination $outputFolder<br><br>            Write-Host &quot;INCLUDED [$releaseText]: $fileName&quot;<br>        }<br>        else {<br>            Write-Host &quot;EXCLUDED [$releaseText]: $fileName&quot;<br>        }<br>    }</pre><h3>Complete filtering script</h3><p>The complete script can be stored as:</p><pre>filter-migrations.ps1</pre><pre>$ErrorActionPreference = &quot;Stop&quot;<br><br>$sourceFolder = &quot;$(Build.SourcesDirectory)/migrations&quot;<br>$outputFolder = &quot;$(Build.ArtifactStagingDirectory)/flyway-migrations&quot;<br>$targetText = &quot;$(TARGET_RELEASE_VERSION)&quot;<br><br>$culture = [System.Globalization.CultureInfo]::InvariantCulture<br><br>$target = :ParseExact(<br>    $targetText,<br>    &quot;yy.MM.dd&quot;,<br>    $culture<br>)<br><br>Remove-Item `<br>    -Path $outputFolder `<br>    -Recurse `<br>    -Force `<br>    -ErrorAction SilentlyContinue<br><br>New-Item `<br>    -ItemType Directory `<br>    -Path $outputFolder `<br>    -Force |<br>    Out-Null<br><br>$files = Get-ChildItem `<br>    -Path $sourceFolder `<br>    -Filter &quot;V*.sql&quot; `<br>    -File<br><br># Fail early if two migrations have the same Flyway timestamp version.<br>$duplicates = $files |<br>    ForEach-Object {<br>        if ($_.Name -match &#39;^V(?&lt;version&gt;\d{12})__&#39;) {<br>            [PSCustomObject]@{<br>                Version = $Matches[&quot;version&quot;]<br>                File = $_.Name<br>            }<br>        }<br>    } |<br>    Group-Object Version |<br>    Where-Object Count -gt 1<br><br>if ($duplicates) {<br>    $details = $duplicates |<br>        ForEach-Object {<br>            &quot;$($_.Name): $($_.Group.File -join &#39;, &#39;)&quot;<br>        }<br><br>    throw &quot;Duplicate Flyway versions detected: $($details -join &#39;; &#39;)&quot;<br>}<br><br>$includedCount = 0<br>$excludedCount = 0<br><br>$files |<br>    Sort-Object Name |<br>    ForEach-Object {<br>        $fileName = $_.Name<br><br>        $pattern = &#39;^V(?&lt;timestamp&gt;\d{12})__(?&lt;description&gt;.+)__v(?&lt;release&gt;\d{2}\.\d{2}\.\d{2})\.sql$&#39;<br><br>        if ($fileName -notmatch $pattern) {<br>            throw &quot;Invalid migration filename: $fileName&quot;<br>        }<br><br>        $timestampText = $Matches[&quot;timestamp&quot;]<br>        $releaseText = $Matches[&quot;release&quot;]<br><br>        # Validate the timestamp as a real date and time.<br>        ParseExact(<br>            $timestampText,<br>            &quot;yyMMddHHmmss&quot;,<br>            $culture<br>        )<br><br>        # Validate the target version as a real release date.<br>        $migrationRelease = :ParseExact(<br>            $releaseText,<br>            &quot;yy.MM.dd&quot;,<br>            $culture<br>        )<br><br>        if ($migrationRelease -le $target) {<br>            Copy-Item `<br>                -Path $_.FullName `<br>                -Destination $outputFolder<br><br>            Write-Host &quot;INCLUDED [$releaseText]: $fileName&quot;<br>            $includedCount++<br>        }<br>        else {<br>            Write-Host &quot;EXCLUDED [$releaseText]: $fileName&quot;<br>            $excludedCount++<br>        }<br>    }<br><br>Write-Host &quot;&quot;<br>Write-Host &quot;Target release: v$targetText&quot;<br>Write-Host &quot;Included migrations: $includedCount&quot;<br>Write-Host &quot;Future migrations excluded: $excludedCount&quot;<br>Write-Host &quot;Flyway location: $outputFolder&quot;</pre><h3>Azure DevOps pipeline integration</h3><p>The release target can be supplied as an Azure DevOps variable:</p><pre>variables:<br>  TARGET_RELEASE_VERSION: &#39;26.10.06&#39;</pre><p>The pipeline first runs the filtering script:</p><pre>steps:<br>- task: PowerShell@2<br>  displayName: Select eligible Flyway migrations<br>  inputs:<br>    targetType: filePath<br>    filePath: &#39;$(Build.SourcesDirectory)/scripts/filter-migrations.ps1&#39;<br>    pwsh: true</pre><p>Flyway then runs against the filtered staging directory:</p><pre>- task: PowerShell@2<br>  displayName: Run Flyway migrations<br>  inputs:<br>    targetType: inline<br>    pwsh: true<br>    script: |<br>      flyway `<br>        -locations=&quot;filesystem:$(Build.ArtifactStagingDirectory)/flyway-migrations&quot; `<br>        migrate</pre><p>A simplified complete pipeline example is:</p><pre>variables:<br>  TARGET_RELEASE_VERSION: &#39;26.10.06&#39;<br><br>steps:<br>- task: PowerShell@2<br>  displayName: Select eligible Flyway migrations<br>  inputs:<br>    targetType: filePath<br>    filePath: &#39;$(Build.SourcesDirectory)/scripts/filter-migrations.ps1&#39;<br>    pwsh: true<br><br>- task: PowerShell@2<br>  displayName: Run Flyway migrations<br>  inputs:<br>    targetType: inline<br>    pwsh: true<br>    script: |<br>      flyway `<br>        -locations=&quot;filesystem:$(Build.ArtifactStagingDirectory)/flyway-migrations&quot; `<br>        migrate</pre><p>The exact Flyway authentication and connection settings can remain in the existing pipeline configuration, environment variables, or Flyway configuration files.</p><p>The relevant change is the migration location:</p><pre>filesystem:$(Build.ArtifactStagingDirectory)/flyway-migrations</pre><p>Flyway should scan the filtered staging directory instead of the full source migration directory.</p><h3>Validation should fail fast</h3><p>The pipeline should enforce the agreed convention instead of silently skipping unexpected files.</p><p>Recommended validations include:</p><ol><li>Require the complete filename structure:</li></ol><pre>`V&lt;YYMMDDHHMMSS&gt;__&lt;Description&gt;__v&lt;YY.MM.DD&gt;.sql`</pre><p>2. Require exactly 12 digits in the Flyway timestamp.</p><p>3. Validate the timestamp as a real date and time.</p><p>4. Validate the target version as a real date.</p><p>5. Require a non-empty migration description.</p><p>6. Reject duplicate Flyway timestamp versions.</p><p>7. Fail when a file does not follow the convention.</p><p>8. Log included and excluded migrations.</p><p>9. Use the same filtered artifact throughout the promotion process.</p><p>That final point is important.</p><p>If the organization separates build and release pipelines, the build pipeline should ideally create the filtered migration artifact, and the release pipeline should deploy that exact immutable artifact.</p><p>This prevents the migration set from changing between build and deployment.</p><h3>Division of responsibilities</h3><p>The process works best when responsibilities remain clearly separated.</p><h3>Developer responsibilities</h3><p>Developers are responsible for:</p><ul><li>Generating a timestamp-based Flyway version.</li><li>Providing a meaningful description.</li><li>Assigning the migration to the correct target release.</li><li>Keeping dependent database changes release-compatible.</li><li>Avoiding changes to migrations that have already been applied.</li><li>Not assigning new migrations to a completed release without following the team’s established recovery process.</li></ul><p>Example:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql</pre><h3>Pipeline responsibilities</h3><p>The pipeline is responsible for:</p><ul><li>Validating the filename convention.</li><li>Validating timestamp and release-date formats.</li><li>Detecting duplicate Flyway versions.</li><li>Selecting migrations eligible through the target release.</li><li>Copying eligible migrations into the staging location.</li><li>Running Flyway against only the staged migrations.</li><li>Failing visibly when the migration set is invalid.</li></ul><h3>Release-team responsibilities</h3><p>The release team is responsible for:</p><ul><li>Setting the target release variable.</li><li>Coordinating feature and migration dependencies.</li><li>Approving the release scope.</li><li>Ensuring environments move forward consistently.</li><li>Preventing a lower release target from being deployed after a higher release has already been promoted.</li></ul><h3>Dependency planning remains a team responsibility</h3><p>Consider these migrations:</p><pre>V261006104321__CreateParentTable__v26.10.16.sql<br>V261007104321__CreateChildTable__v26.10.06.sql</pre><p>If the child table depends on the parent table, the earlier release assignment is invalid.</p><p>The pipeline should not attempt to infer this dependency from SQL.</p><p>That responsibility belongs to:</p><ul><li>The developer implementing the feature.</li><li>Code review.</li><li>Feature planning.</li><li>Release planning.</li><li>Integration testing.</li></ul><p>The pipeline should remain focused on deterministic validation and selection.</p><blockquote><em>The pipeline validates naming and determines eligibility. It does not infer business or feature dependencies.</em></blockquote><p>This keeps the delivery mechanism predictable and avoids introducing a custom dependency engine around Flyway.</p><h3>What this approach enables</h3><p>The convention supports several important development scenarios.</p><h3>Developers can work on future releases early</h3><p>A migration targeting October 16 can be committed while the October 6 release is still active:</p><pre>V261006104323__CreateFutureOrdersTable__v26.10.16.sql</pre><p>The file remains in source control but is excluded by the October 6 release pipeline.</p><h3>Developers do not need the latest sequential version</h3><p>The migration version comes from the timestamp:</p><pre>V261006104323</pre><p>Developers do not need to search the repository for the latest number before creating a migration.</p><h3>Branch collisions are significantly reduced</h3><p>Two developers working independently are unlikely to generate the same timestamp down to the second.</p><p>The pipeline still validates duplicates for safety.</p><h3>Environments can catch up</h3><p>Because selection is cumulative, an environment can deploy a later release and still receive pending migrations from earlier releases.</p><h3>Flyway remains responsible for migration history</h3><p>The solution does not replace Flyway’s schema history or checksum validation.</p><p>Flyway still determines which eligible migrations are pending.</p><h3>No custom Flyway resolver is required</h3><p>The release-specific behavior remains in the Azure DevOps pipeline.</p><p>Flyway receives a normal filesystem location containing valid versioned migrations.</p><h3>Tradeoffs</h3><p>This design introduces a small amount of pipeline logic in exchange for reducing developer coordination.</p><h3>Advantages</h3><ul><li>Reduces migration version collisions.</li><li>Supports parallel development across release windows.</li><li>Keeps future migrations in source control.</li><li>Does not require developers to track a shared sequence.</li><li>Preserves normal Flyway execution and history.</li><li>Supports environments catching up across releases.</li><li>Makes release eligibility explicit in the filename.</li><li>Avoids custom Flyway resolvers.</li></ul><h3>Considerations</h3><ul><li>The pipeline must validate and filter files correctly.</li><li>Teams must define a consistent release-date format.</li><li>Developers must assign the correct target release.</li><li>Feature dependencies must be handled during planning and review.</li><li>The release process should prevent backdating migrations into completed releases.</li><li>Timestamp uniqueness must still be validated.</li><li>The filtered migration artifact should remain consistent across environment promotion.</li></ul><p>These are manageable constraints when ownership is clearly defined.</p><h3>Recommended repository structure</h3><p>A simple repository structure could be:</p><pre>repository/<br>├── migrations/<br>│   ├── V261006104321__CreateCustomerTable__v26.10.06.sql<br>│   ├── V261006104322__CreateCustomerIndex__v26.10.06.sql<br>│   └── V261006104323__CreateFutureOrdersTable__v26.10.16.sql<br>├── scripts/<br>│   └── filter-migrations.ps1<br>└── azure-pipelines.yml</pre><p>During the pipeline run, the eligible files are copied into:</p><pre>$(Build.ArtifactStagingDirectory)/flyway-migrations</pre><p>Flyway scans only that directory.</p><h3>Final convention</h3><p>The final migration naming convention is:</p><pre>V&lt;YYMMDDHHMMSS&gt;__&lt;Description&gt;__v&lt;YY.MM.DD&gt;.sql</pre><p>Example:</p><pre>V261006104321__CreateCustomerTable__v26.10.06.sql</pre><p>The process assigns one purpose to each part:</p><pre>V261006104321</pre><p>Flyway version, uniqueness, and ordering.</p><pre>CreateCustomerTable</pre><p>Human-readable migration description.</p><pre>v26.10.06</pre><p>Target-release eligibility.</p><p>The pipeline applies this rule:</p><pre>migration target date &lt;= pipeline target date</pre><p>Eligible migrations are staged, and Flyway applies only those that remain pending.</p><h3>Conclusion</h3><p>Database migration naming should not require developers to coordinate a shared sequence every time they create a file.</p><p>Likewise, committing a future migration should not cause it to execute in the current release.</p><p>Separating the Flyway timestamp version from the target release date solves both problems:</p><ul><li>The<strong> timestamp provides a unique</strong>, sortable Flyway version.</li><li>The <strong>target date</strong> allows the pipeline to <strong>control release eligibility</strong>.</li><li><strong>Flyway</strong> <strong>continues managing</strong> checksums, ordering, history, and exactly-once execution.</li><li><strong>Developers</strong> and release teams remain <strong>responsible for feature dependencies and release planning.</strong></li></ul><p>The result is a straightforward division of responsibilities:</p><blockquote><strong><em>Timestamp controls Flyway. Target date controls release eligibility. Team planning controls dependencies.</em></strong></blockquote><p>This allows developers to prepare future-release database work without coordinating sequential migration numbers, waiting for the current release to finish, or changing Flyway’s standard migration model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=268cd1731a79" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 2: Efficient Message Processing with Dynamic Go Worker Pools]]></title>
            <link>https://eliasmsedano.medium.com/part-2-efficient-message-processing-with-dynamic-go-worker-pools-f5b076870564?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5b076870564</guid>
            <category><![CDATA[message-queue]]></category>
            <category><![CDATA[autoscaling]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Wed, 23 Apr 2025 00:52:15 GMT</pubDate>
            <atom:updated>2025-04-23T00:53:27.808Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4j4UF0X22pz0JcSr" /><figcaption>Photo by <a href="https://unsplash.com/@vmxhu?utm_source=medium&amp;utm_medium=referral">Szabo Viktor</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://eliasmsedano.medium.com/taming-duplicate-kafka-messages-with-idempotent-processing-part-1-2d46db9e54c3">In Part 1</a>, we explored how to solve the duplicate message challenge in Kafka using idempotent storage in a database. We implemented a solution that safely consumes messages and deduplicates them at the storage layer.</p><p>Now, we’ll dive into Phase 2: efficiently processing those stored messages with a dynamic worker pool. This approach allows multiple server instances to process messages concurrently without duplication, while automatically scaling to match workload demands.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*av7aFEd_XEOB7GpZP1H4gg.png" /></figure><h3>The Processing Challenge</h3><p>After solving the duplicate message problem, we faced a new challenge: how to efficiently process a potentially large backlog of messages across multiple server instances. Our requirements included:</p><ol><li><strong>Horizontal Scalability</strong>: Multiple servers should cooperate without conflicts</li><li><strong>Efficient Resource Usage</strong> Processing capacity should scale with demand while respecting SLAs</li><li><strong>Resilience</strong>: Server crashes shouldn’t cause message loss</li><li><strong>No Duplicate Processing</strong>: Each message must be processed exactly once</li><li><strong>Visibility</strong>: Clear insight into processing status and failures</li></ol><h3>Solution Architecture Overview</h3><p>Our solution consists of three main components working together:</p><ol><li><strong>Persistence Layer</strong>: Abstracts all database operations for message storage and retrieval.</li><li><strong>Message Handler:</strong> Interface for processing different message types.</li><li><strong>Worker Pool:</strong> Dynamically scalable set of workers that process messages.</li></ol><p>This design separates concerns cleanly:</p><ul><li><strong>The persistence layer</strong> ensures database safety and handles deduplication</li><li><strong>The message handler</strong> contains business logic for processing messages</li><li><strong>The worker pool</strong> manages concurrency and resource allocation</li></ul><p>Let’s explore each component in detail.</p><h3>Core Components</h3><h4>Worker Pool Architecture</h4><pre>// MessageProcessor manages a pool of workers that process stored messages<br>type MessageProcessor struct {<br>  node noderole.Node // server node id (optional)<br>  messageHandler IMessageHandler // business logic processor<br>  persistenceService MessagePersistenceService// database operations<br>  event string // event type to process<br>  pollInterval time.Duration // active polling frequency<br>  idleInterval time.Duration // idle polling frequency<br>  minWorkers int // minimum workers to maintain<br>  maxWorkers int // maximum workers allowed<br>  activeWorkers int // currently active workers<br>  stopChan chan struct{} // signal to stop processing<br>  workerChans []chan struct{} // individual worker stop channels<br>  workerStatus []bool // worker busy status<br>  mu sync.Mutex // protects shared state<br>  logger *logger.ContextLogger // logging service<br>  isIdle bool // current activity state<br>}</pre><h4>Message Handler Interface</h4><pre>// IMessageHandler defines the interface for handling different message types<br>type IMessageHandler interface {<br> Handle(messageID int64, event string, payload []byte) error<br>}</pre><p>This allows different message types to have specialized processing logic while using the same worker infrastructure.</p><h4>Persistence Service Interface</h4><pre>// MessagePersistenceService defines the interface for message storage operations<br>type MessagePersistenceService interface {<br>  StoreMessage(msg ProcessingMessageInfo) (bool, error)<br>  ClaimMessage(event, updatedBy string) (*ClaimedMessage, error)<br>  MarkAsSuccess(msgID int64) error<br>  MarkAsError(msgID int64, errMsg string) error<br>  GetBacklogSize(event string) (int, error)<br>  CleanupOldMessages(event string) (int64, error)<br>}</pre><h3>Concurrent Processing with Pessimistic Locking</h3><p>The core of our solution is pessimistic locking at the database level:</p><pre>// ClaimMessage locks and returns a message for processing<br>func (s *DefaultMessagePersistenceService) ClaimMessage(event, updatedBy string) (*ClaimedMessage, error) {<br>  var msg ClaimedMessage<br>  err := s.db.QueryRow(`<br>    UPDATE yourappschema.message_processing_log<br>    SET locked_until = NOW() + INTERVAL &#39;15 minutes&#39;,<br>    updated_by = ?, <br>    update_ts = NOW()<br>    WHERE id = (<br>      SELECT id FROM yourappschema.message_processing_log<br>      WHERE message_key IS NOT NULL<br>        AND event = ?<br>        AND status = &#39;pending&#39;<br>        AND (locked_until IS NULL OR locked_until &lt; NOW())<br>      ORDER BY create_ts<br>      LIMIT 1<br>      FOR UPDATE SKIP LOCKED<br>    )<br>    RETURNING id, event, payload`, <br>    updatedBy, event).Scan(&amp;msg.ID, &amp;msg.Event, &amp;msg.Payload)<br>  <br>  if err != nil {<br>    if err == sql.ErrNoRows {<br>      return nil, nil // No messages to process<br>    }<br>    return nil, err<br>  }<br>  return &amp;msg, nil<br>}</pre><p>This approach uses several PostgreSQL features to ensure safe concurrent processing:</p><p>1.FOR UPDATE SKIP LOCKED: Allows multiple workers to claim different messages simultaneously without blocking<br>2. locked_until timestamp: Prevents other workers from claiming the same message<br>3. update_ts and updated_by: Tracks which worker/server is processing each message</p><h3>Worker Implementation Details</h3><h4>Worker Lifecycle Management</h4><pre>// startWorker creates a new worker goroutine<br>func (mp *MessageProcessor) startWorker(id int) {<br>  mp.logger.Infof(&quot;Starting worker %d for %s&quot;, id+1, mp.event)<br>  // Create channel for signaling the worker to stop<br>  stopChan := make(chan struct{})<br>  mp.workerChans[id] = stopChan<br>  mp.workerStatus[id] = true<br>  // Start the worker goroutine<br>  go mp.worker(id, stopChan)<br>}<br><br>// stopWorker signals a worker to stop<br>func (mp *MessageProcessor) stopWorker(id int) {<br>  if mp.workerChans[id] != nil {<br>    mp.logger.Infof(&quot;Stopping worker %d for %s&quot;, id+1, mp.event)<br>    close(mp.workerChans[id])<br>    mp.workerChans[id] = nil<br>    mp.workerStatus[id] = false<br>  }<br>}<br><br>// worker is the long-running goroutine that processes messages<br>func (mp *MessageProcessor) worker(id int, stopChan &lt;-chan struct{}) {<br>  workerName := fmt.Sprintf(&quot;Worker-%d&quot;, id+1)<br>  ticker := time.NewTicker(mp.pollInterval)<br>  defer ticker.Stop()<br>  for {<br>    select {<br>      case &lt;-stopChan:<br>        // Worker has been signaled to stop<br>        return<br>      case &lt;-ticker.C:<br>        // Try to process the next message<br>        if err := mp.processNextMessage(workerName, id); err != nil {<br>          mp.logger.Errorf(&quot;%s error: %v&quot;, workerName, err)<br>        }<br>    }<br>  }<br>}</pre><h3>Message Processing Flow</h3><pre>// processNextMessage tries to claim and process a message<br>func (mp *MessageProcessor) processNextMessage(workerName string, id int) error {<br>  // Create unique worker identifier<br>  updatedBy := fmt.Sprintf(&quot;%s Handler@%s-%d&quot;, mp.event, mp.node.NodeID, id+1)<br>  // Try to claim a message<br>  claimedMsg, err := mp.persistenceService.ClaimMessage(mp.event, updatedBy)<br>  if err != nil {<br>    return fmt.Errorf(&quot;failed to claim message: %w&quot;, err)<br>  }<br><br>  // No message available to process<br>  if claimedMsg == nil {<br>    return nil<br>  }<br><br>  // Process the message using the handler<br>  err = mp.messageHandler.Handle(claimedMsg.ID, claimedMsg.Event, <br>    []byte(claimedMsg.Payload))<br><br>  if err != nil {<br>    // Mark as error and log failure<br>    mp.persistenceService.MarkAsError(claimedMsg.ID, err.Error())<br>    return fmt.Errorf(&quot;failed to process message %d: %w&quot;, claimedMsg.ID, err)<br>  } <br>  <br>  // Mark message as successfully processed<br>  if err := mp.persistenceService.MarkAsSuccess(claimedMsg.ID); err != nil {<br>    return fmt.Errorf(&quot;failed to mark message %d as success: %w&quot;, <br>      claimedMsg.ID, err)<br>  }<br>  return nil<br>}</pre><h3>Results and Benefits</h3><p>After implementing this two-phase approach, we observed significant improvements:</p><ol><li><strong>Perfect Idempotency</strong>: Even with Kafka delivering duplicates, each message is processed exactly once</li><li><strong>Efficient Resource Usage</strong>: Worker count dynamically adjusts to processing needs</li><li><strong>Linear Scalability</strong>: Multiple server instances cooperate seamlessly to process the workload</li><li><strong>Operational Visibility</strong>: Clear tracking of message processing state</li><li><strong>Resilience</strong>: The system recovers automatically from failures and crashes</li></ol><p>The auto-scaling capability proved particularly valuable during peak processing times. For example, when we loaded 150 records into the queue:</p><ol><li>The system automatically scaled from 2 to configured N workers across multiple server instances</li><li>Processing throughput increased by 4x during the peak load period</li><li>Once the backlog was cleared, the system scaled back to the minimum worker count</li><li>No duplicate processing occurred, even with multiple server instances running</li></ol><h3>Conclusion</h3><p>The two-phase approach to Kafka message processing solves a fundamental tension in distributed systems: how to achieve exactly-once processing semantics in a system with at-least-once delivery guarantees.</p><p>By separating consumption from processing, we gain:</p><ol><li><strong>Safety</strong>: Perfect deduplication through database constraints</li><li><strong>Scalability</strong>: Efficient resource utilization through dynamic worker pools</li><li><strong>Resilience</strong>: Automatic recovery from failures</li><li><strong>Visibility</strong> Clear tracking of message state</li></ol><p>This pattern can be adapted to any messaging system where both reliability and efficiency are required. Rather than fighting the inherent challenges of distributed systems, it embraces them and provides an elegant solution that scales with your needs.</p><p>Remember: In distributed systems, design for resilience, efficiency, and idempotency. The approach described here provides all three.</p><p>As final note, I’ve added details on Dynamic Resource Management (auto-scaling of the workers) &amp; Periodic Database Cleanup for more detailed key points of the solution.</p><h3>Apendix A — Dynamic Resource Management</h3><h4>Monitoring Queue Backlog</h4><p>The key to efficient resource utilization is knowing when to scale up or down:</p><pre>// manageWorkerPool adjusts the number of workers based on queue size<br>func (mp *MessageProcessor) manageWorkerPool() {<br>  ticker := time.NewTicker(30 * time.Second) // Check scaling every 30 seconds<br>  defer ticker.Stop()<br>  for {<br>    select {<br>      case &lt;-ticker.C:<br>        // Get current backlog size through the persistence layer<br>        backlogSize, err := mp.persistenceService.GetBacklogSize(mp.event)<br>        if err != nil {<br>          mp.logger.Errorf(&quot;Failed to get backlog size: %v&quot;, err)<br>          continue<br>        }<br>        // Adjust worker count and polling frequency based on load<br>        mp.adjustWorkerCount(backlogSize)<br>        mp.adjustPollingInterval(backlogSize)<br><br>      case &lt;-mp.stopChan:<br>        return<br>    }<br>  }<br>}</pre><p>The persistence service implements the backlog query:</p><pre>// GetBacklogSize returns count of pending messages for an event<br>func (s *DefaultMessagePersistenceService) GetBacklogSize(event string) (int, error) {<br>  var count int<br>  err := s.db.QueryRow(`<br>    SELECT COUNT(*)<br>    FROM yourappschema.message_processing_log<br>    WHERE message_key IS NOT NULL<br>      AND event = ?<br>      AND status = &#39;pending&#39;<br>      AND locked_until IS NULL<br>    `, event).Scan(&amp;count)<br><br>  if err != nil {<br>    return 0, fmt.Errorf(&quot;failed to get backlog size: %w&quot;, err)<br>  }<br>  return count, nil<br>}</pre><h3>Auto-Scaling Worker Pool</h3><p>Based on the monitored backlog, we dynamically adjust worker count:</p><pre>// adjustWorkerCount scales workers up or down based on backlog<br>func (mp *MessageProcessor) adjustWorkerCount(backlogSize int) {<br>  mp.mu.Lock()<br>  defer mp.mu.Unlock()<br>  // Count active workers<br>  activeCount := 0<br>  for i := 0; i &lt; mp.maxWorkers; i++ {<br>    if mp.workerChans[i] != nil {<br>      activeCount++<br>    }<br>  }<br>  <br>  mp.activeWorkers = activeCount<br><br>  // Scale up: Add workers if backlog exists<br>  if backlogSize &gt; 0 {<br>    // Determine target worker count based on backlog size<br>    targetWorkers := mp.minWorkers<br>    if backlogSize &gt; 100 { // ← adjust backlog size numbers to your needs<br>      targetWorkers = mp.minWorkers + 2 <br>    }<br>    if backlogSize &gt; 500 {<br>       targetWorkers = mp.maxWorkers<br>    }<br>    // Start new workers if needed<br>    if targetWorkers &gt; mp.activeWorkers {<br>      for i := 0; i &lt; mp.maxWorkers &amp;&amp; activeCount &lt; targetWorkers; i++ {<br>        if mp.workerChans[i] == nil {<br>          mp.startWorker(i)<br>          activeCount++<br>        }<br>      }<br>    }<br>  } else if backlogSize == 0 &amp;&amp; mp.activeWorkers &gt; mp.minWorkers {<br>    // Scale down: Keep only min workers if no backlog<br>    for i := 0; i &lt; mp.maxWorkers &amp;&amp; activeCount &gt; mp.minWorkers; i++ {<br>      if mp.workerChans[i] != nil {<br>        mp.stopWorker(i)<br>        activeCount - <br>      }<br>    }<br>  }<br>}</pre><h4>Adaptive Polling Intervals</h4><p>To optimize resource usage further, our system adjusts its polling frequency based on load:</p><pre>// adjustPollingInterval changes polling frequency based on queue state<br>func (mp *MessageProcessor) adjustPollingInterval(backlogSize int) {<br>  if backlogSize &gt; 0 &amp;&amp; mp.isIdle {<br>    // Switch to active mode - poll more frequently<br>    mp.isIdle = false<br>    mp.logger.Infof(&quot;Switch to active polling mode (every %d sec, backlog: %d)&quot;,<br>    int(mp.pollInterval.Seconds()), backlogSize)<br>  } else if backlogSize == 0 &amp;&amp; !mp.isIdle {<br>    // Switch to idle mode - poll less frequently to reduce DB load<br>    mp.isIdle = true <br>    mp.logger.Infof(&quot;Switch to idle polling mode (every %d sec, no backlog)&quot;,<br>    int(mp.idleInterval.Seconds()))<br>  }<br>}<br><br>// getCurrentInterval returns the appropriate polling interval based on current state<br>func (mp *MessageProcessor) getCurrentInterval() time.Duration {<br> if mp.isIdle {<br>   return mp.idleInterval // Slower polling when idle (e.g., 60 seconds)<br> }<br> return mp.pollInterval // Faster polling when active (e.g., 5 seconds)<br>}</pre><h3>Apendix B — Database Maintenance and Cleanup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-3wDYICSbJlQuj_q" /><figcaption>Photo by <a href="https://unsplash.com/@blueshift12?utm_source=medium&amp;utm_medium=referral">Sam LaRussa</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>To prevent database bloat, we implement a cleanup routine:</p><pre>// CleanupOldMessages removes old processed messages<br>func (s *DefaultMessagePersistenceService) CleanupOldMessages(event string) (int64, error) {<br>  res, err := s.db.Exec(`<br>    DELETE FROM yourappschema.message_processing_log <br>    WHERE status = &#39;success&#39; <br>     AND message_key IS NOT NULL<br>     AND event = ?<br>     AND create_ts &lt; NOW() - INTERVAL &#39;7 days&#39;<br>    `, event)<br>  if err != nil {<br>    return 0, err<br>  } <br>  return res.RowsAffected()<br>}</pre><p>This cleanup is executed periodically by a cron job with safety mechanisms:</p><pre>func safeCleanupOldMessages(ctx context.Context, event string) {<br>  defer func() {<br>  if r := recover(); r != nil {<br>    ctx.Log.Errorf(&quot;Recovered from panic in message cleanup: %v&quot;, r)<br>   }<br>  }()<br><br>  persistenceService := services.NewMessagePersistenceService()<br>  rowsAffected, err := persistenceService.CleanupOldMessages(event)<br><br>  if err != nil {<br>    ctx.Log.Errorf(&quot;Failed to clean up old messages (event=%s): %v&quot;, event, err)<br>    return<br>  }<br><br>  if rowsAffected &gt; 0 {<br>     ctx.Log.Infof(&quot;Successfully cleaned up %d old messages for event=%s&quot;, <br>       rowsAffected, event)<br>  }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5b076870564" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taming Duplicate Kafka Messages with Idempotent Processing (Part 1)]]></title>
            <link>https://eliasmsedano.medium.com/taming-duplicate-kafka-messages-with-idempotent-processing-part-1-2d46db9e54c3?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d46db9e54c3</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[message-queue]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Wed, 23 Apr 2025 00:50:16 GMT</pubDate>
            <atom:updated>2025-04-23T00:54:17.416Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In modern distributed systems, message queues like Kafka, SNS, SQS have become essential infrastructure components. While these technologies excel at handling high message volumes and providing fault tolerance, they share a common challenge: duplicate messages. This article explores why duplicates occur, particularly in complex processing scenarios, and presents the first part of our solution — idempotent message storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RCKjL0TDpUCiBumP" /><figcaption>Photo by <a href="https://unsplash.com/@steve_j?utm_source=medium&amp;utm_medium=referral">Steve Johnson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The Duplicate Message Challenge</h3><h4>Why Duplicates Happen</h4><p>Tools like Kafka offers <em>at-least-once</em> delivery guarantees, not <em>exactly-once</em> semantics. This design choice creates several duplicate message scenarios:</p><p><strong>1. Producer Retries</strong>: When a producer doesn’t receive an acknowledgment within the timeout period, it retries sending the message, potentially creating duplicates if the original was actually processed.<br><strong>2. Consumer Failures</strong>: If a consumer processes a message but crashes before committing its offset, the message will be redelivered to another consumer in the group.<br><strong>3. Rebalancing Events</strong>: When a consumer group rebalances (e.g., when a consumer joins/leaves), in-flight messages might be reassigned and processed twice.<br><strong>4. Manual Offset Management Issues:</strong> Improper offset management can lead to message reprocessing after restarts.</p><h4>The External Resource Complexity</h4><p>The duplicate problem becomes particularly challenging in systems with complex processing requirements:</p><ol><li><strong>External API Dependencies:</strong> When message processing depends on external APIs that may be slow or temporarily unavailable.</li><li><strong>Database Transaction Timing:</strong> Long-running database operations may exceed consumer timeout settings.</li><li><strong>Asynchronous Processing: </strong>When complete message processing requires multiple stages that can’t be completed atomically.</li><li><strong>Processing Timeouts:</strong> Default processing timeout settings might be too short for complex operations, forcing offset commits before processing completes.</li></ol><p>I don’t know where I read/heard but here is the quote:</p><blockquote>”In distributed systems, duplicates aren’t an error condition — they’re an inevitability you must design for.”</blockquote><h3>Business Impact of Duplicate Processing</h3><p>Duplicate processing can cause significant problems:</p><ul><li><strong>Data Inconsistencies:</strong> Updated values may be overwritten with stale data</li><li><strong>Redundant Operations:</strong> Wasted resources on unnecessary computations</li><li><strong>Corrupted Aggregations</strong>: Incorrect counts, sums, or other aggregate values</li><li><strong>Trigger Cascades</strong>: Duplicated events triggering multiple downstream processes</li></ul><h3>The Idempotent Table Pattern</h3><p>Rather than fighting against the physics of distributed systems, we implemented an idempotent processing pattern with two key phases:</p><ol><li><strong>Phase 1:</strong> Consume and store messages with deduplication (covered in this article)</li><li><a href="https://eliasmsedano.medium.com/part-2-efficient-message-processing-with-dynamic-go-worker-pools-f5b076870564"><strong>Phase 2:</strong> Process stored messages with worker pools (covered in the next article</a>)</li></ol><h3>Key Components of the Solution</h3><p>Our solution consists of:</p><ol><li><strong>Message Fingerprinting</strong>: Generating unique identifiers for deduplication</li><li><strong>Idempotent Table:</strong> Database table for tracking processed messages</li><li><strong>Persistence Layer:</strong> Abstracting database operations</li></ol><p>Let’s examine each component:</p><h3>Message Fingerprinting</h3><p>To identify duplicates, we generate a unique fingerprint for each message:</p><pre>// YourMessageClaim represents a message to be processed<br>type YourMessageClaim struct {<br>  ID int64<br>  Event string<br>  // or whatever your ingestion messages looks like<br>}<br><br>// GenerateMessageKey creates a unique identifier for idempotent processing<br>func GenerateMessageKey(message *YourMessageClaim) string {<br>  // Combine event type and ID for uniqueness<br>  data := fmt.Sprintf(&quot;%s:%d&quot;, message.Event, message.ID)<br>  // Create SHA-256 hash for fixed length key<br>  hash := sha256.Sum256([]byte(data))<br>  return hex.EncodeToString(hash[:])<br>}</pre><p>This approach has several advantages:</p><ul><li><strong>Deterministic</strong>: The same message always produces the same key.</li><li><strong>Collision-resistant</strong>: SHA-256 provides extremely low collision probability.</li><li><strong>Compact</strong>: Fixed-length representation regardless of message size.</li></ul><h3>The Idempotent Table Schema</h3><p>You have to design a dedicated shared resources among your servers &amp; workers (threads), table to track message processing, e.g:</p><pre>CREATE TABLE yourappschema.message_processing_log (<br> id SERIAL PRIMARY KEY,<br> message_key VARCHAR(255) NOT NULL UNIQUE, - unique fingerprint<br> event VARCHAR(100) NOT NULL, - message type/topic<br> payload TEXT NOT NULL, - original message<br> status VARCHAR(20) DEFAULT &#39;pending&#39;, - processing state<br> response TEXT, - error details if failed<br> locked_until TIMESTAMP, - pessimistic locking<br> create_ts TIMESTAMP DEFAULT NOW(),<br> update_ts TIMESTAMP DEFAULT NOW(),<br> create_by VARCHAR(100), - node server that logged the message from Kafka<br> updated_by VARCHAR(100) - node/worker ID that processed the message<br>);<br>CREATE INDEX idx_msg_key ON yourappschema.message_processing_log(message_key);<br>CREATE INDEX idx_status_event ON yourappschema.message_processing_log(status, event);</pre><p>This schema provides:</p><ul><li><strong>Deduplication</strong>: The UNIQUE constraint on message_key prevents duplicate entries.</li><li><strong>State Tracking</strong>: The status field tracks message processing lifecycle.</li><li><strong>Error Handling</strong>: The response field captures failure details.</li><li><strong>Audit Trail</strong>: Timestamps and processing information for diagnostics.</li><li><strong>Performance</strong>: Indexes support efficient lookups for processing.</li></ul><p>You can go and be creative on what you want to store, but the key point is message id, an attribute to store the event’s payload and the status so you can go back, query and refer on what went well and wrong.</p><h3>Persistence Service for Message Storage</h3><p>We created a dedicated persistence service to abstract database operations:</p><pre>// ProcessingMessageInfo contains the data needed to store a message<br>type ProcessingMessageInfo struct {<br>  MessageKey string<br>  Event string<br>  Payload string<br>  CreateBy string<br>}<br><br>// MessagePersistenceService defines the interface for message storage operations<br>type MessagePersistenceService interface {<br>  StoreMessage(msg ProcessingMessageInfo) (bool, error)<br>  // Other methods for claiming and processing messages will <br>  // be covered in Part 2<br>}<br><br>// DefaultMessagePersistenceService implements the persistence operations<br>type DefaultMessagePersistenceService struct {<br>  db DatabaseExecutor<br>}</pre><h3>Storing Messages with Deduplication</h3><pre>// StoreMessage adds a message to the processing queue if not already present<br>func (s *DefaultMessagePersistenceService) StoreMessage(msg ProcessingMessageInfo) (bool, error) {<br>  // Try to insert, catching unique constraint violations<br>  _, err := s.db.Exec(`<br>     INSERT INTO yourappschema.message_processing_log <br>     (message_key, event, payload, status, create_by)<br>     VALUES (?, ?, ?, &#39;pending&#39;, ?)<br>     ON CONFLICT (message_key) DO NOTHING<br>    `, msg.MessageKey, msg.Event, msg.Payload, msg.CreateBy)<br>  <br>  if err != nil {<br>    return false, err<br>  }<br><br>  // Check if we actually inserted (affected rows = 1) or found a duplicate<br>  var count int<br>  err = s.db.QueryRow(`<br>   SELECT COUNT(*) FROM yourappschema.message_processing_log <br>   WHERE message_key = ? AND create_by = ?`, <br>   msg.MessageKey, msg.CreateBy).Scan(&amp;count)<br><br>  return count &gt; 0, err<br>}</pre><p>This implementation uses the SQL ON CONFLICT DO NOTHING pattern to handle duplicates elegantly. The function returns true if the message was newly inserted, and false if it was a duplicate (allowing the caller to skip further processing if desired).</p><h3>Kafka Consumer Implementation</h3><p>Our Kafka consumer handles the first phase of processing — consuming messages and storing them with deduplication:</p><pre>func (c *KafkaConsumer) ConsumeAndStore(ctx context.Context, topic string) error {<br>  consumer, err := c.consumerGroup.NewConsumer(topic)<br>  if err != nil {<br>    return fmt.Errorf(&quot;failed to create consumer: %w&quot;, err)<br>  }<br>  defer consumer.Close()<br>  for {<br>    select {<br>       case &lt;-ctx.Done():<br>        return ctx.Err()<br>       case msg := &lt;-consumer.Messages():<br>       // Parse the message<br>       var claim YourMessageClaim<br>       if err := json.Unmarshal(msg.Value, &amp;claim); err != nil {<br>       c.logger.Errorf(&quot;Failed to unmarshal message: %v&quot;, err)<br>       // Mark even failed messages as processed<br>       consumer.MarkOffset(msg) <br>       continue<br>    }<br>    // Generate unique key<br>    messageKey := GenerateMessageKey(&amp;claim)<br>    // Store with deduplication<br>    inserted, err := c.persistenceService.StoreMessage(<br>      ProcessingMessageInfo{<br>      MessageKey: messageKey,<br>      Event: claim.Event,<br>      Payload: string(msg.Value),<br>      CreateBy: fmt.Sprintf(&quot;MyTopicConsumer@%s&quot;, c.nodeID),<br>    })<br>    if err != nil {<br>       c.logger.Errorf(&quot;Failed to store message: %v&quot;, err)<br>    } else if !inserted {<br>      c.logger.Debugf(&quot;Duplicate message, skipped: %s&quot;, messageKey)<br>    } else {<br>       c.logger.Infof(&quot;Successfully stored message: %s&quot;, messageKey)<br>    }<br>    // Always mark as processed since we&#39;ve either stored it<br>    // or it was a duplicate<br>    consumer.MarkOffset(msg)<br>    }<br>  }<br>}</pre><p>This consumer implementation:</p><ol><li><strong>Receives</strong> messages from Kafka</li><li><strong>Parses</strong> each message</li><li>Generates a <strong>unique</strong> fingerprint</li><li>Attempts to <strong>store</strong> the message with deduplication</li><li><strong>Marks</strong> the offset regardless of outcome (since duplicates are handled by the database)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eksfnOUPDoHKlKz6Gk4KTg.png" /></figure><h4>Benefits of Separated Consumption and Processing</h4><p>Breaking the solution into separate consumption and processing phases provides several advantages:</p><ol><li><strong>Fast Consumption</strong>: Messages are quickly consumed and stored, avoiding consumer timeout issues.</li><li><strong>Perfect Deduplication</strong>: The database’s UNIQUE constraint guarantees no duplicates.</li><li><strong>Reliable Processing</strong>: Failed processing can be retried without message loss.</li><li><strong>Horizontal Scalability</strong>: Multiple consumers can safely consume without duplication concerns.</li><li><strong>Operational Visibility</strong>: Full audit trail of message processing status</li></ol><h3>What’s Next</h3><p><a href="https://eliasmsedano.medium.com/part-2-efficient-message-processing-with-dynamic-go-worker-pools-f5b076870564">In Part 2</a>, we’ll explore how to efficiently process the deduplicated messages using:</p><ul><li>A worker pool implementation with auto-scaling</li><li>Pessimistic locking for concurrent processing</li><li>Error handling and retry strategies</li><li>Performance tuning of the worker pool</li><li>Database maintenance and cleanup procedures</li></ul><p>We’ll show how this approach allows multiple server instances to process messages concurrently without introducing duplicate processing, while dynamically scaling to match workload demands.</p><p>This two-phase approach allows us to solve the inherent tension between Kafka’s at-least-once delivery model and the need for exactly-once processing semantics in business applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d46db9e54c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interface Composition in Go: A Small Practical Guide]]></title>
            <link>https://eliasmsedano.medium.com/interface-composition-in-go-a-small-practical-guide-5d034b7e66fb?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d034b7e66fb</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 06:19:37 GMT</pubDate>
            <atom:updated>2025-03-05T06:32:32.099Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Interface composition is a powerful feature in Go that allows developers to build complex interfaces from smaller, more focused ones. Let’s explore this concept using a real-world example of database operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Dleam30yczZZW5FI" /><figcaption>Photo by <a href="https://unsplash.com/@macphoto?utm_source=medium&amp;utm_medium=referral">Mauricio Artieda</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Basic Building Blocks</h3><p>In our persistence layer, we start with small, focused interfaces:</p><pre>// Basic CRUD operations<br>type ICanCreate[I any] interface {<br>  Create(payload I) error<br>}<br>type ICanGet[I any, O any] interface {<br>  Get(val I) (*O, error)<br>}<br>type ICanDelete[I any] interface {<br>  Delete(id any) error<br>}<br>type ICanUpdate[I any] interface {<br>  Update(id any, payload I) error<br>}go</pre><h3>Composing Interfaces</h3><p>We can combine these basic interfaces to create more comprehensive ones:</p><pre>// Complete CRUD Repository<br>type IRepository[I any] interface {<br>  ICanCreate[I]<br>  ICanGet[I, I]<br>  ICanUpdate[I]<br>  ICanDelete[I]<br>}<br>// Enhanced Repository with Search Capabilities<br>type ISearchableRepository[I any] interface {<br>  IRepository[I]<br>  ICanSearch[I, I]<br>  ICanSearchBy[I]<br>  ICanCount[I]<br>}</pre><h4>Practical Implementation</h4><p>Here’s how you might implement these interfaces in a real application:</p><pre>type User struct {<br>  ID string<br>  Email string<br>  Name string<br>}<br><br>type UserRepository struct {<br>  db *sql.DB<br>}<br><br>// Implementing IRepository methods<br>func (r *UserRepository) Create(user User) error {<br>  // Insert user into database<br>  return nil<br>}<br><br>func (r *UserRepository) Get(id User) (*User, error) {<br>  // Fetch user from database<br>  return &amp;User{}, nil<br>}</pre><h3>Benefits of Interface Composition</h3><ol><li><strong>Modularity</strong>: Each interface has a single responsibility</li><li><strong>Flexibility</strong>: Pick and choose only the interfaces you need</li><li><strong>Maintainability</strong>: Easier to test and modify individual components</li><li><strong>Reusability</strong>: Small interfaces can be reused across different types</li></ol><h3>Best Practices</h3><ol><li>Keep interfaces small and focused</li><li>Compose larger interfaces from smaller ones</li><li>Follow the Interface Segregation Principle</li><li>Use generic parameters to maintain type safety</li><li>Document interface purposes clearly</li></ol><h3>Not convinced? Let me illustrate interface composition with a humorous example:</h3><p>Composition vs Inheritance (aka Embedding): A Tale of RoboVacuumDog 🤖🧹🐕</p><pre>package examples<br>// Basic capabilities<br>type ICanBark interface {<br>  Bark() string<br>}<br>type ICanVacuum interface {<br>  Clean() error<br>  EmptyDustBin() error<br>}<br><br>type ICanCompute interface {<br>  ProcessCommands() error<br>  ChargeBattery() int<br>}<br><br>// Our lovable components<br>type Dog struct {<br>  Name string<br>}<br>func (d *Dog) Bark() string {<br>  return &quot;Woof! Your floor is dirty!&quot;<br>}<br><br>// the vacuum<br>type Vacuum struct {<br>  DustLevel int<br>}<br>func (v *Vacuum) Clean() error {<br>  v.DustLevel-- <br>  return nil<br>}<br>func (v *Vacuum) EmptyDustBin() error {<br>  v.DustLevel = 0<br>  return nil<br>}<br><br>// the robot<br>type Robot struct {<br>  BatteryLevel int<br>}<br>func (r *Robot) ProcessCommands() error {<br>  return nil<br>}<br>func (r *Robot) ChargeBattery() int {<br>  r.BatteryLevel = 100<br>  return r.BatteryLevel<br>}<br><br>// The magic of composition!<br>type RoboVacuumDog struct {<br> Dog<br> Vacuum<br> Robot<br>}<br><br>// Usage example<br>func Example() {<br>  roboVacuumDog := &amp;RoboVacuumDog{<br>  Dog: Dog{Name: &quot;Sparky&quot;},<br>  Vacuum: Vacuum{DustLevel: 0},<br>  Robot: Robot{BatteryLevel: 100},<br>  }<br>  <br>  // Now our RoboVacuumDog can:<br>  roboVacuumDog.Bark() // Alerts you about dirty floors<br>  roboVacuumDog.Clean() // Cleans them<br>  roboVacuumDog.ChargeBattery() // Recharges itself<br>}</pre><h3>Why This is Better Than infamous “Inheritance”</h3><p>With inheritance (Embedding, understood!), we’d have to create a nightmare hierarchy like:</p><pre>Robot -&gt; CleaningRobot -&gt; BarkingCleaningRobot</pre><p><strong>Problems with embedding/inheritance:</strong></p><ol><li>What if we want a RoboVacuumCat later? We&#39;d need a whole new hierarchy</li><li>“Is-a” relationships become confusing (Is it a Robot that barks or a Dog that computes? 🤔)</li><li>We can’t pick and choose capabilities — it’s all or nothing</li></ol><p><strong>With composition:</strong></p><ol><li>Mix and match capabilities as needed</li><li>Each component has a clear, single responsibility</li><li>Easy to test each component independently</li><li>Can create new combinations without breaking existing code</li><li>When your RoboVacuumDog malfunctions, at least you know if it’s the robot part, the vacuum part, or the dog part! 😄</li></ol><p>The moral of the story: “Favor composition over inheritance” isn’t just a principle — it’s a way to keep your code as flexible as a robot doing downward dog in a yoga class! 🧘‍♂️🤖</p><h3>Conclusion</h3><p>Interface composition in Go provides a clean and flexible way to build complex behavior from simple components. This approach leads to more maintainable and testable code while following solid software engineering principles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d034b7e66fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Crafting” High-Quality User Stories with Gherkin Acceptance Criteria]]></title>
            <link>https://eliasmsedano.medium.com/crafting-high-quality-user-stories-with-gherkin-acceptance-criteria-a8ab959ade1c?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8ab959ade1c</guid>
            <category><![CDATA[agile]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[prompt-engineering]]></category>
            <category><![CDATA[user-stories]]></category>
            <category><![CDATA[product-manager]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Thu, 20 Feb 2025 21:30:23 GMT</pubDate>
            <atom:updated>2025-02-20T21:30:23.702Z</atom:updated>
            <content:encoded><![CDATA[<p>In the breakneck world of software development, clear user stories are as vital as coffee on a Monday morning. They’re the bridge between what the product owner <em>thinks</em> they want and what the developer <em>actually</em> builds (which, let’s be honest, sometimes resembles a bridge made of popsicle sticks). One way to minimize the popsicle stick bridge incidents is by using Gherkin language for acceptance criteria. This article provides some prompt templates to help you generate high-quality user stories, making it easier for your team to stay aligned and productive. Or at least, less misaligned and productively confused.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VNWk16CIxXaDwPCO" /><figcaption>Photo by <a href="https://unsplash.com/@enginakyurt?utm_source=medium&amp;utm_medium=referral">engin akyurt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Why Use Gherkin Language?</h4><p>Gherkin is a business-readable, domain-specific language that lets you describe software behavior without diving into the nitty-gritty implementation details. It uses a simple syntax of Given, When, Then — kind of like a choose-your-own-adventure book for developers, but with less dragons and more debugging. It’s designed to be understandable by both technical and non-technical stakeholders, which is crucial because sometimes, the only thing less understandable than code is a product owner’s explanation of what they <em>really</em> want.</p><h4>How to Use These Templates</h4><ol><li>Open your favorite Gen AI platform (Copilot, ChatGPT, Bard, or whichever digital oracle you consult).</li><li>Choose the template that best matches the complexity of your requirement description. (Template 1 is for when you <em>think</em> you know what you want. Template 4 is for when you <em>definitely</em> don’t.)</li><li>Insert the requirement description into the template. (Remember to be specific! “Make it good” is not a requirement description. It’s a cry for help.)</li><li>Customize the prompt to fit the specific context of your project. (Because every project is unique, just like every snowflake… and every bug.)</li><li>Review and refine the generated user story and acceptance criteria. (Because sometimes, even Gen AI needs a second opinion. Or a third. Or a fourth. It’s a process.)</li></ol><h4>Template 1: Basic User Story</h4><p><strong>Usage:</strong> Use this template for straightforward requirements where a basic user story and acceptance criteria are sufficient.</p><p><strong>Template:</strong></p><pre># Task:<br>Create a user story based on the following requirement description: [Insert Requirement Description].<br><br># Example:<br>Requirement Description: A user needs to be able to log in to the system using their email and password.<br>User Story:<br>As a [type of user], I want to [perform some task] so that [achieve some goal].<br>Acceptance Criteria:<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br><br># Context:<br>- You are the product manager writing stories for a software development team.<br>- Application name is Silo (aka silo site).<br>- Silo is a real estate application that allows its users to search and create listings of properties for lease or sale.<br>- Silo user roles are: admins, users, landlors, brokers, so use them for different perspectives.<br>- The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).</pre><h4>Template 2: Detailed User Story with Preconditions</h4><p><strong>Usage:</strong> Use this template when the requirement includes specific preconditions that need to be met before the action can be performed.</p><p><strong>Template:</strong></p><pre># Task:<br>Generate a user story from the following requirement description: [Insert Requirement Description]. The user story should include a detailed description and acceptance criteria in Gherkin language (Given, When, Then), including any preconditions.<br><br># Example:<br>Requirement Description: A user needs to be able to reset their password if they forget it.<br>User Story:<br>As a [type of user], I want to [perform some task] so that [achieve some goal].<br>Acceptance Criteria:<br>- Given [some precondition], when [some action is carried out], then [a particular set of observable outcomes should occur].<br>- Given [some precondition], when [some action is carried out], then [a particular set of observable outcomes should occur].<br><br># Context:<br>- You are the product manager writing stories for a software development team.<br>- Application name is Silo (aka silo site).<br>- Silo is a real estate application that allows its users to search and create listings of properties for lease or sale.<br>- Silo user roles are: admins, users, landlors, brokers, so use them for different perspectives.<br>- The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).</pre><h4>Template 3: User Story with Multiple Scenarios</h4><p><strong>Usage:</strong> Use this template when the requirement involves multiple scenarios that need to be covered in the acceptance criteria.</p><p><strong>Template:</strong></p><pre># Task:<br>Create a user story from the following requirement description: [Insert Requirement Description]. The user story should include a description and multiple acceptance criteria scenarios written in Gherkin language (Given, When, Then).<br><br># Example:<br>Requirement Description: A user needs to be able to search for properties by location and price range.<br>User Story:<br>As a [type of user], I want to [perform some task] so that [achieve some goal].<br>Acceptance Criteria:<br>Scenario 1:<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br>Scenario 2:<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br><br># Context:<br>- You are the product manager writing stories for a software development team.<br>- Application name is Silo (aka silo site).<br>- Silo is a real estate application that allows its users to search and create listings of properties for lease or sale.<br>- Silo user roles are: admins, users, landlors, brokers, so use them for different perspectives.<br>- The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).</pre><h4>Template 4: User Story with Edge Cases</h4><p><strong>Usage:</strong> Use this template to ensure that both normal and edge cases are covered in the acceptance criteria.</p><p><strong>Template:</strong></p><pre># Task:<br>Generate a user story from the following requirement description: [Insert Requirement Description]. The user story should include a description and acceptance criteria in Gherkin language (Given, When, Then), covering both normal and edge cases.<br><br># Example:<br>Requirement Description: A user needs to be able to update their profile information.<br>User Story:<br>As a [type of user], I want to [perform some task] so that [achieve some goal].<br>Acceptance Criteria:<br>Normal Case:<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br>Edge Case:<br>- Given [some context], when [some action is carried out], then [a particular set of observable outcomes should occur].<br><br># Context:<br>- You are the product manager writing stories for a software development team.<br>- Application name is Silo (aka silo site).<br>- Silo is a real estate application that allows its users to search and create listings of properties for lease or sale.<br>- Silo user roles are: admins, users, landlors, brokers, so use them for different perspectives.<br>- The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).<br></pre><h4>Importance of Context</h4><p>Context is key! It’s like telling a joke — if you don’t set it up right, nobody gets it. The Context Block helps the Gen AI understand your project, your users, and what you’re trying to achieve. Without it, you might end up with a user story that’s technically correct, but completely useless. Kind of like a developer who builds exactly what you asked for, but not what you <em>meant</em>.</p><p><strong>The Context Block:</strong></p><pre>#Context:<br><br>You are the product manager writing stories for a software development team.<br>Application name is [Your app name] (aka [if applies]).<br>[Your app name] is a [Domain] application that allows its users [key actions of your app]<br>The [Your app name] user roles available: admins, researchers, and brokers, so use them for different perspectives.<br>The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).</pre><p>So imagine you work for a real estate company then the context would look like (which is the example I used above):</p><pre># Context:<br>- You are the product manager writing stories for a software development team.<br>- Application name is Silo (aka silo site).<br>- Silo is a real estate application that allows its users to search and create listings of properties for lease or sale.<br>- Silo user roles are: admins, users, landlors, brokers, so use them for different perspectives.<br>- The user story should include a description and a list of acceptance criteria written in Gherkin language (Given, When, Then).<br>Im</pre><h4>Maintenance and Improvement</h4><p>PMs and Developers should:</p><ul><li><strong>Regularly review</strong> and update the templates. (Because requirements change faster than a toddler’s mind.)</li><li><strong>Share feedback</strong>. (Because collaboration is key, even if it sometimes feels like herding cats.)</li><li><strong>Collaborate</strong> on acceptance criteria. (Because developers are the experts at finding loopholes, and PMs are the experts at… well, let’s just say “managing expectations.”)</li></ul><p>By creating these templates, you can streamline the user story creation process and improve communication. This helps deliver high-quality software that meets user needs. (Most of the time.) If you have any questions, please reach out to the project management team. (They’re there for you. Really.)</p><p>And yes, I also got help from Gen AI to write this post, of course I review it, rith?, right?.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8ab959ade1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Error Handling With SOLID Principles]]></title>
            <link>https://medium.com/swlh/angular-error-handling-and-logging-yet-another-story-d3e48ecaca55?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3e48ecaca55</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Mon, 01 Feb 2021 03:31:06 GMT</pubDate>
            <atom:updated>2021-02-24T18:49:20.814Z</atom:updated>
            <content:encoded><![CDATA[<p>Server logging is so important to detect application problems and there are many solutions already out there that help you out to capture, analyze and some also go beyond even offers you to tell the exact commit and who could be the best candidate (git blame, is that you?) who can work and fix it (Sentry-coff-coff). That’s pretty cool, handy or you put the nice adjective.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BPQWL9SfCWtLEu2z" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>However, we all have been in that situation where the company we works for doesn’t want to pay for such solutions because of or whatever reason is. I sort of was that guy who had to look for feasible alternative, in the end, if we do it properly, a combination of your own code with an open-source stack can be a great solution that just hit the point and fulfills the requirements.</p><h4>The requirements</h4><ul><li>To have a mechanism to <strong>capture all errors occurring</strong> in the browser.</li><li>To have a utility to log/send events, errors, or messages of different importance levels (you know it, the classic log levels: info, warn, debug, error, etc).</li><li>As a system, we required components capable of <strong>collect &amp; store “log messages/events”</strong></li><li>As a team, we should be able to see all the captured “logs” that occurred in the last 3 months so the team can analyze errors in different ways (reactive, preventive, monitoring).</li><li>As a system, it is required to have alert messages triggered realtime to inform the support team about “critical errors” occurring in the application</li></ul><h3>The overall Architecture</h3><p>In a big big picture, the solution can be composed of two main macro-components:</p><ol><li>The <strong>Collector, Storage, and Dashboard</strong> for the messages</li><li>The <strong>browser utilities to capture and drop/send the logs/messages to the collector</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TO1vM2T-9pPSPapBtUzEjA.png" /><figcaption>Big picture logging Architecture — Elias Martinez</figcaption></figure><h3>Concrete components</h3><h3>The Collector Storage &amp; Dashboard</h3><p>The collector we did was a simple Go server which the only task was to receive a JSON formatted message and drop it to the std.out file (will make more sense in the next paragraph)</p><p>The storage and dashboard component we can provide by setting up an EKL stack (more precise Elastic Search, Kibana, LogStash &amp; FileBeat) where <strong>FileBeat streams</strong> the <strong>message logged from the Go server</strong> and <strong>push to a LogStash pipe</strong> to slice each attribute and <strong>finally drop an index-able message to ElasticSearch</strong>. Finally, by the nature of <strong>Kibana</strong>, <strong>we can set up queries, dashboards, and alerts</strong> to be sent when considered “critical” messages (defined completely by you) comes in by the nature of occurrence or amount of occurrences.<br>In this article <strong>we will focus on the frontend</strong> part, for the backend part maybe in the future I can provide the details on how to implement it.</p><h3>The Client-Side Big Picture Parts (or Js components)</h3><p>Here is what we will need for the recipe:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bVtKiith8PMcfVsCQIujWQ.png" /></figure><p>Whuuut!!… Easy my friend, let’s “de-structure” ;) this thing:</p><h4>1. ErrorHandler</h4><p>The task of this guy is to listen to <strong>ALL errors occurring in the application</strong>; fortunately Angular provides an interface to use; another framework/libraries I guess they have their own ways, but in theory, we all should be able to implement an <a href="https://developer.mozilla.org/es/docs/Web/API/GlobalEventHandlers/onerror">onerror callback</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*TtR1QUvxsaoF9U0OzOX-kg.png" /><figcaption>Screenshot from Mozilla</figcaption></figure><h4>2. Logger</h4><p>The <strong>Logger</strong> is another service that can receive via dependency injection all the instantiated and configured “transporters” and use them to write on each different log level messages and implementation.</p><h4>3. Transporters</h4><p>Transporters are an interface whose only task is to “write” a message in a particular way. It’s a Template pattern where the implementation can vary between different targets from the simple console.log, HTTP, slack, sockets, etc. this is what would make the framework flexible to integrate with other targets.</p><blockquote>Ok, Where is my money?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NtMSWaWlwoWmAyZN" /><figcaption>Photo by <a href="https://unsplash.com/@melanie_sophie?utm_source=medium&amp;utm_medium=referral">Melanie Pongratz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s see the details, the meaty part, the bull balls, the under the hood, etc.</p><h3>The ErrorHandler Details</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mgfY1o2d5Jkdc77czxxG9g.png" /></figure><p>As the picture stands, the ErrorHandler is sort of a proxy for all errors occurring in the application (either Javascript or HTTP errors) to handle them in something like a funnel/hub. <a href="https://angular.io/api/core/ErrorHandler">Angular</a> provides OOB with his class:</p><pre>class <a href="https://angular.io/api/core/ErrorHandler">ErrorHandler</a> {   <a href="https://angular.io/api/core/ErrorHandler#handleError"><strong>handleError(error: any): void</strong></a><strong> </strong>  }</pre><p>And the way to implement it is too easy as just override the provider with yours, like:</p><pre>class MyErrorHandler implements <a href="https://angular.io/api/core/ErrorHandler">ErrorHandler</a> {   <br>  handleError(error) {     <br>   // do something with the exception   <br>  } <br>}<br>  <br>@<a href="https://angular.io/api/core/NgModule">NgModule</a>({   <br>    providers: <strong>[{provide: </strong><a href="https://angular.io/api/core/ErrorHandler"><strong>ErrorHandler</strong></a><strong>, useClass: MyErrorHandler}] </strong><br>  }<br>) class AppModule {}</pre><p>Now taking the advantage of Dependency injection <strong>we can </strong>easily<strong> inject our </strong>custom <strong>Logger </strong>that will be explained in more detail next. So in the end, our ErrorHandler might look like this:</p><pre>@Injectable()<br>export class <strong>MyErrorHandler</strong> implements ErrorHandler {</pre><pre>  constructor(<strong>@Inject(MyLoggerToken) private logger: MyLogger</strong>) {}</pre><pre>  handleError(error: Error | HttpErrorResponse): void {<br><strong>    this.logger.error(error);</strong><br>  }</pre><pre>}<br></pre><p>Here, what we are doing is basically the injection of a given logger component. The implementation is unknown yet, whatever we inject as the Logger only knows what it can do, to log info, debug, warn or error messages.</p><h3>Now “The LoggerService” details</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*5j0dFB3uvcEA4K33whwgSw.png" /></figure><p>The Logger is maybe the more complex component because the way we implement will define how useful or flexible it behaves, but since we are providing an interface/contract it can be later be replaced for something “better” if you manage to improve it. Another important component are the Transporters which we will see the details later so let’s focus only on the Logger mechanics.</p><p>The mechanics of the logger is simply as to say “it needs to drop messages of different levels”, so we need to define the interface among other utilities and interfaces</p><pre>/**<br>* Logger Injection token<br>*/<br>export const MyLoggerToken = <br>  new InjectionToken&lt;<strong>MyLogger</strong>&gt;(&#39;MyLogger&#39;);</pre><pre>/**<br>* Allowed Log input types to be received by a MyLogger<br>*/<br>export type <strong>LogTypes</strong> = string | Error | HttpErrorResponse;</pre><pre>/**<br>* Logger Interface. Implementations should implement and expose the following methods: info, debug, warn, error<br>*/<br>export interface <strong>MyLogger</strong> {<br>  <strong>info</strong>(message: string): void;<br>  <strong>debug</strong>(message: string): void;<br>  <strong>warn</strong>(message: string): void;<br>  <strong>error</strong>(error: <strong>LogTypes</strong>): void;<br>}</pre><p>So the job of the logger is to drop log messages somewhere depending if the configured log level is higher than what it’s being tried to be logged. Therefore we need to define Log levels.</p><pre>export enum LogLevel {<br> Info = 4,<br> Debug = 3,<br> Warn = 2,<br> Error = 1,<br>}</pre><p>Once we defined the log levels we need to somehow tell the logger what log level is permitted to be logged. for that, we can define an injectable object like <strong>LoggerConfiguration</strong></p><pre>/*<br>* LoggerConfiguration Token<br>*/<br>export const <strong>LoggerConfigurationToken</strong> = <br>  new InjectionToken&lt;<strong>LoggerConfiguration</strong>&gt;(&#39;LoggerConfiguration&#39;);</pre><pre>/**<br>* LoggerConfiguration object interface<br>*/<br>export interface <strong>LoggerConfiguration</strong> {<br>  level: LogLevel;<br>}</pre><p>With proper config interfaces in place we can go ahead and provide them so they can be injected to our <strong>Logger </strong>service class for later provide it in the application itself ready with all his things up, so let’s see the Logger class implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6049eb26f09990bee0fd786b96cb2ea9/href">https://medium.com/media/6049eb26f09990bee0fd786b96cb2ea9/href</a></iframe><p>This is a simplified/basic implementation of a logger service, we consume the config files, we have implemented all the methods in the MyLogger interface and for each transporter configured we transfer the control to write it in the way it is designed to transport it.</p><p>Now let’s see the last piece to wire in.</p><h3>The Transporters</h3><p>First things first, yes I took inspiration from Winston library. Now, Each transporter is responsible to transport/transfer/write the received payload from the logger (which means it is supposed to be written somewhere) to somewhere/someone else. And this “elsewhere” can be literally whatever (console, HTTP log server, slack channel, email, etc…)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*A2q2sLvEFNJXpTJdoLsKDg.png" /></figure><p>That’s it, that simple thing it has to do. merely and simply “write” whatever he gets. The how to write has to be provided as a <a href="https://angular.io/api/core/ClassProvider#multi-value-example">multi-service provider</a>. This way we can have multiple providers injected into the <strong>LoggerService</strong></p><p>Now let’s define a base template abstract class to save us time while implementing each Transporter. To do so we can implement some sort of <a href="https://refactoring.guru/design-patterns/template-method">Template pattern</a> and let’s also introduce one small piece we haven’t mentioned before that we can fix with Dependency Injection which is the transport message template (in red)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7GFpJ8OnZP_p3y9Y_Q2RFA.png" /></figure><p>Having the <strong>AbstractTransporter</strong> implementing the <strong>Transporter</strong> interface will be responsible for registering the transport template which in plain code is just an injected function that will produce a payload of any type based on the <strong>CapturedPayload</strong> interface we implemented in the logger service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a1127f2ae30cdb2e208bc8ed3e8e03d/href">https://medium.com/media/7a1127f2ae30cdb2e208bc8ed3e8e03d/href</a></iframe><p>With the <em>AbstractTransporter</em> ready to be implemented we can start implementing each transporter specificity, like for example console logger, HTTP transporter, slack transporter, email transporter, socket transporter, or third-party transporter (sentry or any other service).</p><p>So let’s take a look at a couple of them, Console and Http transporters</p><h4>ConsoleTransporter</h4><p>This is probably the basic implementation, we don’t even require any template to be consumed or it can be optional.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78bfa48659eb1d9df4e28dc9b576d152/href">https://medium.com/media/78bfa48659eb1d9df4e28dc9b576d152/href</a></iframe><p>Please notice that what we receive in the “doWrite” method is the result of calling the write method in the logger service a the “execute” method</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*lhHmgEz0_SBy3B58GJhHig.png" /></figure><p>And from the next call in the abstract template class</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*I_ZPqFrGDf6KbjboYlRCxQ.png" /></figure><p>Since the Template is defined while creating the transporters it means that we can have one template for all transporters OR we can have different templates for all transporters, however, all receive the same contract:</p><pre>/**<br>* Final captured event, built-in Logger captures this instance<br>* before queuing the transport macro task execution, if custom <br>* Logger implemented it&#39;s highly recommended to capture this <br>* instance before the transporters write processing<br>*/<br>export interface <strong>CapturedPayload</strong> {<br>  payload: LogTypes;<br>  level: LogLevel;<br>  timestamp: Date;<br>}</pre><h4>The HttpTransporter</h4><p>Finally for this post. Here a simple sample for an HttpTransporter, it extends our AbstractTransporter class and extends few more attributes to be able to work like the httpClient and extra configuration like the endpoint URL or whatever your application needs to perform HTTP calls (cookies management, Bearer token providers what ever it is…) in this case we only need the endpoint where we want to “write” the payload and the base httpClient, Bearer token (security stuff) is provided in another component of the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*20eULh0RXEQrIy5A" /><figcaption>Photo by <a href="https://unsplash.com/@rhysatwork?utm_source=medium&amp;utm_medium=referral">Rhys Moult</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this implementation, Let’s use a “batch” processor with a buffer of 500ms to collect all payloads and try to minimize the network calls in case we receive tons of errors (this shouldn’t happen but, we all have horror stories). However, it’s up to you how you implement yours. We are not here to judge the implementation but the overall solution, execution is always debatable right?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10113f0adb32fb590ad6de13359f664b/href">https://medium.com/media/10113f0adb32fb590ad6de13359f664b/href</a></iframe><p>All right where do I connect all the pieces? So far we have defined and separated the responsibilities and we got a bunch of open-close components.</p><h4>The last thing to do is… Provide all the pieces</h4><p>Yeah, we just need to register the logger, transporters &amp; template and we all can guess where. in the <strong>AppModule/CoreModule.</strong></p><h4>Provide the logger</h4><p>The <strong>Logger</strong> can be provided in different ways, either you create all logger functionality in a module (recommended) or directly in the app module or core module(whatever is your root module).</p><p>Let’s assume we implemented a module, therefore we can add something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1eb1a201b431c9350acab5aa00e28b/href">https://medium.com/media/fe1eb1a201b431c9350acab5aa00e28b/href</a></iframe><h4>Provide the Transporters and template</h4><p>The rest of the pieces since they can be implemented in varying vast ways and actually needs to fit with every “user” of the module they should be provided in the app module and via factory providers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ee23fbee3c83dc4bf4efd2c5215c71b/href">https://medium.com/media/3ee23fbee3c83dc4bf4efd2c5215c71b/href</a></iframe><blockquote>That’s all folks.</blockquote><h3>End Thoughts</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ThbtOphIqMUKB4pB" /><figcaption>Photo by <a href="https://unsplash.com/@marxgall?utm_source=medium&amp;utm_medium=referral">Marek Szturc</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We should always think and keep present scalability and maintainability of whatever we do. SOLID principles are proven to grant you both; notice that in this solution we applied some SOLID principles, like SRP and OCP by categorizing and segregating the the parts &amp; tasks in such a way we protect the components from knowing from each other, Each component (Logger, Transporters &amp; Message Template) are replaceable &amp; interchangeable.</p><p>The solution is not yet perfect but is good enough to provide scalability. In the end, this is yet one of many ways to solve this problem, hope it helps, and happy coding my friends!</p><blockquote>Hey!, Have you heard about object composition?</blockquote><p><a href="https://medium.com/swlh/business-composers-with-functional-composition-438f3bef2fa0">“Business Objects” With Functional Composition</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3e48ecaca55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/angular-error-handling-and-logging-yet-another-story-d3e48ecaca55">Angular Error Handling With SOLID Principles</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Business Objects” With Functional Composition]]></title>
            <link>https://medium.com/swlh/business-composers-with-functional-composition-438f3bef2fa0?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/438f3bef2fa0</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Fri, 30 Oct 2020 18:00:54 GMT</pubDate>
            <atom:updated>2021-03-01T05:30:44.744Z</atom:updated>
            <content:encoded><![CDATA[<h3>Functional composition &amp; the creation of the “Business Objects” for your project.</h3><p>There is one thing in Javascript (or any functional programming language) I consider any OOP envy how easy is to implement, Javascript can be criticized, they can laugh at being the only language with a monster book describing the whole language but with a tiny one only telling you the “good parts”. but real easy composition they will never have it.</p><p>The simplicity of it is kind of pure art. we can take one object and convert it into whatever we want him to do by adding behaviors we want even at runtime with just a few lines; that is something they would only dream on build objects with behaviors on the fly. I will be talking with code but first, let’s be clear what a business object I will refer to.</p><h4>The “Business objects”</h4><p>That piece of the code with attributes and behaviors represented in the shape of “classes”.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40emsedano%2FClassSample%3Flite%3Dtrue&amp;display_name=repl.it&amp;url=https%3A%2F%2Frepl.it%2F%40emsedano%2FClassSample%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a82cd29f4a1e1048805744fddf5ddb01/href">https://medium.com/media/a82cd29f4a1e1048805744fddf5ddb01/href</a></iframe><p>Once they are defined they become a kind of written in stone rules that hardly can be changed, they acquire attributes and behaviors but the reality is that they can mutate over time (and sometimes over the runtime), changes that depend on the client/business needs, (even sometimes the humor). What we do now? we go back to the class to re-define it so we add those behaviors or decorate the classes.</p><p>Sometimes we have identical behaviors between totally different objects such as there is no way that inheritance can save us; so duplication comes to the rescue. don’t you feel dirty after having to duplicate the functionality?.</p><pre>Let&#39;s show a silly example of a &quot;real estate domain&quot;</pre><pre>Property<br>- contacts: CompanyContact[] &lt;- agencies or brokers representatives <br>- purposes: Purpose[] &lt;- can be an office, retail, industrial, etc...<br>- amenities: Amenity[] &lt;- pool, gym, metro access, parking, etc...<br>Availability<br>- PropertyRef: Property<br>- contacts: CompanyContact[]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7TN6LpGfiyZBhK2E" /><figcaption>Photo by <a href="https://unsplash.com/@theexplorerdad?utm_source=medium&amp;utm_medium=referral">Joshua Ness</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Here we have Property, and a property can be a building, it can be a land property and hold a variety of attributes. Meanwhile, Availability is a physical space that exists in a property. Both have contacts (in real estate you have people representing the buildings but not one of its availabilities, even doe one availability is IN the property, the set of contacts can be completely different), they have the same functionality but we can’t just inherit the behaviors. we don’t want to inherit amenities and the other stuff. I might want to have a Purpose but not all, an Availability can have only one Purpose (at least for this imaginary real estate company it works like that) either you are an office or a restaurant right?.</p><h4>Where are my “lego bricks”</h4><p>Here is where composition comes to the rescue. If instead of defining a set of rigid classes you define bricks of attributes &amp; behaviors that can build and adapt to any data/business model is gonna be easier to deal with. I want to do a parenthesis and being honest as well. I’m not saying this is a panacea, be conscious that also this process to well define your bricks is hard, is thought and as defining classes will define the course of your application and it’s limitations but I truly believe this is way easier than and help to reduce the amount of code you write.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xstTJ1iYu1eW5tlD" /><figcaption>Photo by <a href="https://unsplash.com/@jamesponddotco?utm_source=medium&amp;utm_medium=referral">James Pond</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Here they are (the lego bricks)</h4><p>Let’s start with some start of initial point with business code involved and we will see how we can start improving it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d66b449e30f91c108848c02fad5ec49d/href">https://medium.com/media/d66b449e30f91c108848c02fad5ec49d/href</a></iframe><p>See repl.it <a href="https://repl.it/join/ucyqmwzp-emsedano">here</a></p><h4>Now we can’t just stop there</h4><p>We are now enabled to just make more lego pieces, let’s start with something “silly” but useful. we’ll start with a safe iterator, we see many x || [] so let’s make it a base function</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab286ca66bfbd4050a7626e7c36baef8/href">https://medium.com/media/ab286ca66bfbd4050a7626e7c36baef8/href</a></iframe><p>what a silly sample right?. let’s continue. We find many times to have flagged elements as primary things right? well, then let’s make a primary selector factory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfc3107206129dfca7efd09ebcf89c46/href">https://medium.com/media/bfc3107206129dfca7efd09ebcf89c46/href</a></iframe><p>It’s a simple curried function, we create a function that receives a couple of attributes, 1st where we have to look (a collection name basically), 2nd what to look for(attribute name), then we return a function that later will receive a state to which we will apply the search for the “primary thing”;</p><blockquote>Also notice we already started using the `<strong>safeIter</strong>`.</blockquote><p>With this in place, we can build our first compose-able thing for “primary purposes”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0f147d85c47c7fa1b86459c587e871e/href">https://medium.com/media/e0f147d85c47c7fa1b86459c587e871e/href</a></iframe><p>Now for contacts, let’s assume that in the contacts array we have items like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dee01e67bf0c8d9b903676a6cbdcca3f/href">https://medium.com/media/dee01e67bf0c8d9b903676a6cbdcca3f/href</a></iframe><p>We can’t use hasPrimaryPurpose first because is not under purposes and second because it does not have the primary column, therefore we need to create another factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a10305134af84b989ebe4145e207c250/href">https://medium.com/media/a10305134af84b989ebe4145e207c250/href</a></iframe><p>We started granulating business logic. Now, remember we said purposes has also their own contacts inside?, well if they keep the same structure, we can now reuse our hasPrimaryContact composed function. We are now in the first stages, we can just only go further.</p><p>Another example, I’m pretty sure you already noticed too much duplication on CRUD operators; let’s do something there, this is gonna be thought so prepare your sharpness.</p><p>We need to know first the outcome function we need to provide to the top composers (PropertyFactory, AvailabilityFactory) if we look at the shape of the delete operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00bb3628dde9d95e4d87b30b716b1b43/href">https://medium.com/media/00bb3628dde9d95e4d87b30b716b1b43/href</a></iframe><p>The first thing we see is, we need to produce a function that receives a “target” object (in the above sample is “purpose”)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef7b7ba186efaed1423f161a54fd488c/href">https://medium.com/media/ef7b7ba186efaed1423f161a54fd488c/href</a></iframe><p>having this, we can start generating more composers, based on different preset/common criteria&#39;s we have in your application, like delete by id, or delete by name, or delete by type or delete by whatever the heck you want.</p><p>see what I mean:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f805be910d2528bcb2f568489d8589d3/href">https://medium.com/media/f805be910d2528bcb2f568489d8589d3/href</a></iframe><p>I think you get the point, every single “brick” function or composer function(whatever you name it) plays a role and can help you out from simple to complex stuff and all depends entirely on you and your sharpness sense.</p><p>Last thing but not less important you have to worry about is how are you going to organize your stuff</p><pre>utils/<br>|--safeIter.js<br>|--byFieldComparator.js </pre><pre>bricks/<br>|--deleteById.js<br>|--deleteByName.js<br>|--hasPrimary.js<br>|--setAsPrimary.js</pre><pre>factories/<br>|--addElementsFactory.js<br>|--deleteElementsFactory.js<br>|--createDeleteFactory.js     // or you can combine them. <br>|--filterElements.js<br>|--hasPrimaryFactory.js<br>|--markAsPrimaryFactory.js</pre><pre>business/<br>|--commons/<br>    |--addContact.js<br>    |--deleteContat.js<br>    |--deleteTenantContacts.js</pre><pre>property/<br>|--PropertyFactory.js</pre><pre>amenity/<br>|--AvailabilityFactory.js</pre><pre>tenants/<br>|--TentantFactory.js</pre><h3>Conclusion</h3><p>I know this is a silly example but I tried to explain the basis on how incrementally you can build your bricks and build as complex/crazy business logic your application might need to create entities and how they relate to each other.</p><p>Imagine you build the objects based on a dynamic input where you define the set available rules/behaviors and based on the input you on the fly (yes at runtime) you build and prepare objects with the behaviors you need/want. this is a good approach to fix the problem of the gorilla banana (at the level of business objects of course).</p><p>I took inspiration from <a href="https://medium.com/u/5b25bc58c2e">Mattias Petter Johansson</a> (which BTW I miss fff), where he mentions more or less the next example: imagine just to say something a mobile game:</p><blockquote>Client: — What if I want a dog character.<br>Dev: — ok I have a dog factory.<br>Client: — oh!, also I want a robot.<br>Dev: — ok I can have a robot factory yes.<br>Client: — can it clean?<br>Dev: — for sure I do also have a cleaner factory.<br>Client: — you know what could be cool, if I want a robot dog cleaner.</blockquote><blockquote>Dev: mmm…</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FZyVkyD3EH4pJUOe" /><figcaption>Photo by <a href="https://unsplash.com/@dulgier?utm_source=medium&amp;utm_medium=referral">Nastya Dulhiier</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Here is where OOP get’s problems you might <strong>end up having a `barking-robot-cleaner` that</strong> <strong>also does poop</strong>.</p><p>Whereas with composition you can build exactly what you want that does the exact things you want do do, if you have bricks for each individuality behaviors like, robot factory + cleaner factory + barker factory. Then you can get what you want, a <em>robot-clenaer-barker</em> (no pooping). I do agree OOP has composition too but have you seen how hard and ceremonial it is? and if you don’t do it well you still may end-up injecting things you don’t want to have available there.</p><p>The big but on this is the fact that how to organize things around is up to every developer can become messy if we don’t do right choices (there is no standard guide other than the usual hints of keeping this related next to each other or categorize them by domain or try to convey with DDD); So well documentation is important and needed to let other devs/contributors know what “bricks” are being offered and what they can do for them.</p><p>In the end, it’s a matter of preferences, It’s up to you… but functional programming is better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Be0Qf6a34Yeip1at" /><figcaption>Photo by <a href="https://unsplash.com/@willvanw?utm_source=medium&amp;utm_medium=referral">Will van Wingerden</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=438f3bef2fa0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/business-composers-with-functional-composition-438f3bef2fa0">“Business Objects” With Functional Composition</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular — How to check authorization based on role and entity-states]]></title>
            <link>https://medium.com/swlh/angular-how-to-check-authorization-based-on-role-and-entity-states-84f5b3e3c78c?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/84f5b3e3c78c</guid>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Fri, 14 Jun 2019 16:05:15 GMT</pubDate>
            <atom:updated>2019-06-14T16:05:15.013Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular — How to check authorization based on role and entity-states</h3><p>Nowadays is very common to find you in the state of building authentication and authorization in your application, digging on the web for authorization libraries and techniques is easy always to find solutions that only offer role-base authorization that prevents only to access a page, however almost often occurs that you need something else, entity-state authorization.</p><p><em>For the TL;DR here is the </em><a href="https://emsedano.github.io/ng-entity-state/"><em>demo</em></a><em> and the </em><a href="https://github.com/emsedano/ng-entity-state"><em>code</em></a><em> used in the demo.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQaOv07qTNF3OA33fg17Nw.png" /><figcaption>Picture <a href="https://unsplash.com/search/photos/code?photo=w7ZyuGYNpRQ">credits</a></figcaption></figure><h4>What the heck is entity-state authorization?</h4><p>Well, I guess there was nothing better in my head to name it while writing this post. However more or less what I am trying to say is related to the situations when you need to grant or un-grant to the current user the ability to apply specific action if the current entity state relies on X state and a different ability when the entity state relies on Y state and It came worst when all this should be on the same screen or even in the same component. Any libraries with role-base authorization will save you for this problem.<br>Tired of reading different articles with role-base page prevent solutions, I started thinking and coding without anything in mind yet, and suddenly it was there, I found simple, straightforward and very flexible solution to solve this kind of problems and its composed of four components.</p><ul><li>A <strong>service</strong> <strong>to provide the current user information</strong> (what really matter is a way to provide the user roles that current user belongs to or has been granted to play in the application)</li><li>One <strong>workflow-permissions map</strong> (JSON file)</li><li>A <strong>permissions service </strong>to actually do the authorization check</li><li><strong>A directive</strong> to consume the check the authorization service</li></ul><h4>Step 1: get current user roles</h4><p>Implement a service to retrieve from the server side (first time) or from session or cookies as you prefer for subsequent uses of it, the important thing here is to provide the user list of abilities (roles).</p><pre>// Example<br>{<br>  name: &#39;John Doe&#39;,<br>  email: &#39;jd@doe.com&#39;,<br>  <strong>roles: [&#39;seller&#39;, &#39;seller_manager&#39;], &lt;-- this is what matters</strong><br>  accessToken: &#39;alotofletersrepresentinganaccesstokenhahahaaa!&#39;<br>  ... more stuff</pre><pre>}</pre><p>In my case more or less this is how it looks:</p><pre>// imports here ...</pre><pre>@Injectable()<br><strong>export class </strong>CurrentUserService {<br>  <strong>private </strong>userSubject = <strong>new </strong>ReplaySubject&lt;User&gt;(1);<br>  <strong>private </strong>hasUser = <strong>false</strong>;</pre><pre>  <strong>constructor</strong>(<strong>private </strong>usersApi: UserApi) {<br>  }</pre><pre>  <strong>public </strong>getUser(): Observable&lt;User&gt; {<br>    <strong>if </strong>(!<strong>this</strong>.hasUser) {<br>      <strong>this</strong>.fetchUser();<br>    }<br>    <strong>return this</strong>.userSubject.asObservable();<br>  }</pre><pre>  <strong>public </strong>fetchUser(): <strong>void </strong>{<br>      <strong>this</strong>.usersApi.getCurrent() // &lt;== <strong>http</strong> call to fetch userInfo<br>        .subscribe(<strong>user</strong> =&gt; {<br>          // <strong>user</strong> should contains roles has been granted<br>          <strong>this</strong>.hasUser = <strong>true</strong>;<br>          <strong>this</strong>.userSubject.next(<strong>user</strong>); <br>          <strong>this</strong>.userSubject.complete();<br>        }, (error) =&gt; {<br>          <strong>this</strong>.hasUser = <strong>false</strong>;<br>          <strong>this</strong>.userSubject.error(error);<br>        });<br>  }</pre><pre>}</pre><h4>Second step: Build your workflow &amp; permissions map</h4><p>This is nothing but the mapping of what can we do and who can we do by building a tree with the different entities and their own states. for example, let’s imagine the following sales process; Our application might have several role types. For our example let’s map the roles to SELLER, solutions ARCHITECT and CLIENT.</p><p>Lest talk about the process:</p><ul><li>First the SELLER places a sales opportunity by executing the action <strong>Add new opportunity</strong> so the opportunity state probably is <strong>created</strong></li><li>At this point, the CLIENT &amp; SELLER can add the requirements to the opportunity so both can apply the action <strong>Add requirements</strong> when the requirements are placed then status of the <strong>opportunity</strong> might change to <strong>submitted</strong></li><li>Once the requirements are placed the ARCHITECT might want to add a solution so he needs an action: <strong>Upload solution </strong>and probably the state might change to <strong>solved</strong></li><li>Once the solution has been provided, the CLIENT might want to accept so he needs an action to <strong>approve solution </strong>and the state would change to <strong>solution_approved</strong></li></ul><p>We are going to cut the process here otherwise this would grow too much and is not the case for this reading. So based on this process the mapping and assuming that the <strong>opportunity</strong> entity has a field that tracks the <strong>state, </strong>our workflow would look like this:</p><pre>{<br>  &quot;opportunity&quot;: {<br>    &quot;addOpportunity&quot;: { &quot;permittedRoles&quot;: [&quot;SELLER&quot;] } },<br>    &quot;<strong>created</strong>&quot;: {<br>      &quot;addRequirement&quot;: {&quot;permittedRoles&quot;: [&quot;SELLER&quot;, &quot;CLIENT&quot;]}<br>    },<br>    &quot;<strong>submitted</strong>&quot;: {<br>      &quot;addSolution&quot;: { &quot;permittedRoles&quot;: [&quot;ARCHITECT&quot;]}<br>    },<br>    &quot;<strong>solved</strong>&quot;: {<br>      &quot;approveSolution&quot;: { &quot;permittedRoles&quot;: [&quot;CLIENT&quot;]}<br>    }<br>}</pre><h4>Step 3: The check authorization service to consume the worflow &amp; permissions map</h4><p>Now that we have the process mapped into a workflow &amp; permissions map, we need to create a service to consume it and check whether user is authorized or not and it could look like this:</p><pre><strong>// import statements here<br>// by the way in angular-cli we can put the JSON file in the <br>// enviroment.ts</strong></pre><pre>@Injectable()<br><strong>export class </strong>WorkflowEvents {<br>  <strong>private readonly </strong>WORKFLOW_EVENTS = environment[&#39;workflow&#39;];<br>  <strong>private </strong>userRoles: Set&lt;<strong>string</strong>&gt;;</pre><pre>// do your remember the step 1 ? it is used here<br>  <strong>constructor</strong>(<strong>private </strong>currentUserService: CurrentUserService) {<br>  }<br>  // returns a boolean observable<br>  <strong>public </strong>checkAuthorization(path: <strong>any</strong>): Observable&lt;<strong>boolean</strong>&gt; {<br>    // we are loading the roles only once<br>   <strong>if </strong>(!<strong>this</strong>.userRoles) {<br>      <strong>return this</strong>.currentUserService.getUser()<br>        .map(currentUser =&gt; currentUser.roles)<br>        .do(roles =&gt; {<br>          <strong>const </strong>roles = roles.map(role =&gt; role.name);<br>          <strong>this</strong>.userRoles = <strong>new </strong>Set(roles);<br>        })<br>        .map(roles =&gt; <strong>this</strong>.doCheckAuthorization(path));<br>    }<br>    <strong>return </strong>Observable.<em>of</em>(<strong>this</strong>.doCheckAuthorization(path));<br>  }</pre><pre>  <strong>private </strong>doCheckAuthorization(path: <strong>string[]</strong>): <strong>boolean </strong>{<br>    <strong>if </strong>(path.length) {<br>      <strong>const </strong>entry = <strong>this</strong>.findEntry(<strong>this</strong>.WORKFLOW_EVENTS, path);<br>      <strong>if </strong>(entry &amp;&amp; entry[&#39;permittedRoles&#39;]<br>             &amp;&amp; <strong>this</strong>.userRoles.size) {<br>        <strong>return </strong>entry.permittedRoles<br>        .some(permittedRole =&gt; <strong>this</strong>.userRoles.has(permittedRole));<br>      }<br>      <strong>return false</strong>;<br>    }<br>    <strong>return false</strong>;<br>  }</pre><pre>/**<br> * Recursively find workflow-map entry based on the path strings<br> */<br><strong>private </strong>findEntry(currentObject: <strong>any</strong>, keys: <strong>string</strong>[], index = 0) {<br>    <strong>const </strong>key = keys[index];<br>    <strong>if </strong>(currentObject[key] &amp;&amp; index &lt; keys.length - 1) {<br>      <strong>return this</strong>.findEntry(currentObject[key], keys, index + 1);<br>    } <strong>else if </strong>(currentObject[key] &amp;&amp; index === keys.length - 1) {<br>      <strong>return </strong>currentObject[key];<br>    } <strong>else </strong>{<br>      <strong>return false</strong>;<br>    }<br>  }</pre><pre>}</pre><p>Basically what it does is to look a valid entry and check if the current user roles are included in the <strong>permittedRoles.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*7jbgb5xBlC941mSw0JaE9Q.png" /></figure><h4>File 4: The directive</h4><p>Once that we have the current user roles, a workflow permissions tree, and a service to check authorization for current user roles, now we need a way to put this alive, and the best way in angular 2/4 is a directive. Initially the directive I wrote was an attribute directive that was toggling the display CSS attribute but this could lead to performance issues because decedent components still being loading to DOM, so is better to <strong>use structural directives</strong> (Thanks to my colleague <a href="https://www.linkedin.com/in/petyo-cholakov-a81a9172/">Petyo Cholakov</a> for this good catch, see the difference <a href="https://www.dev6.com/angular-2-the-three-types-of-directives">here</a>) since we can modify the DOM of the target element and it’s decedents so we can avoid the loading of unused elements.</p><pre>@Directive({<br>  selector: &#39;[appCanAccess]&#39;<br>})<br><strong>export class </strong>CanAccessDirective <strong>implements </strong>OnInit, OnDestroy {<br>  @Input(&#39;appCanAccess&#39;) appCanAccess: <strong>string </strong>| <strong>string</strong>[];<br>  <strong>private </strong>permission$: Subscription;</pre><pre>  <strong>constructor</strong>(<strong>private </strong>templateRef: TemplateRef&lt;<strong>any</strong>&gt;,<br>              <strong>private </strong>viewContainer: ViewContainerRef,<br>              <strong>private </strong>workflowEvents: WorkflowEvents) {<br>  }</pre><pre>  ngOnInit(): <strong>void </strong>{<br>    <strong>this</strong>.applyPermission();<br>  }</pre><pre>  <strong>private </strong>applyPermission(): <strong>void </strong>{<br>    <strong>this</strong>.permission$ = <strong>this</strong>.workflowEvents<br>                        .checkAuthorization(<strong>this</strong>.appCanAccess)<br>      .subscribe(authorized =&gt; {<br>        <strong>if </strong>(authorized) {<br>          <strong>this</strong>.viewContainer.createEmbeddedView(<strong>this</strong>.templateRef);<br>        } <strong>else </strong>{<br>          <strong>this</strong>.viewContainer.clear();<br>        }<br>      });<br>  }</pre><pre>  ngOnDestroy(): <strong>void </strong>{<br>    <strong>this</strong>.permission$.unsubscribe();<br>  }</pre><pre>}</pre><h4>Finally the resultant work</h4><p>Now what we have all that we need is time to put them in action. so in our HTML template, the only thing we need to do is something like the following code</p><p>Let’s assume we have a sample component that includes the <strong>opportunity</strong> object:</p><pre>@Component({<br>  selector: &#39;sp-pricing-panel&#39;,<br>  template: `<br>&lt;same-sample-component&gt;<br>    &lt;button *appCanAccess=&quot;[&#39;opportunity&#39;, &#39;addOpportunity&#39;]&quot;&gt;<br>      Add Opportunity<br>    &lt;/button&gt;</pre><pre>&lt;add-requirement-component *appCanAccess=&quot;[&#39;opportunity&#39;, <strong>opportunityObject.state</strong>, &#39;addRequirement&#39;]&quot;&gt;&lt;/add-required-component&gt;</pre><pre>&lt;add-solution-component *appCanAccess=&quot;[&#39;opportunity&#39;, <strong>opportunityObject.state</strong>, &#39;addSolution&#39;]&quot;&gt;&lt;/add-required-component&gt;</pre><pre>&lt;approve-solution-component *appCanAccess=&quot;[&#39;opportunity&#39;, <strong>opportunityObject.state</strong>, &#39;approveSolution&#39;]&quot;&gt;&lt;/add-required-component&gt;</pre><pre>&lt;/same-sample-component&gt;`<br>})<br><strong>export class </strong>SampleComponent <strong>implements </strong>OnInit {</pre><pre>  @Input() <strong>opportunityObject: any</strong>;</pre><pre><strong>constructor</strong>() {<br>  }</pre><pre>  ngOnInit() {<br>  }<br>}</pre><p>And that’s it, we can have a simple component presenting behavior depending on the user roles and the entity state.</p><p>Thanks to my colleagues <a href="https://www.linkedin.com/in/petyo-cholakov-a81a9172/?ppe=1">Petyo</a> and <a href="https://www.quora.com/profile/Govinda-Kamtamneni-1">Govind</a> for the catch and critics to my bad coding we could find this solution that can works perfectly to our needs, I hope this helps you too.</p><p>JUN 2018, small sample working =&gt; <a href="https://emsedano.github.io/ng-entity-state/">https://emsedano.github.io/ng-entity-state/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84f5b3e3c78c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/angular-how-to-check-authorization-based-on-role-and-entity-states-84f5b3e3c78c">Angular — How to check authorization based on role and entity-states</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript testing: Test external or global dependencies like ‘window’, ‘fetch’ etc]]></title>
            <link>https://eliasmsedano.medium.com/javascript-testing-test-external-or-global-dependencies-like-window-fetch-etc-7693e4dab4e4?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/7693e4dab4e4</guid>
            <category><![CDATA[mocha]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[jasmine]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 14:33:33 GMT</pubDate>
            <atom:updated>2018-07-03T20:01:45.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vVzwGLvNK8Il14IP7ACUYw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/5eBW5GomfhY?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Irvan Smith</a> on <a href="https://unsplash.com/search/photos/javascript?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>How many times you are in the following position where you have a function that depends on outer dependencies or global objects (like window, fetch, document…)</p><pre>function doSomething(oneParam, callback) {<br>   oneDependency.doSomethingElse();   <br>   fetch(&#39;resource/from/somewhere&#39;).then(callback)<br>}</pre><p>Now, when you are trying to test this piece of code, how do you control the binding of the two dependencies ononeDepenency andfetch ? The answer is dependency injection + factoryMethod Here is how:</p><h4>The factory</h4><pre>import  { oneDependency } from  &#39;./somewhere/somefile&#39;;</pre><pre>const doSomething = (oneParam, callback) =&gt; {<br>   const generatedValue = oneDependency.doSomethingElse(oneParam);<br>   // Let your mind to fly<br>   return fetch(&#39;api/&#39; + generatedValue).then(callback);<br>}</pre><pre>export const doSomethingFactory = (oneDependency, fetch) =&gt; {<br>   return doSomething.bind(null, oneDependency, fetch);<br>}</pre><h4>Your tests</h4><pre>import { <strong>doSomethingFactory</strong> } from  &#39;./path/to/your/factory&#39;;</pre><pre><strong>describe</strong>(&#39;DoSomething test suite&#39;, () =&gt; {<br>  let service, fakeOneDependency, fakeFetch;</pre><pre>  <strong>it</strong>(&#39;Should be defined&#39;, (done) =&gt; {<br>    // Given fake dependencies<br>    fakeOneDependency = { doSomethingElse: () =&gt; ({mock: &#39;value&#39;})};<br>    fakeFetch = () =&gt; ({ then: () =&gt; ({fake: &#39;object&#39;})});<br><strong>    // When </strong><br><strong>    service = doSomethingFactory(fakeDependency, fakeFetch);<br></strong>    // Then <strong><br> </strong>   <strong>service</strong>(&#39;the oneParam value&#39;, (response) =&gt; {<br>       expect(response.fake).toEqual(&#39;object&#39;);<br>    });<br>  });<br>})</pre><p>By doing this you have total control on you test to provide the behavior that drives the right expectation you have about your code, this is the extremely easy way to improve and sanitize your testing set.</p><p><strong>Code coverage BTW will be happy too! ;)</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7693e4dab4e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript testing: Test external or global dependencies like ‘window’, ‘fetch’ etc]]></title>
            <link>https://itnext.io/javascript-testing-test-external-or-global-dependencies-like-window-fetch-etc-ac5bd9fca716?source=rss-726697805cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac5bd9fca716</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dependencies]]></category>
            <dc:creator><![CDATA[Elias Martinez]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 14:33:33 GMT</pubDate>
            <atom:updated>2018-07-05T08:08:58.993Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*vVzwGLvNK8Il14IP7ACUYw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/5eBW5GomfhY?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Irvan Smith</a> on <a href="https://unsplash.com/search/photos/javascript?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>How many times you are in the following position where you have a function that depends on outer dependencies or global objects (like window, fetch, document…)</p><pre>function doSomething(oneParam, callback) {<br> oneDependency.doSomethingElse(); <br> fetch(&#39;resource/from/somewhere&#39;).then(callback)<br>}</pre><p>Now, when you are trying to test this piece of code, how do you control the binding of the two dependencies ononeDepenency andfetch ? The answer is dependency injection + factoryMethod Here is how:</p><pre>import { oneDependency } from &#39;./somewhere/somefile&#39;;</pre><pre>const doSomething = (oneParam, callback) =&gt; {<br> const generatedValue = oneDependency.doSomethingElse(oneParam);<br> // Let your mind to fly<br> return fetch(&#39;api/&#39; + generatedValue).then(callback);<br>}</pre><pre>export const doSomethingFactory = (oneDependency, fetch) =&gt; {<br> return doSomething.bind(null, oneDependency, fetch);<br>}</pre><pre>import { <strong>doSomethingFactory</strong> } from &#39;./path/to/your/factory&#39;;</pre><pre><strong>describe</strong>(&#39;DoSomething test suite&#39;, () =&gt; {<br> let service, fakeOneDependency, fakeFetch;</pre><pre><strong>it</strong>(&#39;Should be defined&#39;, (done) =&gt; {<br> // Given fake dependencies<br> fakeOneDependency = { doSomethingElse: () =&gt; ({mock: &#39;value&#39;})};<br> fakeFetch = () =&gt; ({ then: () =&gt; ({fake: &#39;object&#39;})});<br><strong> // When </strong><br><strong> service = doSomethingFactory(fakeDependency, fakeFetch);<br></strong> // Then <strong><br> </strong> <strong>service</strong>(&#39;the oneParam value&#39;, (response) =&gt; {<br> expect(response.fake).toEqual(&#39;object&#39;);<br> });<br> });<br>})</pre><p>By doing this you have total control on you test to provide the behavior that drives the right expectation you have about your code, this is the extremely easy way to improve and sanitize your testing set.</p><p><strong>Code coverage BTW will be happy too! ;)</strong></p><p><em>Originally published at </em><a href="https://medium.com/@eliasmsedano/javascript-testing-test-external-or-global-dependencies-like-window-fetch-etc-7693e4dab4e4"><em>medium.com</em></a><em> on July 3, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac5bd9fca716" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/javascript-testing-test-external-or-global-dependencies-like-window-fetch-etc-ac5bd9fca716">Javascript testing: Test external or global dependencies like ‘window’, ‘fetch’ etc</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>