development
HerrHase 4 months ago
parent 61f9379639
commit 1d38107aa8

@ -1,26 +1,26 @@
# Nano Sqlite
Functions and Classes for handle sqlite in [Bun](https://bun.sh/).
Functions and Classes for handle Sqlite in [Bun](https://bun.sh/).
## API
### getOrCreateSqlite(options = {})
Options are using default Parameters from [Bun](https://bun.sh/docs/api/sqlite). Path
to sqlite can set in .env
As options the default Parameters from [Bun](https://bun.sh/docs/api/sqlite) are available.
The uri of the Sqlite can be set in the .env-file,
```
NANO_SQLITE_PATH="./../storage/db.sqlite"
```
or by parameter,
or by Parameter in options,
```
const db = getOrCreateSqlite({ uri: './../storage/db.sqlite' })
const db = getOrCreateSqlite({ uri: ':memory:' })
```
Per Default WAL mode is enabled. To disable add parameter "wal",
Per Default WAL mode is enabled. To disable add Parameter "wal",
```
const db = getOrCreateSqlite({ path: './../storage/db.sqlite', 'wal': false })
@ -28,7 +28,7 @@ const db = getOrCreateSqlite({ path: './../storage/db.sqlite', 'wal': false })
### runMigrationSqlite(filePath: string, db: object)
Reads SQL-Files from a Directory and execute them in the Sqlite.
This functions Reads SQL-Files from a Directory and execute them in the Sqlite.
### Store

@ -12,9 +12,6 @@ async function runMigrationSqlite(filePath: string, db: object): void {
const SQL_EXTENSION = '.sql'
// resolve path
filePath = path.resolve(__dirname, filePath)
// getting files
const files = await readdir(filePath)

@ -13,11 +13,20 @@ function getOrCreateSqlite(options = {}): Database {
let db
// merge options
// 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
@ -25,6 +34,7 @@ function getOrCreateSqlite(options = {}): Database {
const wal = options.wal
delete options.wal
//
if (Object.keys(options).length === 0) {
db = new Database(uri)
} else {

@ -7,7 +7,7 @@ import { Database } from 'bun:sqlite'
test('migration', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite('./../resources', db)
await runMigrationSqlite(path.join(__dirname, './../resources'), db)
// check for boxes
const results = db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='items'").get()

@ -1,4 +1,5 @@
import { expect, test } from 'bun:test'
import path from 'path'
import ItemStore from './../resources/itemStore.ts'
import runMigrationSqlite from './../src/migration.ts'
@ -14,7 +15,7 @@ test('store', () => {
test('store / insert', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db)
await runMigrationSqlite(path.join(__dirname, './../resources'), db)
const itemStore = new ItemStore(db)
const id = itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' })
@ -26,7 +27,7 @@ test('store / insert', async () => {
test('store / remove', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db)
await runMigrationSqlite(path.join(__dirname, './../resources'), db)
const itemStore = new ItemStore(db)
itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' })
@ -41,7 +42,7 @@ test('store / remove', async () => {
test('store / update', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db)
await runMigrationSqlite(path.join(__dirname, './../resources'), db)
const itemStore = new ItemStore(db)
const id = itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' })

Loading…
Cancel
Save