You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.1 KiB
52 lines
1.1 KiB
/**
|
|
* db
|
|
*
|
|
* @author Björn Hase <me@herr-hase.wtf>
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
|
* @link https://gitea.node001.net/HerrHase/urban-bucket.git
|
|
*
|
|
*/
|
|
|
|
import { Database } from 'bun:sqlite'
|
|
|
|
function getOrCreateSqlite(options = {}): Database {
|
|
|
|
let db
|
|
|
|
// check version for merge options
|
|
// safeIntegers only aviable in version >= 1.1.14
|
|
if (Bun.version < '1.1.14') {
|
|
options = Object.assign({
|
|
uri: process.env.NANO_SQLITE_PATH,
|
|
wal: true
|
|
}, options)
|
|
} else {
|
|
options = Object.assign({
|
|
uri: process.env.NANO_SQLITE_PATH,
|
|
wal: true,
|
|
safeIntegers: true
|
|
}, options)
|
|
}
|
|
|
|
const uri = options.uri
|
|
delete options.uri
|
|
|
|
const wal = options.wal
|
|
delete options.wal
|
|
|
|
//
|
|
if (Object.keys(options).length === 0) {
|
|
db = new Database(uri)
|
|
} else {
|
|
db = new Database(uri, options)
|
|
}
|
|
|
|
if (wal) {
|
|
db.exec('PRAGMA journal_mode = WAL;')
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
export default getOrCreateSqlite
|