Code Explainers

Code explainers tagged #database

python
from django.db import connections
from django.db.utils import OperationalError
from django.core.cache import caches
from django.http import JsonResponse

Building a health check endpoint in Django

health check monitoring database
Intermediate 7 steps
javascript
import type { MetadataRoute } from 'next'
import { db } from '@/lib/db'
 
const URLS_PER_SITEMAP = 50_000

Sharding a large sitemap in Next.js

sitemap pagination seo
Intermediate 6 steps
python
import functools
import time
 
from flask import current_app

Retrying deadlocked DB writes in Flask

decorators retry exponential-backoff
Intermediate 6 steps
python
from flask import Flask, g, current_app
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
 

Scoped SQLAlchemy sessions in Flask

database session-management connection-pooling
Intermediate 6 steps
go
func (h *ReportHandler) ExportTransactions(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
 
	from, to, err := parseDateRange(r.URL.Query())

Streaming a CSV export in Go

streaming http csv
Intermediate 8 steps
javascript
import { after } from "next/server";
import { NextResponse } from "next/server";
import { z } from "zod";
import { resend } from "@/lib/resend";

Deferring work with after() in Next.js

validation background-tasks route-handlers
Intermediate 8 steps
ruby
class BackfillUsersFullName < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!
 
  BATCH_SIZE = 5_000

How to backfill a column safely in Rails

migrations batching backfill
Intermediate 7 steps
python
from flask import Blueprint, jsonify
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
 

Building a health check endpoint in Flask

health-check blueprint error-handling
Intermediate 6 steps
python
import click
from flask.cli import AppGroup
 
from . import db

Custom user CLI commands in Flask

cli click command-group
Intermediate 7 steps
go
type PostCursor struct {
	CreatedAt time.Time
	ID        int64
}

Keyset pagination with cursors in Go

pagination keyset-cursor database
Intermediate 8 steps
java
public class KeysetPaginationRepository {
 
    private final DataSource dataSource;
 

How keyset pagination works in Java

pagination sql jdbc
Intermediate 8 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts},
    http::{request::Parts, StatusCode},
    response::{IntoResponse, Response},

A custom API-key extractor in Axum

authentication extractors trait-implementation
Advanced 8 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
java
@DataJpaTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class OrderRepositoryIT {

Testing a JPA repository against real Postgres in Spring

integration-testing testcontainers jpa
Intermediate 8 steps
php
<?php
 
namespace App\Http\Controllers;
 

Streaming a large CSV export in Laravel

streaming csv-export memory-efficiency
Intermediate 9 steps
php
public function importUsers(array $users): int
{
    $inserted = 0;
 

Bulk-inserting users with batched PDO upserts

batching bulk-insert prepared-statements
Intermediate 6 steps
go
package store
 
import (
	"database/sql"

Wrapping and inspecting errors in Go

error-handling error-wrapping sentinel-errors
Intermediate 8 steps