@cyberskill/shared 2.13.0 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/vitest/vitest.e2e.cjs +1 -1
- package/dist/config/vitest/vitest.e2e.js +1 -1
- package/dist/config/vitest/vitest.unit.cjs +1 -1
- package/dist/config/vitest/vitest.unit.js +1 -1
- package/dist/node/mongo/mongo.util.cjs +4 -4
- package/dist/node/mongo/mongo.util.d.ts +1 -1
- package/dist/node/mongo/mongo.util.js +793 -458
- package/dist/node/ws/ts.util.cjs +1 -1
- package/dist/node/ws/ts.util.js +64 -11
- package/dist/node/ws/ws.type.d.ts +11 -1
- package/package.json +24 -24
- /package/dist/node_modules/.pnpm/{vitest@3.2.4_@types_debug@4.1.12_@types_node@24.1.0_@vitest_browser@3.2.4_jiti@2.5.1_js_7b82ece51006a4f428642448fb1b082c → vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13}/node_modules/vitest/dist/config.cjs +0 -0
- /package/dist/node_modules/.pnpm/{vitest@3.2.4_@types_debug@4.1.12_@types_node@24.1.0_@vitest_browser@3.2.4_jiti@2.5.1_js_7b82ece51006a4f428642448fb1b082c → vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13}/node_modules/vitest/dist/config.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@vitejs/plugin-react-swc"),i=require("lodash-es"),o=require("../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@vitejs/plugin-react-swc"),i=require("lodash-es"),o=require("../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13/node_modules/vitest/dist/config.cjs");function s(e){const r={plugins:[t()],test:{include:["**/*.test.e2e.?(c|m)[jt]s?(x)"],browser:{enabled:!0,provider:"playwright",instances:[{browser:"chromium"},{browser:"firefox"},{browser:"webkit"}]}}};return o.defineConfig(i.merge(r,e))}exports.vitestE2E=s;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import t from "@vitejs/plugin-react-swc";
|
|
2
2
|
import { merge as o } from "lodash-es";
|
|
3
|
-
import { defineConfig as i } from "../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.
|
|
3
|
+
import { defineConfig as i } from "../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13/node_modules/vitest/dist/config.js";
|
|
4
4
|
function f(r) {
|
|
5
5
|
const e = {
|
|
6
6
|
plugins: [t()],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@vitejs/plugin-react-swc"),n=require("lodash-es"),s=require("../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@vitejs/plugin-react-swc"),n=require("lodash-es"),s=require("../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13/node_modules/vitest/dist/config.cjs");function r(e){const t={plugins:[i()],test:{globals:!0,environment:"jsdom",pool:"vmThreads",include:["**/*.test.unit.?(c|m)[jt]s?(x)"],setupFiles:["./vitest.unit.setup.ts"]}};return s.defineConfig(n.merge(t,e))}exports.vitestUnit=r;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import i from "@vitejs/plugin-react-swc";
|
|
2
2
|
import { merge as o } from "lodash-es";
|
|
3
|
-
import { defineConfig as n } from "../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.
|
|
3
|
+
import { defineConfig as n } from "../../node_modules/.pnpm/vitest@3.2.4_@types_debug@4.1.12_@types_node@24.3.0_@vitest_browser@3.2.4_jiti@2.5.1_js_bae9c6abdb9bf325bd1b44dc7dc11e13/node_modules/vitest/dist/config.js";
|
|
4
4
|
function u(t) {
|
|
5
5
|
const e = {
|
|
6
6
|
plugins: [i()],
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
module.exports = ${JSON.stringify(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U=require("lodash-es"),J=require("migrate-mongo"),X=require("mongoose-aggregate-paginate-v2"),Y=require("mongoose-paginate-v2"),k=require("uuid"),Z=require("./mongo.constant.cjs"),F=require("../../util/object/object.util.cjs"),tt=require("../../util/common/common.util.cjs"),$=require("../fs/fs.util.cjs"),T=require("../path/path.constant.cjs"),et=require("../../util/validate/validate.util.cjs"),C=require("../../util/string/string.util.cjs"),V=require("../../constant/response-status.cjs"),N=require("../log/log.util.cjs");var rt=Object.defineProperty,st=Object.defineProperties,nt=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertySymbols,it=Object.prototype.hasOwnProperty,ot=Object.prototype.propertyIsEnumerable,L=(n,e,t)=>e in n?rt(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,A=(n,e)=>{for(var t in e||(e={}))it.call(e,t)&&L(n,t,e[t]);if(B)for(var t of B(e))ot.call(e,t)&&L(n,t,e[t]);return n},x=(n,e)=>st(n,nt(e)),lt=(n,e,t)=>L(n,typeof e!="symbol"?e+"":e,t),v=(n,e,t)=>new Promise((r,s)=>{var i=c=>{try{l(t.next(c))}catch(a){s(a)}},o=c=>{try{l(t.throw(c))}catch(a){s(a)}},l=c=>c.done?r(c.value):Promise.resolve(c.value).then(i,o);l((t=t.apply(n,e)).next())});function M(n){return n===n.toUpperCase()?n.charAt(0).toUpperCase()+n.slice(1).toLowerCase():n}const P={createGenericFields(){return{id:k.v4(),isDel:!1,createdAt:new Date,updatedAt:new Date}},applyPlugins(n,e){e.filter(t=>typeof t=="function").forEach(t=>n.plugin(t))},applyMiddlewares(n,e){e.forEach(({method:t,pre:r,post:s})=>{t&&r&&n.pre(t,r),t&&s&&n.post(t,s)})},createGenericSchema(n){return new n.Schema({id:{type:String,default:k.v4,unique:!0},isDel:{type:Boolean,default:!1}},{timestamps:!0})},createSchema({mongoose:n,schema:e,virtuals:t=[],standalone:r=!1}){const s=new n.Schema(e,{toJSON:{virtuals:!0},toObject:{virtuals:!0}});return t.forEach(({name:i,options:o,get:l})=>{if(P.isDynamicVirtual(o)){const c=s.statics;c._dynamicVirtuals||(c._dynamicVirtuals=[]),c._dynamicVirtuals.push({name:i,options:o});const a=s.virtual(i);l?a.get(l):a.get(function(){var u;return((u=this._populated)==null?void 0:u[i])||(o!=null&&o.count?0:o!=null&&o.justOne?null:[])})}else{const c=s.virtual(i,o);l&&c.get(l)}}),r||s.add(P.createGenericSchema(n)),s},createModel({mongoose:n,name:e,schema:t,virtuals:r=[],pagination:s=!0,aggregate:i=!0,middlewares:o=[]}){if(!e)throw new Error("Model name is required.");if(n.models[e])return n.models[e];const l=P.createSchema({mongoose:n,schema:t,virtuals:r});(s||i)&&P.applyPlugins(l,[s&&Y,i&&X]),P.applyMiddlewares(l,o);const c=n.model(e,l);return r.length>0&&(c._virtualConfigs=r),c},validator:{isRequired(){return function(n){return v(this,null,function*(){return!et.validate.isEmpty(n)})}},isUnique(n){return function(e){return v(this,null,function*(){if(!Array.isArray(n)||n.length===0)throw new Error("Fields must be a non-empty array of strings.");const t={$or:n.map(s=>({[s]:e}))};return!(yield this.constructor.exists(t))})}},matchesRegex(n){return function(e){return v(this,null,function*(){if(!Array.isArray(n)||n.some(t=>!(t instanceof RegExp)))throw new Error("regexArray must be an array of valid RegExp objects.");return n.every(t=>t.test(e))})}}},migrate:x(A({},J),{setConfig:n=>{const e=`// This file is automatically generated by the Cyberskill CLI.
|
|
2
|
+
module.exports = ${JSON.stringify(n,null,4)}`;$.writeFileSync(T.PATH.MIGRATE_MONGO_CONFIG,e);const t=`
|
|
3
3
|
${T.MIGRATE_MONGO_CONFIG}
|
|
4
|
-
|
|
5
|
-
`).includes(T.MIGRATE_MONGO_CONFIG)||F.appendFileSync(T.PATH.GIT_IGNORE,e):F.writeFileSync(T.PATH.GIT_IGNORE,e)}}),regexify(s,t){if(!s)return{};let e=q.cloneDeep(s);if(!t||t.length===0)return e;for(const r of t){const n=r.toString().split("."),c=b.getNestedValue(e,n);if(typeof c=="string"&&c.length>0){const u={$regex:`.*${H.regexSearchMapper(c)}.*`,$options:"i"};e=b.setNestedValue(e,n,u)}}return e},isDynamicVirtual(s){return!!(s&&typeof s.ref=="function")},getNewRecords(s,t,e){return a(this,null,function*(){const r=yield s.findAll({});return r.success?t.filter(c=>!r.result.some(u=>e(u,c))):t})},getExistingRecords(s,t,e){return a(this,null,function*(){const r=yield s.findAll({});return r.success?r.result.filter(c=>t.some(u=>e(c,u))):[]})}};function I(s,t){if(!s||!t||t.length===0)return s;const e=new Set(t.map(r=>r.name));if(Array.isArray(s)){const r=s.filter(n=>{if(typeof n=="string")return!e.has(n);if(typeof n=="object"&&n!==null){const c=n;return!e.has(c.path||c.populate||"")}return!0});return r.length>0?r:void 0}if(typeof s=="string")return e.has(s)?void 0:s;if(typeof s=="object"&&s!==null){const r=s,n=r.path||r.populate||"";return e.has(n)?void 0:s}return s}function ne(s,t,e){if(!s.length||!t||!(e!=null&&e.ref))return[];const r=new Map;return s.forEach(n=>{try{const c=e.ref(n);if(c==null)return;const u=typeof c=="string"?c:String(c);if(u&&u.trim()!==""){const o=re(u);r.has(o)||r.set(o,[]),r.get(o).push(n)}}catch(c){f.catchError(new Error(`Dynamic ref function failed for virtual "${t}": ${c instanceof Error?c.message:String(c)}`))}}),Array.from(r.entries()).map(([n,c])=>({model:n,docs:c}))}function se(s){return s!==null&&typeof s=="object"&&"toObject"in s&&typeof s.toObject=="function"}function z(s,t,e,r,n){return a(this,null,function*(){if(!t.length||!e.length||!r)return t;const c=e.filter(l=>{if(Array.isArray(r))return r.length>0&&r.some(i=>typeof i=="string"?i===l.name:i.path===l.name);if(typeof r=="string")return r===l.name;if(typeof r=="object"&&r!==null){const i=r;return i.path&&i.path===l.name||i.populate&&i.populate===l.name}return!1});if(c.length===0)return t;const u=q.cloneDeep(t.map(l=>se(l)?l.toObject():l));u.forEach(l=>{c.forEach(({name:i,options:d})=>{i in l||(l[i]=d.count?0:d.justOne?null:[])})});const o=new Map;for(const l of c){const{name:i,options:d}=l,m=ne(u,i,d);for(const h of m){o.has(h.model)||o.set(h.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const g=o.get(h.model);g.virtuals.find(V=>V.name===i)||(g.virtuals.push(l),g.localValueSets.set(i,new Set));const D=g.localValueSets.get(i);h.docs.forEach(V=>{const x=V[d.localField];if(x!=null){const _=String(x);D.add(_);let v=-1;const N=V;N.id!==void 0?v=u.findIndex(A=>A.id===N.id):N._id!==void 0&&(v=u.findIndex(A=>{var S,y,p,E;return((y=(S=A._id)==null?void 0:S.toString)==null?void 0:y.call(S))===((E=(p=N._id)==null?void 0:p.toString)==null?void 0:E.call(p))})),v!==-1&&(g.docsByLocalValue.has(_)||g.docsByLocalValue.set(_,[]),g.docsByLocalValue.get(_).push(v))}})}}return yield Promise.all(Array.from(o.entries()).map(l=>a(null,[l],function*([i,d]){const m=s.models[i];if(!m)return;const h=new Set;if(d.localValueSets.forEach(_=>{_.forEach(v=>h.add(v))}),h.size===0)return;const g=[...new Set(d.virtuals.map(_=>_.options.foreignField))],D=Array.from(h);let V;g.length===1?V={[String(g[0])]:{$in:D}}:V={$or:g.map(_=>({[_]:{$in:D}}))};const x=yield m.find(V,n).lean();for(const _ of d.virtuals){const{name:v,options:N}=_,A=x.filter(S=>{const y=S[N.foreignField];return y!=null&&h.has(String(y))});if(N.count){const S=new Map;A.forEach(y=>{var p;const E=(p=y[N.foreignField])==null?void 0:p.toString();E&&S.set(E,(S.get(E)||0)+1)}),d.localValueSets.get(v).forEach(y=>{const p=d.docsByLocalValue.get(y)||[],E=S.get(y)||0;p.forEach(w=>{const j=u[w];j[v]===void 0&&(j[v]=E)})})}else{const S=new Map;A.forEach(y=>{var p;const E=(p=y[N.foreignField])==null?void 0:p.toString();E&&(S.has(E)||S.set(E,[]),S.get(E).push(y))}),d.localValueSets.get(v).forEach(y=>{const p=d.docsByLocalValue.get(y)||[],E=S.get(y)||[],w=N.justOne?E[0]||null:E;p.forEach(j=>{const L=u[j];L[v]=w})})}}}))),u})}class ce{constructor(t,e){te(this,"collection"),this.collection=t.collection(e)}createOne(t){return a(this,null,function*(){try{const e=O(O({},M.createGenericFields()),t);return(yield this.collection.insertOne(e)).acknowledged?{success:!0,message:"Document created successfully",result:e}:{success:!1,message:"Document creation failed",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(e){return f.catchError(e)}})}createMany(t){return a(this,null,function*(){try{const e=t.map(n=>O(O({},M.createGenericFields()),n)),r=yield this.collection.insertMany(e);return r.insertedCount===0?{success:!1,message:"No documents were inserted",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${r.insertedCount} documents created successfully`,result:e}}catch(e){return f.catchError(e)}})}findOne(t){return a(this,null,function*(){try{const e=yield this.collection.findOne(t);return e?{success:!0,message:"Document found",result:e}:{success:!1,message:"Document not found",code:R.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(e){return f.catchError(e)}})}findAll(){return a(this,arguments,function*(t={}){try{return{success:!0,message:"Documents retrieved successfully",result:yield this.collection.find(t).toArray()}}catch(e){return f.catchError(e)}})}count(){return a(this,arguments,function*(t={}){try{const e=yield this.collection.countDocuments(t);return{success:!0,message:`${e} documents counted successfully`,result:e}}catch(e){return f.catchError(e)}})}updateOne(t,e){return a(this,null,function*(){try{const r=yield this.collection.updateOne(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:r}}catch(r){return f.catchError(r)}})}updateMany(t,e){return a(this,null,function*(){try{const r=yield this.collection.updateMany(t,{$set:e});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:r}}catch(r){return f.catchError(r)}})}deleteOne(t){return a(this,null,function*(){try{const e=yield this.collection.deleteOne(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:e}}catch(e){return f.catchError(e)}})}deleteMany(t){return a(this,null,function*(){try{const e=yield this.collection.deleteMany(t);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:e}}catch(e){return f.catchError(e)}})}}class ue{constructor(t){this.model=t}getModelName(){return this.model.modelName}getDynamicVirtuals(){return this.model.schema.statics._dynamicVirtuals}populateDynamicVirtualsForDocument(t,e){return a(this,null,function*(){const r=this.getDynamicVirtuals();if(r&&r.length>0){const n=yield z(this.model.base,[t],r,e);return n&&n[0]?n[0]:t}return t})}populateDynamicVirtualsForDocuments(t,e){return a(this,null,function*(){const r=this.getDynamicVirtuals();return r&&r.length>0&&t.length>0?yield z(this.model.base,t,r,e):t})}findOne(){return a(this,arguments,function*(t={},e={},r={},n){var c,u;try{const o=b.normalizeMongoFilter(t),l=this.model.findOne(o,e,r),i=this.getDynamicVirtuals(),d=I(n,i);d&&l.populate(d);const m=yield l.exec();if(!m)return{success:!1,message:`No ${this.getModelName()} found.`,code:R.RESPONSE_STATUS.NOT_FOUND.CODE};const h=yield this.populateDynamicVirtualsForDocument(m,n);return{success:!0,result:(u=(c=h==null?void 0:h.toObject)==null?void 0:c.call(h))!=null?u:h}}catch(o){return f.catchError(o)}})}findAll(){return a(this,arguments,function*(t={},e={},r={},n){try{const c=b.normalizeMongoFilter(t),u=this.model.find(c,e,r),o=this.getDynamicVirtuals(),l=I(n,o);l&&u.populate(l);const i=yield u.exec();return{success:!0,result:(yield this.populateDynamicVirtualsForDocuments(i,n)).map(m=>{var h,g;return(g=(h=m==null?void 0:m.toObject)==null?void 0:h.call(m))!=null?g:m})}}catch(c){return f.catchError(c)}})}findPaging(){return a(this,arguments,function*(t={},e={}){try{const r=b.normalizeMongoFilter(t),n=this.getDynamicVirtuals(),c=O({},e);e.populate&&(c.populate=I(e.populate,n));const u=yield this.model.paginate(r,c),o=yield this.populateDynamicVirtualsForDocuments(u.docs,e.populate);return{success:!0,result:P(O({},u),{docs:o.map(l=>{var i,d;return(d=(i=l==null?void 0:l.toObject)==null?void 0:i.call(l))!=null?d:l})})}}catch(r){return f.catchError(r)}})}findPagingAggregate(t){return a(this,arguments,function*(e,r={}){try{const n=this.getDynamicVirtuals(),c=O({},r);r.populate&&(c.populate=I(r.populate,n));const u=yield this.model.aggregatePaginate(this.model.aggregate(e),c),o=yield this.populateDynamicVirtualsForDocuments(u.docs,r.populate);return{success:!0,result:P(O({},u),{docs:o.map(l=>{var i,d;return(d=(i=l==null?void 0:l.toObject)==null?void 0:i.call(l))!=null?d:l})})}}catch(n){return f.catchError(n)}})}count(){return a(this,arguments,function*(t={}){try{const e=b.normalizeMongoFilter(t);return{success:!0,result:yield this.model.countDocuments(e)}}catch(e){return f.catchError(e)}})}createOne(t){return a(this,null,function*(){var e,r;try{const n=yield this.model.create(t);return{success:!0,result:(r=(e=n==null?void 0:n.toObject)==null?void 0:e.call(n))!=null?r:n}}catch(n){return f.catchError(n)}})}createMany(t){return a(this,arguments,function*(e,r={}){try{return{success:!0,result:(yield this.model.insertMany(e,r)).map(c=>{var u,o;return(o=(u=c==null?void 0:c.toObject)==null?void 0:u.call(c))!=null?o:c})}}catch(n){return f.catchError(n)}})}updateOne(){return a(this,arguments,function*(t={},e={},r={}){var n,c;try{const u=b.normalizeMongoFilter(t),o=yield this.model.findOneAndUpdate(u,e,O({new:!0},r)).exec();return o?{success:!0,result:(c=(n=o==null?void 0:o.toObject)==null?void 0:n.call(o))!=null?c:o}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:R.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(u){return f.catchError(u)}})}updateMany(){return a(this,arguments,function*(t={},e={},r={}){try{const n=b.normalizeMongoFilter(t);return{success:!0,result:yield this.model.updateMany(n,e,r).exec()}}catch(n){return f.catchError(n)}})}deleteOne(){return a(this,arguments,function*(t={},e={}){var r,n;try{const c=b.normalizeMongoFilter(t),u=yield this.model.findOneAndDelete(c,e).exec();return u?{success:!0,result:(n=(r=u==null?void 0:u.toObject)==null?void 0:r.call(u))!=null?n:u}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:R.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(c){return f.catchError(c)}})}deleteMany(){return a(this,arguments,function*(t={},e={}){try{const r=b.normalizeMongoFilter(t),n=yield this.model.deleteMany(r,e).exec();return n.deletedCount===0?{success:!1,message:"No documents found to delete.",code:R.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:n}}catch(r){return f.catchError(r)}})}createShortId(t,e=4){return a(this,null,function*(){try{const n=Array.from({length:10},(o,l)=>C.generateShortId(t,l+e)),u=(yield Promise.all(n.map(o=>this.model.exists({shortId:o})))).findIndex(o=>!o);if(u!==-1){const o=n[u];if(o)return{success:!0,result:o}}return{success:!1,message:"Failed to create a unique shortId",code:R.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(r){return f.catchError(r)}})}createSlugQuery({slug:t,field:e,isObject:r,haveHistory:n=!1,filter:c}){const u=O({},c!=null?c:{});return r?P(O({},u),{$or:[{[`slug.${e}`]:t},...n?[{slugHistory:{$elemMatch:{[`slug.${e}`]:t}}}]:[]]}):P(O({},u),{$or:[{slug:t},...n?[{slugHistory:t}]:[]]})}createUniqueSlug(t){return a(this,arguments,function*({slug:e,field:r,isObject:n,haveHistory:c,filter:u}){if(!e||typeof e!="string")throw new Error("Invalid slug provided: must be a non-empty string");const o=C.generateSlug(e);if(!(yield this.model.exists(this.createSlugQuery({slug:o,field:r,isObject:n,haveHistory:c,filter:u}))))return o;for(let m=1;m<=W.MONGO_SLUG_MAX_ATTEMPTS;m++){const h=`${o}-${m}`;if(!(yield this.model.exists(this.createSlugQuery({slug:h,field:r,isObject:n,haveHistory:c,filter:u}))))return h}const i=Date.now(),d=Math.random().toString(36).substring(2,8);return`${o}-${i}-${d}`})}createSlug(t){return a(this,arguments,function*({field:e,from:r,filter:n,haveHistory:c}){try{const u=r[e];return q.isObject(u)?{success:!0,result:Object.fromEntries(yield Promise.all(Object.entries(u).map(d=>a(this,[d],function*([m,h]){const g=yield this.createUniqueSlug({slug:h,field:m,isObject:!0,haveHistory:c,filter:n});return[m,g]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:u,field:e,isObject:!1,haveHistory:c,filter:n})}}catch(u){return f.catchError(u)}})}checkSlug(t){return a(this,arguments,function*({slug:e,field:r,from:n,filter:c,haveHistory:u}){try{const o=n[r];if(q.isObject(o)){const h=Object.values(o).map(D=>C.generateSlug(D));return(yield Promise.all(h.map(D=>this.model.exists(this.createSlugQuery({slug:D,field:r,isObject:!0,haveHistory:u,filter:c}))))).some(D=>D)?{success:!0,result:!0}:{success:!0,result:!1}}const i=C.generateSlug(e);return{success:!0,result:(yield this.model.exists(this.createSlugQuery({slug:i,field:r,isObject:!1,filter:c})))!==null}}catch(o){return f.catchError(o)}})}aggregate(t){return a(this,null,function*(){try{return{success:!0,result:yield this.model.aggregate(t)}}catch(e){return f.catchError(e)}})}distinct(t){return a(this,arguments,function*(e,r={},n={}){try{return{success:!0,result:yield this.model.distinct(e,r,n)}}catch(c){return f.catchError(c)}})}}exports.MongoController=ce;exports.MongooseController=ue;exports.mongo=M;
|
|
4
|
+
`;$.pathExistsSync(T.PATH.GIT_IGNORE)?$.readFileSync(T.PATH.GIT_IGNORE,"utf-8").split(`
|
|
5
|
+
`).includes(T.MIGRATE_MONGO_CONFIG)||$.appendFileSync(T.PATH.GIT_IGNORE,t):$.writeFileSync(T.PATH.GIT_IGNORE,t)}}),regexify(n,e){if(!n)return{};let t=U.cloneDeep(n);if(!e||e.length===0)return t;for(const r of e){const s=r.toString().split("."),i=F.getNestedValue(t,s);if(typeof i=="string"&&i.length>0){const o={$regex:`.*${tt.regexSearchMapper(i)}.*`,$options:"i"};t=F.setNestedValue(t,s,o)}}return t},isDynamicVirtual(n){return!!(n&&typeof n.ref=="function")},getNewRecords(n,e,t){return v(this,null,function*(){const r=yield n.findAll({});return r.success?e.filter(i=>!r.result.some(o=>t(o,i))):e})},getExistingRecords(n,e,t){return v(this,null,function*(){const r=yield n.findAll({});return r.success?r.result.filter(i=>e.some(o=>t(i,o))):[]})}};function q(n,e){if(!n||!e||e.length===0)return n;const t=new Set(e.map(r=>r.name));if(Array.isArray(n)){const r=n.filter(s=>{if(typeof s=="string")return!Array.from(t).some(i=>s===i||s.startsWith(`${i}.`));if(typeof s=="object"&&s!==null){const i=s,o=i.path||i.populate||"";return!Array.from(t).some(l=>o===l||o.startsWith(`${l}.`))}return!0});return r.length>0?r:void 0}if(typeof n=="string")return Array.from(t).some(r=>n===r||n.startsWith(`${r}.`))?void 0:n;if(typeof n=="object"&&n!==null){const r=n,s=r.path||r.populate||"";return Array.from(t).some(i=>s===i||s.startsWith(`${i}.`))?void 0:n}return n}function ct(n,e,t){if(!n.length||!e||!(t!=null&&t.ref))return[];const r=new Map;return n.forEach(s=>{try{const i=t.ref(s);if(i==null)return;const o=typeof i=="string"?i:String(i);if(o&&o.trim()!==""){const l=M(o);r.has(l)||r.set(l,[]),r.get(l).push(s)}}catch(i){N.catchError(new Error(`Dynamic ref function failed for virtual "${e}": ${i instanceof Error?i.message:String(i)}`))}}),Array.from(r.entries()).map(([s,i])=>({model:s,docs:i}))}function ut(n){return n!==null&&typeof n=="object"&&"toObject"in n&&typeof n.toObject=="function"}function W(n,e,t,r,s){return v(this,null,function*(){if(!e.length||!t)return e;const i=Array.isArray(t)?t:[t];for(const o of i)typeof o=="string"?yield Q(n,e,o,r,s):o&&typeof o=="object"&&(yield at(n,e,o,r,s));return e})}function Q(n,e,t,r,s){return v(this,null,function*(){var i,o,l,c;const a=t.split(".");if(a.length<2){for(const y of e)yield z(n,y,t,r,void 0,s);return}const u=a[0];if(!u||u.trim()==="")return;const p=a.slice(1).join(".");for(const y of e){const S=y,E=S[u];if(E&&typeof E=="object"){let g=s;const h=g;if(s&&s.schema&&s.schema.virtuals){const f=s.schema.virtuals[u];if(f&&f.options&&f.options.ref){let O;if(typeof f.options.ref=="function"?O=f.options.ref(S):typeof f.options.ref=="string"&&(O=f.options.ref),O){const d=M(O);n.models[d]&&(g=n.models[d])}}}if(!g){const f=(o=(i=s==null?void 0:s.schema)==null?void 0:i.statics)!=null?o:{},d=((s==null?void 0:s._virtualConfigs)||f._dynamicVirtuals||[]).find(m=>m.name===u);if(d&&d.options&&d.options.ref){let m;if(typeof d.options.ref=="function"?m=d.options.ref(S):typeof d.options.ref=="string"&&(m=d.options.ref),m){const j=M(m);n.models[j]&&(g=n.models[j])}}}if(g===h){const f=(c=(l=s==null?void 0:s.schema)==null?void 0:l.statics)!=null?c:{},d=((s==null?void 0:s._virtualConfigs)||f._dynamicVirtuals||[]).find(m=>m.name===u);if(d&&d.options&&d.options.ref){let m;if(typeof d.options.ref=="function"?m=d.options.ref(S):typeof d.options.ref=="string"&&(m=d.options.ref),m){const j=M(m);n.models[j]&&(g=n.models[j])}}}if(Array.isArray(E))for(const f of E)f&&typeof f=="object"&&(yield z(n,f,p,r,u,g));else E&&typeof E=="object"&&(yield z(n,E,p,r,u,g))}}})}function at(n,e,t,r,s){return v(this,null,function*(){const{path:i,populate:o}=t;if(!i)return;if(!o){yield Q(n,e,i,r,s);return}const l=i;for(const c of e){const a=c,u=a[l];let p=s;if(s&&s.schema&&s.schema.virtuals){const y=s.schema.virtuals[l];if(y&&y.options&&y.options.ref){let S;if(typeof y.options.ref=="function"?S=y.options.ref(a):typeof y.options.ref=="string"&&(S=y.options.ref),S){const E=M(S);n.models[E]&&(p=n.models[E])}}}if(!p&&typeof u=="object"&&u&&"entityType"in u){const y=M(String(u.entityType));n.models[y]&&(p=n.models[y])}if(u&&typeof u=="object")if(Array.isArray(u))for(const y of u)y&&typeof y=="object"&&(yield W(n,[y],o,r,p));else u&&typeof u=="object"&&(yield W(n,[u],o,r,p))}})}function G(n,e,t,r){if(!t||!e||!e.schema)return;const s=t.split(".");let i=e.schema;for(let o=0;o<s.length;o++){const l=s[o];if(i&&i.virtuals&&l){const c=i.virtuals[l];if(c&&c.options&&c.options.ref){let a;if(typeof c.options.ref=="function"?a=c.options.ref(r):typeof c.options.ref=="string"&&(a=c.options.ref),a&&typeof a=="string"){if(o===s.length-1)return a;const u=n.models[a];if(u&&u.schema){i=u.schema;continue}}}}if(i&&i.paths&&l){const c=i.paths[l];if(c&&c.schema){i=c.schema;continue}}return}}function ft(n,e){if(e)for(const t of Object.keys(n.models)){const r=n.models[t],s=r==null?void 0:r.schema;if(s&&(s.paths&&s.paths[e]||s.virtuals&&s.virtuals[e]))return r}}function dt(n,e){const t=(e||"").split(".")[0]||"";if(t)for(const r of Object.keys(n.models)){const s=n.models[r],i=s==null?void 0:s.schema;if(i&&(i.paths&&i.paths[t]||i.virtuals&&i.virtuals[t]))return s}}function z(n,e,t,r,s,i){return v(this,null,function*(){var o,l;let c=e.__t;if(!c){if(i){const g=s?`${s}.${t}`:t,h=(g||"").split(".")[0]||"";let f=i;if(!!!((o=f==null?void 0:f.schema)!=null&&o.paths&&f.schema.paths[h]||(l=f==null?void 0:f.schema)!=null&&l.virtuals&&f.schema.virtuals[h])){const m=dt(n,g);m&&(f=m)}const d=G(n,f,g,e);if(d)c=d;else{const m=(g.includes(".")?g.split(".").pop():g)||"";if(m){const j=ft(n,m);if(j){const R=G(n,j,g,e);R&&(c=R)}}}}if(r&&r.length>0){const g=t.split(".").pop()||"",h=r.find(f=>f.name===g);if(h&&h.options.ref){let f;typeof h.options.ref=="function"?f=h.options.ref(e):typeof h.options.ref=="string"&&(f=h.options.ref),f&&typeof f=="string"&&(c=f)}}if(!c){for(const[g,h]of Object.entries(e))if(g==="entityType"&&typeof h=="string"){c=h;break}if(c&&n.models[c]){const g=n.models[c];if(g&&g.schema){const h=g.schema,f=t.split(".").pop()||"",O=(m,j="root")=>{if(!m||!m.virtuals)return;const R=m.virtuals;for(const _ of Object.keys(R))if(_===f){const b=R[_];if(b&&b.options&&b.options.ref){let D;if(typeof b.options.ref=="function"?D=b.options.ref(e):typeof b.options.ref=="string"&&(D=b.options.ref),D&&typeof D=="string")return D}}if(m.paths)for(const _ of Object.keys(m.paths)){const b=m.paths[_];if(b&&b.schema){const D=O(b.schema,`${j}.${_}`);if(D)return D}}},d=O(h);d&&(c=d)}}}if(!c){const g=t.split(".").pop()||"";if(i&&i.schema){const h=i.schema,f=(d,m="root")=>{if(!d||!d.virtuals)return;const j=d.virtuals;for(const R of Object.keys(j))if(R===g){const _=j[R];if(_&&_.options&&_.options.ref){let b;if(typeof _.options.ref=="function"?b=_.options.ref(e):typeof _.options.ref=="string"&&(b=_.options.ref),b&&typeof b=="string")return b}}if(d.paths)for(const R of Object.keys(d.paths)){const _=d.paths[R];if(_&&_.schema){const b=f(_.schema,`${m}.${R}`);if(b)return b}}},O=f(h);O&&(c=O)}if(!c&&i){const h=G(n,i,t,e);h&&(c=h)}if(!c&&r&&r.length>0){const h=r.find(f=>f.name===g);if(h&&h.options.ref){let f;typeof h.options.ref=="function"?f=h.options.ref(e):typeof h.options.ref=="string"&&(f=h.options.ref),f&&typeof f=="string"&&(c=f)}}}if(!c){const g=t.split(".").pop()||"",h=g?g.charAt(0).toUpperCase()+g.slice(1):"";h&&n.models[h]&&(c=h)}}const a=n.models[c];if(!a)return;const u=`${t}Id`,p=`${t}Ids`,y=e[u]||e[p]||e[t];if(!y)return;const S=Array.isArray(y)?y:[y],E=yield a.find({id:{$in:S}}).lean();E.length>0&&(Array.isArray(y)?e[t]=E:e[t]=E[0])})}function K(n,e,t,r,s,i){return v(this,null,function*(){if(!e.length||!t.length||!r)return e;const o=t.filter(a=>{if(Array.isArray(r))return r.length>0&&r.some(u=>{if(typeof u=="string")return u===a.name||u.startsWith(`${a.name}.`);if(u&&typeof u=="object"){const p=u,y=p.path||p.populate||"";return y===a.name||y.startsWith(`${a.name}.`)}return!1});if(typeof r=="string")return r===a.name||r.startsWith(`${a.name}.`);if(typeof r=="object"&&r!==null){const u=r,p=u.path||u.populate||"";return p===a.name||p.startsWith(`${a.name}.`)}return!1});if(o.length===0)return e;const l=U.cloneDeep(e.map(a=>ut(a)?a.toObject():a));l.forEach(a=>{o.forEach(({name:u,options:p})=>{u in a||(a[u]=p.count?0:p.justOne?null:[])})});const c=new Map;for(const a of o){const{name:u,options:p}=a,y=ct(l,u,p);for(const S of y){c.has(S.model)||c.set(S.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const E=c.get(S.model);E.virtuals.find(h=>h.name===u)||(E.virtuals.push(a),E.localValueSets.set(u,new Set));const g=E.localValueSets.get(u);S.docs.forEach(h=>{const f=h[p.localField];if(f!=null){const O=String(f);g.add(O);let d=-1;const m=h;m.id!==void 0?d=l.findIndex(j=>j.id===m.id):m._id!==void 0&&(d=l.findIndex(j=>{var R,_,b,D;return((_=(R=j._id)==null?void 0:R.toString)==null?void 0:_.call(R))===((D=(b=m._id)==null?void 0:b.toString)==null?void 0:D.call(b))})),d!==-1&&(E.docsByLocalValue.has(O)||E.docsByLocalValue.set(O,[]),E.docsByLocalValue.get(O).push(d))}})}}if(yield Promise.all(Array.from(c.entries()).map(a=>v(null,[a],function*([u,p]){const y=n.models[u];if(!y)return;const S=new Set;if(p.localValueSets.forEach(O=>{O.forEach(d=>S.add(d))}),S.size===0)return;const E=[...new Set(p.virtuals.map(O=>O.options.foreignField))],g=Array.from(S);let h;E.length===1?h={[String(E[0])]:{$in:g}}:h={$or:E.map(O=>({[O]:{$in:g}}))};const f=yield y.find(h,s).lean();for(const O of p.virtuals){const{name:d,options:m}=O,j=f.filter(R=>{const _=R[m.foreignField];return _!=null&&S.has(String(_))});if(m.count){const R=new Map;j.forEach(_=>{var b;const D=(b=_[m.foreignField])==null?void 0:b.toString();D&&R.set(D,(R.get(D)||0)+1)}),p.localValueSets.get(d).forEach(_=>{const b=p.docsByLocalValue.get(_)||[],D=R.get(_)||0;b.forEach(w=>{const I=l[w];I[d]===void 0&&(I[d]=D)})})}else{const R=new Map;j.forEach(_=>{var b;const D=(b=_[m.foreignField])==null?void 0:b.toString();D&&(R.has(D)||R.set(D,[]),R.get(D).push(_))}),p.localValueSets.get(d).forEach(_=>{const b=p.docsByLocalValue.get(_)||[],D=R.get(_)||[],w=m.justOne?D[0]||null:D;b.forEach(I=>{const H=l[I];H[d]=w})})}}}))),r){const u=(p=>{const y=Array.isArray(p)?p:[p],S=new Map,E=[];for(const h of y)if(typeof h=="string")if(h.includes(".")){const f=h.split("."),O=f[0]||"",d=f.slice(1).join(".");O&&(S.has(O)||S.set(O,[]),d&&S.get(O).push(d))}else E.push(h);else if(h&&typeof h=="object"){const f=h;if(f.path&&f.path.includes(".")){const O=f.path.split("."),d=O[0]||"",m=O.slice(1).join(".");d&&(S.has(d)||S.set(d,[]),m&&S.get(d).push(m),f.populate&&S.get(d).push(f.populate))}else E.push(h)}const g=[...E];return S.forEach((h,f)=>{const O=[];for(const d of h)(typeof d=="string"||d&&typeof d=="object")&&O.push(d);O.length>0?g.push({path:f,populate:O}):g.push(f)}),g})(r);yield W(n,l,u,t,i)}return l})}class ht{constructor(e,t){lt(this,"collection"),this.collection=e.collection(t)}createOne(e){return v(this,null,function*(){try{const t=A(A({},P.createGenericFields()),e);return(yield this.collection.insertOne(t)).acknowledged?{success:!0,message:"Document created successfully",result:t}:{success:!1,message:"Document creation failed",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(t){return N.catchError(t)}})}createMany(e){return v(this,null,function*(){try{const t=e.map(s=>A(A({},P.createGenericFields()),s)),r=yield this.collection.insertMany(t);return r.insertedCount===0?{success:!1,message:"No documents were inserted",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${r.insertedCount} documents created successfully`,result:t}}catch(t){return N.catchError(t)}})}findOne(e){return v(this,null,function*(){try{const t=yield this.collection.findOne(e);return t?{success:!0,message:"Document found",result:t}:{success:!1,message:"Document not found",code:V.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(t){return N.catchError(t)}})}findAll(){return v(this,arguments,function*(e={}){try{return{success:!0,message:"Documents retrieved successfully",result:yield this.collection.find(e).toArray()}}catch(t){return N.catchError(t)}})}count(){return v(this,arguments,function*(e={}){try{const t=yield this.collection.countDocuments(e);return{success:!0,message:`${t} documents counted successfully`,result:t}}catch(t){return N.catchError(t)}})}updateOne(e,t){return v(this,null,function*(){try{const r=yield this.collection.updateOne(e,{$set:t});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:r}}catch(r){return N.catchError(r)}})}updateMany(e,t){return v(this,null,function*(){try{const r=yield this.collection.updateMany(e,{$set:t});return r.matchedCount===0?{success:!1,message:"No documents matched the filter",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:r}}catch(r){return N.catchError(r)}})}deleteOne(e){return v(this,null,function*(){try{const t=yield this.collection.deleteOne(e);return t.deletedCount===0?{success:!1,message:"No documents matched the filter",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:t}}catch(t){return N.catchError(t)}})}deleteMany(e){return v(this,null,function*(){try{const t=yield this.collection.deleteMany(e);return t.deletedCount===0?{success:!1,message:"No documents matched the filter",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:t}}catch(t){return N.catchError(t)}})}}class pt{constructor(e){this.model=e}getModelName(){return this.model.modelName}getDynamicVirtuals(){if(this.model._virtualConfigs){const r=this.model._virtualConfigs.filter(s=>{var i;return typeof((i=s.options)==null?void 0:i.ref)=="function"});if(r.length>0)return r}return this.model.schema.statics._dynamicVirtuals}populateDynamicVirtualsForDocument(e,t){return v(this,null,function*(){const r=this.getDynamicVirtuals();if(r&&r.length>0){const s=yield K(this.model.base,[e],r,t,void 0,this.model);return s&&s[0]?s[0]:e}return e})}populateDynamicVirtualsForDocuments(e,t){return v(this,null,function*(){const r=this.getDynamicVirtuals();return r&&r.length>0&&e.length>0?yield K(this.model.base,e,r,t,void 0,this.model):e})}findOne(){return v(this,arguments,function*(e={},t={},r={},s){var i,o;try{const l=F.normalizeMongoFilter(e),c=this.model.findOne(l,t,r),a=this.getDynamicVirtuals(),u=q(s,a);u&&c.populate(u);const p=yield c.exec();if(!p)return{success:!1,message:`No ${this.getModelName()} found.`,code:V.RESPONSE_STATUS.NOT_FOUND.CODE};const y=yield this.populateDynamicVirtualsForDocument(p,s);return{success:!0,result:(o=(i=y==null?void 0:y.toObject)==null?void 0:i.call(y))!=null?o:y}}catch(l){return N.catchError(l)}})}findAll(){return v(this,arguments,function*(e={},t={},r={},s){try{const i=F.normalizeMongoFilter(e),o=this.model.find(i,t,r),l=this.getDynamicVirtuals(),c=q(s,l);c&&o.populate(c);const a=yield o.exec();return{success:!0,result:(yield this.populateDynamicVirtualsForDocuments(a,s)).map(p=>{var y,S;return(S=(y=p==null?void 0:p.toObject)==null?void 0:y.call(p))!=null?S:p})}}catch(i){return N.catchError(i)}})}findPaging(){return v(this,arguments,function*(e={},t={}){try{const r=F.normalizeMongoFilter(e),s=this.getDynamicVirtuals(),i=A({},t);t.populate&&(i.populate=q(t.populate,s));const o=yield this.model.paginate(r,i);if(s&&s.length>0){const l=yield this.populateDynamicVirtualsForDocuments(o.docs,t.populate);return{success:!0,result:x(A({},o),{docs:l.map(c=>{var a,u;return(u=(a=c==null?void 0:c.toObject)==null?void 0:a.call(c))!=null?u:c})})}}return{success:!0,result:x(A({},o),{docs:o.docs.map(l=>{var c,a;return(a=(c=l==null?void 0:l.toObject)==null?void 0:c.call(l))!=null?a:l})})}}catch(r){return N.catchError(r)}})}findPagingAggregate(e){return v(this,arguments,function*(t,r={}){try{const s=this.getDynamicVirtuals(),i=A({},r);r.populate&&(i.populate=q(r.populate,s));const o=yield this.model.aggregatePaginate(this.model.aggregate(t),i),l=yield this.populateDynamicVirtualsForDocuments(o.docs,r.populate);return{success:!0,result:x(A({},o),{docs:l.map(c=>{var a,u;return(u=(a=c==null?void 0:c.toObject)==null?void 0:a.call(c))!=null?u:c})})}}catch(s){return N.catchError(s)}})}count(){return v(this,arguments,function*(e={}){try{const t=F.normalizeMongoFilter(e);return{success:!0,result:yield this.model.countDocuments(t)}}catch(t){return N.catchError(t)}})}createOne(e){return v(this,null,function*(){var t,r;try{const s=yield this.model.create(e);return{success:!0,result:(r=(t=s==null?void 0:s.toObject)==null?void 0:t.call(s))!=null?r:s}}catch(s){return N.catchError(s)}})}createMany(e){return v(this,arguments,function*(t,r={}){try{return{success:!0,result:(yield this.model.insertMany(t,r)).map(i=>{var o,l;return(l=(o=i==null?void 0:i.toObject)==null?void 0:o.call(i))!=null?l:i})}}catch(s){return N.catchError(s)}})}updateOne(){return v(this,arguments,function*(e={},t={},r={}){var s,i;try{const o=F.normalizeMongoFilter(e),l=yield this.model.findOneAndUpdate(o,t,A({new:!0},r)).exec();return l?{success:!0,result:(i=(s=l==null?void 0:l.toObject)==null?void 0:s.call(l))!=null?i:l}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:V.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(o){return N.catchError(o)}})}updateMany(){return v(this,arguments,function*(e={},t={},r={}){try{const s=F.normalizeMongoFilter(e);return{success:!0,result:yield this.model.updateMany(s,t,r).exec()}}catch(s){return N.catchError(s)}})}deleteOne(){return v(this,arguments,function*(e={},t={}){var r,s;try{const i=F.normalizeMongoFilter(e),o=yield this.model.findOneAndDelete(i,t).exec();return o?{success:!0,result:(s=(r=o==null?void 0:o.toObject)==null?void 0:r.call(o))!=null?s:o}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:V.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(i){return N.catchError(i)}})}deleteMany(){return v(this,arguments,function*(e={},t={}){try{const r=F.normalizeMongoFilter(e),s=yield this.model.deleteMany(r,t).exec();return s.deletedCount===0?{success:!1,message:"No documents found to delete.",code:V.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:s}}catch(r){return N.catchError(r)}})}createShortId(e,t=4){return v(this,null,function*(){try{const s=Array.from({length:10},(l,c)=>C.generateShortId(e,c+t)),o=(yield Promise.all(s.map(l=>this.model.exists({shortId:l})))).findIndex(l=>!l);if(o!==-1){const l=s[o];if(l)return{success:!0,result:l}}return{success:!1,message:"Failed to create a unique shortId",code:V.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(r){return N.catchError(r)}})}createSlugQuery({slug:e,field:t,isObject:r,haveHistory:s=!1,filter:i}){const o=A({},i!=null?i:{});return r?x(A({},o),{$or:[{[`slug.${t}`]:e},...s?[{slugHistory:{$elemMatch:{[`slug.${t}`]:e}}}]:[]]}):x(A({},o),{$or:[{slug:e},...s?[{slugHistory:e}]:[]]})}createUniqueSlug(e){return v(this,arguments,function*({slug:t,field:r,isObject:s,haveHistory:i,filter:o}){if(!t||typeof t!="string")throw new Error("Invalid slug provided: must be a non-empty string");const l=C.generateSlug(t);if(!(yield this.model.exists(this.createSlugQuery({slug:l,field:r,isObject:s,haveHistory:i,filter:o}))))return l;for(let p=1;p<=Z.MONGO_SLUG_MAX_ATTEMPTS;p++){const y=`${l}-${p}`;if(!(yield this.model.exists(this.createSlugQuery({slug:y,field:r,isObject:s,haveHistory:i,filter:o}))))return y}const a=Date.now(),u=Math.random().toString(36).substring(2,8);return`${l}-${a}-${u}`})}createSlug(e){return v(this,arguments,function*({field:t,from:r,filter:s,haveHistory:i}){try{const o=r[t];return U.isObject(o)?{success:!0,result:Object.fromEntries(yield Promise.all(Object.entries(o).map(u=>v(this,[u],function*([p,y]){const S=yield this.createUniqueSlug({slug:y,field:p,isObject:!0,haveHistory:i,filter:s});return[p,S]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:o,field:t,isObject:!1,haveHistory:i,filter:s})}}catch(o){return N.catchError(o)}})}checkSlug(e){return v(this,arguments,function*({slug:t,field:r,from:s,filter:i,haveHistory:o}){try{const l=s[r];if(U.isObject(l)){const y=Object.values(l).map(E=>C.generateSlug(E));return(yield Promise.all(y.map(E=>this.model.exists(this.createSlugQuery({slug:E,field:r,isObject:!0,haveHistory:o,filter:i}))))).some(E=>E)?{success:!0,result:!0}:{success:!0,result:!1}}const a=C.generateSlug(t);return{success:!0,result:(yield this.model.exists(this.createSlugQuery({slug:a,field:r,isObject:!1,filter:i})))!==null}}catch(l){return N.catchError(l)}})}aggregate(e){return v(this,null,function*(){try{return{success:!0,result:yield this.model.aggregate(e)}}catch(t){return N.catchError(t)}})}distinct(e){return v(this,arguments,function*(t,r={},s={}){try{return{success:!0,result:yield this.model.distinct(t,r,s)}}catch(i){return N.catchError(i)}})}}exports.MongoController=ht;exports.MongooseController=pt;exports.mongo=P;
|
|
@@ -279,7 +279,7 @@ export declare class MongooseController<T extends Partial<C_Document>> {
|
|
|
279
279
|
*/
|
|
280
280
|
private getModelName;
|
|
281
281
|
/**
|
|
282
|
-
* Gets the dynamic virtuals configuration from the model
|
|
282
|
+
* Gets the dynamic virtuals configuration from the model instance.
|
|
283
283
|
*
|
|
284
284
|
* @returns Array of dynamic virtual configurations or undefined if none exist.
|
|
285
285
|
*/
|