php
<?php

namespace App\Entity;

trait RecordsEvents
{

Dispatch Domain Events After Doctrine Flush with a postFlush Subscriber in Symfony

symfony doctrine messenger
by codesnips 4 tabs
python
from datetime import datetime, timedelta, timezone

from fastapi.security import OAuth2PasswordBearer
from jose import jwt
from passlib.context import CryptContext

Reusable OAuth2 Password Bearer JWT Authentication Dependency in FastAPI

fastapi oauth2 jwt
by codesnips 4 tabs
typescript
import { Injectable } from '@nestjs/common';

type Completed = { status: 'completed'; statusCode: number; body: unknown; expiresAt: number };
type InFlight = { status: 'in-flight'; startedAt: number };
type Record = Completed | InFlight;

Idempotency-Key Interceptor in NestJS to Debounce Duplicate Form Submissions

nestjs idempotency interceptor
by codesnips 4 tabs
javascript
const { z } = require('zod');

const ConfigSchema = z.object({
  env: z.enum(['development', 'test', 'production']).default('development'),
  port: z.coerce.number().int().positive().default(3000),
  database: z.object({

Cached Config Singleton in Node.js: Load Once, Share Everywhere

nodejs configuration singleton
by codesnips 3 tabs
erb
<%= form_with url: bulk_update_orders_path, method: :patch, data: { turbo_confirm: "Update selected orders?" } do %>
  <div class="batch-toolbar">
    <%= select_tag :status,
          options_for_select(Order.statuses.keys.map { |s| [s.humanize, s] }),
          prompt: "Set status to\u2026" %>
    <%= submit_tag "Apply to selected", class: "btn" %>

Bulk-Updating Order Statuses with a Checkbox Collection and update_all Batch Action in Rails

rails activerecord update-all
by codesnips 3 tabs
rust
use std::collections::HashMap;
use serde_json::Value;

#[derive(Debug)]
pub enum DispatchError {
    UnknownCommand(String),

Compile-Time Command Dispatch Table With a Rust Declarative Macro

rust macros dispatch
by codesnips 3 tabs
java
package com.example.paging;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

Consume a Paginated REST API by Following Next-Page Links in Java

java pagination rest-api
by codesnips 3 tabs
python
from django.db import models
from django.utils import timezone


class WebhookEvent(models.Model):
    class Status(models.TextChoices):

Idempotent Stripe Webhook Handling in Django with a Unique Event-ID Constraint

django webhooks idempotency
by codesnips 3 tabs
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