diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index 1633f04aa2..6d5a8a70b9 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -31,6 +31,18 @@ public final class SQLiteNormalizedCache { try self.createTableIfNeeded() } + /// + /// Initializer that takes the Connection to use + /// - Parameters: + /// - db: The database Connection to use + /// - 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. + /// - Throws: Any errors attempting to access the database + public init(db: Connection, shouldVacuumOnClear: Bool = false) throws { + self.shouldVacuumOnClear = shouldVacuumOnClear + self.db = db + try self.createTableIfNeeded() + } + private func recordCacheKey(forFieldCacheKey fieldCacheKey: CacheKey) -> CacheKey { let components = fieldCacheKey.components(separatedBy: ".") var updatedComponents = [String]() diff --git a/Tests/ApolloSQLiteTests/CachePersistenceTests.swift b/Tests/ApolloSQLiteTests/CachePersistenceTests.swift index 12ae394435..bdf5422b76 100644 --- a/Tests/ApolloSQLiteTests/CachePersistenceTests.swift +++ b/Tests/ApolloSQLiteTests/CachePersistenceTests.swift @@ -4,6 +4,7 @@ import XCTest import ApolloTestSupport import ApolloSQLiteTestSupport import StarWarsAPI +import SQLite class CachePersistenceTests: XCTestCase { @@ -59,6 +60,10 @@ class CachePersistenceTests: XCTestCase { } } + func testPassInConnectionDoesNotThrow() { + XCTAssertNoThrow(try SQLiteNormalizedCache(db: Connection())) + } + func testClearCache() { let query = HeroNameQuery() let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()