Convert Figma logo to code with AI

yapstudios logoYapDatabase

YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.

3,352
365
3,352
110

Top Related Projects

Realm is a mobile database: a replacement for Core Data & SQLite

13,842

A Cocoa / Objective-C wrapper around SQLite

A type-safe, Swift-language layer over SQLite3.

A toolkit for SQLite databases, with a focus on application development

10,727

WCDB is a cross-platform database framework developed by WeChat.

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.

Quick Overview

YapDatabase is a high-performance database extension for iOS and Mac. It's built on top of SQLite and provides a key/value store with support for collections, secondary indexes, full-text search, and more. YapDatabase is designed to be fast, efficient, and easy to use for iOS and macOS developers.

Pros

  • High performance and efficient memory usage
  • Supports advanced features like secondary indexes and full-text search
  • Thread-safe and designed for concurrent access
  • Extensive documentation and active community support

Cons

  • Primarily focused on Apple platforms (iOS and macOS)
  • Learning curve for developers new to key/value stores
  • May be overkill for simple data storage needs
  • Less popular compared to some other database solutions

Code Examples

  1. Basic read and write operations:
let database = YapDatabase(path: databasePath)
let connection = database.newConnection()

connection.readWrite { transaction in
    transaction.setObject("John Doe", forKey: "user1", inCollection: "users")
    let user = transaction.object(forKey: "user1", inCollection: "users") as? String
    print(user ?? "User not found")
}
  1. Using secondary indexes:
let viewName = "usersByAge"
let grouping = YapDatabaseViewGrouping.withObjectBlock { (transaction, collection, key, object) in
    return "allUsers"
}
let sorting = YapDatabaseViewSorting.withObjectBlock { (transaction, group, collection1, key1, object1, collection2, key2, object2) in
    let age1 = (object1 as? User)?.age ?? 0
    let age2 = (object2 as? User)?.age ?? 0
    return age1.compare(age2)
}

database.register(YapDatabaseView(grouping: grouping, sorting: sorting), withName: viewName)
  1. Full-text search:
let searchName = "messageSearch"
let search = YapDatabaseFullTextSearch.init(columnNames: ["content"])

database.register(search, withName: searchName)

connection.readWrite { transaction in
    let fts = transaction.ext(searchName) as! YapDatabaseFullTextSearchTransaction
    fts.enumerateRows(matching: "hello world") { (document, collection, key, snippets, stop) in
        print("Found match: \(key) in collection: \(collection)")
    }
}

Getting Started

  1. Install YapDatabase using CocoaPods:
pod 'YapDatabase'
  1. Import YapDatabase in your Swift file:
import YapDatabase
  1. Create a database instance:
let databasePath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("database.sqlite").path
let database = YapDatabase(path: databasePath)
  1. Create a connection and perform operations:
let connection = database.newConnection()
connection.readWrite { transaction in
    // Perform database operations here
}

Competitor Comparisons

Realm is a mobile database: a replacement for Core Data & SQLite

Pros of Realm

  • Offers a more intuitive object-oriented API, making it easier for developers to work with data models
  • Provides better performance for large datasets and complex queries
  • Supports real-time synchronization and multi-threading out of the box

Cons of Realm

  • Less flexible schema migration compared to YapDatabase
  • Steeper learning curve for developers familiar with SQLite-based solutions
  • Limited support for custom data types and more complex data structures

Code Comparison

YapDatabase:

let database = YapDatabase(path: databasePath)
let connection = database.newConnection()

connection.readWrite { transaction in
    transaction.setObject(user, forKey: "user1", inCollection: "users")
}

Realm:

let realm = try! Realm()

try! realm.write {
    realm.add(user)
}

Both YapDatabase and Realm are popular database solutions for iOS development, but they have different approaches and strengths. YapDatabase is built on top of SQLite and offers a key-value store with collections, while Realm uses its own persistence engine and provides an object-oriented database model. Realm generally offers better performance and easier data modeling, but YapDatabase may be more suitable for projects requiring fine-grained control over data storage and retrieval.

13,842

A Cocoa / Objective-C wrapper around SQLite

Pros of FMDB

  • Lightweight and simple to use, focusing solely on SQLite database operations
  • Extensive documentation and a large community, making it easier to find solutions
  • Direct SQL query support, offering more flexibility for complex operations

