UNPKG

@onurege3467/zerohelper

Version:

ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.

146 lines (145 loc) 5.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MongoDBDatabase = void 0; const IDatabase_1 = require("./IDatabase"); const mongodb_1 = require("mongodb"); class MongoDBDatabase extends IDatabase_1.IDatabase { constructor(config) { super(); this.db = null; this._isConnected = false; this._queue = []; const uri = config.url || (config.uri ?? `mongodb://${config.host || 'localhost'}:${config.port || 27017}`); this.client = new mongodb_1.MongoClient(uri, { serverSelectionTimeoutMS: 5000, // Fail fast if no server connectTimeoutMS: 5000 }); this._connect(config.database || 'test'); } async _connect(dbName) { try { await this.client.connect(); this.db = this.client.db(dbName); this._isConnected = true; this._flushQueue(); } catch (error) { this._flushQueueWithError(error); // Opsiyonel: Yeniden bağlanma mantığı buraya eklenebilir, // ama testler için fail-fast daha iyidir. } } _flushQueue() { while (this._queue.length > 0) { const item = this._queue.shift(); if (item) { item.operation().then(item.resolve).catch(item.reject); } } } _flushQueueWithError(error) { while (this._queue.length > 0) { const item = this._queue.shift(); if (item) item.reject(error); } } async _execute(fn) { if (this._isConnected) { return fn(); } return new Promise((resolve, reject) => { this._queue.push({ operation: fn, resolve, reject }); }); } // --- Implementations --- async insert(collection, data) { await this.runHooks('beforeInsert', collection, data); return this._execute(async () => { const res = await this.db.collection(collection).insertOne(data); const newId = res.insertedId.toString(); const finalData = { _id: newId, ...data }; await this.runHooks('afterInsert', collection, finalData); return newId; }); } async update(collection, data, where) { await this.runHooks('beforeUpdate', collection, { data, where }); const formattedWhere = this._formatQuery(where); return this._execute(async () => { const res = await this.db.collection(collection).updateMany(formattedWhere, { $set: data }); return res.modifiedCount; }); } async delete(collection, where) { await this.runHooks('beforeDelete', collection, where); const formattedWhere = this._formatQuery(where); return this._execute(async () => { const res = await this.db.collection(collection).deleteMany(formattedWhere); return res.deletedCount; }); } async select(collection, where = {}) { const formattedWhere = this._formatQuery(where); return this._execute(async () => { const docs = await this.db.collection(collection).find(formattedWhere).toArray(); return docs.map(doc => this._serialize(doc)); }); } async selectOne(collection, where = {}) { const formattedWhere = this._formatQuery(where); return this._execute(async () => { const doc = await this.db.collection(collection).findOne(formattedWhere); return doc ? this._serialize(doc) : null; }); } async set(collection, data, where) { const ex = await this.selectOne(collection, where); return ex ? this.update(collection, data, where) : this.insert(collection, { ...where, ...data }); } async bulkInsert(collection, dataArray) { if (!dataArray.length) return 0; return this._execute(async () => { const res = await this.db.collection(collection).insertMany(dataArray); return res.insertedCount; }); } async increment(collection, incs, where = {}) { const formattedWhere = this._formatQuery(where); return this._execute(async () => { const res = await this.db.collection(collection).updateMany(formattedWhere, { $inc: incs }); return res.modifiedCount; }); } async decrement(collection, decs, where = {}) { const incs = {}; for (const k in decs) incs[k] = -decs[k]; return this.increment(collection, incs, where); } async close() { if (this.client) await this.client.close(); this._isConnected = false; } // Helper: _id handling and query formatting _formatQuery(where) { if (!where) return {}; const query = { ...where }; if (query._id && typeof query._id === 'string' && mongodb_1.ObjectId.isValid(query._id)) { query._id = new mongodb_1.ObjectId(query._id); } return query; } _serialize(doc) { if (!doc) return doc; const { _id, ...rest } = doc; // _id'yi string olarak döndür, ZeroHelper standardı return { _id: _id.toString(), ...rest }; } } exports.MongoDBDatabase = MongoDBDatabase; exports.default = MongoDBDatabase;