From 05720416fdd7727d5e7f945c86a7b95d73f80328 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Mon, 22 Jul 2024 23:23:45 +0200 Subject: [PATCH] change function, update documentation --- README.md | 60 +++++++++++++++++++++++++++++++++++++++--- bunfig.toml | 2 -- src/sqlite.ts | 24 +++++++++++------ src/store.ts | 16 +++++------ test/migration.test.ts | 2 +- test/sqlite.test.ts | 2 +- test/store.test.ts | 8 +++--- 7 files changed, 86 insertions(+), 28 deletions(-) delete mode 100644 bunfig.toml diff --git a/README.md b/README.md index 2f04c4b..08c33c4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,59 @@ # Nano Sqlite -Functions and Classes for handle sqlite in [Bun](https://bun.sh/). +Functions and Classes for handle sqlite in [Bun](https://bun.sh/). -## Function runMigrationSqlite(filePath: string, db: object) -## Function getOrCreateSqlite(options = {}) -## Abstract Class Store +## API + +### getOrCreateSqlite(options = {}) + +Options are using default Parameters from [Bun](https://bun.sh/docs/api/sqlite). There +path can be set by .env + +``` +NANO_SQLITE_PATH="./../storage/db.sqlite" +``` + +or by parameter, + +``` +const db = getOrCreateSqlite({ path: './../storage/db.sqlite' }) +``` + +Per Default WAL mode is enabled. To disable add parameter "wal", + +``` +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. + +### Store + +Store is a Abstract Class to extend a Class for an Single Table. There +are no functions to validating Columns. + +``` +class Store extends Store { + constructor(db) { + super(db, '') + } +} +``` + +#### findOneById(id: integer) + +Getting single row by Id. + +#### create(data: object) + +Create a new row in a Table. + +#### update(id: integer, data: object) + +Update single row by Id. + +#### remove(id: integer) + +Remove single row by Id. diff --git a/bunfig.toml b/bunfig.toml deleted file mode 100644 index f6b3dcd..0000000 --- a/bunfig.toml +++ /dev/null @@ -1,2 +0,0 @@ -[install.scopes] -"@nano" = "https://git.node001.net/api/packages/nano/npm/" diff --git a/src/sqlite.ts b/src/sqlite.ts index 6c3815d..7e8d9a4 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -11,21 +11,29 @@ import { Database } from 'bun:sqlite' function getOrCreateSqlite(options = {}): Database { + let db + // merge options options = Object.assign({ - name: process.env.NANO_SQLITE_STORAGE + '/' + process.env.NANO_SQLITE_NAME, - safeIntegers: true + uri: process.env.NANO_SQLITE_PATH, + wal: true }, options) - const name = options.name - delete options.name - + const uri = options.uri + delete options.uri + + const wal = options.wal + delete options.wal + if (Object.keys(options).length === 0) { - options = undefined + db = new Database(uri) + } else { + db = new Database(uri, options) } - const db = new Database(name) - db.exec('PRAGMA journal_mode = WAL;') + if (wal) { + db.exec('PRAGMA journal_mode = WAL;') + } return db } diff --git a/src/store.ts b/src/store.ts index 29a301c..1aad02f 100644 --- a/src/store.ts +++ b/src/store.ts @@ -18,7 +18,7 @@ abstract class Store { * @param {tableName} * */ - constructor(db: object, tableName?: string) { + public constructor(db: object, tableName?: string) { this.db = db this.tableName = tableName } @@ -27,7 +27,7 @@ abstract class Store { * create row * */ - findOneById(id: integer): object { + public findOneById(id: integer): object { return this.db .query('SELECT * FROM ' + this.tableName + ' WHERE id = $id') .get({ @@ -39,7 +39,7 @@ abstract class Store { * create row * */ - create(data: object): integer { + public create(data: object): integer { this.db .query('INSERT INTO ' + this.tableName + ' (' + Object.keys(data).join(', ') + ') VALUES (' + this.prepareInsertBinding(data) + ')') .run(this.prepareData(data)) @@ -51,7 +51,7 @@ abstract class Store { * update row * */ - update(id: integer, data: object): void { + public update(id: integer, data: object): void { this.db .query('UPDATE ' + this.tableName + ' SET ' + this.prepareUpdateBinding(data) + ' WHERE id = $id') .run(this.prepareData(Object.assign(data, { @@ -63,7 +63,7 @@ abstract class Store { * create row * */ - remove(id: integer): remove { + public remove(id: integer): void { this.db .query('DELETE FROM ' + this.tableName + ' WHERE id = $id') .run({ @@ -74,7 +74,7 @@ abstract class Store { /** * */ - prepareData(data: object) { + protected prepareData(data: object) { const results = {} for (const key in data) { @@ -91,7 +91,7 @@ abstract class Store { * @param {object} data * @return {string} */ - prepareInsertBinding(data: array) { + protected prepareInsertBinding(data: array) { let result = [] for (const key in data) { @@ -108,7 +108,7 @@ abstract class Store { * @param {object} data * @return {string} */ - prepareUpdateBinding(data: array) { + protected prepareUpdateBinding(data: array) { let result = [] for (const key in data) { diff --git a/test/migration.test.ts b/test/migration.test.ts index cb3f579..1abb0aa 100644 --- a/test/migration.test.ts +++ b/test/migration.test.ts @@ -6,7 +6,7 @@ import getOrCreateSqlite from './../src/sqlite.ts' import { Database } from 'bun:sqlite' test('migration', async () => { - const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true }) + const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true }) await runMigrationSqlite('./../resources', db) // check for boxes diff --git a/test/sqlite.test.ts b/test/sqlite.test.ts index 70dc4af..a46206f 100644 --- a/test/sqlite.test.ts +++ b/test/sqlite.test.ts @@ -2,6 +2,6 @@ import { expect, test } from 'bun:test' import getOrCreateSqlite from './../src/sqlite.ts' test('sqlite', () => { - const db = getOrCreateSqlite({ 'name': ':memory:' }) + const db = getOrCreateSqlite({ 'uri': ':memory:' }) expect(typeof db === 'object').toBeTruthy(); }) diff --git a/test/store.test.ts b/test/store.test.ts index 8ae7ca1..d893264 100644 --- a/test/store.test.ts +++ b/test/store.test.ts @@ -5,14 +5,14 @@ import runMigrationSqlite from './../src/migration.ts' import getOrCreateSqlite from './../src/sqlite.ts' test('store', () => { - const db = getOrCreateSqlite({ 'name': ':memory:' }) + const db = getOrCreateSqlite({ 'uri': ':memory:' }) const itemStore = new ItemStore(db) expect(typeof itemStore === 'object').toBeTruthy(); }) test('store / insert', async () => { - const db = getOrCreateSqlite({ 'name': ':memory:' }) + const db = getOrCreateSqlite({ 'uri': ':memory:' }) await runMigrationSqlite('./../resources', db) const itemStore = new ItemStore(db) @@ -24,7 +24,7 @@ test('store / insert', async () => { }) test('store / remove', async () => { - const db = getOrCreateSqlite({ 'name': ':memory:' }) + const db = getOrCreateSqlite({ 'uri': ':memory:' }) await runMigrationSqlite('./../resources', db) const itemStore = new ItemStore(db) @@ -39,7 +39,7 @@ test('store / remove', async () => { }) test('store / update', async () => { - const db = getOrCreateSqlite({ 'name': ':memory:' }) + const db = getOrCreateSqlite({ 'uri': ':memory:' }) await runMigrationSqlite('./../resources', db) const itemStore = new ItemStore(db)