惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
D
Docker
K
Kaspersky official blog
Latest news
Latest news
P
Privacy International News Feed
雷峰网
雷峰网
S
Security Affairs
博客园 - 司徒正美
博客园 - Franky
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
C
Check Point Blog
T
Tailwind CSS Blog
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
L
Lohrmann on Cybersecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog
The Hacker News
The Hacker News
V
V2EX
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
S
Securelist
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
The Cloudflare Blog
小众软件
小众软件
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Register - Security
The Register - Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Build a Notes App with ctrodb, Tailwind CSS, and Vanilla JavaScript
Odejobi Abiola Samuel · 2026-06-22 · via DEV Community

Let's build a notes app that runs entirely in the browser. Create, pin, search, and delete notes — all persisted to IndexedDB. No backend, no build step, no npm install.

We'll use ctrodb for the database, Tailwind CSS for styling, and vanilla JavaScript for the glue.

What we're building

A single HTML file with:

  • Add notes with title, body, and tags
  • Pin important notes to the top
  • Search notes by title
  • Delete notes
  • Persistence across page reloads
  • Dark mode UI

Setting up

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ctrodb Notes</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen p-4 sm:p-6">

ctrodb ships as an IIFE that exposes a CtroDB global:

<script src="https://unpkg.com/ctrodb@1.0.1/dist/index.global.js"></script>
<script>
  const { Database } = CtroDB

No bundler, no build step, no npm install.

Schema design

const schema = {
  version: 1,
  collections: {
    notes: {
      fields: {
        title: { type: "string", required: true },
        body: { type: "string", default: "" },
        tags: { type: "array", items: { type: "string" }, default: [] },
        pinned: { type: "boolean", default: false },
        createdAt: { type: "string", default: () => new Date().toISOString() },
      },
      indexes: [
        { field: "createdAt" },
        { field: "pinned" },
      ],
    },
  },
}

The schema validates every write. A note without a title throws a ValidationError. Defaults fill in missing fields.

Connecting

const db = new Database({ name: "notes-app", schema })
await db.connect()
const notes = db.collection("notes")

Rendering notes

Each note card carries its own id directly in the markup via a data-id attribute:

function noteCard(note) {
  const isPinned = note.pinned
  return `
    <div class="bg-gray-800 rounded-lg p-4 border ..." data-id="${note.id}">
      <h3>${escapeHtml(note.title)}</h3>
      <button class="pin-btn ...">${isPinned ? "\u2605" : "\u2606"}</button>
      <button class="delete-btn ...">\u2715</button>
    </div>
  `
}

async function render(message) {
  const query = document.getElementById("search").value.trim()
  const list = query
    ? await notes.query().search("title", query).fetch()
    : await notes.query().sort({ pinned: "desc", createdAt: "desc" }).fetch()

  if (list.length === 0) {
    container.innerHTML = `<p>${message || "No notes yet."}</p>`
    return
  }

  container.innerHTML = list.map(noteCard).join("")
}

Notice render() only builds HTML — it doesn't attach any click handlers. That's deliberate, and it's the key to the next section.

Wiring up pin and delete with event delegation

A common first instinct is to attach a click listener to every .pin-btn and .delete-btn right after rendering, capturing the note from the list array by index:

// Don't do this — see below for why
container.querySelectorAll(".pin-btn").forEach((btn, i) => {
  btn.addEventListener("click", async () => {
    const note = list[i]
    await note.update({ pinned: !note.pinned })
    await render()
  })
})

This works at first glance, but it has two real problems. Every call to render() re-attaches a fresh batch of listeners on top of whatever's still pending from the previous render, and the handler closes over the list array by position — so if a search or sort ever reorders things between render and click, the index can point at the wrong note.

The more robust pattern is event delegation: attach a single listener to the container once, and read the note's id off the clicked card's data-id attribute at click time.

container.addEventListener("click", async (e) => {
  const card = e.target.closest("[data-id]")
  if (!card) return
  const id = Number(card.dataset.id)

  if (e.target.closest(".pin-btn")) {
    const note = await notes.get(id)
    if (note) {
      await note.update({ pinned: !note.pinned })
      await render()
    }
    return
  }

  if (e.target.closest(".delete-btn")) {
    await notes.delete(id)
    await render()
  }
})

One listener, attached once in init(), handles every note — present and future — since clicks bubble up to container regardless of how many times innerHTML gets replaced.