Cons of FMDB

  • Lacks advanced features like caching and indexing found in YapDatabase
  • Requires manual management of database schema and migrations
  • No built-in support for key-value store operations

Code Comparison

FMDB:

FMDatabase *db = [FMDatabase databaseWithPath:@"test.db"];
[db open];
[db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"John"];
FMResultSet *rs = [db executeQuery:@"SELECT * FROM test"];
while ([rs next]) {
    NSString *name = [rs stringForColumn:@"name"];
}

YapDatabase:

YapDatabase *database = [[YapDatabase alloc] initWithPath:@"test.db"];
YapDatabaseConnection *connection = [database newConnection];
[connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
    [transaction setObject:@"John" forKey:@"name" inCollection:@"test"];
    NSString *name = [transaction objectForKey:@"name" inCollection:@"test"];
}];

Both FMDB and YapDatabase are popular iOS database solutions, but they serve different purposes. FMDB is a thin wrapper around SQLite, providing direct access to SQL operations. YapDatabase, on the other hand, offers a higher-level abstraction with additional features like caching and indexing. Choose FMDB for simple SQLite operations and direct SQL control, or YapDatabase for a more robust key-value store with advanced functionality.

A type-safe, Swift-language layer over SQLite3.

Pros of SQLite.swift

  • Type-safe Swift API for SQLite operations
  • Supports custom SQL functions and aggregates
  • Lightweight and focused solely on SQLite integration

Cons of SQLite.swift

  • Limited to SQLite database operations
  • Lacks advanced features like full-text search or view support
  • May require more manual management of database schema

Code Comparison

SQLite.swift:

let db = try Connection("path/to/db.sqlite3")
try db.run("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
try db.run("INSERT INTO users (name) VALUES (?)", "Alice")

YapDatabase:

YapDatabase *database = [[YapDatabase alloc] initWithPath:@"path/to/db.sqlite"];
YapDatabaseConnection *connection = [database newConnection];
[connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
    [transaction setObject:@"Alice" forKey:@"user1" inCollection:@"users"];
}];

Summary

SQLite.swift provides a more Swift-centric approach to SQLite operations with type safety, while YapDatabase offers a higher-level abstraction with additional features like views and full-text search. SQLite.swift is more suitable for projects requiring direct SQLite access, whereas YapDatabase provides a more comprehensive solution for complex data storage needs in iOS applications.

A toolkit for SQLite databases, with a focus on application development

Pros of GRDB.swift

  • Built-in support for Swift Codable protocol, making it easier to work with Swift types
  • More comprehensive query interface with type-safe SQL builders
  • Better support for concurrent database access and multi-threading

Cons of GRDB.swift

  • Steeper learning curve due to more complex API
  • Less extensive documentation compared to YapDatabase
  • Smaller community and fewer third-party extensions

Code Comparison

YapDatabase:

let collection = "users"
let key = "user123"
database.readWrite { transaction in
    let user = User(name: "John", age: 30)
    transaction.setObject(user, forKey: key, inCollection: collection)
}

GRDB.swift:

try dbQueue.write { db in
    let user = User(name: "John", age: 30)
    try user.insert(db)
}

Both YapDatabase and GRDB.swift are powerful database solutions for iOS and macOS applications. YapDatabase is built on top of SQLite and provides a high-level, key-value store interface with additional features like views and relationships. GRDB.swift, on the other hand, offers a more SQL-oriented approach with a focus on type-safety and Swift integration.

While YapDatabase excels in simplicity and ease of use, GRDB.swift provides more advanced features and better integration with modern Swift practices. The choice between the two depends on the specific requirements of your project and your familiarity with SQL concepts.

10,727

WCDB is a cross-platform database framework developed by WeChat.

Pros of WCDB

  • Multi-platform support (iOS, Android, macOS)
  • High performance with optimized SQL execution
  • Built-in encryption and full-text search capabilities

Cons of WCDB

  • Steeper learning curve due to more complex API
  • Less mature community and ecosystem compared to YapDatabase
  • Potentially overkill for simpler database needs

Code Comparison

YapDatabase:

YapDatabase *database = [[YapDatabase alloc] initWithPath:@"database.sqlite"];
YapDatabaseConnection *connection = [database newConnection];
[connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
    [transaction setObject:object forKey:key inCollection:collection];
}];

