<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emanuel Navarro</title>
    <description>The latest articles on DEV Community by Emanuel Navarro (@emanuelnav).</description>
    <link>https://dev.to/emanuelnav</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1075764%2Fb07a7911-6a68-4d79-8c5e-8f3d9ddb5e34.jpeg</url>
      <title>DEV Community: Emanuel Navarro</title>
      <link>https://dev.to/emanuelnav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emanuelnav"/>
    <language>en</language>
    <item>
      <title>How to Easily Dockerize a Next.js Application</title>
      <dc:creator>Emanuel Navarro</dc:creator>
      <pubDate>Mon, 10 Jun 2024 00:31:18 +0000</pubDate>
      <link>https://dev.to/emanuelnav/how-to-easily-dockerize-a-nextjs-application-p3f</link>
      <guid>https://dev.to/emanuelnav/how-to-easily-dockerize-a-nextjs-application-p3f</guid>
      <description>&lt;p&gt;Hi there! In this little post I'm going to show you how to use Docker to containerize your Next.js application.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications inside lightweight, portable containers. Containers are a way to package applications with their dependencies and runtime environment, allowing them to run consistently across different environments, such as development, testing, and production, without any compatibility issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a Next.js project
&lt;/h2&gt;

&lt;p&gt;1.First we need to create a nextjs application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest project_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Create Dockerfile in the root directory of your project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Dockerfile

FROM node:18-alpine

# Initialize a working directory in your new os
WORKDIR /app

# Copy package.json into the new working directory
COPY package*.json ./

RUN npm install

# Copy all the files from your current directory to the working directory of the container
COPY . .

# Expose port 3000 from your container to local network
EXPOSE 3000

CMD npm run dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Create the docker container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t next-docker-demo . 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;next-docker-demo&lt;/code&gt; is the image name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;.&lt;/code&gt; to tell docker that the Dockerfile is in current folder.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4.We can use docker compose, so we don’t need to remember long commands to build or run containers. We can simply just use &lt;code&gt;docker-compose build&lt;/code&gt; and &lt;code&gt;docker-compose up&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add a &lt;code&gt;docker-compose.yml&lt;/code&gt; file to your root directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.8'
services:
  app:
    image: next-docker-demo
    build: .
    volumes:
        - .:/app
        - /app/node_modules
        - /app/.next
    ports:
      - "3000:3000"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;-d&lt;/code&gt; to run the the container in the background.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now if you access &lt;code&gt;http://localhost:3000&lt;/code&gt;, you will see your working app!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Integrate Snowflake with Laravel</title>
      <dc:creator>Emanuel Navarro</dc:creator>
      <pubDate>Mon, 03 Jun 2024 21:29:55 +0000</pubDate>
      <link>https://dev.to/emanuelnav/integrate-snowflake-with-laravel-450g</link>
      <guid>https://dev.to/emanuelnav/integrate-snowflake-with-laravel-450g</guid>
      <description>&lt;p&gt;Snowflake is a cloud-based data warehousing service that offers high performance, scalability, and flexibility for managing large volumes of data. Using the offical pdo driver, PHP applications can directly interact with Snowflake, enabling seamless execution of queries and retrieval of data.&lt;/p&gt;

&lt;p&gt;In order to integrate Snowflake with our Laravel application using PostgreSQL we're going to use the &lt;a href="https://github.com/appsfortableau/laravel-pdo-odbc" rel="noopener noreferrer"&gt;laravel-pdo-odbc&lt;/a&gt; package and the &lt;a href="https://github.com/snowflakedb/pdo_snowflake" rel="noopener noreferrer"&gt;pdo_snowflake&lt;/a&gt; PHP PDO driver from snowflake.&lt;br&gt;
At the end of the post I leave a script with all the steps to build and install the driver in a Dockerfile.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Snowflake PHP PDO driver does not yet support ARM/AARCH64 architecture on Linux.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  PHP PDO Driver
&lt;/h2&gt;

&lt;p&gt;First we have to build and install the pdo driver.&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;To build the Snowflake PHP PDO Driver, we need to have installed the following software:&lt;/p&gt;

&lt;p&gt;On Linux:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PHP 8.1 or higher (Note: support for PHP 8.0 or lower is deprecated).&lt;/li&gt;
&lt;li&gt;gcc 5.2 or higher.&lt;/li&gt;
&lt;li&gt;cmake 2.8 or higher&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the following php modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;php-pdo&lt;/li&gt;
&lt;li&gt;pdo_pgsql&lt;/li&gt;
&lt;li&gt;pdo_odbc&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Building the Driver
&lt;/h3&gt;

