ruby
class CreateAuditLogs < ActiveRecord::Migration[7.1]
  def change
    create_table :audit_logs do |t|
      t.references :auditable, polymorphic: true, null: false
      t.references :user, null: true, foreign_key: true
      t.string :action, null: false

Rails Audit Log for Model Changes Using ActiveRecord Callbacks

rails activerecord callbacks
by codesnips 4 tabs
php
<?php

namespace App\Services;

use App\Models\Order;
use Illuminate\Support\Facades\Cache;

Cache Expensive Dashboard Aggregates and Bust Them on Write in Laravel

laravel caching redis
by codesnips 4 tabs
rust
use serde::{Deserialize, Deserializer};
use time::OffsetDateTime;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserProfile {

Deserializing Optional and Renamed JSON API Fields with Serde in Rust

rust serde serde-json
by codesnips 2 tabs
python
import csv


class _LineBuffer:
    def __init__(self):
        self._data = ""

Stream a Large CSV Export in Chunks from a Flask Endpoint

flask streaming csv
by codesnips 3 tabs
java
package com.example.signup.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

Custom @UniqueEmail Bean Validation Constraint in Spring Boot Signup

spring-boot bean-validation jsr380
by codesnips 4 tabs
javascript
const RETRYABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]);

export function fullJitter(attempt, baseDelay = 300, maxDelay = 10_000) {
  const ceiling = Math.min(maxDelay, baseDelay * 2 ** attempt);
  return Math.random() * ceiling;
}

Fetch With Exponential Backoff and Full Jitter Retries

fetch retry exponential-backoff
by codesnips 3 tabs
typescript
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';

export interface SearchResult {

Debounced Typeahead Search in Angular with switchMap and Cancellation

angular rxjs typeahead
by codesnips 3 tabs
ruby
require 'sinatra/base'
require 'json'

class HealthApp < Sinatra::Base
  configure do
    set :dump_errors, true

Mounting Modular Sinatra Apps Under Route Prefixes with Rack::URLMap

sinatra rack urlmap
by codesnips 3 tabs
go
package pipeline

import "context"

func generate(ctx context.Context, nums ...int) <-chan int {
	out := make(chan int)

Fan-Out/Fan-In Pipeline With Channels and Context Cancellation in Go

go concurrency channels
by codesnips 3 tabs
java
package com.shop.inventory;

import jakarta.persistence.*;

@Entity
@Table(name = "inventory_item")

Optimistic Locking on Inventory Updates with @Version and a Retrying Conflict Handler in Spring Boot

spring-boot jpa hibernate
by codesnips 3 tabs
python
from sqlalchemy import select

from .models import Order, db


def stream_orders(batch_size=1000):

Stream a Large CSV Export in Flask With a Generator Response

flask streaming csv
by codesnips 3 tabs
rust
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::mpsc;

pub fn spawn_watcher(root: &Path) -> notify::Result<(RecommendedWatcher, flume::Receiver<PathBuf>)> {
    let (raw_tx, raw_rx) = mpsc::channel::<notify::Result<Event>>();

Debouncing Rapid Filesystem Events in a Rust Directory Watcher

rust filesystem notify
by codesnips 3 tabs