typescript
import { startOfDay, isToday, isYesterday, format } from 'date-fns';

export interface ChatMessage {
  id: string;
  senderId: string;
  senderName: string;

Group Chat Messages by Day with a Memoized Selector in React

react typescript selectors
by codesnips 3 tabs
java
@Configuration
public class TxConfig {

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager tm = new DataSourceTransactionManager(dataSource);

Multi-Step Order Flow With Spring TransactionTemplate and Savepoint Rollback

spring-boot transactions transactiontemplate
by codesnips 3 tabs
erb
{% load cache %}
<section class="dashboard">
  <h1>{{ team.name }} — Overview</h1>

  {% cache 3600 team_order_stats team.id stats_version %}
    <div class="stat-grid">

Cache an Expensive Dashboard Fragment with Django's Template Cache Tag and Signal-Based Invalidation

django caching template-cache
by codesnips 3 tabs
go
package pipeline

import (
	"bufio"
	"errors"
	"io"

Stream and Transform a Large Log File Line-by-Line with bufio.Scanner

go streaming bufio
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Batching Laravel Queue Jobs to Roll Up Daily Order Totals With a Completion Callback

laravel queues background-jobs
by codesnips 3 tabs
ruby
class OrderCreationService
  Result = Struct.new(:success?, :order, :error, keyword_init: true)

  def initialize(customer:, line_params:)
    @customer = customer
    @line_params = line_params

Transactional Order Creation With Nested Savepoints in Rails

rails activerecord postgres
by codesnips 3 tabs
typescript
import { CanDeactivateFn } from '@angular/router';
import { Observable } from 'rxjs';

export interface HasUnsavedChanges {
  canDeactivate(): boolean | Observable<boolean>;
}

Warn Users About Unsaved Changes With an Angular CanDeactivate Guard

angular routing guards
by codesnips 3 tabs
javascript
const { z } = require('zod');

const envSchema = z
  .object({
    NODE_ENV: z
      .enum(['development', 'test', 'production'])

Validate Environment Variables at Startup with a Zod Schema in Node.js

nodejs zod configuration
by codesnips 3 tabs
go
package worker

import (
	"context"
	"errors"
	"log"

Fan-Out Worker Pool With Context Cancellation and Graceful Shutdown in Go

go concurrency goroutines
by codesnips 2 tabs
ruby
class OnboardingForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  STEPS = %i[account profile preferences].freeze

Multi-Step Wizard Form in Rails with a Form Object and Session-Backed State

rails form-objects activemodel
by codesnips 3 tabs
java
package com.example.payments;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.stereotype.Service;

Instrumenting a Spring Service with Micrometer Counters, Timers, and @Timed

spring-boot micrometer metrics
by codesnips 3 tabs
python
from django.conf import settings
from django.db import models
from django.utils import timezone


class InvoiceQuerySet(models.QuerySet):

Scope a Django DetailView to the Request User with get_queryset

django authorization class-based-views
by codesnips 3 tabs