Tauri SQLite ORM
Introduction
Welcome to Tauri SQLite ORM - A Drizzle-like ORM for Tauri v2
What is Tauri SQLite ORM?
A Drizzle-like TypeScript ORM tailored for Tauri v2's @tauri-apps/plugin-sql (SQLite). It provides a simple, type-safe query builder and migration tools to help you manage your database with ease.
Key Features
Type-Safe Query Building
Build SQL queries with TypeScript, ensuring type safety and autocompletion throughout your application.
const user = await db.select(users)
.where(eq(users._.columns.email, 'john@example.com'))
.first();
// user is fully typed! TypeScript knows all properties
user.id // ✓ Autocomplete
user.name // ✓ Type-safe
Drizzle-Like Schema Definition
Define your database schema using a familiar, chainable API:
export const users = sqliteTable('users', {
id: integer('id').primaryKey().autoincrement(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
createdAt: integer('created_at', { mode: 'timestamp' })
.$defaultFn(() => new Date()),
});
Powerful Relations
Define and query one-to-one, one-to-many, and many-to-many relationships:
const usersWithPosts = await db.select(users)
.include({
posts: {
with: { // ← Nested includes!
comments: true,
tags: true
}
}
})
.all();
Advanced Query Capabilities
Utility Methods
// Check existence
const exists = await db.select(users).where(...).exists(); // boolean
// Count rows
const count = await db.select(users).where(...).count(); // number
// Get single column values
const emails = await db.select(users).pluck('email'); // string[]
// Easy pagination
const { data, totalPages } = await db.select(users).paginate(1, 20);
Rich Operator Set
// Comprehensive operators for every need
where(ne(users._.columns.status, 'banned'))
where(between(products._.columns.price, 10, 100))
where(notIn(users._.columns.id, [1, 2, 3]))
where(startsWith(users._.columns.email, 'admin'))
where(contains(posts._.columns.title, 'TypeScript'))
where(ilike(users._.columns.name, '%john%')) // Case-insensitive
Subqueries
// Use subqueries in WHERE clauses
const engineeringUsers = await db.select(users)
.where(inArray(
users._.columns.departmentId,
subquery(db.select(departments).where(...))
))
.all();
Safety Features
WHERE Clause Validation
Prevent accidental mass updates/deletes:
// ❌ Throws error - prevents accidental data loss
await db.update(users).set({ isActive: false }).execute();
// ✅ Requires WHERE clause
await db.update(users)
.set({ isActive: false })
.where(eq(users._.columns.id, 123))
.execute();
Atomic Operations
// Increment/decrement without race conditions
await db.update(posts)
.increment('views')
.where(eq(posts._.columns.id, postId))
.execute();
Better Error Handling
try {
await db.update(users).set({ name: 'Bob' }).execute();
} catch (error) {
if (error instanceof MissingWhereClauseError) {
// Handle specific error types
}
}
Developer Experience
Query Debugging
// Inspect SQL without executing
const { sql, params } = db.select(users).where(...).toSQL();
console.log('SQL:', sql);
console.log('Params:', params);
Automatic Migrations
// Keep database in sync with schema
await db.migrateIfDirty();
Full Type Inference
// InferSelectModel gives you the exact type
type User = InferSelectModel<typeof users>;
const user: User = await db.select(users).first();
// InferRelationalSelectModel for queries with .include()
const withPosts = { posts: true } as const;
type UserWithPosts = InferRelationalSelectModel<typeof users, typeof usersRelations, typeof withPosts>;
Why Choose This ORM?
- 🎯 Type-Safe: Full TypeScript support with complete type inference
- 🚀 Fast: Lightweight layer over Tauri's SQL plugin with minimal overhead
- 🛡️ Safe: Built-in protections against common mistakes
- 📦 Batteries-Included: Relations, migrations, utilities all built-in
- 🔧 Flexible: Use query builder OR raw SQL when needed
- 📚 Well-Documented: Comprehensive guides and examples
- 🎨 Modern: Drizzle-like API that feels familiar
Quick Start
bun add @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
pnpm add @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
npm install @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
See Installation & Setup for complete setup instructions.
Next Steps
- Installation & Setup - Get started with installation
- Your First Schema & Query - Quick start guide
- Defining Schemas - Learn about schema definition
- Working with Relations - Master relationships
Welcome to Tauri SQLite ORM! 🎉