Index
public struct Index: Indexer
Indexer implemenation to define a Database index used to query the database
Example:
let index = Database.Index(name: "myView", version: "1.0", map: { (document, addRow) in
if let type = document["type"] {
addRow(type, nil)
}
})
let config = Database.OpenDatabaseConfiguration(indexes: [index])
-
Declaration
Swift
public private(set) var name: String
-
Declaration
Swift
public private(set) var version: String
-
Declaration
Swift
public private(set) var map: Indexer.Map
-
Declaration
Swift
public private(set) var reduce: Indexer.Reduce?
-
Initialization of new Index
Important
Versioning:Once created, a database index is stored as part of the data in database, for this reason, changes to an index require special handling.
If any part of the
map
function orreduce
function change, theversion
string should be changed to ensure the index is properly updated. Failure to change this value when updating the code will lead to unpredicable results.Example:
let index = Database.Index(name: "myView", version: "1.0", map: { (document, addRow) in if let type = document["type"] { addRow(type, nil) } }) let config = Database.OpenDatabaseConfiguration(indexes: [index])
Declaration
Swift
public init(name: String, version: String, map: @escaping Map, reduce: Reduce? = nil)
Parameters
name
Name of the index, required to run queries against the index.
version
Version of the index. See Versioning above
map
Map
closure that defines the index. For further information see themap
property.reduce
Optional
Reduce
closure that performs summarization during queries. For further information see thereduce
property.