@onurege3467/zerohelper 6.1.0 → 7.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/.snapshots/config.json +151 -0
- package/.snapshots/readme.md +11 -0
- package/.snapshots/sponsors.md +44 -0
- package/data/test_db.json +3 -0
- package/data/test_db.sqlite +0 -0
- package/data/test_db_cached.sqlite +0 -0
- package/database/index.js +2 -0
- package/database/mysql.js +140 -15
- package/database/pg.js +473 -0
- package/database/sqlite.js +111 -7
- package/package.json +1 -1
- package/readme.md +11 -0
- package/test copy.js +144 -0
- package/test.js +14 -107
package/test copy.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const createDatabase = require('./database');
|
|
3
|
+
const helpers = require('./functions');
|
|
4
|
+
const { setInterval } = require('timers');
|
|
5
|
+
|
|
6
|
+
const mysqlConfig = {
|
|
7
|
+
adapter: 'mysql',
|
|
8
|
+
config: {
|
|
9
|
+
host: process.env.MYSQL_HOST || 'localhost',
|
|
10
|
+
user: process.env.MYSQL_USER || 'root',
|
|
11
|
+
password: process.env.MYSQL_PASSWORD || '',
|
|
12
|
+
database: process.env.MYSQL_DATABASE || 'test_db_zerohelper',
|
|
13
|
+
cache: {
|
|
14
|
+
max: 100,
|
|
15
|
+
ttl: 1000 * 10,
|
|
16
|
+
updateAgeOnGet: true,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
// Absurd configurations for testing edge cases
|
|
21
|
+
async function runDatabaseTests() {
|
|
22
|
+
console.log('Starting database tests...');
|
|
23
|
+
|
|
24
|
+
const databases = [
|
|
25
|
+
{ name: 'MySQL', config: mysqlConfig },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
for (const dbInfo of databases) {
|
|
29
|
+
const { name, config } = dbInfo;
|
|
30
|
+
console.log(`\n--- Testing ${name} Database ---`);
|
|
31
|
+
let db;
|
|
32
|
+
try {
|
|
33
|
+
db = createDatabase(config);
|
|
34
|
+
console.log(`${name} database connected.`);
|
|
35
|
+
|
|
36
|
+
// Test Insert
|
|
37
|
+
const insertResult = await db.insert('users', { name: 'Test User', email: `test_${name.toLowerCase()}@example.com`, int_age: 30, bool_active: true,city: 'Test City' });
|
|
38
|
+
console.log(`${name} Insert:`, insertResult);
|
|
39
|
+
const selectResult = await db.selectOne('users', { _id: insertResult});
|
|
40
|
+
console.log(`${name} Select One:`, selectResult);
|
|
41
|
+
await db.set('users', {city:'Amkara'}, {_id: insertResult });
|
|
42
|
+
|
|
43
|
+
setInterval(async () => {
|
|
44
|
+
const updatedSelectResult = await db.selectOne('users', { _id: insertResult });
|
|
45
|
+
console.log(`${name} Select One after update:`, updatedSelectResult);
|
|
46
|
+
}, 1000);
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(`Error testing ${name} database:`, error);
|
|
52
|
+
} finally {
|
|
53
|
+
if (db) {
|
|
54
|
+
try {
|
|
55
|
+
} catch (closeError) {
|
|
56
|
+
console.error(`Error closing ${name} database connection:`, closeError);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
console.log('\nAll database tests completed.');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function runHelperFunctionTests() {
|
|
66
|
+
console.log('\nStarting helper function tests...');
|
|
67
|
+
|
|
68
|
+
// Random Functions
|
|
69
|
+
console.log('\n--- Testing Random Functions ---');
|
|
70
|
+
console.log('makeUniqueId:', helpers.random.makeUniqueId());
|
|
71
|
+
console.log('randomArray:', helpers.random.randomArray([1, 2, 3]));
|
|
72
|
+
console.log('randomText (5 chars):', helpers.random.randomText(5));
|
|
73
|
+
console.log('randomNumber (1-10):', helpers.random.randomNumber(1, 10));
|
|
74
|
+
console.log('randomEmoji:', helpers.random.randomEmoji());
|
|
75
|
+
console.log('randomHex:', helpers.random.randomHex());
|
|
76
|
+
console.log('randomFloat (1.0-2.0):', helpers.random.randomFloat(1.0, 2.0));
|
|
77
|
+
|
|
78
|
+
// String Functions
|
|
79
|
+
console.log('\n--- Testing String Functions ---');
|
|
80
|
+
console.log('titleCase:', helpers.string.titleCase('hello world'));
|
|
81
|
+
console.log('generateRandomString (8 chars):', helpers.string.generateRandomString(8));
|
|
82
|
+
|
|
83
|
+
// Array Functions
|
|
84
|
+
console.log('\n--- Testing Array Functions ---');
|
|
85
|
+
console.log('shuffleArray:', helpers.array.shuffleArray([1, 2, 3, 4, 5]));
|
|
86
|
+
console.log('flattenArray:', helpers.array.flattenArray([1, [2, [3]], 4]));
|
|
87
|
+
console.log('removeFalsyValues:', helpers.array.removeFalsyValues([0, 1, false, '', null, undefined, 2]));
|
|
88
|
+
const groupByData = [
|
|
89
|
+
{ type: 'A', value: 1 },
|
|
90
|
+
{ type: 'B', value: 2 },
|
|
91
|
+
{ type: 'A', value: 3 },
|
|
92
|
+
];
|
|
93
|
+
console.log('groupBy:', helpers.array.groupBy(groupByData, 'type'));
|
|
94
|
+
const pluckData = [
|
|
95
|
+
{ name: 'Alice', age: 30 },
|
|
96
|
+
{ name: 'Bob', age: 24 },
|
|
97
|
+
];
|
|
98
|
+
console.log('pluck (name):', helpers.array.pluck(pluckData, 'name'));
|
|
99
|
+
const sortByData = [
|
|
100
|
+
{ age: 30 },
|
|
101
|
+
{ age: 20 },
|
|
102
|
+
{ age: 40 },
|
|
103
|
+
];
|
|
104
|
+
console.log('sortBy (age):', helpers.array.sortBy(sortByData, 'age'));
|
|
105
|
+
|
|
106
|
+
// Object Functions
|
|
107
|
+
console.log('\n--- Testing Object Functions ---');
|
|
108
|
+
const filterObjectData = { a: 1, b: 2, c: 3 };
|
|
109
|
+
console.log('filterObjectByKey:', helpers.object.filterObjectByKey(filterObjectData, ['a', 'c']));
|
|
110
|
+
const deepMergeData1 = { a: 1, b: { c: 2 } };
|
|
111
|
+
const deepMergeData2 = { b: { d: 3 }, e: 4 };
|
|
112
|
+
console.log('deepMerge:', helpers.object.deepMerge(deepMergeData1, deepMergeData2));
|
|
113
|
+
|
|
114
|
+
// Crypto Functions
|
|
115
|
+
console.log('\n--- Testing Crypto Functions ---');
|
|
116
|
+
const secret = 'mySuperSecretKey';
|
|
117
|
+
const textToEncrypt = 'Hello Crypto!';
|
|
118
|
+
const encryptedResult = helpers.crypto.encryptText(textToEncrypt, secret);
|
|
119
|
+
const encryptedText = encryptedResult.encryptedText;
|
|
120
|
+
const iv = encryptedResult.iv;
|
|
121
|
+
console.log('encryptText:', encryptedText);
|
|
122
|
+
console.log('decryptText:', helpers.crypto.decryptText(encryptedText, secret, iv));
|
|
123
|
+
const password = 'mySecurePassword';
|
|
124
|
+
const hashedPassword = helpers.crypto.hashPassword(password);
|
|
125
|
+
console.log('hashPassword:', hashedPassword);
|
|
126
|
+
console.log('verifyPassword (true):', helpers.crypto.verifyPassword(password, hashedPassword));
|
|
127
|
+
console.log('verifyPassword (false):', helpers.crypto.verifyPassword('wrongPassword', hashedPassword));
|
|
128
|
+
const jwtPayload = { userId: 123, username: 'testuser' };
|
|
129
|
+
const jwtToken = helpers.crypto.generateJWT(jwtPayload, secret);
|
|
130
|
+
console.log('generateJWT:', jwtToken);
|
|
131
|
+
console.log('verifyJWT:', helpers.crypto.verifyJWT(jwtToken, secret));
|
|
132
|
+
|
|
133
|
+
// Math Functions
|
|
134
|
+
console.log('\n--- Testing Math Functions ---');
|
|
135
|
+
console.log('mean:', helpers.math.mean([1, 2, 3, 4, 5]));
|
|
136
|
+
console.log('isPrime (7):', helpers.math.isPrime(7));
|
|
137
|
+
console.log('isPrime (10):', helpers.math.isPrime(10));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function main() {
|
|
141
|
+
await runDatabaseTests();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
main();
|
package/test.js
CHANGED
|
@@ -20,7 +20,7 @@ const mysqlConfig = {
|
|
|
20
20
|
const mongoConfig = {
|
|
21
21
|
adapter: 'mongodb',
|
|
22
22
|
config: {
|
|
23
|
-
url: process.env.MONGODB_URL || '
|
|
23
|
+
url: process.env.MONGODB_URL || 'MONGODB_URL',
|
|
24
24
|
database: process.env.MONGODB_DATABASE || 'test_mongo_db_zerohelper',
|
|
25
25
|
cache: {
|
|
26
26
|
max: 100,
|
|
@@ -56,61 +56,29 @@ const cachedSqliteConfig = {
|
|
|
56
56
|
},
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
adapter: 'mysql',
|
|
62
|
-
config: {
|
|
63
|
-
host: process.env.MYSQL_HOST || 'localhost',
|
|
64
|
-
user: process.env.MYSQL_USER || 'root',
|
|
65
|
-
password: process.env.MYSQL_PASSWORD || '',
|
|
66
|
-
database: 'very_long_and_absurd_database_name_for_testing_purposes_1234567890_abcdefghijklmnopqrstuvwxyz',
|
|
67
|
-
cache: {
|
|
68
|
-
max: 10,
|
|
69
|
-
ttl: 1000 * 5,
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const mongoAbsurdConfig = {
|
|
75
|
-
adapter: 'mongodb',
|
|
76
|
-
config: {
|
|
77
|
-
url: process.env.MONGODB_URL || 'mongodb://localhost:27017/test',
|
|
78
|
-
database: 'very_long_and_absurd_mongodb_database_name_for_testing_purposes_1234567890_abcdefghijklmnopqrstuvwxyz',
|
|
79
|
-
cache: {
|
|
80
|
-
max: 10,
|
|
81
|
-
ttl: 1000 * 5,
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const jsonAbsurdConfig = {
|
|
87
|
-
adapter: 'json',
|
|
88
|
-
config: {
|
|
89
|
-
filePath: path.join(__dirname, 'data', 'very_long_and_absurd_json_file_path_for_testing_purposes_1234567890_abcdefghijklmnopqrstuvwxyz.json'),
|
|
90
|
-
cache: {
|
|
91
|
-
max: 10,
|
|
92
|
-
ttl: 1000 * 5,
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const sqliteAbsurdConfig = {
|
|
98
|
-
adapter: 'sqlite',
|
|
59
|
+
const pgConfig = {
|
|
60
|
+
adapter: 'postgres',
|
|
99
61
|
config: {
|
|
100
|
-
|
|
62
|
+
host: process.env.PG_HOST || 'localhost',
|
|
63
|
+
user: process.env.PG_USER || 'postgres',
|
|
64
|
+
password: process.env.PG_PASSWORD || '',
|
|
65
|
+
database: process.env.PG_DATABASE || 'test_db_zerohelper_pg',
|
|
101
66
|
cache: {
|
|
102
|
-
max:
|
|
103
|
-
ttl: 1000 *
|
|
67
|
+
max: 100,
|
|
68
|
+
ttl: 1000 * 10,
|
|
69
|
+
updateAgeOnGet: true,
|
|
104
70
|
},
|
|
105
71
|
},
|
|
106
72
|
};
|
|
107
73
|
|
|
74
|
+
// Absurd configurations for testing edge cases
|
|
108
75
|
async function runDatabaseTests() {
|
|
109
76
|
console.log('Starting database tests...');
|
|
110
77
|
|
|
111
78
|
const databases = [
|
|
112
79
|
{ name: 'MySQL', config: mysqlConfig },
|
|
113
80
|
{ name: 'MongoDB', config: mongoConfig },
|
|
81
|
+
{ name: 'PostgreSQL', config: pgConfig },
|
|
114
82
|
{ name: 'JSON', config: jsonConfig },
|
|
115
83
|
{ name: 'SQLite', config: sqliteConfig },
|
|
116
84
|
];
|
|
@@ -124,7 +92,7 @@ async function runDatabaseTests() {
|
|
|
124
92
|
console.log(`${name} database connected.`);
|
|
125
93
|
|
|
126
94
|
// Test Insert
|
|
127
|
-
const insertResult = await db.insert('users', { name: 'Test User', email: `test_${name.toLowerCase()}@example.com`,
|
|
95
|
+
const insertResult = await db.insert('users', { name: 'Test User', email: `test_${name.toLowerCase()}@example.com`, int_age: 30, bool_active: true });
|
|
128
96
|
console.log(`${name} Insert:`, insertResult);
|
|
129
97
|
|
|
130
98
|
// Test SelectOne
|
|
@@ -133,7 +101,7 @@ async function runDatabaseTests() {
|
|
|
133
101
|
|
|
134
102
|
// Test Update
|
|
135
103
|
if (user) {
|
|
136
|
-
const updateResult = await db.update('users', {
|
|
104
|
+
const updateResult = await db.update('users', { int_age: 31 }, { name: 'Test User' });
|
|
137
105
|
console.log(`${name} Update:`, updateResult);
|
|
138
106
|
}
|
|
139
107
|
|
|
@@ -207,66 +175,6 @@ async function runDatabaseTests() {
|
|
|
207
175
|
}
|
|
208
176
|
}
|
|
209
177
|
|
|
210
|
-
// --- Testing Absurd Database Names/Tables/Columns ---
|
|
211
|
-
console.log('\n--- Testing Absurd Database Names/Tables/Columns ---');
|
|
212
|
-
const absurdDatabases = [
|
|
213
|
-
{ name: 'MySQL Absurd', config: mysqlAbsurdConfig },
|
|
214
|
-
{ name: 'MongoDB Absurd', config: mongoAbsurdConfig },
|
|
215
|
-
{ name: 'JSON Absurd', config: jsonAbsurdConfig },
|
|
216
|
-
{ name: 'SQLite Absurd', config: sqliteAbsurdConfig },
|
|
217
|
-
];
|
|
218
|
-
|
|
219
|
-
for (const dbInfo of absurdDatabases) {
|
|
220
|
-
const { name, config } = dbInfo;
|
|
221
|
-
console.log(`\n--- Testing ${name} ---`);
|
|
222
|
-
let db;
|
|
223
|
-
try {
|
|
224
|
-
db = createDatabase(config);
|
|
225
|
-
console.log(`${name} database connected.`);
|
|
226
|
-
|
|
227
|
-
const absurdTableName = 'very_long_and_absurd_table_name_for_testing_purposes_1234567890_abcdefghijklmnopqrstuvwxyz';
|
|
228
|
-
const absurdColumnName = 'very_long_and_absurd_column_name_for_testing_purposes_1234567890_abcdefghijklmnopqrstuvwxyz';
|
|
229
|
-
const absurdValue = 'Absurd Value with !@#$%^&*()-+=';
|
|
230
|
-
|
|
231
|
-
// Test Insert with absurd names
|
|
232
|
-
const insertResult = await db.insert(absurdTableName, { [absurdColumnName]: absurdValue, 'another_col': 'some_data' });
|
|
233
|
-
console.log(`${name} Insert:`, insertResult);
|
|
234
|
-
|
|
235
|
-
// Test SelectOne with absurd names
|
|
236
|
-
const data = await db.selectOne(absurdTableName, { [absurdColumnName]: absurdValue });
|
|
237
|
-
console.log(`${name} SelectOne:`, data);
|
|
238
|
-
|
|
239
|
-
// Test Update with absurd names
|
|
240
|
-
if (data) {
|
|
241
|
-
const updateResult = await db.update(absurdTableName, { 'updated_col': 'updated_data' }, { [absurdColumnName]: absurdValue });
|
|
242
|
-
console.log(`${name} Update:`, updateResult);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Test Select (all) with absurd names
|
|
246
|
-
const allData = await db.select(absurdTableName);
|
|
247
|
-
console.log(`${name} Select All:`, allData);
|
|
248
|
-
|
|
249
|
-
// Test Set (upsert) with absurd names
|
|
250
|
-
const setResult = await db.set(absurdTableName, { 'set_col': 'set_data' }, { [absurdColumnName]: absurdValue });
|
|
251
|
-
console.log(`${name} Set (upsert):`, setResult);
|
|
252
|
-
|
|
253
|
-
// Test Delete with absurd names
|
|
254
|
-
const deleteResult = await db.delete(absurdTableName, { [absurdColumnName]: absurdValue });
|
|
255
|
-
console.log(`${name} Delete:`, deleteResult);
|
|
256
|
-
|
|
257
|
-
} catch (error) {
|
|
258
|
-
console.error(`Error testing ${name} database:`, error);
|
|
259
|
-
} finally {
|
|
260
|
-
if (db) {
|
|
261
|
-
try {
|
|
262
|
-
await db.close();
|
|
263
|
-
console.log(`${name} database connection closed.`);
|
|
264
|
-
} catch (closeError) {
|
|
265
|
-
console.error(`Error closing ${name} database connection:`, closeError);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
178
|
|
|
271
179
|
console.log('\nAll database tests completed.');
|
|
272
180
|
}
|
|
@@ -348,7 +256,6 @@ function runHelperFunctionTests() {
|
|
|
348
256
|
|
|
349
257
|
async function main() {
|
|
350
258
|
await runDatabaseTests();
|
|
351
|
-
runHelperFunctionTests();
|
|
352
259
|
}
|
|
353
260
|
|
|
354
261
|
main();
|