Skip to content

Commit 38acd17

Browse files
author
Matt Darnall
committed
Add initializer that takes database Connection
Because: - It will allow consumers to further configure the Connection in their application and pass it in - See: https://github.com/apollographql/apollo-ios/pull/1162/files#r410367735 This commit: - Adds a new initializer to SQLNormalizedCache
1 parent ec25c75 commit 38acd17

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

Sources/ApolloSQLite/SQLiteNormalizedCache.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public final class SQLiteNormalizedCache {
3131
try self.createTableIfNeeded()
3232
}
3333

34+
///
35+
/// Initializer that takes the Connection to use
36+
/// - Parameters:
37+
/// - db: The database Connection to use
38+
/// - shouldVacuumOnClear: If the database should also be `VACCUM`ed on clear to remove all traces of info. Defaults to `false` since this involves a performance hit, but this should be used if you are storing any Personally Identifiable Information in the cache.
39+
/// - Throws: Any errors attempting to access the database
40+
public init(db: Connection, shouldVacuumOnClear: Bool = false) throws {
41+
self.shouldVacuumOnClear = shouldVacuumOnClear
42+
self.db = db
43+
try self.createTableIfNeeded()
44+
}
45+
3446
private func recordCacheKey(forFieldCacheKey fieldCacheKey: CacheKey) -> CacheKey {
3547
let components = fieldCacheKey.components(separatedBy: ".")
3648
var updatedComponents = [String]()

Tests/ApolloSQLiteTests/CachePersistenceTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import XCTest
44
import ApolloTestSupport
55
import ApolloSQLiteTestSupport
66
import StarWarsAPI
7+
import SQLite
78

89
class CachePersistenceTests: XCTestCase {
910

@@ -59,6 +60,12 @@ class CachePersistenceTests: XCTestCase {
5960
}
6061
}
6162

63+
func testPassInConnection() {
64+
let connection = try? Connection()
65+
XCTAssertNotNil(connection)
66+
XCTAssertNoThrow(try SQLiteNormalizedCache(db: connection!))
67+
}
68+
6269
func testClearCache() {
6370
let query = HeroNameQuery()
6471
let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()

0 commit comments

Comments
 (0)