ruby
class AddSoftDeleteToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :deleted_at, :datetime

    add_index :users, :deleted_at, where: "deleted_at IS NULL", name: "index_users_on_live"

Soft-Delete with a Default Scope, Restore Action, and Unique Index Guard in Rails

rails activerecord soft-delete
by codesnips 3 tabs
javascript
import { useCallback, useEffect, useState } from 'react';

function parseSearch(search) {
  const params = new URLSearchParams(search);
  const out = {};
  for (const [key, value] of params.entries()) out[key] = value;

Sync React Form State to the URL Query String with a Debounced useSearchParams Hook

react hooks url-state
by codesnips 3 tabs
go
package upload

import (
	"errors"
	"io"
	"log"

Streaming Multipart File Uploads to Disk in Go net/http

go net-http multipart
by codesnips 3 tabs
java
package com.example.users.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

Mapping JPA Entities to DTOs with a MapStruct Mapper Injected into a Spring Service

spring-boot mapstruct dto
by codesnips 4 tabs
typescript
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { TenantService } from './tenant.service';

@Injectable()
export class TenantContextMiddleware implements NestMiddleware {

Bind Tenant Context Per Request in NestJS With a Custom @TenantId() Decorator

nestjs multi-tenancy decorators
by codesnips 4 tabs
python
import queue
import time
from dataclasses import dataclass, field, replace
from typing import Any, Dict, Tuple

In-Process Threaded Background Job Queue for Sending Emails Without Redis

background-jobs threading queue
by codesnips 4 tabs
ruby
class ProjectPresenter
  def initialize(project, include_tasks: true)
    @project = project
    @include_tasks = include_tasks
  end

Building Nested JSON Presenters in Rails Without ActiveModel::Serializer

rails presenter json
by codesnips 3 tabs
javascript
const { EventEmitter } = require('events');

class NotificationBus extends EventEmitter {
  constructor(bufferSize = 100) {
    super();
    this.setMaxListeners(0);

Server-Sent Events for Live Notifications with a Reconnectable EventSource Hook

sse server-sent-events eventsource
by codesnips 3 tabs
ruby
require 'sinatra'
require 'json'
require_relative 'book'
require_relative 'helpers'

set :books, {

Sinatra Content Negotiation: Serve HTML or JSON by Accept Header

sinatra rack content-negotiation
by codesnips 4 tabs
rust
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tokio::time::sleep;

#[derive(Debug)]

Rate-Limiting Outbound HTTP with a Token Bucket in Rust (Tokio)

rust tokio rate-limiting
by codesnips 3 tabs
javascript
const jwt = require('jsonwebtoken');

const SECRET = process.env.JWT_SECRET;
const ALGORITHM = 'HS256';
const ACCESS_TTL = '15m';

JWT Authentication Middleware in Express That Populates req.user

express jwt authentication
by codesnips 3 tabs
typescript
import { AsyncLocalStorage } from 'node:async_hooks';

interface Store {
  requestId: string;
}

Propagate a Request ID Through NestJS Logs with AsyncLocalStorage

nestjs logging middleware
by codesnips 4 tabs