java
public record User(Long id, String email, String displayName, boolean active) {

    public static User of(String email, String displayName) {
        return new User(null, email, displayName, true);
    }

Efficient JDBC Batch Inserts With addBatch, executeBatch, and Generated Keys

jdbc batch-insert postgres
by codesnips 3 tabs
typescript
import sharp from 'sharp';

export interface Variant {
  name: string;
  width: number;
  quality: number;

Generate WebP Image Thumbnails on Upload with Sharp and Express

express sharp image-processing
by codesnips 3 tabs
javascript
import { useCallback, useMemo, useState } from 'react';

function compare(a, b) {
  if (typeof a === 'number' && typeof b === 'number') return a - b;
  return String(a ?? '').localeCompare(String(b ?? ''));
}

Building a Paginated, Sortable Data Table with a useTable Hook in React

react hooks pagination
by codesnips 3 tabs
python
import logging
from typing import Awaitable, Callable, List, Tuple

log = logging.getLogger("saga")

Compensation = Callable[[], Awaitable[None]]

Saga-Style Rollback With a Context-Managed Compensating Action Stack

saga rollback context-manager
by codesnips 3 tabs
rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum WebhookEvent {
    #[serde(rename = "payment.succeeded")]

Tagged Webhook Deserialization and Typed Handler Dispatch in Rust with Serde

webhooks serde axum
by codesnips 3 tabs
go
package pqueue

type Job struct {
	ID       string
	Payload  interface{}
	Priority int

Priority Job Queue in Go Backed by container/heap

go container-heap priority-queue
by codesnips 3 tabs
ruby
module Paginatable
  extend ActiveSupport::Concern

  Page = Struct.new(:records, :next_cursor, keyword_init: true)

  DEFAULT_LIMIT = 25

Cursor-Paginated Rails API with ETag and Conditional GET Caching

rails api http-caching
by codesnips 3 tabs
php
<?php

namespace App\Controller;

use App\Entity\Tenant;
use App\Repository\InvoiceRepository;

Resolve the Current Tenant from the Request Host with a Symfony Argument Resolver

symfony multi-tenancy value-resolver
by codesnips 4 tabs
java
package com.example.audit.config;

import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

Automatic JPA Auditing in Spring Boot with @CreatedDate and @LastModifiedBy

spring-boot spring-data-jpa auditing
by codesnips 4 tabs
plaintext
model User {
  id        String    @id @default(uuid())
  email     String    @unique
  name      String
  posts     Post[]
  deletedAt DateTime?

Soft-Delete and Restore in TypeScript with a Prisma Repository and Migration

prisma postgres soft-delete
by codesnips 4 tabs
ruby
class AddSlugToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :slug, :string, null: false, default: ""
    add_index :articles, :slug, unique: true

    # Backfill existing rows before the unique index is relied upon in code.

Generate Unique URL Slugs in Rails with before_validation and a friendly Controller Lookup

rails activerecord slugs
by codesnips 3 tabs
javascript
import React, { createContext, useCallback, useMemo, useRef, useState } from 'react';
import { ToastViewport } from './ToastViewport';

export const ToastContext = createContext(undefined);

let counter = 0;

Build a React Toast Notification System with Context Provider, Hook, and Portal

react toast context-api
by codesnips 4 tabs