Skip to content

Commit

Permalink
Add initializer that takes database Connection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mdarnall committed Jul 27, 2020
1 parent ec25c75 commit d6a4d03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/ApolloSQLite/SQLiteNormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
Expand Down
5 changes: 5 additions & 0 deletions Tests/ApolloSQLiteTests/CachePersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import XCTest
import ApolloTestSupport
import ApolloSQLiteTestSupport
import StarWarsAPI
import SQLite

class CachePersistenceTests: XCTestCase {

Expand Down Expand Up @@ -59,6 +60,10 @@ class CachePersistenceTests: XCTestCase {
}
}

func testPassInConnectionDoesNotThrow() {
XCTAssertNoThrow(try SQLiteNormalizedCache(db: Connection()))
}

func testClearCache() {
let query = HeroNameQuery()
let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()
Expand Down

0 comments on commit d6a4d03

Please sign in to comment.