@onurege3467/zerohelper 9.0.0 → 9.2.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.
Files changed (45) hide show
  1. package/README.md +152 -254
  2. package/dist/bin/zero.d.ts +2 -0
  3. package/dist/bin/zero.js +141 -0
  4. package/dist/database/IDatabase.d.ts +25 -31
  5. package/dist/database/IDatabase.js +38 -0
  6. package/dist/database/cacheWrapper.d.ts +5 -2
  7. package/dist/database/cacheWrapper.js +36 -50
  8. package/dist/database/index.d.ts +3 -2
  9. package/dist/database/index.js +13 -9
  10. package/dist/database/json.d.ts +4 -4
  11. package/dist/database/json.js +85 -87
  12. package/dist/database/mongodb.d.ts +12 -12
  13. package/dist/database/mongodb.js +49 -82
  14. package/dist/database/mysql.d.ts +7 -9
  15. package/dist/database/mysql.js +149 -270
  16. package/dist/database/pg.d.ts +12 -14
  17. package/dist/database/pg.js +113 -222
  18. package/dist/database/redis.d.ts +5 -3
  19. package/dist/database/redis.js +81 -107
  20. package/dist/database/seeder.d.ts +20 -0
  21. package/dist/database/seeder.js +37 -0
  22. package/dist/database/sqlite.d.ts +12 -15
  23. package/dist/database/sqlite.js +108 -223
  24. package/dist/database/telemetry.d.ts +35 -0
  25. package/dist/database/telemetry.js +41 -0
  26. package/dist/database/toon.d.ts +32 -0
  27. package/dist/database/toon.js +209 -0
  28. package/dist/database/types.d.ts +28 -34
  29. package/dist/database/zpack.d.ts +10 -4
  30. package/dist/database/zpack.js +151 -71
  31. package/dist/functions/index.d.ts +16 -0
  32. package/dist/functions/index.js +49 -3
  33. package/dist/functions/security.d.ts +15 -0
  34. package/dist/functions/security.js +46 -0
  35. package/dist/functions/toon.d.ts +7 -0
  36. package/dist/functions/toon.js +118 -0
  37. package/dist/functions/worker.d.ts +5 -0
  38. package/dist/functions/worker.js +35 -0
  39. package/dist/test_v91_advanced.d.ts +1 -0
  40. package/dist/test_v91_advanced.js +48 -0
  41. package/dist/test_v91_basics.d.ts +1 -0
  42. package/dist/test_v91_basics.js +54 -0
  43. package/dist/test_v91_performance.d.ts +1 -0
  44. package/dist/test_v91_performance.js +54 -0
  45. package/package.json +16 -3
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ToonDatabase = void 0;
7
+ const IDatabase_1 = require("./IDatabase");
8
+ const promises_1 = __importDefault(require("fs/promises"));
9
+ const fs_1 = require("fs");
10
+ const path_1 = __importDefault(require("path"));
11
+ const toon_1 = require("../functions/toon");
12
+ class ToonDatabase extends IDatabase_1.IDatabase {
13
+ constructor(config) {
14
+ super();
15
+ this.db = {};
16
+ this.isDirty = false;
17
+ this.isWriting = false;
18
+ this.writeQueue = [];
19
+ this.saveDebounceTimeout = null;
20
+ if (!config || !config.path)
21
+ throw new Error('ToonDB: "path" gereklidir.');
22
+ this.filePath = config.path;
23
+ this.saveInterval = config.saveInterval || 500;
24
+ process.on('exit', () => this.flushSync());
25
+ this.initPromise = this._load();
26
+ }
27
+ async _execute(op, table, fn) {
28
+ const start = Date.now();
29
+ const res = await fn();
30
+ this.recordMetric(op, table, Date.now() - start);
31
+ return res;
32
+ }
33
+ async _load() {
34
+ try {
35
+ const dir = path_1.default.dirname(this.filePath);
36
+ if (!(0, fs_1.existsSync)(dir))
37
+ await promises_1.default.mkdir(dir, { recursive: true });
38
+ if (!(0, fs_1.existsSync)(this.filePath)) {
39
+ this.db = {};
40
+ await this._saveNow();
41
+ return;
42
+ }
43
+ const content = await promises_1.default.readFile(this.filePath, 'utf-8');
44
+ this.db = (0, toon_1.parse)(content);
45
+ }
46
+ catch (error) {
47
+ this.db = {};
48
+ }
49
+ }
50
+ _queueRequest(operation) {
51
+ return new Promise((resolve, reject) => {
52
+ this.writeQueue.push({ operation, resolve, reject });
53
+ this._processQueue();
54
+ });
55
+ }
56
+ async _processQueue() {
57
+ if (this.isWriting || this.writeQueue.length === 0)
58
+ return;
59
+ this.isWriting = true;
60
+ const item = this.writeQueue.shift();
61
+ if (item) {
62
+ try {
63
+ const result = item.operation();
64
+ this.isDirty = true;
65
+ this._scheduleSave();
66
+ item.resolve(result);
67
+ }
68
+ catch (error) {
69
+ item.reject(error);
70
+ }
71
+ finally {
72
+ this.isWriting = false;
73
+ this._processQueue();
74
+ }
75
+ }
76
+ }
77
+ _scheduleSave() {
78
+ if (this.saveDebounceTimeout)
79
+ clearTimeout(this.saveDebounceTimeout);
80
+ this.saveDebounceTimeout = setTimeout(() => this._saveNow(), this.saveInterval);
81
+ }
82
+ async _saveNow() {
83
+ if (!this.isDirty)
84
+ return;
85
+ if (this.saveDebounceTimeout)
86
+ clearTimeout(this.saveDebounceTimeout);
87
+ this.saveDebounceTimeout = null;
88
+ try {
89
+ await promises_1.default.writeFile(this.filePath, (0, toon_1.stringify)(this.db));
90
+ this.isDirty = false;
91
+ }
92
+ catch (error) {
93
+ console.error("ToonDB save error:", error);
94
+ }
95
+ }
96
+ flushSync() {
97
+ if (this.isDirty) {
98
+ try {
99
+ (0, fs_1.writeFileSync)(this.filePath, (0, toon_1.stringify)(this.db));
100
+ this.isDirty = false;
101
+ }
102
+ catch (error) { }
103
+ }
104
+ }
105
+ async ensureTable(table) {
106
+ await this.initPromise;
107
+ if (!this.db[table]) {
108
+ return this._queueRequest(() => { this.db[table] = []; });
109
+ }
110
+ }
111
+ async insert(table, data) {
112
+ await this.runHooks('beforeInsert', table, data);
113
+ return this._execute('insert', table, async () => {
114
+ await this.ensureTable(table);
115
+ return this._queueRequest(() => {
116
+ const maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
117
+ const newId = maxId + 1;
118
+ const newRow = { _id: newId, ...data };
119
+ this.db[table].push(newRow);
120
+ this.runHooks('afterInsert', table, newRow);
121
+ return newId;
122
+ });
123
+ });
124
+ }
125
+ async update(table, data, where) {
126
+ await this.runHooks('beforeUpdate', table, { data, where });
127
+ return this._execute('update', table, async () => {
128
+ await this.ensureTable(table);
129
+ return this._queueRequest(() => {
130
+ let affected = 0;
131
+ this.db[table].forEach(row => {
132
+ if (Object.keys(where).every(k => String(row[k]) === String(where[k]))) {
133
+ Object.assign(row, data);
134
+ affected++;
135
+ }
136
+ });
137
+ this.runHooks('afterUpdate', table, { affected });
138
+ return affected;
139
+ });
140
+ });
141
+ }
142
+ async delete(table, where) {
143
+ await this.runHooks('beforeDelete', table, where);
144
+ return this._execute('delete', table, async () => {
145
+ await this.ensureTable(table);
146
+ return this._queueRequest(() => {
147
+ const initial = this.db[table].length;
148
+ this.db[table] = this.db[table].filter(row => !Object.keys(where).every(k => String(row[k]) === String(where[k])));
149
+ const affected = initial - this.db[table].length;
150
+ this.runHooks('afterDelete', table, { affected });
151
+ return affected;
152
+ });
153
+ });
154
+ }
155
+ async select(table, where = null) {
156
+ return this._execute('select', table, async () => {
157
+ await this.initPromise;
158
+ const results = where && Object.keys(where).length > 0
159
+ ? (this.db[table] || []).filter(row => Object.keys(where).every(k => String(row[k]) === String(where[k])))
160
+ : (this.db[table] || []);
161
+ return JSON.parse(JSON.stringify(results));
162
+ });
163
+ }
164
+ async selectOne(table, where = null) {
165
+ const res = await this.select(table, where);
166
+ return res[0] || null;
167
+ }
168
+ async set(table, data, where) {
169
+ const ex = await this.selectOne(table, where);
170
+ return ex ? this.update(table, data, where) : this.insert(table, { ...where, ...data });
171
+ }
172
+ async bulkInsert(table, dataArray) {
173
+ return this._execute('bulkInsert', table, async () => {
174
+ if (!dataArray.length)
175
+ return 0;
176
+ await this.ensureTable(table);
177
+ return this._queueRequest(() => {
178
+ let maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
179
+ dataArray.forEach(data => { maxId++; this.db[table].push({ _id: maxId, ...data }); });
180
+ return dataArray.length;
181
+ });
182
+ });
183
+ }
184
+ async increment(table, incs, where = {}) {
185
+ return this._execute('increment', table, async () => {
186
+ await this.ensureTable(table);
187
+ return this._queueRequest(() => {
188
+ let affected = 0;
189
+ this.db[table].forEach(row => {
190
+ if (Object.keys(where).every(k => String(row[k]) === String(where[k]))) {
191
+ for (const [f, v] of Object.entries(incs))
192
+ row[f] = (Number(row[f]) || 0) + v;
193
+ affected++;
194
+ }
195
+ });
196
+ return affected;
197
+ });
198
+ });
199
+ }
200
+ async decrement(table, decs, where = {}) {
201
+ const incs = {};
202
+ for (const k in decs)
203
+ incs[k] = -decs[k];
204
+ return this.increment(table, incs, where);
205
+ }
206
+ async close() { await this._saveNow(); }
207
+ }
208
+ exports.ToonDatabase = ToonDatabase;
209
+ exports.default = ToonDatabase;
@@ -1,47 +1,38 @@
1
- export interface MySQLConfig {
2
- host: string;
3
- user: string;
4
- password?: string;
5
- database: string;
6
- port?: number;
7
- connectionLimit?: number;
8
- cache?: CacheConfig;
9
- }
10
- export interface SQLiteConfig {
11
- filename: string;
12
- cache?: CacheConfig;
13
- }
14
- export interface MongoDBConfig {
15
- uri: string;
16
- dbName: string;
1
+ export interface BaseConfig {
17
2
  cache?: CacheConfig;
18
3
  }
19
- export interface PostgreSQLConfig {
20
- host: string;
21
- user: string;
22
- password?: string;
23
- database: string;
24
- port?: number;
25
- connectionLimit?: number;
26
- cache?: CacheConfig;
27
- }
28
- export interface RedisConfig {
4
+ export interface NetworkConfig extends BaseConfig {
29
5
  host?: string;
30
6
  port?: number;
7
+ username?: string;
31
8
  password?: string;
32
- db?: number;
9
+ database?: string;
10
+ url?: string;
11
+ poolSize?: number;
12
+ }
13
+ export interface FileConfig extends BaseConfig {
14
+ path: string;
15
+ }
16
+ export interface MySQLConfig extends NetworkConfig {
17
+ }
18
+ export interface PostgreSQLConfig extends NetworkConfig {
19
+ }
20
+ export interface SQLiteConfig extends FileConfig {
21
+ }
22
+ export interface MongoDBConfig extends NetworkConfig {
23
+ }
24
+ export interface RedisConfig extends NetworkConfig {
33
25
  keyPrefix?: string;
34
- cache?: CacheConfig;
35
26
  }
36
- export interface JsonConfig {
37
- filePath: string;
27
+ export interface JsonConfig extends FileConfig {
38
28
  saveInterval?: number;
39
- cache?: CacheConfig;
40
29
  }
41
- export interface ZPackConfig {
42
- filePath: string;
30
+ export interface ZPackConfig extends FileConfig {
43
31
  autoFlush?: boolean;
44
- cache?: CacheConfig;
32
+ indexFields?: Record<string, string[]>;
33
+ }
34
+ export interface ToonConfig extends FileConfig {
35
+ saveInterval?: number;
45
36
  }
46
37
  export interface CacheConfig {
47
38
  type: 'memory' | 'redis';
@@ -72,5 +63,8 @@ export type DatabaseOptions = {
72
63
  } | {
73
64
  adapter: 'zpack';
74
65
  config: ZPackConfig;
66
+ } | {
67
+ adapter: 'toon';
68
+ config: ToonConfig;
75
69
  };
76
70
  export type AdapterType = DatabaseOptions['adapter'];
@@ -12,11 +12,14 @@ export declare class ZPackDatabase {
12
12
  private _closed;
13
13
  private _autoFlush;
14
14
  private _contentEnd;
15
+ private _compression;
15
16
  constructor(filePath: string, options?: {
16
17
  autoFlush?: boolean;
18
+ compression?: boolean;
17
19
  });
18
20
  open(): Promise<void>;
19
21
  close(): Promise<void>;
22
+ vacuum(): Promise<void>;
20
23
  insert(document: Record<string, any>, docId?: number): Promise<number>;
21
24
  insertBatch(documents: Record<string, any>[]): Promise<number[]>;
22
25
  delete(docId: number): Promise<void>;
@@ -27,9 +30,9 @@ export declare class ZPackDatabase {
27
30
  private _internalWriteFooter;
28
31
  private _writeFooter;
29
32
  private _encodeDocument;
33
+ private _decodeDocument;
30
34
  private _encodeTombstone;
31
35
  private _peekDocMeta;
32
- private _decodeDocument;
33
36
  private _tryLoadIndexFromFooter;
34
37
  private _scanAndRebuildIndex;
35
38
  }
@@ -40,20 +43,23 @@ export declare class ZPackAdapter extends IDatabase {
40
43
  private keyIndex;
41
44
  private rowCache;
42
45
  private secondary;
46
+ private indexedFields;
43
47
  constructor(config: ZPackConfig);
44
48
  private _init;
45
49
  ensureTable(table: string): Promise<void>;
50
+ private _updateSecondaryIndex;
46
51
  private _coerce;
47
52
  private _matches;
48
53
  select<T = any>(table: string, where?: Record<string, any> | null): Promise<T[]>;
49
54
  selectOne<T = any>(table: string, where?: Record<string, any> | null): Promise<T | null>;
50
55
  insert(table: string, data: Record<string, any>): Promise<number>;
51
56
  update(table: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
52
- set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
53
57
  delete(table: string, where: Record<string, any>): Promise<number>;
58
+ set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
54
59
  bulkInsert(table: string, dataArray: Record<string, any>[]): Promise<number>;
55
- increment(table: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
56
- decrement(table: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
60
+ increment(table: string, incs: Record<string, number>, where?: Record<string, any>): Promise<number>;
61
+ decrement(table: string, decs: Record<string, number>, where?: Record<string, any>): Promise<number>;
62
+ vacuum(): Promise<void>;
57
63
  close(): Promise<void>;
58
64
  }
59
65
  export default ZPackAdapter;