The one gotcha: dataset.id is always a string

This is the part that trips people up, so it's worth calling out explicitly. element.dataset.id always returns a string"1", "2", and so on — because HTML attributes are text. But ctrodb's default browser adapter is IndexedDB, and with keyPath: "id", autoIncrement: true, the ids it actually stores are numbers.

IndexedDB key lookups are type-sensitive: the string "1" and the number 1 are different keys. So notes.get(card.dataset.id) silently fails to find anything — no error, no thrown exception, just an undefined result that your if (note) guard quietly swallows. From the UI, it just looks like the pin and delete buttons don't do anything.

The fix is the Number(...) call above:

const id = Number(card.dataset.id)

Easy to miss, easy to fix, and a good reminder that anything pulled from dataset needs to be coerced back to its real type before you use it as a database key.

Adding notes

document.getElementById("add-note-btn").addEventListener("click", async () => {
  const title = document.getElementById("title").value.trim()
  const body = document.getElementById("body").value.trim()
  const tags = document.getElementById("tags").value
    .split(",").map(t => t.trim()).filter(Boolean)

  if (!title) { alert("Title is required."); return }

  await notes.create({ title, body, tags })
  clearForm()
  await render()
})

Search

A 200ms debounce avoids excessive queries:

let searchTimer
document.getElementById("search").addEventListener("input", () => {
  clearTimeout(searchTimer)
  searchTimer = setTimeout(() => render(), 200)
})

query().search("title", query) does case-insensitive substring matching.

Complete code

