@brontosaurus/db
Version:
:ocean: Schema for brontosaurus
78 lines (77 loc) • 3.71 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.getAttemptsByQueryLean = exports.getAttemptsByQuery = exports.getSelectedAccountAttemptPages = exports.getAttemptCountByAccount = exports.getAttemptsByAccountAndPage = exports.getAttemptsByAccount = exports.getAttemptByAttemptIdentifier = exports.getAttemptById = exports.createUnsavedAttempt = void 0;
const random_1 = require("@sudoo/random");
const attempt_1 = require("../model/attempt");
const createUnsavedAttempt = (config) => {
const identifier = random_1.randomUnique();
return new attempt_1.AttemptModel(Object.assign(Object.assign({}, config), { at: new Date(), identifier }));
};
exports.createUnsavedAttempt = createUnsavedAttempt;
const getAttemptById = (attemptId) => __awaiter(void 0, void 0, void 0, function* () {
const attempt = yield attempt_1.AttemptModel.findOne({
_id: attemptId,
});
return attempt;
});
exports.getAttemptById = getAttemptById;
const getAttemptByAttemptIdentifier = (identifier) => __awaiter(void 0, void 0, void 0, function* () {
const attempt = yield attempt_1.AttemptModel.findOne({
identifier,
});
return attempt;
});
exports.getAttemptByAttemptIdentifier = getAttemptByAttemptIdentifier;
const getAttemptsByAccount = (account) => __awaiter(void 0, void 0, void 0, function* () {
const attempts = yield attempt_1.AttemptModel.find({
account,
});
return attempts;
});
exports.getAttemptsByAccount = getAttemptsByAccount;
const getAttemptsByAccountAndPage = (account, limit, page) => __awaiter(void 0, void 0, void 0, function* () {
if (page < 0) {
return [];
}
if (limit < 1) {
return [];
}
const attempts = yield attempt_1.AttemptModel.find({
account,
}).skip(page * limit).limit(limit).sort({ _id: -1 });
return attempts;
});
exports.getAttemptsByAccountAndPage = getAttemptsByAccountAndPage;
const getAttemptCountByAccount = (account) => __awaiter(void 0, void 0, void 0, function* () {
return (yield attempt_1.AttemptModel.countDocuments({
account,
}));
});
exports.getAttemptCountByAccount = getAttemptCountByAccount;
const getSelectedAccountAttemptPages = (account, limit) => __awaiter(void 0, void 0, void 0, function* () {
if (limit <= 0) {
return Infinity;
}
const count = yield attempt_1.AttemptModel.countDocuments({
account,
});
return Math.ceil(count / limit);
});
exports.getSelectedAccountAttemptPages = getSelectedAccountAttemptPages;
const getAttemptsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () {
return yield attempt_1.AttemptModel.find(query);
});
exports.getAttemptsByQuery = getAttemptsByQuery;
const getAttemptsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () {
return yield attempt_1.AttemptModel.find(query).lean();
});
exports.getAttemptsByQueryLean = getAttemptsByQueryLean;