mongo-base-crud
Version:
Class to handler access and handler database
135 lines • 6.1 kB
JavaScript
"use strict";
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 });
const vitest_1 = require("vitest");
const crypto_1 = require("crypto");
const faker_1 = require("@faker-js/faker");
const _1 = require(".");
const createdData = new Map();
function createRandomData(key) {
if (!createdData.has(key)) {
createdData.set(key, {
id: (0, crypto_1.randomUUID)(),
name: faker_1.faker.person.firstName(),
age: Math.round(Math.random() * 45),
});
}
return createdData.get(key);
}
function wait(time) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
setTimeout(resolve, time * 1000);
});
});
}
const config = {
prefixName: "test_",
fullUrl: "mongodb://localhost:27017",
database: "test",
};
const dbAccess = _1.MongoDbAccess.getInstance("test_a", "test", undefined, 9, config);
(0, vitest_1.describe)("Mongo", () => {
(0, vitest_1.it)("should add item without pass id and need to create one", () => __awaiter(void 0, void 0, void 0, function* () {
const x = createRandomData("x");
const instance = yield dbAccess;
const insertResultX = yield instance.insert({ name: x.name });
(0, vitest_1.expect)(insertResultX.id).not.toBe(null);
}), 5000);
(0, vitest_1.it)("should add itens on first collection", () => __awaiter(void 0, void 0, void 0, function* () {
const a = createRandomData("a");
const b = createRandomData("b");
const instance = yield dbAccess;
const insertResultA = yield instance.insert(a);
(0, vitest_1.expect)(insertResultA.id).toBe(a.id);
const insertResultB = yield instance.insert(b);
(0, vitest_1.expect)(insertResultB.id).toBe(b.id);
}), 5000);
(0, vitest_1.it)("should find by id", () => __awaiter(void 0, void 0, void 0, function* () {
const b = createRandomData("b");
const instance = yield dbAccess;
const bById = yield instance.getById(b.id);
(0, vitest_1.expect)(bById === null || bById === void 0 ? void 0 : bById.id).toBe(b.id);
}), 5000);
(0, vitest_1.it)("should find saved itens", () => __awaiter(void 0, void 0, void 0, function* () {
const a = createRandomData("a");
const instance = yield dbAccess;
const findResult = yield instance.find({
filter: {
id: a.id,
name: a.name,
},
});
(0, vitest_1.expect)(findResult.total).toBeGreaterThan(0);
(0, vitest_1.expect)(findResult.list[0].id).toBe(a.id);
}));
(0, vitest_1.it)("should find itens without filter", () => __awaiter(void 0, void 0, void 0, function* () {
const a = createRandomData("a");
const instance = yield dbAccess;
const findResult = yield instance.find({});
(0, vitest_1.expect)(findResult.total).toBeGreaterThan(1);
}));
(0, vitest_1.it)("should update Data using same id", () => __awaiter(void 0, void 0, void 0, function* () {
const a = createRandomData("a");
a.name = "Name changed";
const instance = yield dbAccess;
const insertResult = yield instance.insert(a);
const aById = yield instance.getById(a.id);
(0, vitest_1.expect)(aById === null || aById === void 0 ? void 0 : aById.name).toBe(a.name);
}));
(0, vitest_1.it)("should update partial data using same id", () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const a = {
details: {
name: "Name",
age: 18,
},
};
const instance = yield dbAccess;
const { id } = yield instance.insert(a);
const newAge = 19;
yield instance.partialUpdate(id, { details: { age: newAge } });
const aById = yield instance.getById(id);
(0, vitest_1.expect)((_a = aById === null || aById === void 0 ? void 0 : aById.details) === null || _a === void 0 ? void 0 : _a.name).toBe(a.details.name);
(0, vitest_1.expect)((_b = aById === null || aById === void 0 ? void 0 : aById.details) === null || _b === void 0 ? void 0 : _b.age).toBe(newAge);
}));
(0, vitest_1.it)("should find using part of details.name", () => __awaiter(void 0, void 0, void 0, function* () {
const instance = yield dbAccess;
const filter = {
searchValue: "change",
searchFields: ["details.name"],
};
const filterResult = yield instance.find(filter);
(0, vitest_1.expect)(filterResult.total).toBeGreaterThan(0);
}));
(0, vitest_1.it)("should aggregate data", () => __awaiter(void 0, void 0, void 0, function* () {
const instance = yield dbAccess;
const filter = [
{
$group: {
_id: "$details.name",
count: { $sum: 1 },
},
},
{
$project: {
_id: 0,
name: "$_id",
count: 1,
},
},
];
const aggregateResult = yield instance.aggregate(filter);
(0, vitest_1.expect)(aggregateResult.length).toBeGreaterThan(0);
(0, vitest_1.expect)(aggregateResult[0].count).toBeGreaterThan(0);
}));
});
//# sourceMappingURL=mongo.test.js.map