All files / src index.ts

100% Statements 16/16
100% Branches 8/8
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x   1x 1x 1x                                                               4x   4x 1x     4x 1x     4x         4x   4x     1x  
import sqlite3 from 'sqlite3'
 
import { Sqlite3Statement } from './sqlite3/Sqlite3Statement'
import { Sqlite3Database } from './sqlite3/Sqlite3Database'
import { Sqlite3 } from './interfaces/Sqlite3.interfaces'
 
export interface OpenParams extends Sqlite3.Config {
  /**
   * Use an alternative library instead of sqlite3. The interface of the library must
   * conform to `sqlite3`.
   *
   * The default is to use `sqlite3` as the driver.
   *
   * @see https://github.com/mapbox/node-sqlite3/wiki/API
   */
  driver?: any
 
  /**
   * If true, uses the `sqlite3` built-in database object cache to avoid opening the same
   * database multiple times.
   *
   * Does not apply if `driver` is defined.
   *
   * @see https://github.com/mapbox/node-sqlite3/wiki/Caching
   */
  cached?: boolean
 
  /**
   * Enables verbose mode.
   *
   * This only applies to the `sqlite3` driver.
   */
  verbose?: boolean
}
 
async function open (config: OpenParams): Promise<Sqlite3Database> {
  let driver = config.driver || sqlite3.Database
 
  if (!config.driver && config.cached) {
    driver = sqlite3.cached.Database
  }
 
  if (config.verbose) {
    sqlite3.verbose()
  }
 
  const db = new Sqlite3Database({
    ...config,
    driver
  })
 
  await db.open()
 
  return db
}
 
export { open, Sqlite3Statement, Sqlite3Database, Sqlite3 }