@onurege3467/zerohelper 8.0.0 → 9.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 +292 -612
- package/dist/database/IDatabase.d.ts +77 -0
- package/dist/database/IDatabase.js +10 -0
- package/dist/database/cacheWrapper.d.ts +31 -0
- package/dist/database/cacheWrapper.js +228 -0
- package/dist/database/index.d.ts +11 -0
- package/dist/database/index.js +94 -0
- package/dist/database/json.d.ts +32 -0
- package/dist/database/json.js +210 -0
- package/dist/database/migration.d.ts +21 -0
- package/dist/database/migration.js +97 -0
- package/dist/database/mongodb.d.ts +24 -0
- package/dist/database/mongodb.js +153 -0
- package/dist/database/mysql.d.ts +31 -0
- package/dist/database/mysql.js +385 -0
- package/dist/database/pg.d.ts +30 -0
- package/dist/database/pg.js +300 -0
- package/dist/database/redis.d.ts +23 -0
- package/dist/database/redis.js +157 -0
- package/dist/database/sqlite.d.ts +25 -0
- package/dist/database/sqlite.js +273 -0
- package/dist/database/types.d.ts +76 -0
- package/dist/database/types.js +2 -0
- package/dist/database/zpack.d.ts +59 -0
- package/dist/database/zpack.js +462 -0
- package/dist/functions/index.d.ts +183 -0
- package/dist/functions/index.js +636 -0
- package/dist/functions/temp_isphone.d.ts +1 -0
- package/dist/functions/temp_isphone.js +7 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +45 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +55 -0
- package/dist/test_zpack.d.ts +1 -0
- package/dist/test_zpack.js +64 -0
- package/package.json +23 -6
- package/database/IDatabase.js +0 -92
- package/database/cacheWrapper.js +0 -585
- package/database/index.js +0 -72
- package/database/json.js +0 -281
- package/database/migration.js +0 -227
- package/database/mongodb.js +0 -203
- package/database/mysql.js +0 -526
- package/database/pg.js +0 -527
- package/database/redis.js +0 -342
- package/database/sqlite.js +0 -551
- package/functions/index.js +0 -705
- package/index.js +0 -7
|
@@ -0,0 +1,210 @@
|
|
|
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.JsonDatabase = 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
|
+
class JsonDatabase extends IDatabase_1.IDatabase {
|
|
12
|
+
constructor(config) {
|
|
13
|
+
super();
|
|
14
|
+
this.db = {};
|
|
15
|
+
this.isDirty = false;
|
|
16
|
+
this.isWriting = false;
|
|
17
|
+
this.writeQueue = [];
|
|
18
|
+
this.saveDebounceTimeout = null;
|
|
19
|
+
if (!config || !config.filePath)
|
|
20
|
+
throw new Error('Yapılandırma içinde "filePath" belirtilmelidir.');
|
|
21
|
+
this.filePath = config.filePath;
|
|
22
|
+
this.saveInterval = 500;
|
|
23
|
+
process.on('exit', () => this.flushSync());
|
|
24
|
+
this.initPromise = this._load();
|
|
25
|
+
}
|
|
26
|
+
async _load() {
|
|
27
|
+
try {
|
|
28
|
+
const dir = path_1.default.dirname(this.filePath);
|
|
29
|
+
await promises_1.default.mkdir(dir, { recursive: true });
|
|
30
|
+
const fileContent = await promises_1.default.readFile(this.filePath, 'utf-8');
|
|
31
|
+
this.db = JSON.parse(fileContent);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (error.code === 'ENOENT') {
|
|
35
|
+
this.db = {};
|
|
36
|
+
await this._saveNow();
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.db = {};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
_queueRequest(operation) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
this.writeQueue.push({ operation, resolve, reject });
|
|
46
|
+
this._processQueue();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async _processQueue() {
|
|
50
|
+
if (this.isWriting || this.writeQueue.length === 0)
|
|
51
|
+
return;
|
|
52
|
+
this.isWriting = true;
|
|
53
|
+
const item = this.writeQueue.shift();
|
|
54
|
+
if (item) {
|
|
55
|
+
try {
|
|
56
|
+
const result = item.operation();
|
|
57
|
+
this.isDirty = true;
|
|
58
|
+
this._scheduleSave();
|
|
59
|
+
item.resolve(result);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
item.reject(error);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
this.isWriting = false;
|
|
66
|
+
this._processQueue();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
_scheduleSave() {
|
|
71
|
+
if (this.saveDebounceTimeout)
|
|
72
|
+
clearTimeout(this.saveDebounceTimeout);
|
|
73
|
+
this.saveDebounceTimeout = setTimeout(() => this._saveNow(), this.saveInterval);
|
|
74
|
+
}
|
|
75
|
+
async _saveNow() {
|
|
76
|
+
if (!this.isDirty)
|
|
77
|
+
return;
|
|
78
|
+
if (this.saveDebounceTimeout)
|
|
79
|
+
clearTimeout(this.saveDebounceTimeout);
|
|
80
|
+
this.saveDebounceTimeout = null;
|
|
81
|
+
try {
|
|
82
|
+
await promises_1.default.writeFile(this.filePath, JSON.stringify(this.db, null, 2));
|
|
83
|
+
this.isDirty = false;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error("Veritabanı dosyasına yazılırken hata:", error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
flushSync() {
|
|
90
|
+
if (this.isDirty) {
|
|
91
|
+
try {
|
|
92
|
+
(0, fs_1.writeFileSync)(this.filePath, JSON.stringify(this.db, null, 2));
|
|
93
|
+
this.isDirty = false;
|
|
94
|
+
}
|
|
95
|
+
catch (error) { }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
_matches(row, where) {
|
|
99
|
+
return Object.keys(where).every(key => row[key] === where[key]);
|
|
100
|
+
}
|
|
101
|
+
async ensureTable(table) {
|
|
102
|
+
await this.initPromise;
|
|
103
|
+
if (!this.db[table]) {
|
|
104
|
+
return this._queueRequest(() => {
|
|
105
|
+
this.db[table] = [];
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async insert(table, data) {
|
|
110
|
+
await this.ensureTable(table);
|
|
111
|
+
return this._queueRequest(() => {
|
|
112
|
+
const maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
|
|
113
|
+
const newId = maxId + 1;
|
|
114
|
+
const newRow = { _id: newId, ...data };
|
|
115
|
+
this.db[table].push(newRow);
|
|
116
|
+
return newId;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async update(table, data, where) {
|
|
120
|
+
await this.ensureTable(table);
|
|
121
|
+
return this._queueRequest(() => {
|
|
122
|
+
let affectedRows = 0;
|
|
123
|
+
this.db[table].forEach(row => {
|
|
124
|
+
if (this._matches(row, where)) {
|
|
125
|
+
Object.assign(row, data);
|
|
126
|
+
affectedRows++;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return affectedRows;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async delete(table, where) {
|
|
133
|
+
await this.ensureTable(table);
|
|
134
|
+
return this._queueRequest(() => {
|
|
135
|
+
const initialLength = this.db[table].length;
|
|
136
|
+
this.db[table] = this.db[table].filter(row => !this._matches(row, where));
|
|
137
|
+
return initialLength - this.db[table].length;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
async select(table, where = null) {
|
|
141
|
+
await this.initPromise;
|
|
142
|
+
const tableData = this.db[table] || [];
|
|
143
|
+
let results = tableData;
|
|
144
|
+
if (where && Object.keys(where).length > 0) {
|
|
145
|
+
results = results.filter(row => this._matches(row, where));
|
|
146
|
+
}
|
|
147
|
+
return JSON.parse(JSON.stringify(results));
|
|
148
|
+
}
|
|
149
|
+
async set(table, data, where) {
|
|
150
|
+
const existing = await this.select(table, where);
|
|
151
|
+
if (existing.length === 0) {
|
|
152
|
+
return this.insert(table, { ...where, ...data });
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
return this.update(table, data, where);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async selectOne(table, where = null) {
|
|
159
|
+
const results = await this.select(table, where);
|
|
160
|
+
return results[0] || null;
|
|
161
|
+
}
|
|
162
|
+
async bulkInsert(table, dataArray) {
|
|
163
|
+
if (!Array.isArray(dataArray) || dataArray.length === 0)
|
|
164
|
+
return 0;
|
|
165
|
+
await this.ensureTable(table);
|
|
166
|
+
return this._queueRequest(() => {
|
|
167
|
+
let maxId = this.db[table].reduce((max, row) => (row._id > max ? row._id : max), 0);
|
|
168
|
+
dataArray.forEach(data => {
|
|
169
|
+
maxId++;
|
|
170
|
+
this.db[table].push({ _id: maxId, ...data });
|
|
171
|
+
});
|
|
172
|
+
return dataArray.length;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
async increment(table, increments, where = {}) {
|
|
176
|
+
await this.ensureTable(table);
|
|
177
|
+
return this._queueRequest(() => {
|
|
178
|
+
let affectedCount = 0;
|
|
179
|
+
this.db[table].forEach(row => {
|
|
180
|
+
if (this._matches(row, where)) {
|
|
181
|
+
for (const [field, value] of Object.entries(increments)) {
|
|
182
|
+
row[field] = (Number(row[field]) || 0) + value;
|
|
183
|
+
}
|
|
184
|
+
affectedCount++;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
return affectedCount;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
async decrement(table, decrements, where = {}) {
|
|
191
|
+
await this.ensureTable(table);
|
|
192
|
+
return this._queueRequest(() => {
|
|
193
|
+
let affectedCount = 0;
|
|
194
|
+
this.db[table].forEach(row => {
|
|
195
|
+
if (this._matches(row, where)) {
|
|
196
|
+
for (const [field, value] of Object.entries(decrements)) {
|
|
197
|
+
row[field] = (Number(row[field]) || 0) - value;
|
|
198
|
+
}
|
|
199
|
+
affectedCount++;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
return affectedCount;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async close() {
|
|
206
|
+
await this._saveNow();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.JsonDatabase = JsonDatabase;
|
|
210
|
+
exports.default = JsonDatabase;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IDatabase } from './IDatabase';
|
|
2
|
+
export interface MigrationFile {
|
|
3
|
+
name: string;
|
|
4
|
+
path: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class MigrationManager {
|
|
7
|
+
private db;
|
|
8
|
+
private migrationsDir;
|
|
9
|
+
private migrationsTable;
|
|
10
|
+
constructor(database: IDatabase, options?: any);
|
|
11
|
+
private ensureMigrationsDir;
|
|
12
|
+
ensureMigrationsTable(): Promise<void>;
|
|
13
|
+
createMigration(name: string, description?: string): string;
|
|
14
|
+
getMigrationFiles(): MigrationFile[];
|
|
15
|
+
getExecutedMigrations(): Promise<string[]>;
|
|
16
|
+
getPendingMigrations(): Promise<MigrationFile[]>;
|
|
17
|
+
runMigration(migrationFile: MigrationFile, direction?: 'up' | 'down'): Promise<boolean>;
|
|
18
|
+
migrate(): Promise<void>;
|
|
19
|
+
rollback(steps?: number): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export default MigrationManager;
|
|
@@ -0,0 +1,97 @@
|
|
|
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.MigrationManager = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
class MigrationManager {
|
|
10
|
+
constructor(database, options = {}) {
|
|
11
|
+
this.db = database;
|
|
12
|
+
this.migrationsDir = options.migrationsDir || './migrations';
|
|
13
|
+
this.migrationsTable = options.migrationsTable || 'migrations';
|
|
14
|
+
this.ensureMigrationsDir();
|
|
15
|
+
}
|
|
16
|
+
ensureMigrationsDir() {
|
|
17
|
+
if (!fs_1.default.existsSync(this.migrationsDir)) {
|
|
18
|
+
fs_1.default.mkdirSync(this.migrationsDir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async ensureMigrationsTable() {
|
|
22
|
+
try {
|
|
23
|
+
await this.db.selectOne(this.migrationsTable, {});
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (this.db.constructor.name === 'MySQLDatabase') {
|
|
27
|
+
await this.db.query(`CREATE TABLE IF NOT EXISTS ${this.migrationsTable} (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL UNIQUE, executed_at DATETIME DEFAULT CURRENT_TIMESTAMP)`);
|
|
28
|
+
}
|
|
29
|
+
else if (this.db.constructor.name === 'PostgreSQLDatabase') {
|
|
30
|
+
await this.db.query(`CREATE TABLE IF NOT EXISTS ${this.migrationsTable} (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL UNIQUE, executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
await this.db.query(`CREATE TABLE IF NOT EXISTS ${this.migrationsTable} (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL UNIQUE, executed_at DATETIME DEFAULT CURRENT_TIMESTAMP)`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
createMigration(name, description = '') {
|
|
38
|
+
const timestamp = Date.now();
|
|
39
|
+
const filename = `${timestamp}_${name}.ts`;
|
|
40
|
+
const filePath = path_1.default.join(this.migrationsDir, filename);
|
|
41
|
+
const template = `import { IDatabase } from "../database/IDatabase";\n\nexport const up = async (db: IDatabase) => {\n // Migration logic here\n};\n\nexport const down = async (db: IDatabase) => {\n // Rollback logic here\n};\n`;
|
|
42
|
+
fs_1.default.writeFileSync(filePath, template);
|
|
43
|
+
return filename;
|
|
44
|
+
}
|
|
45
|
+
getMigrationFiles() {
|
|
46
|
+
return fs_1.default.readdirSync(this.migrationsDir)
|
|
47
|
+
.filter(file => file.endsWith('.ts') || file.endsWith('.js'))
|
|
48
|
+
.sort()
|
|
49
|
+
.map(file => ({ name: file, path: path_1.default.join(this.migrationsDir, file) }));
|
|
50
|
+
}
|
|
51
|
+
async getExecutedMigrations() {
|
|
52
|
+
await this.ensureMigrationsTable();
|
|
53
|
+
try {
|
|
54
|
+
const executed = await this.db.select(this.migrationsTable, {});
|
|
55
|
+
return executed.map((row) => row.name);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async getPendingMigrations() {
|
|
62
|
+
const all = this.getMigrationFiles();
|
|
63
|
+
const executed = await this.getExecutedMigrations();
|
|
64
|
+
return all.filter(m => !executed.includes(m.name));
|
|
65
|
+
}
|
|
66
|
+
async runMigration(migrationFile, direction = 'up') {
|
|
67
|
+
try {
|
|
68
|
+
const migration = require(path_1.default.resolve(migrationFile.path));
|
|
69
|
+
if (typeof migration[direction] !== 'function')
|
|
70
|
+
throw new Error(`Migration ${migrationFile.name} has no ${direction} method`);
|
|
71
|
+
await migration[direction](this.db);
|
|
72
|
+
if (direction === 'up')
|
|
73
|
+
await this.db.insert(this.migrationsTable, { name: migrationFile.name });
|
|
74
|
+
else
|
|
75
|
+
await this.db.delete(this.migrationsTable, { name: migrationFile.name });
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
console.error(`Migration ${migrationFile.name} error:`, error);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async migrate() {
|
|
84
|
+
const pending = await this.getPendingMigrations();
|
|
85
|
+
for (const m of pending)
|
|
86
|
+
await this.runMigration(m, 'up');
|
|
87
|
+
}
|
|
88
|
+
async rollback(steps = 1) {
|
|
89
|
+
const executed = await this.getExecutedMigrations();
|
|
90
|
+
const all = this.getMigrationFiles();
|
|
91
|
+
const toRollback = executed.slice(-steps).reverse().map(name => all.find(m => m.name === name)).filter((m) => !!m);
|
|
92
|
+
for (const m of toRollback)
|
|
93
|
+
await this.runMigration(m, 'down');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.MigrationManager = MigrationManager;
|
|
97
|
+
exports.default = MigrationManager;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IDatabase } from './IDatabase';
|
|
2
|
+
export declare class MongoDBDatabase extends IDatabase {
|
|
3
|
+
private config;
|
|
4
|
+
private client;
|
|
5
|
+
private db;
|
|
6
|
+
private _queue;
|
|
7
|
+
private _connected;
|
|
8
|
+
private _connectionPromise;
|
|
9
|
+
constructor(config: any);
|
|
10
|
+
private _queueRequest;
|
|
11
|
+
private _processQueue;
|
|
12
|
+
ensureCollection(collection: string): Promise<void>;
|
|
13
|
+
insert(collection: string, data: Record<string, any>): Promise<any>;
|
|
14
|
+
bulkInsert(collection: string, dataArray: Record<string, any>[]): Promise<number>;
|
|
15
|
+
update(collection: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
|
|
16
|
+
delete(collection: string, where: Record<string, any>): Promise<number>;
|
|
17
|
+
select<T = any>(collection: string, where?: Record<string, any>): Promise<T[]>;
|
|
18
|
+
selectOne<T = any>(collection: string, where?: Record<string, any>): Promise<T | null>;
|
|
19
|
+
set(collection: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
|
|
20
|
+
increment(collection: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
21
|
+
decrement(collection: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export default MongoDBDatabase;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoDBDatabase = void 0;
|
|
4
|
+
const IDatabase_1 = require("./IDatabase");
|
|
5
|
+
const mongodb_1 = require("mongodb");
|
|
6
|
+
class MongoDBDatabase extends IDatabase_1.IDatabase {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
super();
|
|
9
|
+
this.db = null;
|
|
10
|
+
this._queue = [];
|
|
11
|
+
this._connected = false;
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.client = new mongodb_1.MongoClient(config.url || config.uri);
|
|
14
|
+
this._connectionPromise = new Promise(async (resolve, reject) => {
|
|
15
|
+
try {
|
|
16
|
+
await this.client.connect();
|
|
17
|
+
this.db = this.client.db(config.database || config.dbName);
|
|
18
|
+
this._connected = true;
|
|
19
|
+
resolve(this.db);
|
|
20
|
+
this._processQueue();
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error("MongoDB connection error:", error);
|
|
24
|
+
reject(error);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async _queueRequest(operation) {
|
|
29
|
+
if (this._connected) {
|
|
30
|
+
return operation();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
this._queue.push({ operation, resolve, reject });
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async _processQueue() {
|
|
39
|
+
if (!this._connected)
|
|
40
|
+
return;
|
|
41
|
+
while (this._queue.length > 0) {
|
|
42
|
+
const item = this._queue.shift();
|
|
43
|
+
if (item) {
|
|
44
|
+
try {
|
|
45
|
+
const result = await item.operation();
|
|
46
|
+
item.resolve(result);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
item.reject(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async ensureCollection(collection) {
|
|
55
|
+
return this._queueRequest(async () => {
|
|
56
|
+
const db = await this._connectionPromise;
|
|
57
|
+
const collections = await db.listCollections({ name: collection }).toArray();
|
|
58
|
+
if (collections.length === 0) {
|
|
59
|
+
await db.createCollection(collection);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
async insert(collection, data) {
|
|
64
|
+
return this._queueRequest(async () => {
|
|
65
|
+
await this.ensureCollection(collection);
|
|
66
|
+
const db = await this._connectionPromise;
|
|
67
|
+
const result = await db.collection(collection).insertOne(data);
|
|
68
|
+
return result.insertedId;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async bulkInsert(collection, dataArray) {
|
|
72
|
+
return this._queueRequest(async () => {
|
|
73
|
+
if (!Array.isArray(dataArray) || dataArray.length === 0)
|
|
74
|
+
return 0;
|
|
75
|
+
await this.ensureCollection(collection);
|
|
76
|
+
const db = await this._connectionPromise;
|
|
77
|
+
const result = await db.collection(collection).insertMany(dataArray);
|
|
78
|
+
return result.insertedCount;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async update(collection, data, where) {
|
|
82
|
+
return this._queueRequest(async () => {
|
|
83
|
+
await this.ensureCollection(collection);
|
|
84
|
+
const db = await this._connectionPromise;
|
|
85
|
+
const result = await db.collection(collection).updateMany(where, { $set: data });
|
|
86
|
+
return Number(result.modifiedCount);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async delete(collection, where) {
|
|
90
|
+
return this._queueRequest(async () => {
|
|
91
|
+
await this.ensureCollection(collection);
|
|
92
|
+
const db = await this._connectionPromise;
|
|
93
|
+
const result = await db.collection(collection).deleteMany(where);
|
|
94
|
+
return result.deletedCount;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async select(collection, where = {}) {
|
|
98
|
+
return this._queueRequest(async () => {
|
|
99
|
+
await this.ensureCollection(collection);
|
|
100
|
+
const db = await this._connectionPromise;
|
|
101
|
+
const results = await db.collection(collection).find(where).toArray();
|
|
102
|
+
return results;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async selectOne(collection, where = {}) {
|
|
106
|
+
return this._queueRequest(async () => {
|
|
107
|
+
await this.ensureCollection(collection);
|
|
108
|
+
const db = await this._connectionPromise;
|
|
109
|
+
const result = await db.collection(collection).findOne(where);
|
|
110
|
+
return result;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async set(collection, data, where) {
|
|
114
|
+
return this._queueRequest(async () => {
|
|
115
|
+
await this.ensureCollection(collection);
|
|
116
|
+
const db = await this._connectionPromise;
|
|
117
|
+
const existing = await db.collection(collection).findOne(where);
|
|
118
|
+
if (!existing) {
|
|
119
|
+
const result = await db.collection(collection).insertOne({ ...where, ...data });
|
|
120
|
+
return result.insertedId;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const result = await db.collection(collection).updateOne(where, { $set: data });
|
|
124
|
+
return result.modifiedCount;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async increment(collection, increments, where = {}) {
|
|
129
|
+
return this._queueRequest(async () => {
|
|
130
|
+
await this.ensureCollection(collection);
|
|
131
|
+
const db = await this._connectionPromise;
|
|
132
|
+
const result = await db.collection(collection).updateMany(where, { $inc: increments });
|
|
133
|
+
return Number(result.modifiedCount);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async decrement(collection, decrements, where = {}) {
|
|
137
|
+
return this._queueRequest(async () => {
|
|
138
|
+
await this.ensureCollection(collection);
|
|
139
|
+
const db = await this._connectionPromise;
|
|
140
|
+
const negativeIncrements = {};
|
|
141
|
+
for (const [key, value] of Object.entries(decrements)) {
|
|
142
|
+
negativeIncrements[key] = -value;
|
|
143
|
+
}
|
|
144
|
+
const result = await db.collection(collection).updateMany(where, { $inc: negativeIncrements });
|
|
145
|
+
return Number(result.modifiedCount);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async close() {
|
|
149
|
+
await this.client.close();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.MongoDBDatabase = MongoDBDatabase;
|
|
153
|
+
exports.default = MongoDBDatabase;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IDatabase } from './IDatabase';
|
|
2
|
+
import { MySQLConfig } from './types';
|
|
3
|
+
export declare class MySQLDatabase extends IDatabase {
|
|
4
|
+
private config;
|
|
5
|
+
private pool;
|
|
6
|
+
private _queue;
|
|
7
|
+
private _connected;
|
|
8
|
+
private _connectionPromise;
|
|
9
|
+
constructor(config: MySQLConfig);
|
|
10
|
+
private _getColumnType;
|
|
11
|
+
private _getBestColumnType;
|
|
12
|
+
private _ensureMissingColumns;
|
|
13
|
+
private _queueRequest;
|
|
14
|
+
private _processQueue;
|
|
15
|
+
query(sql: string, params?: any[]): Promise<any>;
|
|
16
|
+
ensureTable(table: string, data?: Record<string, any>): Promise<void>;
|
|
17
|
+
insert(table: string, data: Record<string, any>): Promise<number>;
|
|
18
|
+
update(table: string, data: Record<string, any>, where: Record<string, any>): Promise<number>;
|
|
19
|
+
delete(table: string, where: Record<string, any>): Promise<number>;
|
|
20
|
+
select<T = any>(table: string, where?: Record<string, any> | null): Promise<T[]>;
|
|
21
|
+
set(table: string, data: Record<string, any>, where: Record<string, any>): Promise<any>;
|
|
22
|
+
selectOne<T = any>(table: string, where?: Record<string, any> | null): Promise<T | null>;
|
|
23
|
+
bulkInsert(table: string, dataArray: Record<string, any>[]): Promise<number>;
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
private _serializeValue;
|
|
26
|
+
private _deserializeValue;
|
|
27
|
+
increment(table: string, increments: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
28
|
+
decrement(table: string, decrements: Record<string, number>, where?: Record<string, any>): Promise<number>;
|
|
29
|
+
private _buildWhereClause;
|
|
30
|
+
}
|
|
31
|
+
export default MySQLDatabase;
|