- A SQLite database wrapper written in Swift that requires no SQL knowledge to use.
- No need to keep track of columns used in the database; it's automatic.
- Completely thread safe. AgileDB is implemented as a Swift
actorand is fully Swift 6 concurrency compliant. - Built around Async/Await
- Use the publisher method to work with Combine and SwiftUI
- Swift Package Manager (Recommended)
- Include all .swift source files in your project
- The easiest way to use AgileDB is to create a class or struct that adheres to the DBObject Protocol. These entities will automatically be
Codable. Encoded values are saved to the database. (See below for supported value types) - Alternately, you can use low level methods that work from a JSON dictionary. Supported types in the JSON node are String, Int, Double, Bool, [String], [Int], [Double].
- For any method that returns an optional, that value is nil if an error occured and could not return a proper value.
- DBbjects can have the following types saved and read to the DB: DBObject, Int, Double, String, Date, Bool, Dictionary, Codable Struct [DBObject], [Int], [Double], [String], [Date], [Dictionary], [Codable Struct]. All properties may be optional. For DBObject properties, the key is stored so the referenced objects can be edited and saved independently
- Bool properties read from the database will be interpreted as follows: An integer 0 = false and any other number is true. For string values "1", "yes", "YES", "true", and "TRUE" evaluate to true.
public protocol DBObject: Codable, Sendable {
static var table: DBTable { get }
var key: String { get set }
var codingKeys: [CodingKey] { get }
}codingKeyshas a default implementation that returns an empty array, encoding all of the object's properties. Provide your own implementation to limit which properties are encoded.
/**
Instantiate object and populate with values from the database, recursively if necessary. If instantiation fails, nil is returned.
- parameter db: Database object holding the data.
- parameter key: Key of the data entry.
*/
public init?(db: AgileDB, key: String) async
/**
Save the object's encoded values to the database.
- parameter db: Database object to hold the data.
- parameter expiration: Optional Date specifying when the data is to be automatically deleted. Default value is nil specifying no automatic deletion.
- parameter saveNestedObjects: Save nested DBObjects and arrays of DBObjects. Default value is true.
- returns: Discardable Bool value of a successful save.
*/
@discardableResult
public func save(to db: AgileDB, autoDeleteAfter expiration: Date? = nil, saveNestedObjects: Bool = true) async -> Bool
/**
Remove the object from the database
- parameter db: Database object that holds the data. This does not delete nested objects.
- returns: Discardable Bool value of a successful deletion.
*/
public func delete(from db: AgileDB) async -> Bool
/**
Asynchronously instantiate object and populate with values from the database, recursively if necessary.
- parameter db: Database object to hold the data.
- parameter key: Key of the data entry.
- returns: DBObject
- throws: DBError
*/
public static func load(from db: AgileDB, for key: String) async throws -> Selfimport AgileDB
enum Table {
static let categories: DBTable = "Categories"
static let accounts: DBTable = "Accounts"
static let people: DBTable = "People"
}
struct Category: DBObject {
static var table: DBTable { return Table.categories }
var key = UUID().uuidString
var accountKey = ""
var name = ""
var inSummary = true
}
// save to database
await category.save(to: db)
// save to database, automatically delete after designated date
await category.save(to: db, autoDeleteAfter: deletionDate)
// instantiate and pull from DB asynchronously
guard let category = await Category(db: db, key: categoryKey) else { return }
// delete from DB
await category.delete(from: db)- Works with DBObject elements
- Instantiate the class with a reference to the database and the keys
- Only keys are stored to minimize memory usage
- Objects are loaded asynchronously on demand, either with the
object(at:)method or by iterating the results as anAsyncSequencewithfor await - The database publisher returns an instance of this class
let keys = try await db.keysInTable(Category.table)
let categories = DBResults<Category>(db: db, keys: keys)
// Iterate as an AsyncSequence (objects load on demand):
for await category in categories {
// use category object
}
// Or load a specific object by index:
guard let first = await categories.object(at: 0) else { return }- Use the publisher with Combine subscribers and SwiftUI
- Sends DBResults as needed to reflect finished queries and updated results
- Uses completion to send possible errors
/**
Returns a Publisher for generic DBResults. Uses the table of the DBObject for results.
- parameter sortOrder: Optional string that gives a comma delimited list of properties to sort by.
- parameter conditions: Optional array of DBConditions that specify what conditions must be met.
- parameter validateObjects: Optional bool that condition sets will be validated against the table. Any set that refers to json objects that do not exist in the table will be ignored. Default value is false.
- returns: DBResultssPublisher
*/
let publisher: DBResultsPublisher<Transaction> = db.publisher()
let _ = publisher.sink(receiveCompletion: { _ in }) { ( results) in
// assign to AnyCancellable property
}See if a given table holds a given key.
let table: DBTable = "categories"
do {
let hasKey = try await AgileDB.shared.tableHasKey(table: table, key: "category1")
if hasKey {
// table has key
} else {
// table didn't have key
}
} catch {
// handle error
}See if a given table holds all given keys.
let table: DBTable = "categories"
do {
let hasKeys = try await AgileDB.shared.tableHasAllKeys(table: table, keys: ["category1","category2","category3"])
if hasKeys {
// table has all keys
} else {
// table didn't have all keys
}
} catch {
// handle error
}Return an array of keys in a given table. Optionally specify sort order based on a value at the root level
let table: DBTable = "categories"
do {
let tableKeys = try await AgileDB.shared.keysInTable(table, sortOrder: "name, date desc")
// process keys
} catch {
// handle error
}Return an array of keys from the given table sorted in the way specified matching the given conditions.
/**
All conditions in the same set are ANDed together. Separate sets are ORed against each other. (set:0 AND set:0 AND set:0) OR (set:1 AND set:1 AND set:1) OR (set:2)
Unsorted Example:
let accountCondition = DBCondition(set:0,objectKey:"account",conditionOperator:.equal, value:"ACCT1")
do {
let keys = try await AgileDB.shared.keysInTable("table1", sortOrder: nil, conditions: [accountCondition])
// use keys
} catch {
// handle error
}
- parameter table: The DBTable to return keys from.
- parameter sortOrder: Optional string that gives a comma delimited list of properties to sort by.
- parameter conditions: Optional array of DBConditions that specify what conditions must be met.
- parameter validateObjects: Optional bool that condition sets will be validated against the table. Any set that refers to json objects that do not exist in the table will be ignored. Default value is false.
- returns: [String] Returns an array of keys from the table.
- throws: DBError when the database could not be opened or another error occurred.
public func keysInTable(_ table: DBTable, sortOrder: String? = nil, conditions: [DBCondition]? = nil, validateObjects: Bool = false) async throws -> [String]
*/
let table: DBTable = "accounts"
let accountCondition = DBCondition(set:0,objectKey:"account", conditionOperator:.equal, value:"ACCT1")
do {
let keys = try await AgileDB.shared.keysInTable(table, sortOrder: nil, conditions: [accountCondition])
// process keys
} catch {
// handle error
}Data can be set or retrieved manually as shown here or your class/struct can adhere to the DBObject protocol, documented above, and use the built-in init and save methods for greater ease and flexibility.
Set value in table
let table: DBTable = "Transactions"
let key = UUID().uuidString
let dict = [
"key": key
, "accountKey": "Checking"
, "locationKey" :"Kroger"
, "categoryKey": "Food"
]
let data = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
let json = String(data: data, encoding: .utf8)!
// the key object in the json value is ignored in the setValue method
if await AgileDB.shared.setValueInTable(table, for: key, to: json) {
// success
} else {
// handle error
}Retrieve value for a given key
let table: DBTable = "categories"
do {
let jsonValue = try await AgileDB.shared.valueFromTable(table, for: "category1")
// process value
} catch {
// handle error
}
do {
let dictValue = try await AgileDB.shared.dictValueFromTable(table, for: "category1")
// process dictionary value
} catch {
// handle error
}Delete the value for a given key
let table: DBTable = "categories"
if await AgileDB.shared.deleteFromTable(table, for: "category1") {
// value was deleted
} else {
// handle error
}AgileDB is an actor, so data is retrieved asynchronously using await. Methods that can fail are marked throws.
let db = AgileDB.shared
let table: DBTable = "categories"
do {
let value = try await db.valueFromTable(table, for: key)
} catch {
}Asynchronous methods available
- tableHasKey
- tableHasAllKeys
- keysInTable
- valueFromTable
- dictValueFromTable
- sqlSelect
- load in the DBObject protocol
AgileDB lets you run standard SQL select statements for more complex queries — joins, aggregates, grouping, and so on. sqlSelect returns an array of rows (each row an array of values), or throws on error.
Each object is stored as a single JSON document in its table's value column, alongside the key, addedDateTime, updatedDateTime, and autoDeleteDateTime columns. Individual object properties are therefore not their own columns. To reference a property as if it were a column, use SQLite's json_extract(value, '$.propertyName'):
let db = AgileDB.shared
// "account" is a property inside each row's JSON document, not a physical column,
// so it must be addressed with json_extract.
let sql = "select key from accounts where json_extract(value, '$.account') = 'ACCT1'"
do {
let results = try await db.sqlSelect(sql)
// process results
} catch {
// handle error
}key, addedDateTime, updatedDateTime, and autoDeleteDateTime are real columns and may be referenced directly by name.
A nested DBObject property stores the referenced object's key, and a [DBObject] property stores an array of keys. Join them back to their tables using json_extract (for a single reference) and json_each (to expand an array of keys):
// Invoice -> Client (single reference): the invoice's "client" property holds the client's key.
let clientJoin = """
select json_extract(c.value, '$.name')
from Invoice i
join InvoiceClient c on json_extract(i.value, '$.client') = c.key
where json_extract(i.value, '$.number') = 1001
"""
// Invoice -> line items (array of keys): expand the array with json_each, then join by key.
let itemTotals = """
select json_extract(c.value, '$.name'),
sum(json_extract(item.value, '$.quantity') * json_extract(item.value, '$.price'))
from Invoice i
join InvoiceClient c on json_extract(i.value, '$.client') = c.key,
json_each(i.value, '$.lineItems') je
join InvoiceLineItem item on je.value = item.key
group by i.key
"""To query a property by name directly (and to index it for performance), declare it with setIndexesForTable(_:to:). Each indexed property becomes an indexed, generated column derived from the JSON document, so it can be used by name in SQL:
await db.setIndexesForTable("accounts", to: ["account"])
// "account" is now a real, indexed column and can be referenced directly.
let results = try await db.sqlSelect("select key from accounts where account = 'ACCT1'")AgileDB can sync with other instances of itself by enabling syncing before processing any data and then sharing a sync log.
/**
Enables syncing. Once enabled, a log is created for all current values in the tables.
- returns: Bool If syncing was successfully enabled.
*/
public func enableSyncing() async -> Bool
/**
Disables syncing.
- returns: Bool If syncing was successfully disabled.
*/
public func disableSyncing() async -> Bool
/**
Read-only array of unsynced tables. Any tables not in this array will be synced.
*/
private(set) public var unsyncedTables: [DBTable]
/**
Sets the tables that are not to be synced.
- parameter tables: Array of tables that are not to be synced.
- returns: Bool If list was set successfully.
*/
public func setUnsyncedTables(_ tables: [DBTable]) async -> Bool
/**
Creates a sync file that can be used on another AgileDB instance to sync data. This is a synchronous call.
- parameter filePath: The full path, including the file itself, to be used for the log file.
- parameter lastSequence: The last sequence used for the given target Initial sequence is 0.
- parameter targetDBInstanceKey: The dbInstanceKey of the target database. Use the dbInstanceKey method to get the DB's instanceKey.
- returns: (Bool,Int) If the file was successfully created and the lastSequence that should be used in subsequent calls to this instance for the given targetDBInstanceKey.
*/
public func createSyncFileAtURL(_ localURL: URL!, lastSequence: Int, targetDBInstanceKey: String) async -> (Bool, Int)
/**
Processes a sync file created by another instance of AgileDB This is a synchronous call.
- parameter filePath: The path to the sync file.
- parameter syncProgress: Optional function that will be called periodically giving the percent complete.
- returns: (Bool,String,Int) If the sync file was successfully processed,the instanceKey of the submiting DB, and the lastSequence that should be used in subsequent calls to the createSyncFile method of the instance that was used to create this file. If the database couldn't be opened or syncing hasn't been enabled, then the instanceKey will be empty and the lastSequence will be equal to zero.
*/
public typealias syncProgressUpdate = (_ percentComplete: Double) -> Void
public func processSyncFileAtURL(_ localURL: URL!, syncProgress: syncProgressUpdate?) async -> (Bool, String, Int)- Objects are now stored as a single JSON document in each table's
valuecolumn (rather than one physical column per property plus a side table for arrays). Direct SQLselectstatements must reference properties withjson_extract(value, '$.property'); usejson_eachto join across array-of-key relationships, or declare indexes withsetIndexesForTable(_:to:)to expose properties as named, indexed columns. - AgileDB is now implemented as a Swift
actorand is fully Swift 6 language-mode compliant. - Asynchronous (async/await) methods are now the primary API. Most low-level methods are
async, and those that can fail areasync throws. - Dictionary values now use
any Sendablerather thanAnyObject. - DBObject now conforms to
Sendableand exposes acodingKeysproperty (defaults to encoding all properties). - DBObject
savegained asaveNestedObjectsparameter (default true). - DBResults loads objects on demand and conforms to
AsyncSequence; iterate it withfor awaitor load a single object with the asynchronousobject(at:)method. unsyncedTablesandsetUnsyncedTables(_:)now use[DBTable].- CocoaPods support removed; install via Swift Package Manager.
- Minimum platforms raised (iOS 18, macOS 12, tvOS 18, watchOS 9); built with the Swift 6.2 toolchain.
- All DBObject methods have async/await counterparts
- Developed and tested with Xcode 14.2
- Updated DBObject encoding to support Dictonary, Codable Struct, [Dictionary], and [Codable Struct]
- Converted project to proper Swift Package
- Minimum swift version updated to 5.5
- Added async/await support
- New method: tableHasAllKeys and it's asynchronous equivilent
- DBObjects can recursively save and load DBObject and [DBObject] properties (Technically the key is stored so the referenced objects can be edited and saved independently)
- New publisher method for use in SwiftUI and Combine. The publisher returns the new DBResults object. All active publishers will send new results if published DBResults has added, deleted, or updated keys.
- New DBResults object that's subscripted. Only the keys are stored for better memory use.
- DBObject structures can now store [String], [Int], [Double], and [Date] types. (Nested objects not supported.)
- Developed and tested with Xcode 10.2 using Swift 5
- Introduction of DBObject protocol. See below.
- debugMode property renamed to isDebugging.
keyobject in value provided is now ignored instead of giving error.- New parameter
validateObjectsin the keysInTable method will ignore condition sets that refer to objects not in the table.
- Developed and tested with Xcode 10.1
- Several methods deprecated with a renamed version available for clarity at the point of use.
- Data can be retrieved asynchronously.
- The class property
sharedInstancehas been renamed toshared. - Methods are no longer class-level, they must be accessed through an instance of the db. A simple way to update to this is to simply append .shared to the class name in any existing code.