Skip to content

Commit

Permalink
stable release
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflake107 committed Feb 3, 2021
1 parent 09f9557 commit ce0f253
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 16 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Quick.DB
The hackable **[quick.db](https://npmjs.com/package/quick.db)**.

# Under Development
🚧

> **You can easily switch between this library and quick.db if you are using `json.sqlite` file as database.**
# Examples
Expand All @@ -23,6 +20,32 @@ for (const data of db) {
}
```

## Basic Example

```js
// Setting an object in the database:
console.log(db.set('userInfo', { difficulty: 'Easy' }))
// -> { difficulty: 'Easy' }

// Pushing an element to an array (that doesn't exist yet) in an object:
console.log(db.push('userInfo.items', 'Sword'))
// -> { difficulty: 'Easy', items: ['Sword'] }

// Adding to a number (that doesn't exist yet) in an object:
console.log(db.add('userInfo.balance', 500))
// -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }

// Repeating previous examples:
console.log(db.push('userInfo.items', 'Watch'))
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }
console.log(db.add('userInfo.balance', 500))
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }

// Fetching individual properties
console.log(db.get('userInfo.balance')) // -> 1000
console.log(db.get('userInfo.items')) // ['Sword', 'Watch']
```

## Creating a table

```js
Expand Down Expand Up @@ -108,6 +131,9 @@ console.log(db.get("foo"));
- db.size()
- db.eval()
- db.prepareTable()
- db.export()
- db.use()
- db.allTableArray()

## Properties
- db.database
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devsnowflake/quick.db",
"version": "1.0.2-dev",
"version": "1.0.0",
"description": "The hackable quick.db",
"main": "index.js",
"scripts": {
Expand All @@ -13,7 +13,14 @@
"keywords": [
"quick.db",
"database",
"sqlite"
"sqlite",
"better-sqlite3",
"sqlite3",
"db",
"easy",
"simple",
"fast",
"enmap"
],
"author": "Snowflake107",
"license": "Apache-2.0",
Expand Down
57 changes: 51 additions & 6 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Database {
/**
* The SQLite3 database.
*/
Object.defineProperty(this, "_database", {
value: options.database || new SQLite(name, options),
writable: false,
enumerable: true
Object.defineProperties(this, {
_database: {
value: options.database || new SQLite(name, options),
writable: true,
enumerable: false
},
});

this.prepareTable();
Expand Down Expand Up @@ -261,9 +263,9 @@ class Database {
try { data = JSON.parse(data) } catch { }

if (target && typeof data === "object") {
data = lodash.unset(data, target);
const r = lodash.unset(data, target);
if (!r) return false;
data = JSON.stringify(data);

this.database.prepare(`UPDATE ${table} SET json = (?) WHERE ID = (?)`).run(data, id);

return true;
Expand Down Expand Up @@ -554,6 +556,49 @@ class Database {
tables: Object.values(data).map(m => m.name)
};
}

/**
* Exports this db
* @param {object} options Export options
*/
export(options = { stringify: false, format: false, tableName: null, allTable: false }) {
let data = {
data: options.allTable ? this.allTableArray() : this.all({ table: options.tableName || this.tableName }),
mod: "quick.db",
generatedTimestamp: new Date().getTime()
};

if (options.stringify) data = JSON.stringify(data, null, options.format ? (typeof options.format === "number" ? options.format : "\t") : null);

return data;
}

use(database) {
if (!database) throw new Error("Database was not provided!");
if (database.prototype instanceof Database || database instanceof Database) this._database = database._database;
if (database instanceof SQLite || database.prototype instanceof SQLite || database instanceof this._database || database.prototype instanceof this._database) this._database = database;

throw new Error("Invalid database");
}

/**
* Returns all table(s) as array.
*/
allTableArray() {
const { tables } = this.tables();
let arr = [];

tables.forEach((table, index) => {
arr.push({
id: index,
table: table,
data: this.all({ table: table })
})
})

return arr;
}

}

module.exports = Database;
20 changes: 15 additions & 5 deletions src/Static.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const Database = require("./Database");
const db = new Database("json.sqlite", {
path: "./",
table: "json"
});

module.exports = () => db;
// lazy load db to make only one instance for static
let db;

/**
* Satic quick.db
* @returns {Database}
*/
module.exports = () => {
if (!db) db = new Database("json.sqlite", {
path: "./",
table: "json"
});

return db;
};

0 comments on commit ce0f253

Please sign in to comment.