WCDB:

let database = Database(withPath: "database.sqlite")
let objects = database.getTable(named: "objects")
try database.insert(object, on: objects)

Summary

WCDB offers multi-platform support and advanced features like encryption and full-text search, making it suitable for complex applications. However, it may have a steeper learning curve compared to YapDatabase. YapDatabase provides a simpler API and is more mature, but lacks some of the advanced features and cross-platform capabilities of WCDB. The choice between the two depends on the specific requirements of your project and the level of complexity you're willing to manage.

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.

Pros of Couchbase Lite iOS

  • Supports multi-device synchronization and offline-first capabilities
  • Offers built-in conflict resolution mechanisms
  • Provides a more comprehensive ecosystem with server-side components

Cons of Couchbase Lite iOS

  • Steeper learning curve due to more complex architecture
  • Potentially higher resource usage and overhead
  • May be overkill for simpler, single-device applications

Code Comparison

YapDatabase:

let database = YapDatabase(path: databasePath)
let connection = database.newConnection()

connection.readWrite { transaction in
    transaction.setObject(myObject, forKey: "key", inCollection: "collection")
}

Couchbase Lite iOS:

let database = try Database(name: "mydb")
let document = MutableDocument()
    .setString("value", forKey: "key")
try database.saveDocument(document)

YapDatabase offers a simpler API for basic key-value storage, while Couchbase Lite iOS provides a document-based approach with more advanced features. YapDatabase is generally easier to set up and use for local storage, whereas Couchbase Lite iOS is designed for more complex scenarios involving synchronization and distributed systems.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

YapDatabaseLogo Build Status Pod Version Carthage compatible

YapDatabase is a collection/key/value store and so much more. It's built atop sqlite, for Swift & Objective-C developers, targeting macOS, iOS, tvOS & watchOS.

 

Hello World:

// Create and/or Open the database file
let db = YapDatabase()

// Configure database:
// We're going to store String's in the collection "test".
// To store custom classes/structs, just implement Swift's Codable protocol.
db.registerCodableSerialization(String.self, forCollection: "test")

// Get a connection to the database
// (You can have multiple connections for concurrency.)
let connection = db.newConnection()

// Write an item to the database
connection.readWrite {(transaction) in
  transaction.setObject("hello", forKey: "world", inCollection: "test")
}

// Read it back
connection.read {(transaction) in
  let str = transaction.object(forKey: "world", inCollection: "test") as? String
  // str == "hello"
}

 

YapDB has the following features:

  • Concurrency. You can read from the database while another thread is simultaneously making modifications to the database. So you never have to worry about blocking the main thread, and you can easily write to the database on a background thread. And, of course, you can read from the database on multiple threads simultaneously.
  • Built-In Caching. A configurable object cache is built-in. Of course sqlite has caching too. But it's caching raw serialized bytes, and we're dealing with objects. So having a built-in cache means you can skip the deserialization process, and get your objects much faster.
  • Metadata. Ever wanted to store extra data along with your object? Like maybe a timestamp of when it was downloaded. Or a fully separate but related object? You're in luck. Metadata support comes standard. Along with its own separate configurable cache too!
  • Views. Need to filter, group & sort your data? No problem. YapDatabase comes with Views. And you don't even need to write esoteric SQL queries. Views work using closures, so you just provide a bit of native code. Plus they automatically update themselves, and they make animating tables super easy.
  • Secondary Indexing. Speed up your queries by indexing important properties. And then use SQL style queries to quickly find your items.
  • Full Text Search. Built atop sqlite's FTS module (contributed by google), you can add extremely speedy searching to your app with minimal effort.
  • Relationships. You can setup relationships between objects and even configure cascading delete rules.
  • Hooks. Perform custom app-specific logic when certain events take place, such as an object being modified or deleted.
  • Sync. Support for syncing with Apple's CloudKit is available out of the box. There's even a fully functioning example project that demonstrates writing a syncing Todo app.
  • Extensions. More than just a key/value store, YapDatabase comes with an extensions architecture built-in. You can even create your own extensions.
  • Performance. Fetch thousands of objects on the main thread without dropping a frame.

See what the API looks like in "hello world" for YapDatabase
Learn more by visiting the wiki