Skip to content

Commit ce0f253

Browse files
author
Snowflake107
committed
stable release
1 parent 09f9557 commit ce0f253

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Quick.DB
22
The hackable **[quick.db](https://npmjs.com/package/quick.db)**.
33

4-
# Under Development
5-
🚧
6-
74
> **You can easily switch between this library and quick.db if you are using `json.sqlite` file as database.**
85
96
# Examples
@@ -23,6 +20,32 @@ for (const data of db) {
2320
}
2421
```
2522

23+
## Basic Example
24+
25+
```js
26+
// Setting an object in the database:
27+
console.log(db.set('userInfo', { difficulty: 'Easy' }))
28+
// -> { difficulty: 'Easy' }
29+
30+
// Pushing an element to an array (that doesn't exist yet) in an object:
31+
console.log(db.push('userInfo.items', 'Sword'))
32+
// -> { difficulty: 'Easy', items: ['Sword'] }
33+
34+
// Adding to a number (that doesn't exist yet) in an object:
35+
console.log(db.add('userInfo.balance', 500))
36+
// -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }
37+
38+
// Repeating previous examples:
39+
console.log(db.push('userInfo.items', 'Watch'))
40+
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }
41+
console.log(db.add('userInfo.balance', 500))
42+
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }
43+
44+
// Fetching individual properties
45+
console.log(db.get('userInfo.balance')) // -> 1000
46+
console.log(db.get('userInfo.items')) // ['Sword', 'Watch']
47+
```
48+
2649
## Creating a table
2750

2851
```js
@@ -108,6 +131,9 @@ console.log(db.get("foo"));
108131
- db.size()
109132
- db.eval()
110133
- db.prepareTable()
134+
- db.export()
135+
- db.use()
136+
- db.allTableArray()
111137

112138
## Properties
113139
- db.database

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devsnowflake/quick.db",
3-
"version": "1.0.2-dev",
3+
"version": "1.0.0",
44
"description": "The hackable quick.db",
55
"main": "index.js",
66
"scripts": {
@@ -13,7 +13,14 @@
1313
"keywords": [
1414
"quick.db",
1515
"database",
16-
"sqlite"
16+
"sqlite",
17+
"better-sqlite3",
18+
"sqlite3",
19+
"db",
20+
"easy",
21+
"simple",
22+
"fast",
23+
"enmap"
1724
],
1825
"author": "Snowflake107",
1926
"license": "Apache-2.0",

src/Database.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class Database {
2828
/**
2929
* The SQLite3 database.
3030
*/
31-
Object.defineProperty(this, "_database", {
32-
value: options.database || new SQLite(name, options),
33-
writable: false,
34-
enumerable: true
31+
Object.defineProperties(this, {
32+
_database: {
33+
value: options.database || new SQLite(name, options),
34+
writable: true,
35+
enumerable: false
36+
},
3537
});
3638

3739
this.prepareTable();
@@ -261,9 +263,9 @@ class Database {
261263
try { data = JSON.parse(data) } catch { }
262264

263265
if (target && typeof data === "object") {
264-
data = lodash.unset(data, target);
266+
const r = lodash.unset(data, target);
267+
if (!r) return false;
265268
data = JSON.stringify(data);
266-
267269
this.database.prepare(`UPDATE ${table} SET json = (?) WHERE ID = (?)`).run(data, id);
268270

269271
return true;
@@ -554,6 +556,49 @@ class Database {
554556
tables: Object.values(data).map(m => m.name)
555557
};
556558
}
559+
560+
/**
561+
* Exports this db
562+
* @param {object} options Export options
563+
*/
564+
export(options = { stringify: false, format: false, tableName: null, allTable: false }) {
565+
let data = {
566+
data: options.allTable ? this.allTableArray() : this.all({ table: options.tableName || this.tableName }),
567+
mod: "quick.db",
568+
generatedTimestamp: new Date().getTime()
569+
};
570+
571+
if (options.stringify) data = JSON.stringify(data, null, options.format ? (typeof options.format === "number" ? options.format : "\t") : null);
572+
573+
return data;
574+
}
575+
576+
use(database) {
577+
if (!database) throw new Error("Database was not provided!");
578+
if (database.prototype instanceof Database || database instanceof Database) this._database = database._database;
579+
if (database instanceof SQLite || database.prototype instanceof SQLite || database instanceof this._database || database.prototype instanceof this._database) this._database = database;
580+
581+
throw new Error("Invalid database");
582+
}
583+
584+
/**
585+
* Returns all table(s) as array.
586+
*/
587+
allTableArray() {
588+
const { tables } = this.tables();
589+
let arr = [];
590+
591+
tables.forEach((table, index) => {
592+
arr.push({
593+
id: index,
594+
table: table,
595+
data: this.all({ table: table })
596+
})
597+
})
598+
599+
return arr;
600+
}
601+
557602
}
558603

559604
module.exports = Database;

src/Static.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const Database = require("./Database");
2-
const db = new Database("json.sqlite", {
3-
path: "./",
4-
table: "json"
5-
});
62

7-
module.exports = () => db;
3+
// lazy load db to make only one instance for static
4+
let db;
5+
6+
/**
7+
* Satic quick.db
8+
* @returns {Database}
9+
*/
10+
module.exports = () => {
11+
if (!db) db = new Database("json.sqlite", {
12+
path: "./",
13+
table: "json"
14+
});
15+
16+
return db;
17+
};

0 commit comments

Comments
 (0)