Skip to content

Commit

Permalink
使用 Prettier 改進代碼質量
Browse files Browse the repository at this point in the history
  • Loading branch information
lint-action committed Sep 18, 2023
1 parent de89f3d commit cf019e0
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 229 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* Export thing all methods, functions, etc
*/

const startedAt = Date.now()
const startedAt = Date.now();

const Table = require('./main/table.js')
const read = require('./main/read.js')
const Table = require("./main/table.js");
const read = require("./main/read.js");

module.exports = {
options: (options) => (this.db = new Table('database', options)),
options: (options) => (this.db = new Table("database", options)),
startedAt,
version: require('./package.json').version,
version: require("./package.json").version,
Table,
Read: read,
set: (key, value) => this.db.set(key, value),
Expand All @@ -29,5 +29,5 @@ module.exports = {
delete: (key) => this.db.delete(key),
deleteAll: () => this.db.deleteTable(),
import: (data) => this.db.import(data),
importQuick: (data) => this.db.importQuick(data)
}
importQuick: (data) => this.db.importQuick(data),
};
66 changes: 33 additions & 33 deletions main/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Base {
* @param {string} filename
*/

constructor (table, filename) {
this.table = table
this.filename = filename
this.db = require('better-sqlite3')(this.filename)
constructor(table, filename) {
this.table = table;
this.filename = filename;
this.db = require("better-sqlite3")(this.filename);
}

/**
Expand All @@ -22,53 +22,53 @@ class Base {
* @param {any} value data
*/

set (key, value) {
set(key, value) {
this.db
.prepare(
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`,
)
.run()
.run();

let fetchedData = this.db
.prepare(`SELECT * FROM ${this.table} WHERE key = (?)`)
.get(key)
.get(key);

if (!fetchedData) {
this.db
.prepare(`INSERT INTO ${this.table} (key, value) VALUES (?,?)`)
.run(key, '{}')
.run(key, "{}");
fetchedData = this.db
.prepare(`SELECT * FROM ${this.table} WHERE key = (?)`)
.get(key)
.get(key);
}

this.db
.prepare(`UPDATE ${this.table} SET value = (?) WHERE key = (?)`)
.run(JSON.stringify(value), key)
return value
.run(JSON.stringify(value), key);
return value;
}

/**
* Get value of a key
* @param {string} key ID
*/

get (key) {
get(key) {
this.db
.prepare(
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`,
)
.run()
.run();

const value = this.db
.prepare(`SELECT * FROM ${this.table} WHERE key = (?)`)
.get(key)
.get(key);

if (!value) return null
if (!value) return null;
try {
return JSON.parse(value.value)
return JSON.parse(value.value);
} catch (e) {
return value.value
return value.value;
}
}

Expand All @@ -77,42 +77,42 @@ class Base {
* @param {string} key ID
*/

delete (key) {
delete(key) {
this.db
.prepare(
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`,
)
.run()
this.db.prepare(`DELETE FROM ${this.table} WHERE key = (?)`).run(key)
.run();
this.db.prepare(`DELETE FROM ${this.table} WHERE key = (?)`).run(key);
}

/**
* Get all values of a key
*/

all () {
all() {
this.db
.prepare(
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`
`CREATE TABLE IF NOT EXISTS ${this.table} (key TEXT, value TEXT)`,
)
.run()
.run();

const statement = this.db.prepare(
`SELECT * FROM ${this.table} WHERE key IS NOT NULL`
)
const data = statement.iterate()
const result = []
`SELECT * FROM ${this.table} WHERE key IS NOT NULL`,
);
const data = statement.iterate();
const result = [];

for (const set of data) {
result.push({ key: set.key, value: JSON.parse(set.value) })
result.push({ key: set.key, value: JSON.parse(set.value) });
}

return result
return result;
}
}

/**
* Export the table
*/

module.exports = Base
module.exports = Base;
20 changes: 10 additions & 10 deletions main/read.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Base = require('./base.js')
const Base = require("./base.js");

/**
* Read EDB Sqlite files and will return data!
Expand All @@ -13,27 +13,27 @@ class Read {
* const data = new Read('myOldFile.sqlite', { table: 'myCustomTable' })
*/

constructor (filename, options = {}) {
if (!filename) throw new Error('Missing filepath or filename!')
if (!options.table) options.table = 'database'
constructor(filename, options = {}) {
if (!filename) throw new Error("Missing filepath or filename!");
if (!options.table) options.table = "database";

this.filename = filename
this.options = options
this.base = new Base(this.options.table, this.filename)
this.filename = filename;
this.options = options;
this.base = new Base(this.options.table, this.filename);
}

/**
* Render the data by the given options from the constructor
* @example const renderedData = data.get() // Now you have got all the data in object[] form
*/

get () {
return this.base.all()
get() {
return this.base.all();
}
}

/**
* Export the table
*/

module.exports = Read
module.exports = Read;
Loading

0 comments on commit cf019e0

Please sign in to comment.