postgres

ruby
class Reminder < ApplicationRecord
  belongs_to :user

  validates :time_zone, inclusion: { in: ActiveSupport::TimeZone::MAPPING.values }
  validates :local_time, presence: true

Time Zone Safe Scheduling

rails timezone scheduling
by codesnips 3 tabs
go
package store

import (
  "context"

  "github.com/jackc/pgx/v5"

Row-level locking with SELECT ... FOR UPDATE in a transaction

go postgres transactions
by Leah Thompson 1 tab
javascript
class BatchLoader {
  constructor(batchFn, { cacheKeyFn = (k) => k } = {}) {
    this.batchFn = batchFn;
    this.cacheKeyFn = cacheKeyFn;
    this.cache = new Map();
    this.queue = [];

Coalescing Concurrent Reads with a DataLoader-Style Batch Loader in Node

dataloader batching graphql
by codesnips 3 tabs
ruby
class InventoryLevel < ApplicationRecord
  belongs_to :warehouse

  UPSERT_COLUMNS = %w[warehouse_id sku on_hand reserved updated_at].freeze

  def self.upsert_counts(rows)

Bulk-Upsert Inventory Counts in One Query From a Sidekiq Job

rails postgres upsert
by codesnips 3 tabs
sql
CREATE TABLE password_reset_tokens (
  id          BIGSERIAL PRIMARY KEY,
  user_id     BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  token_hash  TEXT NOT NULL,
  expires_at  TIMESTAMPTZ NOT NULL,
  used_at     TIMESTAMPTZ,

Password reset tokens: hash + expiry

security express authentication
by codesnips 3 tabs
typescript
import { QueryResultRow } from 'pg';
import { pool, Queryable } from './db';

export abstract class BaseRepository<T extends QueryResultRow> {
  protected abstract readonly table: string;

Repository pattern for DB access (small, pragmatic)

node postgres architecture
by codesnips 4 tabs
ruby
class Order < ApplicationRecord
  class InvalidTransition < StandardError; end

  enum status: { pending: 0, paid: 1, shipped: 2, cancelled: 3 }

  has_many :order_transitions, -> { order(:created_at) }, dependent: :destroy

Order State Machine With Guarded Transitions and an Audit Trail in Rails

rails state-machine activerecord
by codesnips 3 tabs
go
package health

import (
	"context"
	"sync"
	"time"

Concurrent Readiness Health Check Endpoint in Go with Timeouts

go health-check readiness
by codesnips 3 tabs
ruby
class User < ApplicationRecord
  normalizes :phone, with: ->(value) { PhoneNormalizer.call(value) }, apply_to_nil: false

  validates :phone,
            presence: true,
            uniqueness: { case_sensitive: false },

Normalizing Phone Numbers on Assignment with Rails normalizes and a Custom Serializer

rails activerecord normalization
by codesnips 3 tabs
javascript
const fs = require('fs');
const readline = require('readline');

async function* streamCsvRows(filePath) {
  const stream = fs.createReadStream(filePath, { encoding: 'utf8' });
  const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });

Stream and Import a Large CSV File Line-by-Line with Node.js readline

nodejs streams readline
by codesnips 3 tabs
go
package migrate

import (
  "context"

  "github.com/jackc/pgx/v5/pgxpool"

Run database migrations on startup (with a guard)

go postgres migrations
by Leah Thompson 1 tab