Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 104299a

Browse files
authored
Merge pull request #8 from Rubenennj/master
Added typings and some code cleanup and improvements
2 parents 63e3fbd + 6aece24 commit 104299a

File tree

8 files changed

+74
-154
lines changed

8 files changed

+74
-154
lines changed

lib/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import { Events } from './typings/types/Events';
99
import { DEFAULT_TABLE } from './util/constants';
1010
import { methods } from './methods/exportMethods';
1111
import { findTables } from './functions/findTables';
12+
import { Data } from './typings/types/Data';
13+
import { sanitize } from './functions/sanitize';
1214

13-
export class ByteDatabase extends EventEmitter<Events> {
14-
public _raw: TypeDatabase;
15-
public options: DefaultByteProperties;
15+
export class ByteDatabase<T extends Data = Data> extends EventEmitter<Events> {
16+
public readonly _raw: TypeDatabase;
1617

1718
constructor(
18-
path: string,
19-
options: DefaultByteProperties = {
19+
path = ':memory:',
20+
public readonly options: DefaultByteProperties = {
2021
sanitize: true,
2122
},
2223
) {
2324
super();
2425

25-
path ??= ":memory:"
2626
this.options = options;
2727
this._raw = new Database(path, options);
2828
this._raw
@@ -32,20 +32,24 @@ export class ByteDatabase extends EventEmitter<Events> {
3232
.run();
3333
}
3434

35-
public findTables() {
35+
private trySanitize(what: string) {
36+
return this.options.sanitize ? sanitize(what) : what
37+
}
38+
39+
public findTables(): any[] {
3640
return findTables(this);
3741
}
3842

39-
public insert(key: string, val: any, table: string = DEFAULT_TABLE) {
43+
public insert(key: string, val: T, table: string = DEFAULT_TABLE): Database.RunResult {
4044
return methods.insert(key, val, table, this);
4145
}
4246

43-
public find(key: string, table: string = DEFAULT_TABLE) {
47+
public find(key: string, table: string = DEFAULT_TABLE): T {
4448
return methods.find(key, table, this);
4549
}
4650

47-
public all(table: string = DEFAULT_TABLE) {
48-
return methods.all(table, this);
51+
public all(table: string = DEFAULT_TABLE): T[] {
52+
return methods.all(table, this) as T[];
4953
}
5054

5155
public wipe(table: string = DEFAULT_TABLE) {

lib/methods/exportMethods.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { wipe } from './wipe';
55
import { del } from './delete';
66

77
export const methods = {
8-
insert: insert,
9-
find: find,
10-
all: all,
11-
wipe: wipe,
12-
delete: del,
13-
};
8+
insert,
9+
all,
10+
wipe,
11+
find,
12+
delete: del
13+
} as const;

lib/methods/insert.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ByteError } from '../structures/ByteError';
33
import { DEFAULT_TABLE } from '../util/constants';
44
import { set } from 'lodash';
55
import { RowManager } from '../structures/RowManager';
6+
import { Data } from '../typings/types/Data';
67

78
export function insert(
89
key: string,
@@ -26,14 +27,14 @@ export function insert(
2627
if (key.includes('.')) {
2728
let splitted = key.split('.');
2829
let ParentKey = splitted[0];
29-
let got = db.find(ParentKey);
30+
let got = db.find(ParentKey) as object;
3031
let _ = set(got ?? {}, splitted.slice(1).join('.'), value);
3132

32-
return _rows.insertRowByKey(ParentKey, _, table);
33+
return _rows.insertRowByKey(ParentKey, _ as Data, table);
3334
} else {
34-
let got = db.find(key);
35+
let got = db.find(key) as object;
3536
let _ = set(got ?? {}, key, value);
3637

37-
return _rows.insertRowByKey(key, _, table);
38+
return _rows.insertRowByKey(key, _ as Data, table);
3839
}
3940
}

lib/structures/RowManager.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { ByteDatabase } from '../index';
2-
import { sanitize } from '../functions/sanitize';
32
import { DEFAULT_TABLE } from '../util/constants';
43

54
export class RowManager {
6-
_database: ByteDatabase;
5+
private readonly _database: ByteDatabase;
76

87
constructor(db: ByteDatabase) {
98
this._database = db;
@@ -15,7 +14,7 @@ export class RowManager {
1514
table: string = DEFAULT_TABLE,
1615
) {
1716
value = value !== 'string' ? JSON.stringify(value) : value;
18-
table = this._database.options.sanitize ? sanitize(table) : table;
17+
table = this.trySanitize(table)
1918
return this._database._raw
2019
.prepare(`INSERT INTO ${table} (ID,Json) VALUES (?,?)`)
2120
.run(key, value);
@@ -27,22 +26,26 @@ export class RowManager {
2726
table: string = DEFAULT_TABLE,
2827
) {
2928
value = value !== 'string' ? JSON.stringify(value) : value;
30-
table = this._database.options.sanitize ? sanitize(table) : table;
29+
table = this.trySanitize(table)
3130
return this._database._raw
3231
.prepare(`UPDATE ${table} SET Json = (?) WHERE ID = (?)`)
3332
.run(key, value);
3433
}
3534

35+
private trySanitize(...args: Parameters<ByteDatabase["trySanitize"]>) {
36+
return this._database["trySanitize"](...args)
37+
}
38+
3639
findRowByKey(key: string, table: string = DEFAULT_TABLE) {
37-
table = this._database.options.sanitize ? sanitize(table) : table;
40+
table = this.trySanitize(table)
3841
const val = this._database._raw
3942
.prepare(`SELECT Json FROM ${table} WHERE ID = @key`)
4043
.get({ key });
4144
return val != null ? JSON.parse(val.Json) : null;
4245
}
4346

4447
findAllRows(table: string = DEFAULT_TABLE) {
45-
table = this._database.options.sanitize ? sanitize(table) : table;
48+
table = this.trySanitize(table)
4649
const val = this._database._raw.prepare(`SELECT * FROM ${table}`).iterate();
4750
const data: object[] = [];
4851
for (const i of val) {
@@ -57,12 +60,12 @@ export class RowManager {
5760
}
5861

5962
deleteAllRows(table: string = DEFAULT_TABLE) {
60-
table = this._database.options.sanitize ? sanitize(table) : table;
63+
table = this.trySanitize(table)
6164
return this._database._raw.prepare(`DELETE FROM ${table}`).run();
6265
}
6366

6467
deleteRowByKey(key: string, table: string = DEFAULT_TABLE) {
65-
table = this._database.options.sanitize ? sanitize(table) : table;
68+
table = this.trySanitize(table)
6669
return this._database._raw
6770
.prepare(`DELETE FROM ${table} WHERE ID = @key`)
6871
.run({ key });

lib/structures/Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { CREATE_TABLE_REGEX } from '../util/constants';
33
import { ByteError } from './ByteError';
44

55
export class Table {
6-
private _database: ByteDatabase;
7-
private _tableName: string;
6+
private readonly _database: ByteDatabase;
7+
private readonly _tableName: string;
88

99
constructor(name: string, db: ByteDatabase) {
1010
this._database = db;

lib/typings/types/Data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Data = {
2+
[key: PropertyKey]: unknown
3+
} | string | unknown[]

lib/typings/types/ToRecord.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Converts an interface to a typed record
3+
*/
4+
export type ToRecord<T> = {
5+
[P in keyof T]: T[P]
6+
}

0 commit comments

Comments
 (0)