@hemia/db-connector 0.0.2 → 0.0.4
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
export
|
|
1
|
+
import mongoose__default from 'mongoose';
|
|
2
|
+
export * from 'mongoose';
|
|
3
3
|
import { ValidationError, DatabaseError, AccessDeniedError, Sequelize } from 'sequelize';
|
|
4
4
|
export { DataTypes, Model as ModelSequelize, Op, QueryTypes, Sequelize, Transaction, fn } from 'sequelize';
|
|
5
5
|
|
|
@@ -65,7 +65,7 @@ class MongoDBConnector extends NoSQLConnector {
|
|
|
65
65
|
this.currentSession = null;
|
|
66
66
|
this.config = param;
|
|
67
67
|
this.provider = DBNoSQLType.MongoDB;
|
|
68
|
-
this.mongooseConnection =
|
|
68
|
+
this.mongooseConnection = mongoose__default.connection;
|
|
69
69
|
}
|
|
70
70
|
connect() {
|
|
71
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -73,13 +73,13 @@ class MongoDBConnector extends NoSQLConnector {
|
|
|
73
73
|
if (this.mongooseConnection.readyState === 1) {
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
|
-
yield
|
|
76
|
+
yield mongoose__default.connect(`mongodb://${this.config.host}:${this.config.port || 27017}`, {
|
|
77
77
|
user: this.config.user,
|
|
78
78
|
pass: this.config.password,
|
|
79
79
|
dbName: this.config.database,
|
|
80
80
|
authSource: this.config.authSource
|
|
81
81
|
});
|
|
82
|
-
this.mongooseConnection =
|
|
82
|
+
this.mongooseConnection = mongoose__default.connection;
|
|
83
83
|
console.log('Connected to MongoDB');
|
|
84
84
|
}
|
|
85
85
|
catch (error) {
|
|
@@ -278,6 +278,97 @@ class MongoDBConnector extends NoSQLConnector {
|
|
|
278
278
|
}
|
|
279
279
|
});
|
|
280
280
|
}
|
|
281
|
+
executeWithModel(model, operation, session) {
|
|
282
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
283
|
+
try {
|
|
284
|
+
return yield operation(model, session);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
console.error('Error en operación personalizada:', error);
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
updateMany(model, query, updateData, session) {
|
|
293
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
294
|
+
try {
|
|
295
|
+
const result = yield model.updateMany(query, updateData, { session });
|
|
296
|
+
return { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
console.error('Error al actualizar documentos:', error);
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
deleteMany(model, query, session) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
try {
|
|
307
|
+
const result = yield model.deleteMany(query, { session });
|
|
308
|
+
return { deletedCount: result.deletedCount };
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
console.error('Error al eliminar múltiples documentos:', error);
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
countDocuments(model, query) {
|
|
317
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
318
|
+
try {
|
|
319
|
+
return yield model.countDocuments(query);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
console.error('Error al contar documentos:', error);
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
distinct(model, field, query) {
|
|
328
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
329
|
+
try {
|
|
330
|
+
return yield model.distinct(field, query);
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
console.error('Error al obtener valores distintos:', error);
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
insertMany(model, docs, session) {
|
|
339
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
340
|
+
try {
|
|
341
|
+
return yield model.insertMany(docs, { session });
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
console.error('Error al insertar múltiples documentos:', error);
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
exists(model, query) {
|
|
350
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
351
|
+
try {
|
|
352
|
+
const result = yield model.exists(query);
|
|
353
|
+
return result !== null;
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
console.error('Error al verificar existencia:', error);
|
|
357
|
+
throw error;
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
bulkWrite(model, operations, session) {
|
|
362
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
363
|
+
try {
|
|
364
|
+
return yield model.bulkWrite(operations, { session });
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
console.error('Error en operación bulk:', error);
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
281
372
|
}
|
|
282
373
|
|
|
283
374
|
class NoSQLConnectionManager {
|
|
@@ -278,6 +278,97 @@ class MongoDBConnector extends NoSQLConnector {
|
|
|
278
278
|
}
|
|
279
279
|
});
|
|
280
280
|
}
|
|
281
|
+
executeWithModel(model, operation, session) {
|
|
282
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
283
|
+
try {
|
|
284
|
+
return yield operation(model, session);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
console.error('Error en operación personalizada:', error);
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
updateMany(model, query, updateData, session) {
|
|
293
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
294
|
+
try {
|
|
295
|
+
const result = yield model.updateMany(query, updateData, { session });
|
|
296
|
+
return { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
console.error('Error al actualizar documentos:', error);
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
deleteMany(model, query, session) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
try {
|
|
307
|
+
const result = yield model.deleteMany(query, { session });
|
|
308
|
+
return { deletedCount: result.deletedCount };
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
console.error('Error al eliminar múltiples documentos:', error);
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
countDocuments(model, query) {
|
|
317
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
318
|
+
try {
|
|
319
|
+
return yield model.countDocuments(query);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
console.error('Error al contar documentos:', error);
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
distinct(model, field, query) {
|
|
328
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
329
|
+
try {
|
|
330
|
+
return yield model.distinct(field, query);
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
console.error('Error al obtener valores distintos:', error);
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
insertMany(model, docs, session) {
|
|
339
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
340
|
+
try {
|
|
341
|
+
return yield model.insertMany(docs, { session });
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
console.error('Error al insertar múltiples documentos:', error);
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
exists(model, query) {
|
|
350
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
351
|
+
try {
|
|
352
|
+
const result = yield model.exists(query);
|
|
353
|
+
return result !== null;
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
console.error('Error al verificar existencia:', error);
|
|
357
|
+
throw error;
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
bulkWrite(model, operations, session) {
|
|
362
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
363
|
+
try {
|
|
364
|
+
return yield model.bulkWrite(operations, { session });
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
console.error('Error en operación bulk:', error);
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
281
372
|
}
|
|
282
373
|
|
|
283
374
|
class NoSQLConnectionManager {
|
|
@@ -568,18 +659,6 @@ class DBConnector {
|
|
|
568
659
|
}
|
|
569
660
|
DBConnector.instance = null;
|
|
570
661
|
|
|
571
|
-
Object.defineProperty(exports, "Document", {
|
|
572
|
-
enumerable: true,
|
|
573
|
-
get: function () { return mongoose.Document; }
|
|
574
|
-
});
|
|
575
|
-
Object.defineProperty(exports, "Model", {
|
|
576
|
-
enumerable: true,
|
|
577
|
-
get: function () { return mongoose.Model; }
|
|
578
|
-
});
|
|
579
|
-
Object.defineProperty(exports, "Schema", {
|
|
580
|
-
enumerable: true,
|
|
581
|
-
get: function () { return mongoose.Schema; }
|
|
582
|
-
});
|
|
583
662
|
Object.defineProperty(exports, "DataTypes", {
|
|
584
663
|
enumerable: true,
|
|
585
664
|
get: function () { return sequelize.DataTypes; }
|
|
@@ -613,3 +692,9 @@ exports.DBError = DBError;
|
|
|
613
692
|
exports.MySQLConnectionError = MySQLConnectionError;
|
|
614
693
|
exports.NoSQLConnector = NoSQLConnector;
|
|
615
694
|
exports.SqlConnector = SqlConnector;
|
|
695
|
+
Object.keys(mongoose).forEach(function (k) {
|
|
696
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
697
|
+
enumerable: true,
|
|
698
|
+
get: function () { return mongoose[k]; }
|
|
699
|
+
});
|
|
700
|
+
});
|
|
@@ -27,4 +27,17 @@ export declare class MongoDBConnector extends NoSQLConnector {
|
|
|
27
27
|
deleteById<T extends Document>(model: Model<T>, id: string, session?: ClientSession): Promise<boolean>;
|
|
28
28
|
findAllFromMultipleModels<U extends Document>(models: Array<Model<U>>, filter: object): Promise<U[]>;
|
|
29
29
|
aggregate<T extends Document>(model: Model<T>, pipeline: mongoose.PipelineStage[], options?: AggregateOptions): Promise<any[]>;
|
|
30
|
+
executeWithModel<T extends Document, R>(model: Model<T>, operation: (model: Model<T>, session?: ClientSession) => Promise<R>, session?: ClientSession): Promise<R>;
|
|
31
|
+
updateMany<T extends Document>(model: Model<T>, query: object, updateData: object, session?: ClientSession): Promise<{
|
|
32
|
+
matchedCount: number;
|
|
33
|
+
modifiedCount: number;
|
|
34
|
+
}>;
|
|
35
|
+
deleteMany<T extends Document>(model: Model<T>, query: object, session?: ClientSession): Promise<{
|
|
36
|
+
deletedCount: number;
|
|
37
|
+
}>;
|
|
38
|
+
countDocuments<T extends Document>(model: Model<T>, query: object): Promise<number>;
|
|
39
|
+
distinct<T extends Document>(model: Model<T>, field: string, query?: object): Promise<any[]>;
|
|
40
|
+
insertMany<T extends Document>(model: Model<T>, docs: Partial<T>[], session?: ClientSession): Promise<T[]>;
|
|
41
|
+
exists<T extends Document>(model: Model<T>, query: object): Promise<boolean>;
|
|
42
|
+
bulkWrite<T extends Document>(model: Model<T>, operations: any[], session?: ClientSession): Promise<any>;
|
|
30
43
|
}
|
|
@@ -37,4 +37,17 @@ export declare abstract class NoSQLConnector {
|
|
|
37
37
|
abstract findAllFromMultipleModels<U extends Document>(models: Array<Model<U>>, filter: object): Promise<U[]>;
|
|
38
38
|
abstract aggregate<T>(model: any, pipeline: PipelineStage[]): Promise<any[]>;
|
|
39
39
|
abstract aggregate<T extends Document>(model: Model<T>, pipeline: PipelineStage[], options?: AggregateOptions): Promise<any[]>;
|
|
40
|
+
abstract executeWithModel<T extends Document, R>(model: Model<T>, operation: (model: Model<T>, session?: ClientSession) => Promise<R>, session?: ClientSession): Promise<R>;
|
|
41
|
+
abstract exists<T extends Document>(model: Model<T>, query: object): Promise<boolean>;
|
|
42
|
+
abstract updateMany<T extends Document>(model: Model<T>, query: object, updateData: object, session?: ClientSession): Promise<{
|
|
43
|
+
matchedCount: number;
|
|
44
|
+
modifiedCount: number;
|
|
45
|
+
}>;
|
|
46
|
+
abstract deleteMany<T extends Document>(model: Model<T>, query: object, session?: ClientSession): Promise<{
|
|
47
|
+
deletedCount: number;
|
|
48
|
+
}>;
|
|
49
|
+
abstract countDocuments<T extends Document>(model: Model<T>, query: object): Promise<number>;
|
|
50
|
+
abstract distinct<T extends Document>(model: Model<T>, field: string, query?: object): Promise<any[]>;
|
|
51
|
+
abstract insertMany<T extends Document>(model: Model<T>, docs: Partial<T>[], session?: ClientSession): Promise<T[]>;
|
|
52
|
+
abstract bulkWrite<T extends Document>(model: Model<T>, operations: any[], session?: ClientSession): Promise<any>;
|
|
40
53
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { DBNoSQLType } from "./types/DBNoSQLTypes";
|
|
|
7
7
|
export { NoSQLConnector } from "./abstract/NoSQLConnector";
|
|
8
8
|
export { SqlConnector, Filter } from "./abstract/SQLConnector";
|
|
9
9
|
export { NoSQLOptions } from "./types/NoSQLOptions";
|
|
10
|
-
export
|
|
10
|
+
export * from 'mongoose';
|
|
11
11
|
export { Model as ModelSequelize, Sequelize, DataTypes, CreateOptions, UpdateOptions, DestroyOptions, FindOptions, Attributes, WhereOptions, QueryOptions, InferAttributes, CreationAttributes, InferCreationAttributes, QueryTypes, QueryOptionsWithType, Transaction, Op, fn } from "sequelize";
|
|
12
12
|
export { DBError } from "./errors/DBError";
|
|
13
13
|
export { MySQLConnectionError } from "./errors/MySQLConnectionError";
|