Skip to content

koombea/couchbase-lite-wrapper-ios

Repository files navigation

Couchbase Lite Wrapper for iOS and MacOS

License Swift Package Manager

Couchbase Lite Wrapper is a library written in Swift that makes it easy for you to implement database CRUD operations with Couchbase Lite for iOS and MacOS

Requirements

  • iOS 13.0+ | MacOS 10.13+

Installation

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

The Package Manager is included in Swift 3.0 and above.

dependencies: [
        .package(name: "CouchbaseLiteWrapper",
                 url: "https://github.com/koombea/couchbase-lite-wrapper-ios.git", 
                 from: "1.0.0"),
    ],

The Basics

Setup

Create a couchbase database for each of the models you want to store

couchbaseDatabase = CouchbaseDatabase(databaseName: "User")

Create / Update document

let document = CouchbaseDocument(id: "1", attributes: ["name": "Brad", "last_name": "Depp"])
couchbaseDatabase.save(document)

Fetch documents

let expression = Expression.property("attributes.name").equalTo(Expression.string("Brad"))
let documents = couchbaseDatabase.fetchAll(whereExpression: expression)

Delete Documents

let expression = Expression.property("attributes.name").equalTo(Expression.string("Brad"))
couchbaseDatabase.deleteAll(whereExpression: expression)

Couchbase Lite Wrapper + Codable

Codable is a type alias for the Encodable and Decodable protocols part of the Swift Standard Library framework that makes your data types encodable and decodable for compatibility with external representations such as JSON.

Having a User: Codable model, you can fetch models like this:

let users = couchbaseDatabase.fetchAll(User.self)