Shallows alternatives and similar libraries
Based on the "Database" category.
Alternatively, view Shallows alternatives based on common mentions on social networks and blogs.
-
MMKV
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX. -
GRDB.swift
A toolkit for SQLite databases, with a focus on application development -
YapDatabase
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers. -
ParseAlternatives
GraphQLite is a toolkit to work with GraphQL servers easily. It also provides several other features to make life easier during iOS application development. [Moved to: https://github.com/relatedcode/GraphQLite] -
Couchbase Mobile
Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps. -
FCModel
An alternative to Core Data for people who like having direct SQL access. -
UserDefaults
Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS -
CTPersistance
iOS Database Persistence Layer with SQLite, your next Persistence Layer! -
Unrealm
Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm. -
Prephirences
Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults -
RealmIncrementalStore
Realm-powered Core Data persistent store -
UserDefaultsStore
Why not use UserDefaults to store Codable objects ๐ -
PredicateEditor
A GUI for dynamically creating NSPredicates at runtime to query data in your iOS app. -
Nora
Nora is a Firebase abstraction layer for FirebaseDatabase and FirebaseStorage -
realm-cocoa-converter
A library that provides the ability to import/export Realm files from a variety of data container formats. -
SecureDefaults
Elevate the security of your UserDefaults with this lightweight โจ wrapper that adds an additional layer of AES-256 encryption -
MySQL
A stand-alone Swift wrapper around the MySQL client library, enabling access to MySQL servers. -
PersistenceKit
Store and retrieve Codable objects to various persistence layers, in a couple lines of code! -
PersistentStorageSerializable
Swift library that makes easier to serialize the user's preferences (app's settings) with system User Defaults or Property List file on disk. -
YapDatabaseExtensions
YapDatabase extensions for use with Swift -
TypedDefaults
TypedDefaults is a utility library to type-safely use NSUserDefaults. -
MongoDB
A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers. -
ObjectiveRocks
An Objective-C wrapper for RocksDB - A Persistent Key-Value Store for Flash and RAM Storage. -
PostgreSQL
A stand-alone Swift wrapper around the libpq client library, enabling access to PostgreSQL servers. -
SQLite
A stand-alone Swift wrapper around the SQLite 3 client library. -
Storez
๐พ Safe, statically-typed, store-agnostic key-value storage written in Swift! -
FileMaker
A stand-alone Swift wrapper around the FileMaker XML Web publishing interface, enabling access to FileMaker servers.
Appwrite - The Open Source Firebase alternative introduces iOS support
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Shallows or a related project?
README
Shallows
Shallows is a generic abstraction layer over lightweight data storage and persistence. It provides a Storage<Key, Value>
type, instances of which can be easily transformed and composed with each other. It gives you an ability to create highly sophisticated, effective and reliable caching/persistence solutions.
Shallows is deeply inspired by Carlos and this amazing talk by Brandon Kase.
Shallows is a really small, component-based project, so if you need even more controllable solution โ build one yourself! Our source code is there to help.
Usage
struct City : Codable {
let name: String
let foundationYear: Int
}
let diskStorage = DiskStorage.main.folder("cities", in: .cachesDirectory)
.mapJSONObject(City.self) // Storage<Filename, City>
let kharkiv = City(name: "Kharkiv", foundationYear: 1654)
diskStorage.set(kharkiv, forKey: "kharkiv")
diskStorage.retrieve(forKey: "kharkiv") { (result) in
if let city = try? result.get() { print(city) }
}
Guide
A main type of Shallows is Storage<Key, Value>
. It's an abstract, type-erased structure which doesn't contain any logic -- it needs to be provided with one. The most basic one is MemoryStorage
:
let storage = MemoryStorage<String, Int>().asStorage() // Storage<String, Int>
Storage instances have retrieve
and set
methods, which are asynhronous and fallible:
storage.retrieve(forKey: "some-key") { (result) in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}
storage.set(10, forKey: "some-key") { (result) in
switch result {
case .success:
print("Value set!")
case .failure(let error):
print(error)
}
}
Transforms
Keys and values can be mapped:
let storage = DiskStorage.main.folder("images", in: .cachesDirectory) // Storage<Filename, Data>
let images = storage
.mapValues(to: UIImage.self,
transformIn: { data in try UIImage.init(data: data).unwrap() },
transformOut: { image in try UIImagePNGRepresentation(image).unwrap() }) // Storage<Filename, UIImage>
enum ImageKeys : String {
case kitten, puppy, fish
}
let keyedImages = images
.usingStringKeys()
.mapKeys(toRawRepresentableType: ImageKeys.self) // Storage<ImageKeys, UIImage>
keyedImages.retrieve(forKey: .kitten, completion: { result in /* .. */ })
NOTE: There are several convenience methods defined on Storage
with value of Data
: .mapString(withEncoding:)
, .mapJSON()
, .mapJSONDictionary()
, .mapJSONObject(_:)
.mapPlist(format:)
, .mapPlistDictionary(format:)
, .mapPlistObject(_:)
.
Storages composition
Another core concept of Shallows is composition. Hitting a disk every time you request an image can be slow and inefficient. Instead, you can compose MemoryStorage
and FileSystemStorage
:
let efficient = MemoryStorage<Filename, UIImage>().combined(with: imageStorage)
It does several things:
- When trying to retrieve an image, the memory storage first will be checked first, and if it doesn't contain a value, the request will be made to disk storage.
- If disk storage stores a value, it will be pulled to memory storage and returned to a user.
- When setting an image, it will be set both to memory and disk storage.
Read-only storage
If you don't want to expose writing to your storage, you can make it a read-only storage:
let readOnly = storage.asReadOnlyStorage() // ReadOnlyStorage<Key, Value>
Read-only storages can also be mapped and composed:
let immutableFileStorage = DiskStorage.main.folder("immutable", in: .applicationSupportDirectory)
.mapString(withEncoding: .utf8)
.asReadOnlyStorage()
let storage = MemoryStorage<Filename, String>()
.backed(by: immutableFileStorage)
.asReadOnlyStorage() // ReadOnlyStorage<Filename, String>
Write-only storage
In similar way, write-only storage is also available:
let writeOnly = storage.asWriteOnlyStorage() // WriteOnlyStorage<Key, Value>
Different ways of composition
Compositions available for Storage
:
.combined(with:)
(see Storages composition).backed(by:)
will work the same ascombined(with:)
, but it will not push the value to the back storage.pushing(to:)
will not retrieve the value from the back storage, but will push to it onset
Compositions available for ReadOnlyStorage
:
.backed(by:)
Compositions available for WriteOnlyStorage
:
.pushing(to:)
Single element storage
You can have a storage with keys Void
. That means that you can store only one element there. Shallows provides a convenience .singleKey
method to create it:
let settings = DiskStorage.main.folder("settings", in: .applicationSupportDirectory)
.mapJSONDictionary()
.singleKey("settings") // Storage<Void, [String : Any]>
settings.retrieve { (result) in
// ...
}
Synchronous storage
Storages in Shallows are asynchronous by design. However, in some situations (for example, when scripting or testing) it could be useful to have synchronous storages. You can make any storage synchronous by calling .makeSyncStorage()
on it:
let strings = DiskStorage.main.folder("strings", in: .cachesDirectory)
.mapString(withEncoding: .utf8)
.makeSyncStorage() // SyncStorage<Filename, String>
let existing = try strings.retrieve(forKey: "hello")
try strings.set(existing.uppercased(), forKey: "hello")
Mutating value for key
Shallows provides a convenient .update
method on storages:
let arrays = MemoryStorage<String, [Int]>()
arrays.update(forKey: "some-key", { $0.append(10) })
Zipping storages
Zipping is a very powerful feature of Shallows. It allows you to compose your storages in a way that you get result only when both of them completes for your request. For example:
let strings = MemoryStorage<String, String>()
let numbers = MemoryStorage<String, Int>()
let zipped = zip(strings, numbers) // Storage<String, (String, Int)>
zipped.retrieve(forKey: "some-key") { (result) in
if let (string, number) = try? result.get() {
print(string)
print(number)
}
}
zipped.set(("shallows", 3), forKey: "another-key")
Isn't it nice?
Recovering from errors
You can protect your storage instance from failures using fallback(with:)
or defaulting(to:)
methods:
let storage = MemoryStorage<String, Int>()
let protected = storage.fallback(with: { error in
switch error {
case MemoryStorageError.noValue:
return 15
default:
return -1
}
})
let storage = MemoryStorage<String, Int>()
let defaulted = storage.defaulting(to: -1)
This is especially useful when using update
method:
let storage = MemoryStorage<String, [Int]>()
storage.defaulting(to: []).update(forKey: "first", { $0.append(10) })
That means that in case of failure retrieving existing value, update
will use default value of []
instead of just failing the whole update.
Using NSCacheStorage
NSCache
is a tricky class: it supports only reference types, so you're forced to use, for example, NSData
instead of Data
and so on. To help you out, Shallows provides a set of convenience extensions for legacy Foundation types:
let nscache = NSCacheStorage<NSURL, NSData>()
.toNonObjCKeys()
.toNonObjCValues() // Storage<URL, Data>
Making your own storage
To create your own caching layer, you should conform to StorageProtocol
. That means that you should define these two methods:
func retrieve(forKey key: Key, completion: @escaping (Result<Value, Error>) -> ())
func set(_ value: Value, forKey key: Key, completion: @escaping (Result<Void, Error>) -> ())
Where Key
and Value
are associated types.
NOTE: Please be aware that you are responsible for the thread-safety of your implementation. Very often retrieve
and set
will not be called from the main thread, so you should make sure that no race conditions will occur.
To use it as Storage<Key, Value>
instance, simply call .asStorage()
on it:
let storage = MyStorage().asStorage()
You can also conform to a ReadOnlyStorageProtocol
only. That way, you only need to define a retrieve(forKey:completion:)
method.
Installation
Swift Package Manager
Starting with Xcode 11, Shallows is officially available only via Swift Package Manager.
In Xcode 11 or greater, in you project, select: File > Swift Packages > Add Pacakage Dependency
In the search bar type
https://github.com/dreymonde/Shallows
Then proceed with installation.
If you can't find anything in the panel of the Swift Packages you probably haven't added yet your github account. You can do that under the Preferences panel of your Xcode, in the Accounts section.
For command-line based apps, you can just add this directly to your Package.swift file:
dependencies: [
.package(url: "https://github.com/dreymonde/Shallows", from: "0.11.0"),
]
Manual
Of course, you always have an option of just copying-and-pasting the code.
Deprecated dependency managers
Last Shallows version to support Carthage and Cocoapods is 0.10.0. Carthage and Cocoapods will no longer be officially supported.
Carthage:
github "dreymonde/Shallows" ~> 0.10.0
Cocoapods:
pod 'Shallows', '~> 0.10.0'