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
keywords
that 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
cache
The keyword cache. Used to restore the cache.
-
Method to configure the keyword cache and
keyword
column 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
main
The main colum of your database
secondary
The secondary colum of your database
lang
The language used for tokenizing the fields
completion
Simple progress indicator callback. The first
Int
will be the actual row and the secondInt
is 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
contains
The pattern we’re looking to select.
key
Which column is used
range
The 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
keywords
columnDeclaration
Swift
public func keywords(keys: Set<String>, record: OSRecord)
Parameters
keys
A Set of keywords we’re looking to insert
record
The 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
records
A list of
OSRecord
main
The main colum of your database
secondary
The secondary colum of your database
lang
The language used for tokenizing the fields
-
Set interface between
OSDatabase
and your database.Declaration
Parameters
select
This 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.add
This function will be called to add and preprocess a row. It takes as argument a list of
OSRecord
to be added in your databasekeywords
This function will be called to insert keywords in the
keywords
column. It will take 2 arguments: aSet<String>
of keywords that can be converted to anArray
and theOSRecord
that should be modified.