OSDatabase
public class OSDatabase
OSDatabase is an object that enables OrionSearch to interact with any databases. It also contains a keywords cache
that is used for rewriting the query. The cache will be auto-configured with the configure method, but if you already ran this function, you can just restore the cache by initiating the class with it:
let db1 = OSDatabase()
db1.configure(main: "title", secondary: "content")
let db2 = new OSDatabase(db1.keywordsCache) // same as db1
⚠️ Note about database structure: Every database should have a column named
keywordsthat will be used by OrionSearch to filter records.
Set up bindings for your DB
In this section, we’ll show you a detailed example with the SQLite 3 database.
const db = new OSDatabase() // Creates the object
/* Implementing methods */
db.setPlugin(
(key, contains, range) => { // Select method
const r = range == null ? [0, Number.MAX_SAFE_INTEGER] : range // Creates a range
let rows = []
if (contains == null) { // If OrionSearch needs to query everything. Used for the configure method.
const query = data.prepare(`SELECT rowid, * FROM main WHERE rowid BETWEEN ${r[0]} AND ${r[1]}`)
rows = query.all()
} else { // Otherwise
const query = data.prepare(`SELECT rowid, * FROM main WHERE ${key} LIKE "%${contains}%" AND rowid BETWEEN ${r[0]} AND ${r[1]}`)
rows = query.all()
}
let out = []
rows.forEach(row => { // Setting the main key.
const record = new OSRecord(row) // Creating a record based on the retrieved data.
record.main("headlines") // "headlines" is now the main column for this record.
out.push(record)
})
return out // Returning all the records
},
add => { // Add a record
const query = data.prepare(`INSERT INTO main VALUES (${add.values})`)
query.run()
},
(keywords, record) => { // Manage "keywords" column. It will insert an array of records in the desired row.
const keyQuery = data.prepare(`UPDATE main SET keywords = "${[...keywords].join(' ')}" WHERE rowid = ${record.data.rowid};`)
keyQuery.run()
}
)
-
The keyword cache. Used to transfer database cache.
Declaration
Swift
public var keywordsCache: Set<String> -
OSDatabase’s initializer
Declaration
Swift
public init(cache: Set<String> = Set())Parameters
cacheThe keyword cache. Used to restore the cache.
-
Method to configure the keyword cache and
keywordcolumn of your database. It acts as a setup function.⚠️ Must be ran before trying to search anything. Can be ran once.
Declaration
Swift
public func configure(main: String, secondary: String? = nil, lang: String = "en", completion: ((Int, Int) -> Void)? = nil)Parameters
mainThe main colum of your database
secondaryThe secondary colum of your database
langThe language used for tokenizing the fields
completionSimple progress indicator callback. The first
Intwill be the actual row and the secondIntis the total. -
This function acts as a binding between OrionSearch select function and your database one.
Declaration
Swift
public func select(contains: String? = nil, key: String = "keywords", range: Range<Int>? = nil) -> [OSRecord]Parameters
containsThe pattern we’re looking to select.
keyWhich column is used
rangeThe range of rows we’re looking for.
Return Value
It returns an array of
OSRecord -
This function is used to insert the keywords into the
keywordscolumnDeclaration
Swift
public func keywords(keys: Set<String>, record: OSRecord)Parameters
keysA Set of keywords we’re looking to insert
recordThe current record / row.
-
Add records to your database while parsing them
Declaration
Swift
public func add(records: [OSRecord], main: String, secondary: String? = nil, lang: String = "en")Parameters
recordsA list of
OSRecordmainThe main colum of your database
secondaryThe secondary colum of your database
langThe language used for tokenizing the fields
-
Set interface between
OSDatabaseand your database.Declaration
Parameters
selectThis function will be called everytime OrionSearch needs to select something. It takes 3 arguments: the column, the expected value / pattern and the optionnal range. If the second or / and third arguments are equal to
nil, then return every rows.addThis function will be called to add and preprocess a row. It takes as argument a list of
OSRecordto be added in your databasekeywordsThis function will be called to insert keywords in the
keywordscolumn. It will take 2 arguments: aSet<String>of keywords that can be converted to anArrayand theOSRecordthat should be modified.
View on GitHub
OSDatabase Class Reference