TILs - Fueling Curiosity, One Insight at a Time

At Codemancers, we believe every day is an opportunity to grow. This section is where our team shares bite-sized discoveries, technical breakthroughs and fascinating nuggets of wisdom we've stumbled upon in our work.

Published
Author
user-image
Vaibhav Yadav
Senior System Analyst
Using choice Input in GitHub Actions

Choice input in Github actions can be used to provide a predefined list of options for workflow dispatch events. This makes it easier for users to select from a set of valid options when triggering workflows manually.

Example:


Code

name: Example Workflow

on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Select the environment'
        required: true
        default: 'sandbox'
        type: choice
        options:
          - sandbox
          - uat
          - production

jobs:
  example_job:
    runs-on: ubuntu-latest
    steps:
      - name: Display Inputs
        run: |
          echo "Environment: ${{ github.event.inputs.environment }}"


#githubactions #devops #automation #dropdown
Published
Author
user-image
Adithya Hebbar
System Analyst
To generate an Entity-Relationship Diagram (ERD) using Prisma-erd-generator, follow these steps :
Install the following package

Code

npm i -D prisma-erd-generator @mermaid-js/mermaid-cli
# or
yarn add -D prisma-erd-generator @mermaid-js/mermaid-cli


Add this to your schema.prisma

Code

generator erd {
  provider = "prisma-erd-generator"
}


Run the generator

Code

npx prisma generate


#javascript #erd
Published
Author
user-image
Adithya Hebbar
System Analyst
To use Client Components, states, useState, useEffect, onClick, and other client-side features, add "use client" at the top of your file. This ensures the component runs on the client side.

#javascript #nextjs
Published
Author
user-image
Adithya Hebbar
System Analyst
Key difference between App router and Page router :

App Router:
File-based routing: Uses nested folders to define routes.
Components: Server Components by default.
Data fetching: Uses fetch function.
Layouts: Can be nested and dynamic.
Dynamic routes: Supported, but syntax differs.
Client-side navigation: Supported with router.push.
Priority: Takes precedence over Page Router.
Page Router:
File-based routing: Files directly represent routes.
Components: Client Components by default.
Data fetching: Uses getServerSideProps, getStaticProps, getInitialProps.
Layouts: Static.
Dynamic routes: Supported.
Client-side navigation: Supported with Link component.
Priority: Fallback if no matching route in App Router.
#javascript #nextjs
Published
Author
user-image
Soniya Rayabagi
How to schedule tasks using cron expressions in GitHub Actions:
By defining a cron schedule in the workflow YAML file, we can automate the execution of tasks at specific intervals.
For instance, setting */5 * * * * in the cron expression triggers the workflow every 5 minutes.
Monitoring workflow runs in the "Actions" tab of the GitHub repository allows to verify that the scheduled tasks are executing as intended.
#cronjobs #workflowautomation
Published
Author
user-image
Mahesh Bhosle
DevOps Engineer
When terraform state file is locked and you are unable to acquire a state lock, you can use terraform force-unlock <LOCK_ID> to forcefully remove the lock.
#terrafrom
Published
Author
user-image
Vaibhav Yadav
Senior System Analyst
Use git reset --mixed HEAD~1 command to undo your last commit without losing the changes made in your last commit.

#git #gitReset #github
Published
Author
user-image
Adithya Hebbar
System Analyst
In TypeScript interfaces, ?: denotes optional properties. Here's an example:


Code

interface User {
    name: string;
    age?: number; // Optional
}

const user1: User = { name: "Joe Mama" }; // Age is optional
const user2: User = { name: "Tony", age: 30 };

console.log(user1); // Output: { name: "Joe Mama" }
console.log(user2); // Output: { name: "Tony", age: 30 }


#javascript #typescript
Published
Author
user-image
Adithya Hebbar
System Analyst
Utility Types:
TypeScript's Utility Types simplify complex type manipulations. For instance, Partial<T> makes all properties of type T optional


Code

interface User {
    id: number;
    name: string;
    email: string;
}

function displayUser(user: Partial<User>) {
    console.log(user);
}

displayuser({ name: "Joe Mama" }); // Partial<T> will make the properties optional hence this is valid


#javascript #typescript
Published
Author
user-image
Ayush Srivastava
System Analyst
EAGER LOADING

Eager loading in Rails is a technique used to optimize database queries by loading associated records of the objects returned by the query, thereby reducing the number of database calls. This is particularly useful when you have associations defined in your models and you want to avoid the "N+1 query problem."

What is the N+1 Query Problem?

“N+1 Queries” are a very common cause of repeated queries in Rails applications. This happens when you make a request for a single row in one table, and then make an additional request per element in a has_many relationship, usually in a loop.
Here’s an example:

Consider two models: Author and Book, where an Author has many Books.

Code

class Author < ApplicationRecord 
  has_many :books 
 end



Code

class Book < ApplicationRecord 
  belongs_to :author 
end


Scenario Without Eager Loading
When you fetch authors and then access their books, Rails performs an additional query for each author to get their books.

Code

# Fetching all authors and their books 
authors = Author.all 

authors.each do |author| 
  puts author.books.pluck(:title) 
end


This will result in one query to fetch the authors and multiple queries to fetch books for each author, leading to the N+1 query problem.

How Eager Loading Works

Eager loading addresses this problem by loading all the necessary data in as few queries as possible, usually through the use of includes, eager_load, or preload.

Using Eager Loading with includes

To avoid the N+1 query problem, you can use includes to preload the associated books when fetching authors.

Code

# Fetching all authors and their books with eager loading 
authors = Author.includes(:books) 

authors.each do |author| 
  puts author.books.pluck(:title) 
end


With includes, Rails performs a single query to fetch all authors and another single query to fetch all associated books.

Using Eager Loading with eager_load

eager_load forces Rails to use a SQL JOIN to load the associated records. This can be useful if you need to filter or sort based on the associated records.

Code

# Fetching all authors and their books with eager loading using JOIN 
authors = Author.eager_load(:books) 

authors.each do |author| 
  puts author.books.pluck(:title) 
end


This approach performs a single query with a LEFT OUTER JOIN.

Using Eager Loading with preload

preload is similar to includes, but it always uses separate queries for loading the associations.

Code

# Fetching all authors and their books with eager loading using separate queries 
authors = Author.preload(:books) 

authors.each do |author| 
  puts author.books.pluck(:title) 
end


preload is useful when you know that separate queries will be more efficient, for example, when fetching a large number of records.

Summary

Eager loading is a powerful tool in Rails that helps to optimize database access by pre-loading associations. Using includes, eager_load, or preload appropriately can significantly improve the performance of your application by reducing the number of database queries.

#rails #db #optimizing_queries

Showing 22 to 24 of 82 results

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals