kemalcr/kemal/kemal-database

Claude Code and agent skill by kemalcr/kemal

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Database initialization and interaction with SQLite and raw SQL in Kemal, following established project patterns.

Install

npx vskill@latest install kemalcr/kemal/kemal-database

SKILL.md

The same SKILL.md was published earlier as kemalcr/kemal/kemal-database.

Kemal Database Integration

This skill provides expert guidance on integrating SQLite databases with Kemal using the db and sqlite3 shards, strictly following patterns from `kemal-by-example`.

Core Mandates

  • Dependencies: Always require "db" and require "sqlite3" in the main application file. Add them to shard.yml.
  • Connection: Centralize the database connection in a config/database.cr file. Use environment variables for the URL:

```crystal module MyProject module Database extend self DATABASE_URL = ENV["DATABASE_URL"]? || "sqlite3:./db/database.db"

@@mutex = Mutex.new @@connection : DB::Database? = nil

def connection : DB::Database @@connection || @@mutex.synchronize { @@connection ||= DB.open(DATABASE_URL) } end end end ```

  • Schema Setup: Use a Schema.setup method to run CREATE TABLE IF NOT EXISTS statements:

```crystal module MyProject module Schema extend self

def setup Database.connection.exec <<-SQL CREATE TABLE IF NOT EXISTS items ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, created_at TEXT NOT NULL ) SQL end end end ```

  • Model Mapping: Use include DB::Serializable in models for automatic mapping from SQL results:

``crystal class Item include DB::Serializable property id : Int64? property name : String property created_at : String end ``

  • Data Retrieval: Use query_all and query_one? with explicit column lists and the as: Class argument:
  • Database.connection.query_all("SELECT id, name, created_at FROM items", as: Item)
  • Database.connection.query_one?("SELECT id, name, created_at FROM items WHERE id = ?", id, as: Item)

Patterns from Source Code

Database Module Pattern (All Projects)
crystal
# config/database.cr
module Blog
module Database
extend self
DATABASE_URL = ENV["DATABASE_URL"]? || "sqlite3:./db/blog.db"
@@mutex = Mutex.new
@@connection : DB::Database? = nil
def connection : DB::Database
@@connection || @@mutex.synchronize { @@connection ||= DB.open(DATABASE_URL) }
end
end
end
Schema Setup with Multiple Tables
crystal
# config/schema.cr
module Ecommerce
module Schema
extend self
def setup
Database.connection.exec <<-SQL
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)
SQL
Database.connection.exec <<-SQL
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
price_cents INTEGER NOT NULL,
inventory_count INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)
SQL
end
end
end
Model with DB::Serializable
crystal
# models/post.cr
class Post
include DB::Serializable
getter id : Int64?
getter title : String
getter body : String
getter created_at : String
getter updated_at : String
def initialize(
@title : String,
@body : String,
@id : Int64? = nil,
@created_at : String = Time.utc.to_s,
@updated_at : String = Time.utc.to_s,
)
end
def self.all : Array(Post)
Blog::Database.connection.query_all(
"SELECT id, title, body, created_at, updated_at FROM posts ORDER BY created_at DESC",
as: Post
)
end
def self.find(id : Int64) : Post?
Blog::Database.connection.query_one?(
"SELECT id, title, body, created_at, updated_at FROM posts WHERE id = ?",
id,
as: Post
)
end
def self.create(title : String, body : String) : Post
now = Time.utc.to_s
# `Database.connection` is a pooled `DB::Database`: a separate
# `SELECT last_insert_rowid()` may run on a different connection and return
# the wrong id. Read the id from the `DB::ExecResult` of the INSERT itself.
result = Blog::Database.connection.exec(
"INSERT INTO posts (title, body, created_at, updated_at) VALUES (?, ?, ?, ?)",
title,
body,
now,
now
)
id = result.last_insert_id
find(id) || raise "Failed to load post ##{id} after creation"
end
def update(title : String, body : String)
Blog::Database.connection.exec(
"UPDATE posts SET title = ?, body = ?, updated_at = ? WHERE id = ?",
title,
body,
Time.utc.to_s,
@id
)
end
def delete
Blog::Database.connection.exec(
"DELETE FROM posts WHERE id = ?",
@id
)
end
end
Initialization in Main File
crystal
# blog.cr
require "kemal"
require "db"
require "sqlite3"
require "./config/database"
require "./config/schema"
require "./models/post"
require "./routes/home"
require "./routes/posts"
Blog::Schema.setup
Kemal.run

Best Practices

  • Initialization: Call Schema.setup before Kemal.run in the main application file.
  • Timestamps: Always use Time.utc.to_s for storing created_at and updated_at as TEXT in SQLite.
  • ID Handling: Use Int64? for IDs to accommodate auto-incrementing fields during creation.
  • Transactions: Use Database.connection.transaction do |tx| ... end for atomic operations.
  • SQL Execution: Use Database.connection.exec("SQL", args) for insert, update, and delete operations.
  • Explicit Column Lists: List all columns explicitly in SELECT statements rather than using SELECT *.

When to Use

  • When setting up a new database or modifying an existing schema.
  • When implementing model methods that interact with the database.
  • When configuring database connections and initialization logic.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
3,918
Repository forks
205
7-day trend score
41

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/kemalcr/kemal/kemal-database/badge)](https://verified-skill.com/skills/kemalcr/kemal/kemal-database)

Something wrong with this skill or its page? Tell us.