postgres

ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :tenant, :request_id

  def tenant=(tenant)
    super
    Rails.logger.tagged("tenant=#{tenant&.id}") if tenant

Tenant Isolation in Rails with CurrentAttributes and an around_action

rails multi-tenancy current-attributes
by codesnips 4 tabs
python
import base64
import json
from datetime import datetime


class InvalidCursor(Exception):

Cursor-Based Pagination for a Flask JSON API Blueprint

flask pagination cursor
by codesnips 3 tabs
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
typescript
import { Injectable, Scope } from '@nestjs/common';
import { DataSource, EntityManager, QueryRunner } from 'typeorm';

@Injectable({ scope: Scope.REQUEST })
export class TransactionContext {
  private queryRunner?: QueryRunner;

Request-Scoped TypeORM QueryRunner Provider for Transactional Writes in NestJS

nestjs typeorm transactions
by codesnips 3 tabs
java
package com.example.docs.domain;

import jakarta.persistence.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

Soft-Delete JPA Entities with Hibernate @SQLDelete and @Where Filtering

spring-boot hibernate jpa
by codesnips 3 tabs
python
import uuid
from django.db import models
from django.utils import timezone


class OutboxManager(models.Manager):

Transactional Outbox with Timer-Based Flushing and Exponential Backoff in Django

django outbox postgres
by codesnips 3 tabs
typescript
import { Knex } from 'knex';

export interface RawDailyRow {
  day: string;
  order_count: string;
  gross_cents: string;

Aggregate Daily Order Totals Into a Report With Knex and a Formatter

knex postgres reporting
by codesnips 3 tabs
ruby
class Cart < ApplicationRecord
  has_many :cart_items, dependent: :destroy

  EXPIRY_WINDOW = 2.hours

  scope :active, -> { where(state: :active) }

Expire Stale Shopping Carts With a Rails Scope and a Recurring Reaper Job

rails activerecord background-jobs
by codesnips 3 tabs
python
import base64
import json

from django.core.exceptions import BadRequest

Cursor-Paginated JSON Feed with a Django Class-Based ListView

django pagination cursor-pagination
by codesnips 3 tabs
go
package export

import (
	"encoding/csv"
	"log"
	"net/http"

Streaming Large CSV Exports in Go Without Buffering the Whole File

go http csv
by codesnips 3 tabs
ruby
class OverdueInvoicesQuery
  def initialize(relation: Invoice.all, as_of: Time.current)
    @relation = relation
    @as_of = as_of
  end

ActiveRecord::Relation as a Boundary (No Arrays)

rails activerecord query-objects
by codesnips 3 tabs
ruby
class AddVersionsToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :versions, :jsonb, null: false, default: []
    add_index :articles, :versions, using: :gin
  end
end

Versioning Rails Records with a JSON Snapshot Column and Diff Helper

rails postgres jsonb
by codesnips 3 tabs