mongo-base-crud
Version:
Class to handler access and handler database
107 lines • 4.34 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCrud = void 0;
const typescript_singleton_1 = require("typescript-singleton");
const index_js_1 = require("./database/mongo/index.js");
class BaseCrud {
static getInstance(collectionName, dbName, indexes = {}, connectionTryingTimes = 1, defaultConfig) {
return typescript_singleton_1.Singleton.getInstance(`${dbName}_${collectionName}`, BaseCrud, collectionName, dbName, indexes, connectionTryingTimes, defaultConfig);
}
constructor(collectionName, dbName, indexes = {}, connectionTryingTimes = 1, defaultConfig) {
this.collectionName = collectionName;
this.dbName = dbName;
if (!this.collectionName) {
throw new Error("Collection name is required");
}
if (!this.dbName) {
throw new Error("Database name is required");
}
this.dbInterface = index_js_1.MongoDbAccess.getInstance(this.collectionName, this.dbName, indexes, connectionTryingTimes, defaultConfig);
}
save(data) {
return __awaiter(this, void 0, void 0, function* () {
if (data.hasOwnProperty("updatedAt")) {
data.updatedAt = new Date();
}
if (data.hasOwnProperty("createdAt") && !data["createdAt"]) {
data.createdAt = new Date();
}
const result = yield (yield this.dbInterface).insert(data);
return result;
});
}
update(data) {
return __awaiter(this, void 0, void 0, function* () {
if (data.hasOwnProperty("updatedAt")) {
data.updatedAt = new Date();
}
const result = yield (yield this.dbInterface).update(data);
return result;
});
}
partialUpdate(id, data) {
return __awaiter(this, void 0, void 0, function* () {
if (data.hasOwnProperty("updatedAt")) {
data.updatedAt = new Date();
}
const result = yield (yield this.dbInterface).partialUpdate(id, data);
return result;
});
}
getById(id) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield (yield this.dbInterface).getById(id);
return result;
});
}
find(filter_1, select_1) {
return __awaiter(this, arguments, void 0, function* (filter, select, skip = 0, limit = 10, orderBy, direction, searchValue, searchFields) {
const result = yield (yield this.dbInterface).find({
filter,
select,
offset: {
skip,
limit,
},
orderBy,
direction,
searchValue,
searchFields,
});
return result;
});
}
findAll(filter, select, orderBy, direction, searchValue, searchFields) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.dbInterface).findAll({
filter,
select,
orderBy,
direction,
searchValue,
searchFields,
});
});
}
aggregate(query) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.dbInterface).aggregate(query);
});
}
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.dbInterface).delete(id);
});
}
}
exports.BaseCrud = BaseCrud;
//# sourceMappingURL=index.js.map
;