&lt;p&gt;1.Set the PHP_HOME environment variable to the path to the bin directory containing the &lt;code&gt;phpize&lt;/code&gt; executable. For example, if the &lt;code&gt;phpize&lt;/code&gt; executable is in &lt;code&gt;/usr/local/bin&lt;/code&gt;, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PHP_HOME=/usr/local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Clone the &lt;code&gt;pdo_snowflake&lt;/code&gt; repository, and run the script to build the driver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/snowflakedb/pdo_snowflake.git
cd pdo_snowflake
./scripts/build_pdo_snowflake.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To verify that the driver was succesfully built, run the next command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$PHP_HOME/bin/php -dextension=modules/pdo_snowflake.so -m | grep pdo_snowflake
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should appear &lt;code&gt;pdo_snowflake&lt;/code&gt; in the output from the command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing the Driver
&lt;/h3&gt;

&lt;p&gt;1.Copy &lt;code&gt;pdo_snowflake.so&lt;/code&gt; from the modules subdirectory in the repository to the PHP extension directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp modules/pdo_snowflake.so /usr/local/lib/php/extensions/no-debug-non-zts-*/ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Copy &lt;code&gt;cacert.pem&lt;/code&gt; from the &lt;code&gt;libsnowflakeclient&lt;/code&gt; subdirectory in the repository to the PHP configuration directory containing the PHP configuration files. To find the PHP configuration directory, you can use this command &lt;code&gt;$PHP_HOME/bin/php -ini&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp ./libsnowflakeclient/cacert.pem /usr/local/etc/php/cacert.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.In the same directory that contains the PHP configuration files, create a config file named &lt;code&gt;pdo_snowflake.ini&lt;/code&gt; that contains the following settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extension=pdo_snowflake.so
pdo_snowflake.cacert=&amp;lt;path to PHP config directory&amp;gt;/cacert.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.If you are using PHP with an application server or web server (e.g. Apache or nginx), restart the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrating the PHP Driver in Laravel
&lt;/h2&gt;

&lt;p&gt;Once we have the driver set up we need to install the package to make the integration with laravel.&lt;/p&gt;

&lt;p&gt;1.To add the package to your project, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require yoramdelangen/laravel-pdo-odbc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Register the service provider in the &lt;code&gt;app&lt;/code&gt; file from you project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.php

