From 1d38107aa887d90a7fdfb4450f1ac3370c07e116 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Wed, 24 Jul 2024 23:58:54 +0200 Subject: [PATCH] adding --- README.md | 12 ++++++------ src/migration.ts | 3 --- src/sqlite.ts | 20 +++++++++++++++----- test/migration.test.ts | 2 +- test/store.test.ts | 7 ++++--- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 37d23d1..531af7e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/migration.ts b/src/migration.ts index cb076c1..961e141 100644 --- a/src/migration.ts +++ b/src/migration.ts @@ -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) diff --git a/src/sqlite.ts b/src/sqlite.ts index 7e8d9a4..5580d6d 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -13,11 +13,20 @@ function getOrCreateSqlite(options = {}): Database { let db - // merge options - options = Object.assign({ - uri: process.env.NANO_SQLITE_PATH, - wal: true - }, 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 { diff --git a/test/migration.test.ts b/test/migration.test.ts index 1abb0aa..27b6f05 100644 --- a/test/migration.test.ts +++ b/test/migration.test.ts @@ -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() diff --git a/test/store.test.ts b/test/store.test.ts index d893264..e071919 100644 --- a/test/store.test.ts +++ b/test/store.test.ts @@ -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' })