Here's the full HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ctrodb Notes</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <script>
    tailwind.config = { darkMode: "class" }
  </script>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen p-4 sm:p-6">
  <div class="max-w-2xl mx-auto">

    <h1 class="text-3xl font-bold mb-8 flex items-center gap-3">
      <span class="text-blue-400">&#9670;</span>
      ctrodb Notes
    </h1>

    <!-- Search -->
    <div class="bg-gray-800 rounded-lg p-4 mb-6 border border-gray-700">
      <input
        id="search"
        type="text"
        placeholder="Search notes by title..."
        class="w-full bg-gray-700 rounded px-3 py-2 text-gray-100 placeholder-gray-400 border border-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
      />
    </div>

    <!-- New Note Form -->
    <div class="bg-gray-800 rounded-lg p-4 mb-6 border border-gray-700">
      <h2 class="font-semibold mb-3 text-gray-300">New Note</h2>
      <input
        id="title"
        type="text"
        placeholder="Title (required)"
        required
        class="w-full bg-gray-700 rounded px-3 py-2 mb-3 text-gray-100 placeholder-gray-400 border border-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
      />
      <textarea
        id="body"
        placeholder="Body (optional)"
        rows="3"
        class="w-full bg-gray-700 rounded px-3 py-2 mb-3 text-gray-100 placeholder-gray-400 border border-gray-600 focus:outline-none focus:border-blue-500 transition-colors resize-none"
      ></textarea>
      <input
        id="tags"
        type="text"
        placeholder="Tags (comma-separated)"
        class="w-full bg-gray-700 rounded px-3 py-2 mb-3 text-gray-100 placeholder-gray-400 border border-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
      />
      <button
        id="add-note-btn"
        class="bg-blue-600 hover:bg-blue-500 text-white px-4 py-2 rounded font-medium transition-colors"
      >
        + Add Note
      </button>
    </div>

    <!-- Notes List -->
    <div id="notes-container" class="space-y-3"></div>

  </div>

  <script src="https://unpkg.com/ctrodb@1.0.1/dist/index.global.js"></script>
  <script>
  const { Database } = CtroDB

  const schema = {
    version: 1,
    collections: {
      notes: {
        fields: {
          title: { type: "string", required: true },
          body: { type: "string", default: "" },
          tags: { type: "array", items: { type: "string" }, default: [] },
          pinned: { type: "boolean", default: false },
          createdAt: { type: "string", default: () => new Date().toISOString() },
        },
        indexes: [
          { field: "createdAt" },
          { field: "pinned" },
        ],
      },
    },
  }

  let db, notes, container

  function escapeHtml(str) {
    const div = document.createElement("div")
    div.textContent = str
    return div.innerHTML
  }

  function noteCard(note) {
    const isPinned = note.pinned
    return `
      <div class="bg-gray-800 rounded-lg p-4 border transition-colors ${isPinned ? "border-yellow-500/60" : "border-gray-700"}" data-id="${note.id}">
        <div class="flex items-start justify-between gap-4">
          <div class="flex-1 min-w-0">
            <h3 class="font-semibold text-lg truncate">${escapeHtml(note.title)}</h3>
            ${note.body ? `<p class="text-gray-400 mt-1 line-clamp-3">${escapeHtml(note.body)}</p>` : ""}
            ${note.tags.length ? `
              <div class="flex flex-wrap gap-2 mt-3">
                ${note.tags.map(t => `<span class="text-xs bg-gray-700 text-gray-300 px-2 py-0.5 rounded-full">${escapeHtml(t)}</span>`).join("")}
              </div>
            ` : ""}
            <p class="text-xs text-gray-500 mt-3">${new Date(note.createdAt).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}</p>
          </div>
          <div class="flex gap-1 shrink-0">
            <button class="pin-btn p-1.5 rounded hover:bg-gray-700 transition-colors ${isPinned ? "text-yellow-400" : "text-gray-500 hover:text-yellow-400"}" title="${isPinned ? "Unpin" : "Pin"}">
              ${isPinned ? "\u2605" : "\u2606"}
            </button>
            <button class="delete-btn p-1.5 rounded hover:bg-gray-700 text-gray-500 hover:text-red-400 transition-colors" title="Delete">\u2715</button>
          </div>
        </div>
      </div>
    `
  }

  function clearForm() {
    document.getElementById("title").value = ""
    document.getElementById("body").value = ""
    document.getElementById("tags").value = ""
  }

  async function render(message) {
    const query = document.getElementById("search").value.trim()
    const list = query
      ? await notes.query().search("title", query).fetch()
      : await notes.query().sort({ pinned: "desc", createdAt: "desc" }).fetch()

    if (list.length === 0) {
      container.innerHTML = `<p class="text-gray-500 text-center py-8">${message || (query ? "No notes match your search." : "No notes yet. Create one above!")}</p>`
      return
    }

    container.innerHTML = list.map(noteCard).join("")
  }

  async function init() {
    db = new Database({ name: "notes-app", schema })
    await db.connect()
    notes = db.collection("notes")
    container = document.getElementById("notes-container")

    // Add note
    document.getElementById("add-note-btn").addEventListener("click", async () => {
      const title = document.getElementById("title").value.trim()
      const body = document.getElementById("body").value.trim()
      const rawTags = document.getElementById("tags").value

      if (!title) { alert("Title is required."); return }

      const tags = rawTags.split(",").map(t => t.trim()).filter(Boolean)
      await notes.create({ title, body, tags })
      clearForm()
      await render()
    })

    // Search with debounce
    let searchTimer
    document.getElementById("search").addEventListener("input", () => {
      clearTimeout(searchTimer)
      searchTimer = setTimeout(() => render(), 200)
    })

    // Event delegation for pin and delete
    container.addEventListener("click", async (e) => {
      const card = e.target.closest("[data-id]")
      if (!card) return
      const id = Number(card.dataset.id)

      if (e.target.closest(".pin-btn")) {
        const note = await notes.get(id)
        if (note) {
          await note.update({ pinned: !note.pinned })
          await render()
        }
        return
      }

      if (e.target.closest(".delete-btn")) {
        await notes.delete(id)
        await render()
      }
    })

    await render()
  }

  init()
  </script>
</body>
</html>

Save it as notes.html and open it in any browser. Add notes, pin them, search, delete — all operations work with full IndexedDB persistence.

What's happening

  • ctrodb connects to IndexedDB, creates the notes-app database
  • The schema validates every write — wrong types or missing required fields throw
  • query().search("title", query) does case-insensitive substring matching
  • A single delegated click listener on #notes-container handles pin and delete for every note, current and future — no listeners to re-attach on each render
  • Ids read from data-id are coerced with Number(...) before being passed back to ctrodb, since IndexedDB's autoincrement keys are numeric but dataset values are always strings
  • Everything runs in-process — no network requests, no server

Try the browser playground at https://ctrodb.vercel.app/playground.


ctrodb is open source. GitHub: https://github.com/ctrotech-tutor/ctrodb. Docs: https://ctrodb.vercel.app/docs/getting-started/quick-start.