'providers' =&amp;gt; [
  // ...
  LaravelPdoOdbc\ODBCServiceProvider::class,
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Add a Database configuration into the &lt;code&gt;database&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// database.php

'snowflake' =&amp;gt; [
    'driver' =&amp;gt; 'snowflake_native',
    'account' =&amp;gt; '{account_name}.eu-west-1',
    'username' =&amp;gt; '{username}',
    'password' =&amp;gt; '{password}',
    'database' =&amp;gt; '{database}',
    'warehouse' =&amp;gt; '{warehouse}',
    'schema' =&amp;gt; 'PUBLIC', // change it if necessary.
    'options' =&amp;gt; [
        // Required for Snowflake usage
        \PDO::ODBC_ATTR_USE_CURSOR_LIBRARY =&amp;gt; \PDO::ODBC_SQL_USE_DRIVER
    ]
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Finish! Now you can use Eloquent ORM, and other Illuminate components as usual.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Facade
$users = DB::connection('snowflake')
            -&amp;gt;table('users')
            -&amp;gt;where('created_at', '&amp;gt;', '2024-03-15')
            -&amp;gt;get();

# ORM
$users = User::all();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To know more about the PHP pdo driver and the laravel package, you can check the documentation here:&lt;/p&gt;

&lt;p&gt;PHP PDO drive: &lt;a href="https://github.com/snowflakedb/pdo_snowflake" rel="noopener noreferrer"&gt;https://github.com/snowflakedb/pdo_snowflake&lt;/a&gt;&lt;br&gt;
Laravel package: &lt;a href="https://github.com/appsfortableau/laravel-pdo-odbc" rel="noopener noreferrer"&gt;https://github.com/appsfortableau/laravel-pdo-odbc&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Dockerfile
&lt;/h2&gt;

&lt;p&gt;If you use Docker in your local environment, here is the script to add into your dockerfile to download and install the php pdo driver.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: If you are using Docker on an ARM architecture, you can force it to a x86 architecture adding &lt;code&gt;platform linux/x86_64&lt;/code&gt; on the &lt;code&gt;docker-compose.yml&lt;/code&gt; file in the container that you are installing the driver.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Dockerfile

# Install system dependencies
RUN apt-get update &amp;amp;&amp;amp; apt-get install -y \
    cmake \
    &amp;amp;&amp;amp; docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    &amp;amp;&amp;amp; apt-get clean &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/* \
    &amp;amp;&amp;amp; docker-php-ext-configure pdo_odbc --with-pdo-odbc=unixODBC,/usr \
    &amp;amp;&amp;amp; docker-php-ext-install \
    pdo \
    pdo_pgsql \
    pdo_odbc \

    ENV PHP_HOME /usr/local

    # Pull the Snowflake PDO repo and install the driver
    RUN mkdir /tmp/pdo_snowflake &amp;amp;&amp;amp; \
        git clone https://github.com/snowflakedb/pdo_snowflake.git /tmp/pdo_snowflake
    RUN cd /tmp/pdo_snowflake &amp;amp;&amp;amp; \
        bash ./scripts/build_pdo_snowflake.sh &amp;amp;&amp;amp; \
        cp modules/pdo_snowflake.so /usr/local/lib/php/extensions/no-debug-non-zts-*/ &amp;amp;&amp;amp; \
        cp ./libsnowflakeclient/cacert.pem /usr/local/etc/php/cacert.pem
    RUN echo "extension=pdo_snowflake.so" &amp;gt; /usr/local/etc/php/conf.d/pdo_snowflake.ini &amp;amp;&amp;amp; \
        echo "pdo_snowflake.cacert=/usr/local/etc/php/cacert.pem" &amp;gt;&amp;gt; /usr/local/etc/php/conf.d/pdo_snowflake.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>php</category>
      <category>snowflake</category>
      <category>database</category>
    </item>
    <item>
      <title>Logging changes in Laravel using Traits</title>
      <dc:creator>Emanuel Navarro</dc:creator>
      <pubDate>Thu, 29 Feb 2024 02:46:40 +0000</pubDate>
      <link>https://dev.to/emanuelnav/logging-changes-in-laravel-using-traits-4fo3</link>
      <guid>https://dev.to/emanuelnav/logging-changes-in-laravel-using-traits-4fo3</guid>
      <description>&lt;p&gt;In this post I'll show you how to track all the changes (created, updated and deleted) from any model that you want using laravel traits.&lt;/p&gt;

&lt;p&gt;In Laravel, traits are a powerful tool for reusing code across multiple classes. They provide a way to share methods among classes without using traditional inheritance. Traits are like "partial classes" or mixins, allowing you to group related functionality together and avoid repetitive code.&lt;/p&gt;

&lt;p&gt;First, let's create the migration for the table to record all the changes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:migration create_changelogs_table&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/database/migrations/create_changelogs_table.php

public function up(): void
{
    Schema::create('changelogs', function (Blueprint $table) {
        $table-&amp;gt;id();
        $table-&amp;gt;unsignedBigInteger('user_id')-&amp;gt;nullable();
        $table-&amp;gt;string('action');
        $table-&amp;gt;string('model', 100);
        $table-&amp;gt;json('old_values')-&amp;gt;nullable();
        $table-&amp;gt;json('new_values')-&amp;gt;nullable();
        $table-&amp;gt;json('changed_values')-&amp;gt;nullable();
        $table-&amp;gt;timestamps();
        $table-&amp;gt;foreign('user_id')-&amp;gt;references('id')-&amp;gt;on('users')
            -&amp;gt;onDelete('NO ACTION')-&amp;gt;onUpdate('NO ACTION');
    });
}

public function down(): void    
{
    Schema:dropIfExists('changelogs');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll use the models &lt;a href="https://laravel.com/docs/10.x/eloquent#events" rel="noopener noreferrer"&gt;events&lt;/a&gt; to create the custom Observable trait:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/Traits/ObservableTrait.php

&amp;lt;?php

namespace App\Traits;

use App\Models\ChangeLog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

trait ObservableTrait
{
    // The bootObservable() will be called on model instantiation automatically

    public static function bootObservable()
    {
        static::saved(function (Model $model) {
            if($model-&amp;gt;wasRecentlyCreated) {
                static::logChange($model, 'CREATED');
            } else {
                if(! $model-&amp;gt;getChanges()) {
                    return;
                }
                static::logChange($model, 'UPDATED');
            }
        });

        static::deleted(function (Model $model) {
            static::logChange($model, 'DELETED');
        });
    }

    public static function logChange(Model $model, string $action) 
    {
        ChangeLog::create([
            'user_id' =&amp;gt; Auth::check() ? Auth::user()-&amp;gt;id : null,
            'action'  =&amp;gt; $action,
            'model'   =&amp;gt; $model::class,
            'old_values' =&amp;gt; $action !== 'CREATED' ? $model-&amp;gt;getOriginal() : null,
            'new_values' =&amp;gt; $action !== 'DELETED' ? $model-&amp;gt;getAttributes() : null,
            'changed_values' =? $action == 'UPDATED' ? $model-&amp;gt;getChanges() : null,
        ]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use this trait in any model, just include the namespace and import it inside the model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/Models/User.php

&amp;lt;?php

namespace App\Models;

use App\Traits\ObservableTrait;

class User extends Authenticatable
{
    use ObservableTrait;
    //
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time you create, update or delete the model it will create a changelog record.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you want to log changes for specific cases, for example when a user log in, just use the &lt;code&gt;logChange&lt;/code&gt; method in the controller:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/Http/Controllers/Auth/LoginController.php

public function authenticated(Request $request, User $user){
    self::logChange($user, 'LOGGED');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;

&lt;p&gt;I hope you find this post helpful.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>traits</category>
      <category>logs</category>
      <category>php</category>
    </item>
  </channel>
</rss>
