@onurege3467/zerohelper 1.4.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/database/index.js CHANGED
@@ -1,9 +1,11 @@
1
- var jsonDatabasse = require('./jsondatabase/index')
2
- var MongoDB = require('./mongodb/index')
3
- var MySQL = require('./mysql/index')
1
+ var JsonDatabase = require("./jsondatabase/index");
2
+ var MongoDB = require("./mongodb/index");
3
+ var MySQLDatabase = require("./mysql/index");
4
+ var SQLiteDatabase = require("./sqldb/index");
4
5
 
5
6
  module.exports = {
6
- JsonDatabase: jsonDatabasse,
7
- MongoDB,
8
- MySQLDatabase:MySQL
9
- }
7
+ JsonDatabase,
8
+ MongoDB,
9
+ MySQLDatabase,
10
+ SQLiteDatabase,
11
+ };
@@ -0,0 +1,151 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const sqlite3 = require("sqlite3").verbose();
4
+ var i = 0;
5
+ class Database {
6
+ constructor(dbFilePath) {
7
+ this.dbFilePath = dbFilePath || path.join(__dirname, "database.sqlite");
8
+ this.db = null;
9
+ }
10
+
11
+ initialize() {
12
+ if (!fs.existsSync(this.dbFilePath)) {
13
+ console.log("Creating database file...");
14
+ fs.writeFileSync(this.dbFilePath, "");
15
+ }
16
+
17
+ this.db = new sqlite3.Database(this.dbFilePath, (err) => {
18
+ if (err) {
19
+ console.error("Error opening database:", err.message);
20
+ } else {
21
+ console.log("Connected to the SQLite database.");
22
+ this.runQuery(
23
+ `CREATE TABLE IF NOT EXISTS key_value_store (key TEXT PRIMARY KEY, value TEXT)`
24
+ )
25
+ .then(() => {
26
+ console.log("Table initialized");
27
+ i = 1;
28
+ })
29
+ .catch((err) => console.error(err));
30
+ }
31
+ });
32
+ }
33
+
34
+ set(key, value) {
35
+ return this.runQuery(
36
+ `INSERT INTO key_value_store (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
37
+ [key, JSON.stringify(value)]
38
+ );
39
+ }
40
+
41
+ get(key) {
42
+ return this.getQuery(`SELECT value FROM key_value_store WHERE key = ?`, [
43
+ key,
44
+ ]).then((row) => (row ? JSON.parse(row.value) : null));
45
+ }
46
+
47
+ delete(key) {
48
+ return this.runQuery(`DELETE FROM key_value_store WHERE key = ?`, [key]);
49
+ }
50
+
51
+ has(key) {
52
+ return this.getQuery(`SELECT 1 FROM key_value_store WHERE key = ?`, [
53
+ key,
54
+ ]).then((row) => !!row);
55
+ }
56
+
57
+ push(key, value) {
58
+ return this.get(key).then((currentValue) => {
59
+ if (!Array.isArray(currentValue)) {
60
+ currentValue = [];
61
+ }
62
+ currentValue.push(value);
63
+ return this.set(key, currentValue);
64
+ });
65
+ }
66
+
67
+ add(key, value) {
68
+ return this.get(key).then((currentValue) => {
69
+ if (typeof currentValue !== "number") {
70
+ currentValue = 0;
71
+ }
72
+ return this.set(key, currentValue + value);
73
+ });
74
+ }
75
+
76
+ sub(key, value) {
77
+ return this.get(key).then((currentValue) => {
78
+ if (typeof currentValue !== "number") {
79
+ currentValue = 0;
80
+ }
81
+ return this.set(key, currentValue - value);
82
+ });
83
+ }
84
+
85
+ runQuery(query, params = []) {
86
+ if (!i) {
87
+ console.log("Database not initialized");
88
+ return Promise.reject(new Error("Database not initialized"));
89
+ }
90
+ return new Promise((resolve, reject) => {
91
+ this.db.run(query, params, function (err) {
92
+ if (err) {
93
+ reject(err);
94
+ } else {
95
+ resolve(this);
96
+ }
97
+ });
98
+ });
99
+ }
100
+
101
+ getQuery(query, params = []) {
102
+ if (!i) {
103
+ console.log("Database not initialized");
104
+ return Promise.reject(new Error("Database not initialized"));
105
+ }
106
+ return new Promise((resolve, reject) => {
107
+ this.db.get(query, params, (err, row) => {
108
+ if (err) {
109
+ reject(err);
110
+ } else {
111
+ resolve(row);
112
+ }
113
+ });
114
+ });
115
+ }
116
+
117
+ close() {
118
+ this.db.close((err) => {
119
+ if (err) {
120
+ console.error("Error closing database:", err.message);
121
+ } else {
122
+ console.log("Database connection closed.");
123
+ }
124
+ });
125
+ }
126
+ }
127
+
128
+ module.exports = Database;
129
+
130
+ // Example usage:
131
+ // const db = new Database();
132
+ // db.initialize();
133
+ // db.set('foo', 'bar')
134
+ // .then(() => db.get('foo'))
135
+ // .then((value) => console.log('Value:', value)) // Output: bar
136
+ // .then(() => db.push('array', 'x'))
137
+ // .then(() => db.get('array'))
138
+ // .then((value) => console.log('Array:', value)) // Output: ['x']
139
+ // .then(() => db.add('number', 1))
140
+ // .then(() => db.get('number'))
141
+ // .then((value) => console.log('Number:', value)) // Output: 1
142
+ // .then(() => db.sub('number', 1))
143
+ // .then(() => db.get('number'))
144
+ // .then((value) => console.log('Number after sub:', value)) // Output: 0
145
+ // .then(() => db.has('foo'))
146
+ // .then((exists) => console.log('Exists:', exists)) // Output: true
147
+ // .then(() => db.delete('foo'))
148
+ // .then(() => db.has('foo'))
149
+ // .then((exists) => console.log('Exists after delete:', exists)) // Output: false
150
+ // .catch((err) => console.error(err))
151
+ // .finally(() => db.close());
package/database/test.js CHANGED
@@ -1,37 +1,51 @@
1
- require('dotenv').config();
2
- const runMongoDB = async()=>{
3
- const { MongoDB} = require('./index')
4
-
5
- var db = await MongoDB.createData('database','collection','data',undefined,'mongourl')
6
-
7
- db.set('foo','bar')
8
- }
9
-
10
- runMongoDB()
11
- const runMySQL = async()=>{
12
- const {MySQLDatabase}=require('./index')
13
- const db = new MySQLDatabase();
14
- await db.connect({
15
- host: 'localhost',
16
- port: '3306',
17
- user: 'root',
18
- password: '',
19
- database: 'database',
20
- charset: 'utf8mb4',
21
- });
22
-
23
- db.on('connected', async connection => {
24
- console.log('Database Connected');
25
- });
26
-
27
- db.set('table','foo','bar')
28
-
29
- }
1
+ require("dotenv").config();
2
+ const runMongoDB = async () => {
3
+ const { MongoDB } = require("./index");
4
+
5
+ var db = await MongoDB.createData(
6
+ "database",
7
+ "collection",
8
+ "data",
9
+ undefined,
10
+ "mongourl"
11
+ );
12
+
13
+ db.set("foo", "bar");
14
+ };
15
+
16
+ const runMySQL = async () => {
17
+ const { MySQLDatabase } = require("./index");
18
+ const db = new MySQLDatabase();
19
+ await db.connect({
20
+ host: "localhost",
21
+ port: "3306",
22
+ user: "root",
23
+ password: "",
24
+ database: "database",
25
+ charset: "utf8mb4",
26
+ });
27
+
28
+ db.on("connected", async (connection) => {
29
+ console.log("Database Connected");
30
+ });
31
+
32
+ db.set("table", "foo", "bar");
33
+ };
30
34
 
31
35
  //runMySQL()
32
36
 
33
- const runJsonDatabase = async()=>{
34
- const {JsonDatabase} = require('./index')
35
- var db = new JsonDatabase()
36
- db.set('foo','bar')
37
- }
37
+ const runJsonDatabase = async () => {
38
+ const { JsonDatabase } = require("./index");
39
+ var db = new JsonDatabase();
40
+ db.set("foo", "bar");
41
+ };
42
+
43
+ const runSQLite = async () => {
44
+ const { database } = require("../index");
45
+ var db = new database.SQLiteDatabase();
46
+ await db.initialize();
47
+ await db.set("foo", "bar");
48
+ console.log(await db.get("foo"));
49
+ };
50
+
51
+ runSQLite();
@@ -77,6 +77,13 @@ function shuffleArray(array) {
77
77
  }
78
78
  return array;
79
79
  }
80
+ function titleCase(sentence) {
81
+ return sentence
82
+ .toLowerCase()
83
+ .split(" ")
84
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
85
+ .join(" ");
86
+ }
80
87
  module.exports = {
81
88
  makeUniqueId,
82
89
  randomArray,
@@ -88,5 +95,6 @@ module.exports = {
88
95
  uuid,
89
96
  isUUID,
90
97
  },
91
- shuffleArray
98
+ shuffleArray,
99
+ titleCase
92
100
  };
package/index.js CHANGED
@@ -1,7 +1,12 @@
1
- var database = require('./database/index')
2
- var functions = require('./functions/functions')
1
+ var database = require("./database/index");
2
+ var functions = require("./functions/functions");
3
3
 
4
4
  module.exports = {
5
- database,
6
- functions
7
- }
5
+ database: {
6
+ JsonDatabase: database.JsonDatabase,
7
+ MongoDB: database.MongoDB,
8
+ MySQLDatabase: database.MySQLDatabase,
9
+ SQLiteDatabase: database.SQLiteDatabase,
10
+ },
11
+ functions,
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "1.4.1",
3
+ "version": "2.0.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"
@@ -13,7 +13,10 @@
13
13
  "mongodb",
14
14
  "mysql",
15
15
  "db",
16
- "jsondatabase"
16
+ "jsondatabase",
17
+ "sqlitedatabase",
18
+ "sqlite3",
19
+ "quick.db"
17
20
  ],
18
21
  "author": "Onure9e",
19
22
  "license": "ISC",
@@ -23,7 +26,8 @@
23
26
  "lodash": "^4.17.21",
24
27
  "mongodb": "^6.12.0",
25
28
  "path": "^0.12.7",
26
- "promise-mysql": "^5.2.0"
29
+ "promise-mysql": "^5.2.0",
30
+ "sqlite3": "^5.1.7"
27
31
  },
28
32
  "description": ""
29
33
  }
package/readme.md CHANGED
@@ -1,94 +1,135 @@
1
1
  # ZeroHelper
2
+
2
3
  ZeroHelper is a package with database and some functions.
3
4
 
4
5
  ## Installing ZeroHelper
6
+
5
7
  ```bash
6
8
  npm i @onurege3467/zerohelper
7
9
  ```
10
+
8
11
  ## Using ZeroHelper for helper functions
12
+
9
13
  ```js
10
- var {functions} = require('@onurege3467/zerohelper')
11
-
12
- console.log(functions.makeUniqueId()) // returns like this m63ku5dsi45hppk24i
13
- console.log(functions.uid.uuid()) // returns a uuid like 280fbb78-913a-4694-9b7b-f44eb74deb28
14
- console.log(functions.uid.isUUID('some text')) // returns true or false
15
- console.log(functions.randomArray([1,2,3,4,5])) // selects random variable in this array
16
- console.log(functions.randomEmoji()) // returns a random emoji
17
- console.log(functions.randomHex()) // returns a random hex code
18
- console.log(functions.randomNumber()) // returns random number
19
- console.log(functions.randomText()) // returns random text
20
- console.log(functions.shuffleArray([1,2,3,4,5,6,7,8,9,0])) // returns shuffled array like [3,5,2,1,7,6,8,9,0]
14
+ var { functions } = require("@onurege3467/zerohelper");
15
+
16
+ console.log(functions.makeUniqueId()); // returns like this m63ku5dsi45hppk24i
17
+ console.log(functions.uid.uuid()); // returns a uuid like 280fbb78-913a-4694-9b7b-f44eb74deb28
18
+ console.log(functions.uid.isUUID("some text")); // returns true or false
19
+ console.log(functions.randomArray([1, 2, 3, 4, 5])); // selects random variable in this array
20
+ console.log(functions.randomEmoji()); // returns a random emoji
21
+ console.log(functions.randomHex()); // returns a random hex code
22
+ console.log(functions.randomNumber()); // returns random number
23
+ console.log(functions.randomText()); // returns random text
24
+ console.log(functions.shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); // returns shuffled array like [3,5,2,1,7,6,8,9,0]
25
+ console.log(functions.titleCase("HellO tHis iS DEMO teXt")); // returns Hello This Is Demo Text
21
26
  ```
27
+
22
28
  ## Using ZeroHelper As Database
29
+
23
30
  ZeroHelper's database is divided into 3 parts.
31
+
24
32
  ### Firtst Step
33
+
25
34
  ```js
26
- const zerohelper = require('@onurege3467/zerohelper') // if you don't do this the functions may crush
35
+ const zerohelper = require("@onurege3467/zerohelper"); // if you don't do this the functions may crush
27
36
  ```
37
+
28
38
  ### 1. JsonDatabase
39
+
29
40
  ```js
30
- const JsonDatabase = require('@onurege3467/zerohelper/database/jsondatabase')
31
- const db = new JsonDatabase()
41
+ const JsonDatabase = require("@onurege3467/zerohelper/database/jsondatabase");
42
+ const db = new JsonDatabase();
32
43
 
33
- db.set('foo','bar') // sets foo to bar
34
- db.push('array','x') // pushs x to array
35
- db.delete('foo') // deletes foo
44
+ db.set("foo", "bar"); // sets foo to bar
45
+ db.push("array", "x"); // pushs x to array
46
+ db.delete("foo"); // deletes foo
36
47
 
37
- db.add('number',1) // adds 1 to number
38
- db.sub('number',1) // subtracts 1 from number
48
+ db.add("number", 1); // adds 1 to number
49
+ db.sub("number", 1); // subtracts 1 from number
39
50
 
40
- db.get('foo') // gets foo value
41
- db.has('foo') // returns true or false
51
+ db.get("foo"); // gets foo value
52
+ db.has("foo"); // returns true or false
42
53
  ```
54
+
43
55
  ### 2. MongoDB
56
+
44
57
  ```js
45
58
  (async function () {
46
- const MongoDB = require('@onurege3467/zerohelper/database/mongodb')
47
- var db = await MongoDB.createData('database','collection','data',undefined,'mongourl')
48
- db.set('foo','bar') // sets foo to bar
49
- db.push('array','x') // pushs x to array
50
- db.delete('foo') // deletes foo
51
-
52
- db.add('number',1) // adds 1 to number
53
- db.sub('number',1) // subtracts 1 from number
59
+ const MongoDB = require("@onurege3467/zerohelper/database/mongodb");
60
+ var db = await MongoDB.createData(
61
+ "database",
62
+ "collection",
63
+ "data",
64
+ undefined,
65
+ "mongourl"
66
+ );
67
+ db.set("foo", "bar"); // sets foo to bar
68
+ db.push("array", "x"); // pushs x to array
69
+ db.delete("foo"); // deletes foo
70
+
71
+ db.add("number", 1); // adds 1 to number
72
+ db.sub("number", 1); // subtracts 1 from number
73
+
74
+ db.get("foo"); // gets foo value
75
+ db.has("foo"); // returns true or false
76
+
77
+ db.ping(); // returns database ping
78
+ })();
79
+ ```
54
80
 
55
- db.get('foo') // gets foo value
56
- db.has('foo') // returns true or false
81
+ ### 3. MySQL
57
82
 
58
- db.ping() // returns database ping
83
+ ```js
84
+ (async function () {
85
+ const mysql = require("@onurege3467/zerohelper/database/mysql");
86
+
87
+ const db = new mysql();
88
+ await db.connect({
89
+ host: "localhost",
90
+ port: "3306",
91
+ user: "root",
92
+ password: "",
93
+ database: "database",
94
+ charset: "utf8mb4",
95
+ });
96
+
97
+ db.on("connected", async (connection) => {
98
+ console.log("Database Connected");
99
+ });
100
+
101
+ db.set("table", "foo", "bar"); // sets foo to bar
102
+ db.push("table", "array", "x"); // pushs x to array
103
+ db.delete("table", "foo"); // deletes foo
104
+
105
+ db.add("table", "number", 1); // adds 1 to number
106
+ db.sub("table", "number", 1); // subtracts 1 from number
107
+
108
+ db.get("table", "foo"); // gets foo value
109
+ db.has("table", "foo"); // returns true or false
110
+
111
+ db.ping(); // returns database ping
112
+ })();
113
+ ```
59
114
 
60
- })()
115
+ ### 3. SQLiteDB
61
116
 
62
- ```
63
- ### 3. MySQL
64
117
  ```js
65
118
  (async function () {
66
- const mysql = require('@onurege3467/zerohelper/database/mysql')
67
-
68
- const db = new mysql();
69
- await db.connect({
70
- host: 'localhost',
71
- port: '3306',
72
- user: 'root',
73
- password: '',
74
- database: 'database',
75
- charset: 'utf8mb4',
76
- });
77
-
78
- db.on('connected', async connection => {
79
- console.log('Database Connected');
80
- });
81
-
82
- db.set('table','foo','bar') // sets foo to bar
83
- db.push('table','array','x') // pushs x to array
84
- db.delete('table','foo') // deletes foo
85
-
86
- db.add('table','number',1) // adds 1 to number
87
- db.sub('table','number',1) // subtracts 1 from number
88
-
89
- db.get('table','foo') // gets foo value
90
- db.has('table','foo') // returns true or false
91
-
92
- db.ping() // returns database ping
93
- })()
119
+ const SQLDB = require("@onurege3467/zerohelper/database/sqldb");
120
+
121
+ const db = new SQLDB();
122
+
123
+ await db.initialize();
124
+
125
+ db.set("table", "foo", "bar"); // sets foo to bar
126
+ db.push("table", "array", "x"); // pushs x to array
127
+ db.delete("table", "foo"); // deletes foo
128
+
129
+ db.add("table", "number", 1); // adds 1 to number
130
+ db.sub("table", "number", 1); // subtracts 1 from number
131
+
132
+ db.get("table", "foo"); // gets foo value
133
+ db.has("table", "foo"); // returns true or false
134
+ })();
94
135
  ```