@onurege3467/zerohelper 7.1.0 → 8.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/README.md +762 -0
- package/database/IDatabase.js +18 -0
- package/database/cacheWrapper.js +526 -62
- package/database/index.js +9 -2
- package/database/json.js +59 -0
- package/database/migration.js +227 -0
- package/database/mongodb.js +45 -0
- package/database/mysql.js +61 -0
- package/database/pg.js +64 -0
- package/database/redis.js +342 -0
- package/database/sqlite.js +69 -0
- package/functions/index.js +267 -0
- package/package.json +11 -7
- package/.snapshots/config.json +0 -151
- package/.snapshots/readme.md +0 -11
- package/.snapshots/sponsors.md +0 -44
- package/1.json +0 -15
- package/readme.md +0 -418
- package/test.js +0 -261
- package/test_mysqlonly.js +0 -144
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onurege3467/zerohelper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node test.js"
|
|
@@ -18,14 +18,18 @@
|
|
|
18
18
|
"postgresql",
|
|
19
19
|
"utility",
|
|
20
20
|
"functions",
|
|
21
|
-
"nodejs"
|
|
21
|
+
"nodejs",
|
|
22
|
+
"validation",
|
|
23
|
+
"logger",
|
|
24
|
+
"migration",
|
|
25
|
+
"increment",
|
|
26
|
+
"decrement"
|
|
22
27
|
],
|
|
23
28
|
"author": "Onure9e",
|
|
24
29
|
"license": "ISC",
|
|
25
|
-
"description": "ZeroHelper is a versatile JavaScript library offering helper functions
|
|
30
|
+
"description": "ZeroHelper is a versatile JavaScript library offering helper functions, validation, logging, database utilities and migration system for developers. It supports MongoDB, MySQL, SQLite, Redis, and PostgreSQL with increment/decrement operations.",
|
|
26
31
|
"dependencies": {
|
|
27
32
|
"bcrypt": "^5.1.1",
|
|
28
|
-
|
|
29
33
|
"crypto": "^1.0.1",
|
|
30
34
|
"csv": "^6.3.11",
|
|
31
35
|
"dotenv": "^16.4.7",
|
|
@@ -33,13 +37,13 @@
|
|
|
33
37
|
"js-yaml": "^4.1.0",
|
|
34
38
|
"jsonwebtoken": "^9.0.2",
|
|
35
39
|
"lodash": "^4.17.21",
|
|
40
|
+
"lru-cache": "^10.0.0",
|
|
36
41
|
"mongodb": "^6.12.0",
|
|
37
42
|
"mysql2": "^3.14.1",
|
|
38
43
|
"path": "^0.12.7",
|
|
39
44
|
"pg": "^8.14.1",
|
|
40
45
|
"promise-mysql": "^5.2.0",
|
|
41
|
-
"redis": "^
|
|
42
|
-
"sqlite3": "^5.1.7"
|
|
43
|
-
"lru-cache": "^10.0.0"
|
|
46
|
+
"redis": "^5.8.2",
|
|
47
|
+
"sqlite3": "^5.1.7"
|
|
44
48
|
}
|
|
45
49
|
}
|
package/.snapshots/config.json
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"excluded_patterns": [
|
|
3
|
-
".git",
|
|
4
|
-
".gitignore",
|
|
5
|
-
"gradle",
|
|
6
|
-
"gradlew",
|
|
7
|
-
"gradlew.*",
|
|
8
|
-
"node_modules",
|
|
9
|
-
".snapshots",
|
|
10
|
-
".idea",
|
|
11
|
-
".vscode",
|
|
12
|
-
"*.log",
|
|
13
|
-
"*.tmp",
|
|
14
|
-
"target",
|
|
15
|
-
"dist",
|
|
16
|
-
"build",
|
|
17
|
-
".DS_Store",
|
|
18
|
-
"*.bak",
|
|
19
|
-
"*.swp",
|
|
20
|
-
"*.swo",
|
|
21
|
-
"*.lock",
|
|
22
|
-
"*.iml",
|
|
23
|
-
"coverage",
|
|
24
|
-
"*.min.js",
|
|
25
|
-
"*.min.css",
|
|
26
|
-
"__pycache__",
|
|
27
|
-
".marketing",
|
|
28
|
-
".env",
|
|
29
|
-
".env.*",
|
|
30
|
-
"*.jpg",
|
|
31
|
-
"*.jpeg",
|
|
32
|
-
"*.png",
|
|
33
|
-
"*.gif",
|
|
34
|
-
"*.bmp",
|
|
35
|
-
"*.tiff",
|
|
36
|
-
"*.ico",
|
|
37
|
-
"*.svg",
|
|
38
|
-
"*.webp",
|
|
39
|
-
"*.psd",
|
|
40
|
-
"*.ai",
|
|
41
|
-
"*.eps",
|
|
42
|
-
"*.indd",
|
|
43
|
-
"*.raw",
|
|
44
|
-
"*.cr2",
|
|
45
|
-
"*.nef",
|
|
46
|
-
"*.mp4",
|
|
47
|
-
"*.mov",
|
|
48
|
-
"*.avi",
|
|
49
|
-
"*.wmv",
|
|
50
|
-
"*.flv",
|
|
51
|
-
"*.mkv",
|
|
52
|
-
"*.webm",
|
|
53
|
-
"*.m4v",
|
|
54
|
-
"*.wfp",
|
|
55
|
-
"*.prproj",
|
|
56
|
-
"*.aep",
|
|
57
|
-
"*.psb",
|
|
58
|
-
"*.xcf",
|
|
59
|
-
"*.sketch",
|
|
60
|
-
"*.fig",
|
|
61
|
-
"*.xd",
|
|
62
|
-
"*.db",
|
|
63
|
-
"*.sqlite",
|
|
64
|
-
"*.sqlite3",
|
|
65
|
-
"*.mdb",
|
|
66
|
-
"*.accdb",
|
|
67
|
-
"*.frm",
|
|
68
|
-
"*.myd",
|
|
69
|
-
"*.myi",
|
|
70
|
-
"*.ibd",
|
|
71
|
-
"*.dbf",
|
|
72
|
-
"*.rdb",
|
|
73
|
-
"*.aof",
|
|
74
|
-
"*.pdb",
|
|
75
|
-
"*.sdb",
|
|
76
|
-
"*.s3db",
|
|
77
|
-
"*.ddb",
|
|
78
|
-
"*.db-shm",
|
|
79
|
-
"*.db-wal",
|
|
80
|
-
"*.sqlitedb",
|
|
81
|
-
"*.sql.gz",
|
|
82
|
-
"*.bak.sql",
|
|
83
|
-
"dump.sql",
|
|
84
|
-
"dump.rdb",
|
|
85
|
-
"*.vsix",
|
|
86
|
-
"*.jar",
|
|
87
|
-
"*.war",
|
|
88
|
-
"*.ear",
|
|
89
|
-
"*.zip",
|
|
90
|
-
"*.tar",
|
|
91
|
-
"*.tar.gz",
|
|
92
|
-
"*.tgz",
|
|
93
|
-
"*.rar",
|
|
94
|
-
"*.7z",
|
|
95
|
-
"*.exe",
|
|
96
|
-
"*.dll",
|
|
97
|
-
"*.so",
|
|
98
|
-
"*.dylib",
|
|
99
|
-
"*.app",
|
|
100
|
-
"*.dmg",
|
|
101
|
-
"*.iso",
|
|
102
|
-
"*.msi",
|
|
103
|
-
"*.deb",
|
|
104
|
-
"*.rpm",
|
|
105
|
-
"*.apk",
|
|
106
|
-
"*.aab",
|
|
107
|
-
"*.ipa",
|
|
108
|
-
"*.pkg",
|
|
109
|
-
"*.nupkg",
|
|
110
|
-
"*.snap",
|
|
111
|
-
"*.whl",
|
|
112
|
-
"*.gem",
|
|
113
|
-
"*.pyc",
|
|
114
|
-
"*.pyo",
|
|
115
|
-
"*.pyd",
|
|
116
|
-
"*.class",
|
|
117
|
-
"*.o",
|
|
118
|
-
"*.obj",
|
|
119
|
-
"*.lib",
|
|
120
|
-
"*.a",
|
|
121
|
-
"*.map",
|
|
122
|
-
".npmrc"
|
|
123
|
-
],
|
|
124
|
-
"default": {
|
|
125
|
-
"default_prompt": "Enter your prompt here",
|
|
126
|
-
"default_include_all_files": false,
|
|
127
|
-
"default_include_entire_project_structure": true
|
|
128
|
-
},
|
|
129
|
-
"included_patterns": [
|
|
130
|
-
"build.gradle",
|
|
131
|
-
"settings.gradle",
|
|
132
|
-
"gradle.properties",
|
|
133
|
-
"pom.xml",
|
|
134
|
-
"Makefile",
|
|
135
|
-
"CMakeLists.txt",
|
|
136
|
-
"package.json",
|
|
137
|
-
"requirements.txt",
|
|
138
|
-
"Pipfile",
|
|
139
|
-
"Gemfile",
|
|
140
|
-
"composer.json",
|
|
141
|
-
".editorconfig",
|
|
142
|
-
".eslintrc.json",
|
|
143
|
-
".eslintrc.js",
|
|
144
|
-
".prettierrc",
|
|
145
|
-
".babelrc",
|
|
146
|
-
".dockerignore",
|
|
147
|
-
".gitattributes",
|
|
148
|
-
".stylelintrc",
|
|
149
|
-
".npmrc"
|
|
150
|
-
]
|
|
151
|
-
}
|
package/.snapshots/readme.md
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
# Snapshots Directory
|
|
2
|
-
|
|
3
|
-
This directory contains snapshots of your code for AI interactions. Each snapshot is a markdown file that includes relevant code context and project structure information.
|
|
4
|
-
|
|
5
|
-
## What's included in snapshots?
|
|
6
|
-
- Selected code files and their contents
|
|
7
|
-
- Project structure (if enabled)
|
|
8
|
-
- Your prompt/question for the AI
|
|
9
|
-
|
|
10
|
-
## Configuration
|
|
11
|
-
You can customize snapshot behavior in `config.json`.
|
package/.snapshots/sponsors.md
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# Thank you for using Snapshots for AI
|
|
2
|
-
|
|
3
|
-
Thanks for using Snapshots for AI. We hope this tool has helped you solve a problem or two.
|
|
4
|
-
|
|
5
|
-
If you would like to support our work, please help us by considering the following offers and requests:
|
|
6
|
-
|
|
7
|
-
## Ways to Support
|
|
8
|
-
|
|
9
|
-
### Join the GBTI Network!!! 🙏🙏🙏
|
|
10
|
-
The GBTI Network is a community of developers who are passionate about open source and community-driven development. Members enjoy access to exclussive tools, resources, a private MineCraft server, a listing in our members directory, co-op opportunities and more.
|
|
11
|
-
|
|
12
|
-
- Support our work by becoming a [GBTI Network member](https://gbti.network/membership/).
|
|
13
|
-
|
|
14
|
-
### Try out BugHerd 🐛
|
|
15
|
-
BugHerd is a visual feedback and bug-tracking tool designed to streamline website development by enabling users to pin feedback directly onto web pages. This approach facilitates clear communication among clients, designers, developers, and project managers.
|
|
16
|
-
|
|
17
|
-
- Start your free trial with [BugHerd](https://partners.bugherd.com/55z6c8az8rvr) today.
|
|
18
|
-
|
|
19
|
-
### Hire Developers from Codeable 👥
|
|
20
|
-
Codeable connects you with top-tier professionals skilled in frameworks and technologies such as Laravel, React, Django, Node, Vue.js, Angular, Ruby on Rails, and Node.js. Don't let the WordPress focus discourage you. Codeable experts do it all.
|
|
21
|
-
|
|
22
|
-
- Visit [Codeable](https://www.codeable.io/developers/?ref=z8h3e) to hire your next team member.
|
|
23
|
-
|
|
24
|
-
### Lead positive reviews on our marketplace listing ⭐⭐⭐⭐⭐
|
|
25
|
-
- Rate us on [VSCode marketplace](https://marketplace.visualstudio.com/items?itemName=GBTI.snapshots-for-ai)
|
|
26
|
-
- Review us on [Cursor marketplace](https://open-vsx.org/extension/GBTI/snapshots-for-ai)
|
|
27
|
-
|
|
28
|
-
### Star Our GitHub Repository ⭐
|
|
29
|
-
- Star and watch our [repository](https://github.com/gbti-network/vscode-snapshots-for-ai)
|
|
30
|
-
|
|
31
|
-
### 📡 Stay Connected
|
|
32
|
-
Follow us on your favorite platforms for updates, news, and community discussions:
|
|
33
|
-
- **[Twitter/X](https://twitter.com/gbti_network)**
|
|
34
|
-
- **[GitHub](https://github.com/gbti-network)**
|
|
35
|
-
- **[YouTube](https://www.youtube.com/channel/UCh4FjB6r4oWQW-QFiwqv-UA)**
|
|
36
|
-
- **[Dev.to](https://dev.to/gbti)**
|
|
37
|
-
- **[Daily.dev](https://dly.to/zfCriM6JfRF)**
|
|
38
|
-
- **[Hashnode](https://gbti.hashnode.dev/)**
|
|
39
|
-
- **[Discord Community](https://gbti.network)**
|
|
40
|
-
- **[Reddit Community](https://www.reddit.com/r/GBTI_network)**
|
|
41
|
-
|
|
42
|
-
---
|
|
43
|
-
|
|
44
|
-
Thank you for supporting open source software! 🙏
|
package/1.json
DELETED
package/readme.md
DELETED
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
# ZeroHelper 🚀
|
|
2
|
-
|
|
3
|
-
ZeroHelper is a versatile JavaScript package providing helper functions and database utilities for developers. It includes essential tools for manipulating data, generating random values, performing cryptographic operations, and interacting with various databases like MySQL, MongoDB, PostgreSQL, SQLite, and Redis.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 📑 Table of Contents
|
|
8
|
-
|
|
9
|
-
1. [📦 Installation](#-installation)
|
|
10
|
-
2. [✨ Helper Functions](#-helper-functions)
|
|
11
|
-
- [🎲 Random Functions](#random-functions-)
|
|
12
|
-
- [🔠 String Functions](#string-functions-)
|
|
13
|
-
- [📊 Array Functions](#array-functions-)
|
|
14
|
-
- [🔧 Object Functions](#object-functions-)
|
|
15
|
-
- [🔒 Crypto Functions](#crypto-functions-)
|
|
16
|
-
- [➗ Math Functions](#math-functions-)
|
|
17
|
-
3. [💾 Database Utilities](#-database-utilities)
|
|
18
|
-
- [🏭 Using the Database Factory](#-using-the-database-factory)
|
|
19
|
-
- [📚 API Reference](#-api-reference)
|
|
20
|
-
4. [🔄 Database Migration](#database-migration)
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## 🚀 Installing ZeroHelper
|
|
25
|
-
|
|
26
|
-
To install ZeroHelper, use npm:
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npm i @onurege3467/zerohelper
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## 🛠️ Using ZeroHelper for helper functions
|
|
33
|
-
|
|
34
|
-
# Random Functions 🎲
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
const helpers = require("@onurege3467/zerohelper/functions");
|
|
38
|
-
|
|
39
|
-
const id = helpers.random.makeUniqueId();
|
|
40
|
-
console.log(id); // Example: "lzx8k9x8k9"
|
|
41
|
-
|
|
42
|
-
const item = helpers.random.randomArray([1, 2, 3, 4, 5]);
|
|
43
|
-
console.log(item); // Example: 3
|
|
44
|
-
|
|
45
|
-
const text = helpers.random.randomText(10);
|
|
46
|
-
console.log(text); // Example: "aBcDeFgHiJ"
|
|
47
|
-
|
|
48
|
-
const number = helpers.random.randomNumber(1, 100);
|
|
49
|
-
console.log(number); // Example: 42
|
|
50
|
-
|
|
51
|
-
const emoji = helpers.random.randomEmoji();
|
|
52
|
-
console.log(emoji); // Example: "😄"
|
|
53
|
-
|
|
54
|
-
const hex = helpers.random.randomHex();
|
|
55
|
-
console.log(hex); // Example: "#A1B2C3"
|
|
56
|
-
|
|
57
|
-
const float = helpers.random.randomFloat(1.5, 5.5);
|
|
58
|
-
console.log(float); // Example: 3.14
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
# String Functions 🔠
|
|
62
|
-
|
|
63
|
-
```js
|
|
64
|
-
const title = helpers.string.titleCase("hello world");
|
|
65
|
-
console.log(title); // "Hello World"
|
|
66
|
-
|
|
67
|
-
const randomString = helpers.string.generateRandomString(8);
|
|
68
|
-
console.log(randomString); // Example: "AbCdEfGh"
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
# Array Functions 📊
|
|
72
|
-
|
|
73
|
-
```js
|
|
74
|
-
const shuffled = helpers.array.shuffleArray([1, 2, 3, 4, 5]);
|
|
75
|
-
console.log(shuffled); // Example: [3, 1, 5, 4, 2]
|
|
76
|
-
|
|
77
|
-
const flat = helpers.array.flattenArray([1, [2, [3, 4]], 5]);
|
|
78
|
-
console.log(flat); // [1, 2, 3, 4, 5]
|
|
79
|
-
|
|
80
|
-
const filtered = helpers.array.removeFalsyValues([0, 1, false, 2, "", 3]);
|
|
81
|
-
console.log(filtered); // [1, 2, 3]
|
|
82
|
-
|
|
83
|
-
const grouped = helpers.array.groupBy(
|
|
84
|
-
[
|
|
85
|
-
{ category: "fruit", name: "apple" },
|
|
86
|
-
{ category: "fruit", name: "banana" },
|
|
87
|
-
{ category: "vegetable", name: "carrot" },
|
|
88
|
-
],
|
|
89
|
-
"category"
|
|
90
|
-
);
|
|
91
|
-
console.log(grouped);
|
|
92
|
-
// {
|
|
93
|
-
// fruit: [{ category: "fruit", name: "apple" }, { category: "fruit", name: "banana" }],
|
|
94
|
-
// vegetable: [{ category: "vegetable", name: "carrot" }]
|
|
95
|
-
// }
|
|
96
|
-
|
|
97
|
-
const names = helpers.array.pluck(
|
|
98
|
-
[{ name: "Alice" }, { name: "Bob" }, { name: "Charlie" }],
|
|
99
|
-
"name"
|
|
100
|
-
);
|
|
101
|
-
console.log(names); // ["Alice", "Bob", "Charlie"]
|
|
102
|
-
|
|
103
|
-
const sorted = helpers.array.sortBy(
|
|
104
|
-
[{ age: 30 }, { age: 20 }, { age: 40 }],
|
|
105
|
-
"age"
|
|
106
|
-
);
|
|
107
|
-
console.log(sorted); // [{ age: 20 }, { age: 30 }, { age: 40 }]
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
# Object Functions 🔧
|
|
111
|
-
|
|
112
|
-
```js
|
|
113
|
-
const filtered = helpers.object.filterObjectByKey(
|
|
114
|
-
{ name: "Alice", age: 25, city: "New York" },
|
|
115
|
-
["name", "city"]
|
|
116
|
-
);
|
|
117
|
-
console.log(filtered); // { name: "Alice", city: "New York" }
|
|
118
|
-
|
|
119
|
-
const merged = helpers.object.deepMerge(
|
|
120
|
-
{ a: 1, b: { c: 2 } },
|
|
121
|
-
{ b: { d: 3 }, e: 4 }
|
|
122
|
-
);
|
|
123
|
-
console.log(merged); // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
# Crypto Functions 🔒
|
|
127
|
-
|
|
128
|
-
```js
|
|
129
|
-
const secret = "mySecretKey";
|
|
130
|
-
const encrypted = helpers.crypto.encryptText("Hello, World!", secret);
|
|
131
|
-
console.log(encrypted); // Encrypted text
|
|
132
|
-
|
|
133
|
-
const decrypted = helpers.crypto.decryptText(encrypted, secret);
|
|
134
|
-
console.log(decrypted); // "Hello, World!"
|
|
135
|
-
|
|
136
|
-
const hash = helpers.crypto.hashPassword("myPassword");
|
|
137
|
-
console.log(hash); // Hashed password
|
|
138
|
-
|
|
139
|
-
const isValid = helpers.crypto.verifyPassword("myPassword", hash);
|
|
140
|
-
console.log(isValid); // true or false
|
|
141
|
-
|
|
142
|
-
const token = helpers.crypto.generateJWT({ userId: 1 }, "mySecret");
|
|
143
|
-
console.log(token); // JWT
|
|
144
|
-
|
|
145
|
-
const payload = helpers.crypto.verifyJWT(token, "mySecret");
|
|
146
|
-
console.log(payload); // { userId: 1, iat: ..., exp: ... }
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
# Math Functions ➗
|
|
150
|
-
|
|
151
|
-
```js
|
|
152
|
-
const avg = helpers.math.mean([1, 2, 3, 4, 5]);
|
|
153
|
-
console.log(avg); // 3
|
|
154
|
-
|
|
155
|
-
const prime = helpers.math.isPrime(7);
|
|
156
|
-
console.log(prime); // true
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
## 💾 Using ZeroHelper as Database
|
|
160
|
-
|
|
161
|
-
ZeroHelper provides multiple database utilities for seamless integration with various databases.
|
|
162
|
-
|
|
163
|
-
## 💾 Using ZeroHelper as Database
|
|
164
|
-
|
|
165
|
-
ZeroHelper provides a flexible database factory for seamless integration with various databases.
|
|
166
|
-
|
|
167
|
-
### 🏭 Using the Database Factory
|
|
168
|
-
|
|
169
|
-
ZeroHelper's database factory allows you to create database instances with various adapters. You can also enable and configure an LRU (Least Recently Used) cache for read operations to improve performance.
|
|
170
|
-
|
|
171
|
-
#### Enabling and Configuring Cache
|
|
172
|
-
|
|
173
|
-
To enable caching, pass a `cache` object within the `config` object when creating a database instance. The `cache` object can have the following properties:
|
|
174
|
-
|
|
175
|
-
- `max`: (Number, optional) The maximum number of items to store in the cache. Defaults to `500`.
|
|
176
|
-
- `ttl`: (Number, optional) The time to live for cache entries in milliseconds. Defaults to `300000` (5 minutes).
|
|
177
|
-
- `updateAgeOnGet`: (Boolean, optional) If `true`, accessing an item refreshes its `ttl`. Defaults to `false`.
|
|
178
|
-
|
|
179
|
-
**Example with Cache Enabled:**
|
|
180
|
-
|
|
181
|
-
```javascript
|
|
182
|
-
const path = require('path');
|
|
183
|
-
const createDatabase = require('@onurege3467/zerohelper/database');
|
|
184
|
-
|
|
185
|
-
const cachedSqliteDb = createDatabase({
|
|
186
|
-
adapter: 'sqlite',
|
|
187
|
-
config: {
|
|
188
|
-
filePath: path.join(__dirname, 'data', 'cached_development.sqlite'),
|
|
189
|
-
cache: {
|
|
190
|
-
max: 1000, // Cache up to 1000 items
|
|
191
|
-
ttl: 1000 * 60 * 10, // Cache items for 10 minutes
|
|
192
|
-
updateAgeOnGet: true // Refresh TTL on access
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
// Subsequent `select` or `selectOne` calls for the same query will hit the cache
|
|
198
|
-
// Write operations (insert, update, delete, set, bulkInsert) will automatically invalidate the cache for the affected table.
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
**Basic Usage (without cache configuration shown):**
|
|
202
|
-
|
|
203
|
-
```javascript
|
|
204
|
-
const path = require('path');
|
|
205
|
-
const createDatabase = require('@onurege3467/zerohelper/database'); // Adjust the path to your project structure
|
|
206
|
-
|
|
207
|
-
// --- 1. Using SQLite (Fast and Lightweight) ---
|
|
208
|
-
const sqliteDb = createDatabase({
|
|
209
|
-
adapter: 'sqlite',
|
|
210
|
-
config: {
|
|
211
|
-
filePath: path.join(__dirname, 'data', 'development.sqlite')
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// --- 2. Using MySQL (For Production) ---
|
|
216
|
-
const mysqlDb = createDatabase({
|
|
217
|
-
adapter: 'mysql',
|
|
218
|
-
config: {
|
|
219
|
-
host: 'localhost',
|
|
220
|
-
user: 'root',
|
|
221
|
-
password: 'your_password',
|
|
222
|
-
database: 'my_app_prod'
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
// --- 3. Using a JSON File (For Simple Prototyping) ---
|
|
227
|
-
const jsonDb = createDatabase({
|
|
228
|
-
adapter: 'json',
|
|
229
|
-
config: {
|
|
230
|
-
filePath: path.join(__dirname, 'data', 'db.json')
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
// --- 4. Using MongoDB ---
|
|
235
|
-
const mongoDb = createDatabase({
|
|
236
|
-
adapter: 'mongodb',
|
|
237
|
-
config: {
|
|
238
|
-
url: 'mongodb://localhost:27017',
|
|
239
|
-
database: 'my_app_mongo'
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// --- 5. Using PG ---
|
|
244
|
-
const mongoDb = createDatabase({
|
|
245
|
-
adapter: 'postgres',
|
|
246
|
-
config: {
|
|
247
|
-
host: 'localhost',
|
|
248
|
-
user: 'root',
|
|
249
|
-
password: 'your_password',
|
|
250
|
-
database: 'my_app_prod'
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
### Basic Operations (CRUD)
|
|
256
|
-
|
|
257
|
-
No matter which adapter you choose, you can use the following methods in exactly the same way.
|
|
258
|
-
|
|
259
|
-
```javascript
|
|
260
|
-
async function main() {
|
|
261
|
-
// Let's use sqliteDb for this example
|
|
262
|
-
const db = createDatabase({
|
|
263
|
-
adapter: 'sqlite',
|
|
264
|
-
config: { filePath: 'app.db' }
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
try {
|
|
268
|
-
// Insert Data
|
|
269
|
-
const newUserId = await db.insert('users', { name: 'John Doe', email: 'john@example.com', age: 30 });
|
|
270
|
-
console.log(`New user added with ID: ${newUserId}`);
|
|
271
|
-
|
|
272
|
-
// Select Data
|
|
273
|
-
const user = await db.selectOne('users', { name: 'John Doe' });
|
|
274
|
-
console.log('Found user:', user);
|
|
275
|
-
|
|
276
|
-
// Update Data
|
|
277
|
-
await db.update('users', { age: 31 }, { email: 'john@example.com' });
|
|
278
|
-
console.log('User updated.');
|
|
279
|
-
|
|
280
|
-
// Update if exists, or insert if not (Upsert)
|
|
281
|
-
await db.set('users', { city: 'New York' }, { name: 'John Doe' }); // Updates existing
|
|
282
|
-
await db.set('users', { name: 'Jane Doe', email: 'jane@example.com' }, { name: 'Jane Doe' }); // Inserts new
|
|
283
|
-
|
|
284
|
-
// Select All Data
|
|
285
|
-
const allUsers = await db.select('users');
|
|
286
|
-
console.log('All users:', allUsers);
|
|
287
|
-
|
|
288
|
-
// Delete Data
|
|
289
|
-
await db.delete('users', { name: 'John Doe' });
|
|
290
|
-
console.log('User deleted.');
|
|
291
|
-
|
|
292
|
-
} catch (error) {
|
|
293
|
-
console.error('An error occurred:', error);
|
|
294
|
-
} finally {
|
|
295
|
-
// Close the connection
|
|
296
|
-
await db.close();
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
main();
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
### 📚 API Reference
|
|
304
|
-
|
|
305
|
-
All adapters implement the following common methods (the `IDatabase` interface):
|
|
306
|
-
|
|
307
|
-
- `select(table, where)`: Returns an array of all rows matching the query.
|
|
308
|
-
- `selectOne(table, where)`: Returns the first row matching the query as an object, or `null`.
|
|
309
|
-
- `insert(table, data)`: Inserts a new row and returns its ID.
|
|
310
|
-
- `update(table, data, where)`: Updates rows matching the query and returns the number of affected rows.
|
|
311
|
-
- `delete(table, where)`: Deletes rows matching the query and returns the number of affected rows.
|
|
312
|
-
- `set(table, data, where)`: Updates a record if it exists, otherwise inserts it as a new record.
|
|
313
|
-
- `bulkInsert(table, dataArray)`: Inserts an array of objects in a single, optimized operation.
|
|
314
|
-
- `close()`: Safely closes the database connection or file handle.
|
|
315
|
-
|
|
316
|
-
## Database Migration
|
|
317
|
-
|
|
318
|
-
```javascript
|
|
319
|
-
const path = require('path');
|
|
320
|
-
const createDatabase = require('@onurege3467/zerohelper/database'); // Adjust the path to your project structure
|
|
321
|
-
|
|
322
|
-
// --- 1. Using SQLite (Fast and Lightweight) ---
|
|
323
|
-
const sqliteDb = createDatabase({
|
|
324
|
-
adapter: 'sqlite',
|
|
325
|
-
config: {
|
|
326
|
-
filePath: path.join(__dirname, 'data', 'development.sqlite')
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
// --- 2. Using MySQL (For Production) ---
|
|
331
|
-
const mysqlDb = createDatabase({
|
|
332
|
-
adapter: 'mysql',
|
|
333
|
-
config: {
|
|
334
|
-
host: 'localhost',
|
|
335
|
-
user: 'root',
|
|
336
|
-
password: 'your_password',
|
|
337
|
-
database: 'my_app_prod'
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
// --- 3. Using a JSON File (For Simple Prototyping) ---
|
|
342
|
-
const jsonDb = createDatabase({
|
|
343
|
-
adapter: 'json',
|
|
344
|
-
config: {
|
|
345
|
-
filePath: path.join(__dirname, 'data', 'db.json')
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
// --- 4. Using MongoDB ---
|
|
350
|
-
const mongoDb = createDatabase({
|
|
351
|
-
adapter: 'mongodb',
|
|
352
|
-
config: {
|
|
353
|
-
url: 'mongodb://localhost:27017',
|
|
354
|
-
database: 'my_app_mongo'
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
### Basic Operations (CRUD)
|
|
360
|
-
|
|
361
|
-
No matter which adapter you choose, you can use the following methods in exactly the same way.
|
|
362
|
-
|
|
363
|
-
```javascript
|
|
364
|
-
async function main() {
|
|
365
|
-
// Let's use sqliteDb for this example
|
|
366
|
-
const db = createDatabase({
|
|
367
|
-
adapter: 'sqlite',
|
|
368
|
-
config: { filePath: 'app.db' }
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
try {
|
|
372
|
-
// Insert Data
|
|
373
|
-
const newUserId = await db.insert('users', { name: 'John Doe', email: 'john@example.com', age: 30 });
|
|
374
|
-
console.log(`New user added with ID: ${newUserId}`);
|
|
375
|
-
|
|
376
|
-
// Select Data
|
|
377
|
-
const user = await db.selectOne('users', { name: 'John Doe' });
|
|
378
|
-
console.log('Found user:', user);
|
|
379
|
-
|
|
380
|
-
// Update Data
|
|
381
|
-
await db.update('users', { age: 31 }, { email: 'john@example.com' });
|
|
382
|
-
console.log('User updated.');
|
|
383
|
-
|
|
384
|
-
// Update if exists, or insert if not (Upsert)
|
|
385
|
-
await db.set('users', { city: 'New York' }, { name: 'John Doe' }); // Updates existing
|
|
386
|
-
await db.set('users', { name: 'Jane Doe', email: 'jane@example.com' }, { name: 'Jane Doe' }); // Inserts new
|
|
387
|
-
|
|
388
|
-
// Select All Data
|
|
389
|
-
const allUsers = await db.select('users');
|
|
390
|
-
console.log('All users:', allUsers);
|
|
391
|
-
|
|
392
|
-
// Delete Data
|
|
393
|
-
await db.delete('users', { name: 'John Doe' });
|
|
394
|
-
console.log('User deleted.');
|
|
395
|
-
|
|
396
|
-
} catch (error) {
|
|
397
|
-
console.error('An error occurred:', error);
|
|
398
|
-
} finally {
|
|
399
|
-
// Close the connection
|
|
400
|
-
await db.close();
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
main();
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
### 📚 API Reference
|
|
408
|
-
|
|
409
|
-
All adapters implement the following common methods (the `IDatabase` interface):
|
|
410
|
-
|
|
411
|
-
- `select(table, where)`: Returns an array of all rows matching the query.
|
|
412
|
-
- `selectOne(table, where)`: Returns the first row matching the query as an object, or `null`.
|
|
413
|
-
- `insert(table, data)`: Inserts a new row and returns its ID.
|
|
414
|
-
- `update(table, data, where)`: Updates rows matching the query and returns the number of affected rows.
|
|
415
|
-
- `delete(table, where)`: Deletes rows matching the query and returns the number of affected rows.
|
|
416
|
-
- `set(table, data, where)`: Updates a record if it exists, otherwise inserts it as a new record.
|
|
417
|
-
- `bulkInsert(table, dataArray)`: Inserts an array of objects in a single, optimized operation.
|
|
418
|
-
- `close()`: Safely closes the database connection or file handle.
|