Swiftpack.co - DnV1eX/SQLiteCombine as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by DnV1eX.
DnV1eX/SQLiteCombine 1.1.1
The Combine framework declarative Swift API for SQLite C-language library that implements SQL database engine.
⭐️ 1
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/DnV1eX/SQLiteCombine.git", from: "1.1.1")

SQLiteCombine

A lightweight Swift library exposing SQLite database access through Combine publishers. Bringing all the power of reactive programming it strives to stay close to the SQLite engine. Data can be stored in four types: Int, Double, String and Data, each of which can be optional. You create a publisher with SQL query and values. The publisher output type is typically a tuple.

Usage Example

import SQLiteCombine

let url = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    .appendingPathComponent(filename, isDirectory: false).appendingPathExtension("db")
let sqlite = try! SQLite(filename: url.path)

let queue = DispatchQueue(label: "Database Queue")

sqlite.publisher("SELECT id, name, state FROM entities WHERE state!=\(Entity.State.deleted.rawValue)")
    .subscribe(on: queue)
    .tryMap(Entity.init)
    .collect()
    .replaceError(with: [])
    .receive(on: DispatchQueue.main)
    .assign(to: &$entities)

Notes

  • SQL statement string interpolation supports keys "\(K: tableName)" and multiple value binding "\(value1, value2, value3)".
  • The output tuple supports up to 32 elements, use an array when exceeded.
  • For write queries with Void output, it will be sent to subscribers upon execution and can be counted.
  • It is recommended to subscribe to publishers on a serial background queue to avoid database locks.
  • The project is not fully documented yet, but it is covered with tests that can be used as a reference.

License

Copyright © 2021 DnV1eX. All rights reserved. Licensed under the Apache License, Version 2.0.

GitHub

link
Stars: 1
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics