change function, update documentation

development
HerrHase 5 months ago
parent 10c3fcdf28
commit 05720416fd

@ -1,7 +1,59 @@
# Nano Sqlite # 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) ## API
## Function getOrCreateSqlite(options = {})
## Abstract Class Store ### 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-name>Store extends Store {
constructor(db) {
super(db, '<table-name>')
}
}
```
#### 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.

@ -1,2 +0,0 @@
[install.scopes]
"@nano" = "https://git.node001.net/api/packages/nano/npm/"

@ -11,21 +11,29 @@ import { Database } from 'bun:sqlite'
function getOrCreateSqlite(options = {}): Database { function getOrCreateSqlite(options = {}): Database {
let db
// merge options // merge options
options = Object.assign({ options = Object.assign({
name: process.env.NANO_SQLITE_STORAGE + '/' + process.env.NANO_SQLITE_NAME, uri: process.env.NANO_SQLITE_PATH,
safeIntegers: true wal: true
}, options) }, options)
const name = options.name const uri = options.uri
delete options.name delete options.uri
const wal = options.wal
delete options.wal
if (Object.keys(options).length === 0) { if (Object.keys(options).length === 0) {
options = undefined db = new Database(uri)
} else {
db = new Database(uri, options)
} }
const db = new Database(name) if (wal) {
db.exec('PRAGMA journal_mode = WAL;') db.exec('PRAGMA journal_mode = WAL;')
}
return db return db
} }

@ -18,7 +18,7 @@ abstract class Store {
* @param {tableName} * @param {tableName}
* *
*/ */
constructor(db: object, tableName?: string) { public constructor(db: object, tableName?: string) {
this.db = db this.db = db
this.tableName = tableName this.tableName = tableName
} }
@ -27,7 +27,7 @@ abstract class Store {
* create row * create row
* *
*/ */
findOneById(id: integer): object { public findOneById(id: integer): object {
return this.db return this.db
.query('SELECT * FROM ' + this.tableName + ' WHERE id = $id') .query('SELECT * FROM ' + this.tableName + ' WHERE id = $id')
.get({ .get({
@ -39,7 +39,7 @@ abstract class Store {
* create row * create row
* *
*/ */
create(data: object): integer { public create(data: object): integer {
this.db this.db
.query('INSERT INTO ' + this.tableName + ' (' + Object.keys(data).join(', ') + ') VALUES (' + this.prepareInsertBinding(data) + ')') .query('INSERT INTO ' + this.tableName + ' (' + Object.keys(data).join(', ') + ') VALUES (' + this.prepareInsertBinding(data) + ')')
.run(this.prepareData(data)) .run(this.prepareData(data))
@ -51,7 +51,7 @@ abstract class Store {
* update row * update row
* *
*/ */
update(id: integer, data: object): void { public update(id: integer, data: object): void {
this.db this.db
.query('UPDATE ' + this.tableName + ' SET ' + this.prepareUpdateBinding(data) + ' WHERE id = $id') .query('UPDATE ' + this.tableName + ' SET ' + this.prepareUpdateBinding(data) + ' WHERE id = $id')
.run(this.prepareData(Object.assign(data, { .run(this.prepareData(Object.assign(data, {
@ -63,7 +63,7 @@ abstract class Store {
* create row * create row
* *
*/ */
remove(id: integer): remove { public remove(id: integer): void {
this.db this.db
.query('DELETE FROM ' + this.tableName + ' WHERE id = $id') .query('DELETE FROM ' + this.tableName + ' WHERE id = $id')
.run({ .run({
@ -74,7 +74,7 @@ abstract class Store {
/** /**
* *
*/ */
prepareData(data: object) { protected prepareData(data: object) {
const results = {} const results = {}
for (const key in data) { for (const key in data) {
@ -91,7 +91,7 @@ abstract class Store {
* @param {object} data * @param {object} data
* @return {string} * @return {string}
*/ */
prepareInsertBinding(data: array) { protected prepareInsertBinding(data: array) {
let result = [] let result = []
for (const key in data) { for (const key in data) {
@ -108,7 +108,7 @@ abstract class Store {
* @param {object} data * @param {object} data
* @return {string} * @return {string}
*/ */
prepareUpdateBinding(data: array) { protected prepareUpdateBinding(data: array) {
let result = [] let result = []
for (const key in data) { for (const key in data) {

@ -6,7 +6,7 @@ import getOrCreateSqlite from './../src/sqlite.ts'
import { Database } from 'bun:sqlite' import { Database } from 'bun:sqlite'
test('migration', async () => { 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) await runMigrationSqlite('./../resources', db)
// check for boxes // check for boxes

@ -2,6 +2,6 @@ import { expect, test } from 'bun:test'
import getOrCreateSqlite from './../src/sqlite.ts' import getOrCreateSqlite from './../src/sqlite.ts'
test('sqlite', () => { test('sqlite', () => {
const db = getOrCreateSqlite({ 'name': ':memory:' }) const db = getOrCreateSqlite({ 'uri': ':memory:' })
expect(typeof db === 'object').toBeTruthy(); expect(typeof db === 'object').toBeTruthy();
}) })

@ -5,14 +5,14 @@ import runMigrationSqlite from './../src/migration.ts'
import getOrCreateSqlite from './../src/sqlite.ts' import getOrCreateSqlite from './../src/sqlite.ts'
test('store', () => { test('store', () => {
const db = getOrCreateSqlite({ 'name': ':memory:' }) const db = getOrCreateSqlite({ 'uri': ':memory:' })
const itemStore = new ItemStore(db) const itemStore = new ItemStore(db)
expect(typeof itemStore === 'object').toBeTruthy(); expect(typeof itemStore === 'object').toBeTruthy();
}) })
test('store / insert', async () => { test('store / insert', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:' }) const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db) await runMigrationSqlite('./../resources', db)
const itemStore = new ItemStore(db) const itemStore = new ItemStore(db)
@ -24,7 +24,7 @@ test('store / insert', async () => {
}) })
test('store / remove', async () => { test('store / remove', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:' }) const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db) await runMigrationSqlite('./../resources', db)
const itemStore = new ItemStore(db) const itemStore = new ItemStore(db)
@ -39,7 +39,7 @@ test('store / remove', async () => {
}) })
test('store / update', async () => { test('store / update', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:' }) const db = getOrCreateSqlite({ 'uri': ':memory:' })
await runMigrationSqlite('./../resources', db) await runMigrationSqlite('./../resources', db)
const itemStore = new ItemStore(db) const itemStore = new ItemStore(db)

Loading…
Cancel
Save