@syngrisi/syngrisi
Version:
Syngrisi - Visual Testing Tool
159 lines (155 loc) • 5.67 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/server/models/plugins/index.ts
var plugins_exports = {};
__export(plugins_exports, {
paginate: () => paginate_plugin_default,
paginateDistinct: () => paginateDistinct_plugin_default,
toJSON: () => toJSON_plugin_default
});
module.exports = __toCommonJS(plugins_exports);
// 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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
paginate,
paginateDistinct,
toJSON
});
//# sourceMappingURL=index.js.map