UNPKG

@syngrisi/syngrisi

Version:
248 lines (243 loc) 7.81 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/server/models/Test.model.ts var Test_model_exports = {}; __export(Test_model_exports, { default: () => Test_model_default }); module.exports = __toCommonJS(Test_model_exports); var import_mongoose = __toESM(require("mongoose")); // src/server/models/plugins/paginate.plugin.ts var paginate = (schema) => { schema.statics.paginate = async function(filter, options) { let sort; if (options.sortBy) { const sortingCriteria = []; options.sortBy.split(",").forEach((sortOption) => { const [key, order] = sortOption.split(":"); sortingCriteria.push((order === "desc" ? "-" : "") + key); }); sort = sortingCriteria.join(" "); } else { sort = { _id: -1 }; } const limit = options.limit && parseInt(options.limit.toString(), 10) >= 0 ? parseInt(options.limit.toString(), 10) : 10; const page = options.page && parseInt(options.page.toString(), 10) > 0 ? parseInt(options.page.toString(), 10) : 1; const skip = (page - 1) * limit; const countPromise = this.countDocuments(filter).exec(); let docsPromise = this.find(filter).sort(sort).skip(skip).limit(limit); if (options.populate) { options.populate.split(",").forEach((populateOption) => { docsPromise = docsPromise.populate( populateOption.split(".").reverse().reduce((a, b) => ({ path: b, populate: a })) ); }); } docsPromise = docsPromise.exec(); return Promise.all([countPromise, docsPromise]).then((values) => { const [totalResults, results] = values; const totalPages = Math.ceil(totalResults / limit); const result = { results, page, limit, totalPages, totalResults, timestamp: Number(Date.now() + String(process.hrtime()[1]).slice(3, 6)) }; return Promise.resolve(result); }); }; }; var paginate_plugin_default = paginate; // src/server/models/plugins/toJSON.plugin.ts var deleteAtPath = (obj, path, index) => { if (index === path.length - 1) { delete obj[path[index]]; return; } deleteAtPath(obj[path[index]], path, index + 1); }; var toJSON = (schema) => { let transform; if (schema.options.toJSON && schema.options.toJSON.transform) { transform = schema.options.toJSON.transform; } schema.options.toJSON = Object.assign(schema.options.toJSON || {}, { transform(doc, ret, options) { Object.keys(schema.paths).forEach((path) => { if (schema.paths[path].options && schema.paths[path].options.private) { deleteAtPath(ret, path.split("."), 0); } }); ret.id = ret._id.toString(); delete ret.__v; delete ret.createdAt; delete ret.updatedAt; if (transform) { return transform(doc, ret, options); } } }); }; var toJSON_plugin_default = toJSON; // src/server/models/plugins/paginateDistinct.plugin.ts var import_bson = require("bson"); var paginateDistinct = (schema) => { schema.statics.paginateDistinct = async function(filter, options) { let sort; if (options.sortBy) { options.sortBy.split(",").forEach((sortOption) => { const [key, order] = sortOption.split(":"); sort[key] = order === "desc" ? -1 : 1; }); } else { sort = { _id: -1 }; } let limit = options.limit && parseInt(options.limit.toString(), 10) >= 0 ? parseInt(options.limit.toString(), 10) : 10; limit = limit === 0 ? 9007199254740991 : limit; const page = options.page && parseInt(options.page.toString(), 10) > 0 ? parseInt(options.page.toString(), 10) : 1; const skip = (page - 1) * limit; const groupAggregateObj = { $group: { _id: `$${options.field}` } }; const documentsCount = (await this.aggregate([groupAggregateObj]).exec()).length; const aggregateArr = [ { $match: import_bson.EJSON.parse(filter.filter || "{}") }, groupAggregateObj, { $sort: sort }, { $skip: skip }, { $limit: limit } ]; const aggregatedDocs = (await this.aggregate(aggregateArr)).filter((x) => x._id).map((x) => { if (x[options.field]) { return x[options.field][0]; } return { name: x._id }; }); return Promise.all([documentsCount, aggregatedDocs]).then((values) => { const [totalResults, results] = values; const totalPages = Math.ceil(totalResults / limit); const result = { results, page, limit, totalPages, totalResults, timestamp: (/* @__PURE__ */ new Date()).getTime() }; return Promise.resolve(result); }); }; }; var paginateDistinct_plugin_default = paginateDistinct; // src/server/models/Test.model.ts var TestSchema = new import_mongoose.Schema( { name: { type: String, required: "TestSchema: the test name is empty" }, description: { type: String }, status: { type: String }, browserName: { type: String }, browserVersion: { type: String }, branch: { type: String }, tags: { type: [String] }, viewport: { type: String }, calculatedViewport: { type: String }, os: { type: String }, app: { type: import_mongoose.Schema.Types.ObjectId, ref: "VRSApp", required: [true, 'TestSchema: The "app" field must be required'] }, blinking: { type: Number, default: 0 }, updatedDate: { type: Date }, startDate: { type: Date }, checks: [ { type: import_mongoose.default.Schema.Types.ObjectId, ref: "VRSCheck" } ], suite: { type: import_mongoose.Schema.Types.ObjectId, ref: "VRSSuite" }, run: { type: import_mongoose.Schema.Types.ObjectId, ref: "VRSRun" }, markedAs: { type: String, enum: ["Bug", "Accepted", "Unaccepted", "Partially"] }, creatorId: { type: import_mongoose.Schema.Types.ObjectId, ref: "VRSUser" }, creatorUsername: { type: String }, meta: { type: Object } }, { strictQuery: true } ); TestSchema.plugin(toJSON_plugin_default); TestSchema.plugin(paginate_plugin_default); TestSchema.plugin(paginateDistinct_plugin_default); var Test = import_mongoose.default.model("VRSTest", TestSchema); var Test_model_default = Test; //# sourceMappingURL=Test.model.js.map