Skip to content

crunchybananas/SwiftSQLiteUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftSQLiteUtils

An ergonomic SQLite toolkit for Swift, inspired by Simon Willison's sqlite-utils.

Note: SwiftSQLiteUtils is an original Swift library inspired by sqlite-utils' API design philosophy. sqlite-utils is created by Simon Willison, licensed under Apache 2.0. No sqlite-utils source code was used in this project.

Features

  • Ergonomic CRUD — insert, upsert, update, delete with minimal boilerplate
  • Codable integration — insert and query Swift structs directly
  • Auto-create tables — generate tables from Codable types automatically
  • Table introspection — list tables, columns, indexes, row counts
  • JSON import/export — import from and export to JSON
  • Query builder — fluent select().where().orderBy().limit() API
  • Full-text search — FTS5 table creation and search
  • Transactions — simple transaction { } block API
  • Zero dependencies — uses system SQLite3, nothing else

Quick Start

import SwiftSQLiteUtils

// Open (or create) a database
let db = try Database(path: "myapp.db")

// Define a model
struct Dog: Codable {
  var name: String
  var age: Int
  var breed: String
}

// Auto-create table and insert
try db.createTable(for: Dog.self)
try db.insert(Dog(name: "Cleo", age: 6, breed: "Labrador"))
try db.insert(Dog(name: "Waffle", age: 3, breed: "Terrier"))

// Query
let dogs: [Dog] = try db.select(from: "dogs")
let old: [Dog] = try db.select(from: "dogs", where: "age > ?", params: [5])

// Count, introspect
let count = try db.count("dogs")
let tables = try db.tableNames()
let columns = try db.columns(of: "dogs")

// JSON export
let json = try db.exportJSON(from: "dogs")

// Upsert
try db.upsert(Dog(name: "Cleo", age: 7, breed: "Labrador"), table: "dogs", onConflict: "name")

// Raw SQL
let rows = try db.execute("SELECT name, age FROM dogs WHERE breed = ?", params: ["Labrador"])

// In-memory database
let mem = try Database()

Installation

Swift Package Manager

dependencies: [
  .package(url: "https://github.com/crunchybananas/SwiftSQLiteUtils.git", from: "0.1.0"),
]

License

MIT — see LICENSE.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages