@cyberskill/shared 2.12.0 → 2.13.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/node/mongo/mongo.util.cjs +5 -5
- package/dist/node/mongo/mongo.util.js +187 -184
- package/package.json +5 -5
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("lodash-es"),B=require("migrate-mongo"),K=require("mongoose-aggregate-paginate-v2"),Q=require("mongoose-paginate-v2"),U=require("uuid"),W=require("./mongo.constant.cjs"),b=require("../../util/object/object.util.cjs"),H=require("../../util/common/common.util.cjs"),F=require("../fs/fs.util.cjs"),
|
|
2
|
-
module.exports = ${JSON.stringify(
|
|
3
|
-
${
|
|
4
|
-
`;F.pathExistsSync(
|
|
5
|
-
`).includes(M.MIGRATE_MONGO_CONFIG)||F.appendFileSync(M.PATH.GIT_IGNORE,e):F.writeFileSync(M.PATH.GIT_IGNORE,e)}}),regexify(c,t){if(!c)return{};let e=q.cloneDeep(c);if(!t||t.length===0)return e;for(const r of t){const n=r.toString().split("."),s=b.getNestedValue(e,n);if(typeof s=="string"&&s.length>0){const u={$regex:`.*${H.regexSearchMapper(s)}.*`,$options:"i"};e=b.setNestedValue(e,n,u)}}return e},isDynamicVirtual(c){return!!(c&&typeof c.ref=="function")},getNewRecords(c,t,e){return a(this,null,function*(){const r=yield c.findAll({});return r.success?t.filter(s=>!r.result.some(u=>e(u,s))):t})},getExistingRecords(c,t,e){return a(this,null,function*(){const r=yield c.findAll({});return r.success?r.result.filter(s=>t.some(u=>e(s,u))):[]})}};function I(c,t){if(!c||!t||t.length===0)return c;const e=new Set(t.map(r=>r.name));if(Array.isArray(c)){const r=c.filter(n=>{if(typeof n=="string")return!e.has(n);if(typeof n=="object"&&n!==null){const s=n;return!e.has(s.path||s.populate||"")}return!0});return r.length>0?r:void 0}if(typeof c=="string")return e.has(c)?void 0:c;if(typeof c=="object"&&c!==null){const r=c,n=r.path||r.populate||"";return e.has(n)?void 0:c}return c}function ne(c,t,e){if(!c.length||!t||!(e!=null&&e.ref))return[];const r=new Map;return c.forEach(n=>{try{const s=e.ref(n);if(s==null)return;const u=typeof s=="string"?s:String(s);if(u&&u.trim()!==""){const l=re(u);r.has(l)||r.set(l,[]),r.get(l).push(n)}}catch(s){f.catchError(new Error(`Dynamic ref function failed for virtual "${t}": ${s instanceof Error?s.message:String(s)}`))}}),Array.from(r.entries()).map(([n,s])=>({model:n,docs:s}))}function z(c,t,e,r,n){return a(this,null,function*(){if(!t.length||!e.length||!r)return t;const s=e.filter(o=>{if(Array.isArray(r))return r.length>0&&r.some(i=>typeof i=="string"?i===o.name:i.path===o.name);if(typeof r=="string")return r===o.name;if(typeof r=="object"&&r!==null){const i=r;return i.path&&i.path===o.name||i.populate&&i.populate===o.name}return!1});if(s.length===0)return t;const u=q.cloneDeep(t);u.forEach(o=>{s.forEach(({name:i,options:d})=>{i in o||(o[i]=d.count?0:d.justOne?null:[])})});const l=new Map;for(const o of s){const{name:i,options:d}=o,m=ne(u,i,d);for(const h of m){l.has(h.model)||l.set(h.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const g=l.get(h.model);g.virtuals.find(V=>V.name===i)||(g.virtuals.push(o),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(l.entries()).map(o=>a(null,[o],function*([i,d]){const m=c.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 se{constructor(t,e){te(this,"collection"),this.collection=t.collection(e)}createOne(t){return a(this,null,function*(){try{const e=O(O({},T.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({},T.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 ce{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 s,u;try{const l=b.normalizeMongoFilter(t),o=this.model.findOne(l,e,r),i=this.getDynamicVirtuals(),d=I(n,i);d&&o.populate(d);const m=yield o.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=(s=h==null?void 0:h.toObject)==null?void 0:s.call(h))!=null?u:h}}catch(l){return f.catchError(l)}})}findAll(){return a(this,arguments,function*(t={},e={},r={},n){try{const s=b.normalizeMongoFilter(t),u=this.model.find(s,e,r),l=this.getDynamicVirtuals(),o=I(n,l);o&&u.populate(o);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(s){return f.catchError(s)}})}findPaging(){return a(this,arguments,function*(t={},e={}){try{const r=b.normalizeMongoFilter(t),n=this.getDynamicVirtuals(),s=O({},e);e.populate&&(s.populate=I(e.populate,n));const u=yield this.model.paginate(r,s),l=yield this.populateDynamicVirtualsForDocuments(u.docs,e.populate);return{success:!0,result:P(O({},u),{docs:l.map(o=>{var i,d;return(d=(i=o==null?void 0:o.toObject)==null?void 0:i.call(o))!=null?d:o})})}}catch(r){return f.catchError(r)}})}findPagingAggregate(t){return a(this,arguments,function*(e,r={}){try{const n=this.getDynamicVirtuals(),s=O({},r);r.populate&&(s.populate=I(r.populate,n));const u=yield this.model.aggregatePaginate(this.model.aggregate(e),s),l=yield this.populateDynamicVirtualsForDocuments(u.docs,r.populate);return{success:!0,result:P(O({},u),{docs:l.map(o=>{var i,d;return(d=(i=o==null?void 0:o.toObject)==null?void 0:i.call(o))!=null?d:o})})}}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(s=>{var u,l;return(l=(u=s==null?void 0:s.toObject)==null?void 0:u.call(s))!=null?l:s})}}catch(n){return f.catchError(n)}})}updateOne(){return a(this,arguments,function*(t={},e={},r={}){var n,s;try{const u=b.normalizeMongoFilter(t),l=yield this.model.findOneAndUpdate(u,e,O({new:!0},r)).exec();return l?{success:!0,result:(s=(n=l==null?void 0:l.toObject)==null?void 0:n.call(l))!=null?s:l}:{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 s=b.normalizeMongoFilter(t),u=yield this.model.findOneAndDelete(s,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(s){return f.catchError(s)}})}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},(l,o)=>C.generateShortId(t,o+e)),u=(yield Promise.all(n.map(l=>this.model.exists({shortId:l})))).findIndex(l=>!l);if(u!==-1){const l=n[u];if(l)return{success:!0,result:l}}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:s}){const u=O({},s!=null?s:{});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:s,filter:u}){if(!e||typeof e!="string")throw new Error("Invalid slug provided: must be a non-empty string");const l=C.generateSlug(e);if(!(yield this.model.exists(this.createSlugQuery({slug:l,field:r,isObject:n,haveHistory:s,filter:u}))))return l;for(let m=1;m<=W.MONGO_SLUG_MAX_ATTEMPTS;m++){const h=`${l}-${m}`;if(!(yield this.model.exists(this.createSlugQuery({slug:h,field:r,isObject:n,haveHistory:s,filter:u}))))return h}const i=Date.now(),d=Math.random().toString(36).substring(2,8);return`${l}-${i}-${d}`})}createSlug(t){return a(this,arguments,function*({field:e,from:r,filter:n,haveHistory:s}){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:s,filter:n});return[m,g]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:u,field:e,isObject:!1,haveHistory:s,filter:n})}}catch(u){return f.catchError(u)}})}checkSlug(t){return a(this,arguments,function*({slug:e,field:r,from:n,filter:s,haveHistory:u}){try{const l=n[r];if(q.isObject(l)){const h=Object.values(l).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:s}))))).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:s})))!==null}}catch(l){return f.catchError(l)}})}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(s){return f.catchError(s)}})}}exports.MongoController=se;exports.MongooseController=ce;exports.mongo=T;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("lodash-es"),B=require("migrate-mongo"),K=require("mongoose-aggregate-paginate-v2"),Q=require("mongoose-paginate-v2"),U=require("uuid"),W=require("./mongo.constant.cjs"),b=require("../../util/object/object.util.cjs"),H=require("../../util/common/common.util.cjs"),F=require("../fs/fs.util.cjs"),T=require("../path/path.constant.cjs"),k=require("../../util/validate/validate.util.cjs"),C=require("../../util/string/string.util.cjs"),R=require("../../constant/response-status.cjs"),f=require("../log/log.util.cjs");var J=Object.defineProperty,X=Object.defineProperties,Y=Object.getOwnPropertyDescriptors,G=Object.getOwnPropertySymbols,Z=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable,$=(s,t,e)=>t in s?J(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,O=(s,t)=>{for(var e in t||(t={}))Z.call(t,e)&&$(s,e,t[e]);if(G)for(var e of G(t))ee.call(t,e)&&$(s,e,t[e]);return s},P=(s,t)=>X(s,Y(t)),te=(s,t,e)=>$(s,typeof t!="symbol"?t+"":t,e),a=(s,t,e)=>new Promise((r,n)=>{var c=l=>{try{o(e.next(l))}catch(i){n(i)}},u=l=>{try{o(e.throw(l))}catch(i){n(i)}},o=l=>l.done?r(l.value):Promise.resolve(l.value).then(c,u);o((e=e.apply(s,t)).next())});function re(s){return s===s.toUpperCase()?s.charAt(0).toUpperCase()+s.slice(1).toLowerCase():s}const M={createGenericFields(){return{id:U.v4(),isDel:!1,createdAt:new Date,updatedAt:new Date}},applyPlugins(s,t){t.filter(e=>typeof e=="function").forEach(e=>s.plugin(e))},applyMiddlewares(s,t){t.forEach(({method:e,pre:r,post:n})=>{e&&r&&s.pre(e,r),e&&n&&s.post(e,n)})},createGenericSchema(s){return new s.Schema({id:{type:String,default:U.v4,unique:!0},isDel:{type:Boolean,default:!1}},{timestamps:!0})},createSchema({mongoose:s,schema:t,virtuals:e=[],standalone:r=!1}){const n=new s.Schema(t,{toJSON:{virtuals:!0},toObject:{virtuals:!0}});return e.forEach(({name:c,options:u,get:o})=>{if(M.isDynamicVirtual(u)){const l=n.statics;l._dynamicVirtuals||(l._dynamicVirtuals=[]),l._dynamicVirtuals.push({name:c,options:u});const i=n.virtual(c);o?i.get(o):i.get(function(){var d;return((d=this._populated)==null?void 0:d[c])||(u!=null&&u.count?0:u!=null&&u.justOne?null:[])})}else{const l=n.virtual(c,u);o&&l.get(o)}}),r||n.add(M.createGenericSchema(s)),n},createModel({mongoose:s,name:t,schema:e,virtuals:r=[],pagination:n=!0,aggregate:c=!0,middlewares:u=[]}){if(!t)throw new Error("Model name is required.");if(s.models[t])return s.models[t];const o=M.createSchema({mongoose:s,schema:e,virtuals:r});return(n||c)&&M.applyPlugins(o,[n&&Q,c&&K]),M.applyMiddlewares(o,u),s.model(t,o)},validator:{isRequired(){return function(s){return a(this,null,function*(){return!k.validate.isEmpty(s)})}},isUnique(s){return function(t){return a(this,null,function*(){if(!Array.isArray(s)||s.length===0)throw new Error("Fields must be a non-empty array of strings.");const e={$or:s.map(n=>({[n]:t}))};return!(yield this.constructor.exists(e))})}},matchesRegex(s){return function(t){return a(this,null,function*(){if(!Array.isArray(s)||s.some(e=>!(e instanceof RegExp)))throw new Error("regexArray must be an array of valid RegExp objects.");return s.every(e=>e.test(t))})}}},migrate:P(O({},B),{setConfig:s=>{const t=`// This file is automatically generated by the Cyberskill CLI.
|
|
2
|
+
module.exports = ${JSON.stringify(s,null,4)}`;F.writeFileSync(T.PATH.MIGRATE_MONGO_CONFIG,t);const e=`
|
|
3
|
+
${T.MIGRATE_MONGO_CONFIG}
|
|
4
|
+
`;F.pathExistsSync(T.PATH.GIT_IGNORE)?F.readFileSync(T.PATH.GIT_IGNORE,"utf-8").split(`
|
|
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;
|
|
@@ -7,36 +7,36 @@ import { MONGO_SLUG_MAX_ATTEMPTS as J } from "./mongo.constant.js";
|
|
|
7
7
|
import { getNestedValue as X, setNestedValue as H, normalizeMongoFilter as V } from "../../util/object/object.util.js";
|
|
8
8
|
import { regexSearchMapper as Y } from "../../util/common/common.util.js";
|
|
9
9
|
import { writeFileSync as q, pathExistsSync as Z, readFileSync as ee, appendFileSync as te } from "../fs/fs.util.js";
|
|
10
|
-
import { PATH as
|
|
10
|
+
import { PATH as w, MIGRATE_MONGO_CONFIG as L } from "../path/path.constant.js";
|
|
11
11
|
import { validate as re } from "../../util/validate/validate.util.js";
|
|
12
|
-
import { generateShortId as
|
|
12
|
+
import { generateShortId as ne, generateSlug as I } from "../../util/string/string.util.js";
|
|
13
13
|
import { RESPONSE_STATUS as D } from "../../constant/response-status.js";
|
|
14
14
|
import { catchError as f } from "../log/log.util.js";
|
|
15
|
-
var
|
|
15
|
+
var se = Object.defineProperty, ue = Object.defineProperties, le = Object.getOwnPropertyDescriptors, U = Object.getOwnPropertySymbols, ce = Object.prototype.hasOwnProperty, oe = Object.prototype.propertyIsEnumerable, $ = (s, t, e) => t in s ? se(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e, v = (s, t) => {
|
|
16
16
|
for (var e in t || (t = {}))
|
|
17
|
-
ce.call(t, e) && $(
|
|
17
|
+
ce.call(t, e) && $(s, e, t[e]);
|
|
18
18
|
if (U)
|
|
19
19
|
for (var e of U(t))
|
|
20
|
-
oe.call(t, e) && $(
|
|
21
|
-
return
|
|
22
|
-
},
|
|
23
|
-
var
|
|
20
|
+
oe.call(t, e) && $(s, e, t[e]);
|
|
21
|
+
return s;
|
|
22
|
+
}, F = (s, t) => ue(s, le(t)), ie = (s, t, e) => $(s, typeof t != "symbol" ? t + "" : t, e), a = (s, t, e) => new Promise((r, n) => {
|
|
23
|
+
var u = (o) => {
|
|
24
24
|
try {
|
|
25
25
|
c(e.next(o));
|
|
26
26
|
} catch (i) {
|
|
27
|
-
|
|
27
|
+
n(i);
|
|
28
28
|
}
|
|
29
29
|
}, l = (o) => {
|
|
30
30
|
try {
|
|
31
31
|
c(e.throw(o));
|
|
32
32
|
} catch (i) {
|
|
33
|
-
|
|
33
|
+
n(i);
|
|
34
34
|
}
|
|
35
|
-
}, c = (o) => o.done ? r(o.value) : Promise.resolve(o.value).then(
|
|
36
|
-
c((e = e.apply(
|
|
35
|
+
}, c = (o) => o.done ? r(o.value) : Promise.resolve(o.value).then(u, l);
|
|
36
|
+
c((e = e.apply(s, t)).next());
|
|
37
37
|
});
|
|
38
|
-
function ae(
|
|
39
|
-
return
|
|
38
|
+
function ae(s) {
|
|
39
|
+
return s === s.toUpperCase() ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : s;
|
|
40
40
|
}
|
|
41
41
|
const x = {
|
|
42
42
|
/**
|
|
@@ -62,8 +62,8 @@ const x = {
|
|
|
62
62
|
* @param schema - The Mongoose schema to apply plugins to.
|
|
63
63
|
* @param plugins - An array of plugin functions or false values to filter and apply.
|
|
64
64
|
*/
|
|
65
|
-
applyPlugins(
|
|
66
|
-
t.filter((e) => typeof e == "function").forEach((e) =>
|
|
65
|
+
applyPlugins(s, t) {
|
|
66
|
+
t.filter((e) => typeof e == "function").forEach((e) => s.plugin(e));
|
|
67
67
|
},
|
|
68
68
|
/**
|
|
69
69
|
* Applies middleware functions to a Mongoose schema.
|
|
@@ -72,9 +72,9 @@ const x = {
|
|
|
72
72
|
* @param schema - The Mongoose schema to apply middleware to.
|
|
73
73
|
* @param middlewares - An array of middleware configurations with method, pre, and post functions.
|
|
74
74
|
*/
|
|
75
|
-
applyMiddlewares(
|
|
76
|
-
t.forEach(({ method: e, pre: r, post:
|
|
77
|
-
e && r &&
|
|
75
|
+
applyMiddlewares(s, t) {
|
|
76
|
+
t.forEach(({ method: e, pre: r, post: n }) => {
|
|
77
|
+
e && r && s.pre(e, r), e && n && s.post(e, n);
|
|
78
78
|
});
|
|
79
79
|
},
|
|
80
80
|
/**
|
|
@@ -85,8 +85,8 @@ const x = {
|
|
|
85
85
|
* @param mongoose - The Mongoose instance to create the schema with.
|
|
86
86
|
* @returns A Mongoose schema with generic document fields.
|
|
87
87
|
*/
|
|
88
|
-
createGenericSchema(
|
|
89
|
-
return new
|
|
88
|
+
createGenericSchema(s) {
|
|
89
|
+
return new s.Schema(
|
|
90
90
|
{
|
|
91
91
|
id: { type: String, default: G, unique: !0 },
|
|
92
92
|
isDel: { type: Boolean, default: !1 }
|
|
@@ -107,34 +107,34 @@ const x = {
|
|
|
107
107
|
* @returns A configured Mongoose schema.
|
|
108
108
|
*/
|
|
109
109
|
createSchema({
|
|
110
|
-
mongoose:
|
|
110
|
+
mongoose: s,
|
|
111
111
|
schema: t,
|
|
112
112
|
virtuals: e = [],
|
|
113
113
|
standalone: r = !1
|
|
114
114
|
}) {
|
|
115
|
-
const
|
|
115
|
+
const n = new s.Schema(t, {
|
|
116
116
|
toJSON: { virtuals: !0 },
|
|
117
117
|
// So `res.json()` and other `JSON.stringify()` functions include virtuals
|
|
118
118
|
toObject: { virtuals: !0 }
|
|
119
119
|
// So `console.log()` and other functions that use `toObject()` include virtuals
|
|
120
120
|
});
|
|
121
|
-
return e.forEach(({ name:
|
|
121
|
+
return e.forEach(({ name: u, options: l, get: c }) => {
|
|
122
122
|
if (x.isDynamicVirtual(l)) {
|
|
123
|
-
const o =
|
|
123
|
+
const o = n.statics;
|
|
124
124
|
o._dynamicVirtuals || (o._dynamicVirtuals = []), o._dynamicVirtuals.push({
|
|
125
|
-
name:
|
|
125
|
+
name: u,
|
|
126
126
|
options: l
|
|
127
127
|
});
|
|
128
|
-
const i =
|
|
128
|
+
const i = n.virtual(u);
|
|
129
129
|
c ? i.get(c) : i.get(function() {
|
|
130
130
|
var d;
|
|
131
|
-
return ((d = this._populated) == null ? void 0 : d[
|
|
131
|
+
return ((d = this._populated) == null ? void 0 : d[u]) || (l != null && l.count ? 0 : l != null && l.justOne ? null : []);
|
|
132
132
|
});
|
|
133
133
|
} else {
|
|
134
|
-
const o =
|
|
134
|
+
const o = n.virtual(u, l);
|
|
135
135
|
c && o.get(c);
|
|
136
136
|
}
|
|
137
|
-
}), r ||
|
|
137
|
+
}), r || n.add(x.createGenericSchema(s)), n;
|
|
138
138
|
},
|
|
139
139
|
/**
|
|
140
140
|
* Creates a Mongoose model with plugins, middleware, and pagination support.
|
|
@@ -153,23 +153,23 @@ const x = {
|
|
|
153
153
|
* @throws {Error} When the model name is not provided.
|
|
154
154
|
*/
|
|
155
155
|
createModel({
|
|
156
|
-
mongoose:
|
|
156
|
+
mongoose: s,
|
|
157
157
|
name: t,
|
|
158
158
|
schema: e,
|
|
159
159
|
virtuals: r = [],
|
|
160
|
-
pagination:
|
|
161
|
-
aggregate:
|
|
160
|
+
pagination: n = !0,
|
|
161
|
+
aggregate: u = !0,
|
|
162
162
|
middlewares: l = []
|
|
163
163
|
}) {
|
|
164
164
|
if (!t)
|
|
165
165
|
throw new Error("Model name is required.");
|
|
166
|
-
if (
|
|
167
|
-
return
|
|
168
|
-
const c = x.createSchema({ mongoose:
|
|
169
|
-
return (
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
]), x.applyMiddlewares(c, l),
|
|
166
|
+
if (s.models[t])
|
|
167
|
+
return s.models[t];
|
|
168
|
+
const c = x.createSchema({ mongoose: s, schema: e, virtuals: r });
|
|
169
|
+
return (n || u) && x.applyPlugins(c, [
|
|
170
|
+
n && k,
|
|
171
|
+
u && W
|
|
172
|
+
]), x.applyMiddlewares(c, l), s.model(t, c);
|
|
173
173
|
},
|
|
174
174
|
/**
|
|
175
175
|
* Validation utilities for Mongoose schemas.
|
|
@@ -184,9 +184,9 @@ const x = {
|
|
|
184
184
|
* @returns A validation function that returns true if the field is not empty.
|
|
185
185
|
*/
|
|
186
186
|
isRequired() {
|
|
187
|
-
return function(
|
|
187
|
+
return function(s) {
|
|
188
188
|
return a(this, null, function* () {
|
|
189
|
-
return !re.isEmpty(
|
|
189
|
+
return !re.isEmpty(s);
|
|
190
190
|
});
|
|
191
191
|
};
|
|
192
192
|
},
|
|
@@ -199,12 +199,12 @@ const x = {
|
|
|
199
199
|
* @returns A validation function that returns true if the value is unique across the specified fields.
|
|
200
200
|
* @throws {Error} When fields is not a non-empty array of strings.
|
|
201
201
|
*/
|
|
202
|
-
isUnique(
|
|
202
|
+
isUnique(s) {
|
|
203
203
|
return function(t) {
|
|
204
204
|
return a(this, null, function* () {
|
|
205
|
-
if (!Array.isArray(
|
|
205
|
+
if (!Array.isArray(s) || s.length === 0)
|
|
206
206
|
throw new Error("Fields must be a non-empty array of strings.");
|
|
207
|
-
const e = { $or:
|
|
207
|
+
const e = { $or: s.map((n) => ({ [n]: t })) };
|
|
208
208
|
return !(yield this.constructor.exists(e));
|
|
209
209
|
});
|
|
210
210
|
};
|
|
@@ -218,12 +218,12 @@ const x = {
|
|
|
218
218
|
* @returns A validation function that returns true if the value matches all regex patterns.
|
|
219
219
|
* @throws {Error} When regexArray is not an array of valid RegExp objects.
|
|
220
220
|
*/
|
|
221
|
-
matchesRegex(
|
|
221
|
+
matchesRegex(s) {
|
|
222
222
|
return function(t) {
|
|
223
223
|
return a(this, null, function* () {
|
|
224
|
-
if (!Array.isArray(
|
|
224
|
+
if (!Array.isArray(s) || s.some((e) => !(e instanceof RegExp)))
|
|
225
225
|
throw new Error("regexArray must be an array of valid RegExp objects.");
|
|
226
|
-
return
|
|
226
|
+
return s.every((e) => e.test(t));
|
|
227
227
|
});
|
|
228
228
|
};
|
|
229
229
|
}
|
|
@@ -232,7 +232,7 @@ const x = {
|
|
|
232
232
|
* Migration utilities for MongoDB.
|
|
233
233
|
* This object extends the migrate-mongo library with additional configuration utilities.
|
|
234
234
|
*/
|
|
235
|
-
migrate:
|
|
235
|
+
migrate: F(v({}, Q), {
|
|
236
236
|
/**
|
|
237
237
|
* Sets the migration configuration and updates .gitignore.
|
|
238
238
|
* This function creates a migration configuration file and ensures it's properly
|
|
@@ -240,15 +240,15 @@ const x = {
|
|
|
240
240
|
*
|
|
241
241
|
* @param options - Migration configuration options to write to the config file.
|
|
242
242
|
*/
|
|
243
|
-
setConfig: (
|
|
243
|
+
setConfig: (s) => {
|
|
244
244
|
const t = `// This file is automatically generated by the Cyberskill CLI.
|
|
245
|
-
module.exports = ${JSON.stringify(
|
|
246
|
-
q(
|
|
245
|
+
module.exports = ${JSON.stringify(s, null, 4)}`;
|
|
246
|
+
q(w.MIGRATE_MONGO_CONFIG, t);
|
|
247
247
|
const e = `
|
|
248
248
|
${L}
|
|
249
249
|
`;
|
|
250
|
-
Z(
|
|
251
|
-
`).includes(L) || te(
|
|
250
|
+
Z(w.GIT_IGNORE) ? ee(w.GIT_IGNORE, "utf-8").split(`
|
|
251
|
+
`).includes(L) || te(w.GIT_IGNORE, e) : q(w.GIT_IGNORE, e);
|
|
252
252
|
}
|
|
253
253
|
}),
|
|
254
254
|
/**
|
|
@@ -260,20 +260,20 @@ ${L}
|
|
|
260
260
|
* @param fields - An array of field names to convert to regex patterns.
|
|
261
261
|
* @returns A new filter object with string values converted to regex patterns.
|
|
262
262
|
*/
|
|
263
|
-
regexify(
|
|
264
|
-
if (!
|
|
263
|
+
regexify(s, t) {
|
|
264
|
+
if (!s)
|
|
265
265
|
return {};
|
|
266
|
-
let e = B(
|
|
266
|
+
let e = B(s);
|
|
267
267
|
if (!t || t.length === 0)
|
|
268
268
|
return e;
|
|
269
269
|
for (const r of t) {
|
|
270
|
-
const
|
|
271
|
-
if (typeof
|
|
270
|
+
const n = r.toString().split("."), u = X(e, n);
|
|
271
|
+
if (typeof u == "string" && u.length > 0) {
|
|
272
272
|
const l = {
|
|
273
|
-
$regex: `.*${Y(
|
|
273
|
+
$regex: `.*${Y(u)}.*`,
|
|
274
274
|
$options: "i"
|
|
275
275
|
};
|
|
276
|
-
e = H(e,
|
|
276
|
+
e = H(e, n, l);
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
279
|
return e;
|
|
@@ -284,8 +284,8 @@ ${L}
|
|
|
284
284
|
* @param options - The virtual options to check.
|
|
285
285
|
* @returns True if the options contain a dynamic ref function.
|
|
286
286
|
*/
|
|
287
|
-
isDynamicVirtual(
|
|
288
|
-
return !!(
|
|
287
|
+
isDynamicVirtual(s) {
|
|
288
|
+
return !!(s && typeof s.ref == "function");
|
|
289
289
|
},
|
|
290
290
|
/**
|
|
291
291
|
* Generic utility function to get new records from the database
|
|
@@ -294,12 +294,12 @@ ${L}
|
|
|
294
294
|
* @param filterFn - Function to determine if a record already exists
|
|
295
295
|
* @returns Array of records that don't exist in the database
|
|
296
296
|
*/
|
|
297
|
-
getNewRecords(
|
|
297
|
+
getNewRecords(s, t, e) {
|
|
298
298
|
return a(this, null, function* () {
|
|
299
|
-
const r = yield
|
|
299
|
+
const r = yield s.findAll({});
|
|
300
300
|
return r.success ? t.filter(
|
|
301
|
-
(
|
|
302
|
-
(l) => e(l,
|
|
301
|
+
(u) => !r.result.some(
|
|
302
|
+
(l) => e(l, u)
|
|
303
303
|
)
|
|
304
304
|
) : t;
|
|
305
305
|
});
|
|
@@ -311,65 +311,68 @@ ${L}
|
|
|
311
311
|
* @param filterFn - Function to determine if a record exists
|
|
312
312
|
* @returns Array of existing records that match the filter criteria
|
|
313
313
|
*/
|
|
314
|
-
getExistingRecords(
|
|
314
|
+
getExistingRecords(s, t, e) {
|
|
315
315
|
return a(this, null, function* () {
|
|
316
|
-
const r = yield
|
|
316
|
+
const r = yield s.findAll({});
|
|
317
317
|
return r.success ? r.result.filter(
|
|
318
|
-
(
|
|
319
|
-
(l) => e(
|
|
318
|
+
(u) => t.some(
|
|
319
|
+
(l) => e(u, l)
|
|
320
320
|
)
|
|
321
321
|
) : [];
|
|
322
322
|
});
|
|
323
323
|
}
|
|
324
324
|
};
|
|
325
|
-
function C(
|
|
326
|
-
if (!
|
|
327
|
-
return
|
|
325
|
+
function C(s, t) {
|
|
326
|
+
if (!s || !t || t.length === 0)
|
|
327
|
+
return s;
|
|
328
328
|
const e = new Set(t.map((r) => r.name));
|
|
329
|
-
if (Array.isArray(
|
|
330
|
-
const r =
|
|
331
|
-
if (typeof
|
|
332
|
-
return !e.has(
|
|
333
|
-
if (typeof
|
|
334
|
-
const
|
|
335
|
-
return !e.has(
|
|
329
|
+
if (Array.isArray(s)) {
|
|
330
|
+
const r = s.filter((n) => {
|
|
331
|
+
if (typeof n == "string")
|
|
332
|
+
return !e.has(n);
|
|
333
|
+
if (typeof n == "object" && n !== null) {
|
|
334
|
+
const u = n;
|
|
335
|
+
return !e.has(u.path || u.populate || "");
|
|
336
336
|
}
|
|
337
337
|
return !0;
|
|
338
338
|
});
|
|
339
339
|
return r.length > 0 ? r : void 0;
|
|
340
340
|
}
|
|
341
|
-
if (typeof
|
|
342
|
-
return e.has(
|
|
343
|
-
if (typeof
|
|
344
|
-
const r =
|
|
345
|
-
return e.has(
|
|
341
|
+
if (typeof s == "string")
|
|
342
|
+
return e.has(s) ? void 0 : s;
|
|
343
|
+
if (typeof s == "object" && s !== null) {
|
|
344
|
+
const r = s, n = r.path || r.populate || "";
|
|
345
|
+
return e.has(n) ? void 0 : s;
|
|
346
346
|
}
|
|
347
|
-
return
|
|
347
|
+
return s;
|
|
348
348
|
}
|
|
349
|
-
function de(
|
|
350
|
-
if (!
|
|
349
|
+
function de(s, t, e) {
|
|
350
|
+
if (!s.length || !t || !(e != null && e.ref))
|
|
351
351
|
return [];
|
|
352
352
|
const r = /* @__PURE__ */ new Map();
|
|
353
|
-
return
|
|
353
|
+
return s.forEach((n) => {
|
|
354
354
|
try {
|
|
355
|
-
const
|
|
356
|
-
if (
|
|
355
|
+
const u = e.ref(n);
|
|
356
|
+
if (u == null)
|
|
357
357
|
return;
|
|
358
|
-
const l = typeof
|
|
358
|
+
const l = typeof u == "string" ? u : String(u);
|
|
359
359
|
if (l && l.trim() !== "") {
|
|
360
360
|
const c = ae(l);
|
|
361
|
-
r.has(c) || r.set(c, []), r.get(c).push(
|
|
361
|
+
r.has(c) || r.set(c, []), r.get(c).push(n);
|
|
362
362
|
}
|
|
363
|
-
} catch (
|
|
364
|
-
f(new Error(`Dynamic ref function failed for virtual "${t}": ${
|
|
363
|
+
} catch (u) {
|
|
364
|
+
f(new Error(`Dynamic ref function failed for virtual "${t}": ${u instanceof Error ? u.message : String(u)}`));
|
|
365
365
|
}
|
|
366
|
-
}), Array.from(r.entries()).map(([
|
|
366
|
+
}), Array.from(r.entries()).map(([n, u]) => ({ model: n, docs: u }));
|
|
367
367
|
}
|
|
368
|
-
function
|
|
368
|
+
function fe(s) {
|
|
369
|
+
return s !== null && typeof s == "object" && "toObject" in s && typeof s.toObject == "function";
|
|
370
|
+
}
|
|
371
|
+
function z(s, t, e, r, n) {
|
|
369
372
|
return a(this, null, function* () {
|
|
370
373
|
if (!t.length || !e.length || !r)
|
|
371
374
|
return t;
|
|
372
|
-
const
|
|
375
|
+
const u = e.filter((o) => {
|
|
373
376
|
if (Array.isArray(r))
|
|
374
377
|
return r.length > 0 && r.some((i) => typeof i == "string" ? i === o.name : i.path === o.name);
|
|
375
378
|
if (typeof r == "string")
|
|
@@ -380,16 +383,16 @@ function z(u, t, e, r, s) {
|
|
|
380
383
|
}
|
|
381
384
|
return !1;
|
|
382
385
|
});
|
|
383
|
-
if (
|
|
386
|
+
if (u.length === 0)
|
|
384
387
|
return t;
|
|
385
|
-
const l = B(t);
|
|
388
|
+
const l = B(t.map((o) => fe(o) ? o.toObject() : o));
|
|
386
389
|
l.forEach((o) => {
|
|
387
|
-
|
|
390
|
+
u.forEach(({ name: i, options: d }) => {
|
|
388
391
|
i in o || (o[i] = d.count ? 0 : d.justOne ? null : []);
|
|
389
392
|
});
|
|
390
393
|
});
|
|
391
394
|
const c = /* @__PURE__ */ new Map();
|
|
392
|
-
for (const o of
|
|
395
|
+
for (const o of u) {
|
|
393
396
|
const { name: i, options: d } = o, h = de(l, i, d);
|
|
394
397
|
for (const m of h) {
|
|
395
398
|
c.has(m.model) || c.set(m.model, {
|
|
@@ -407,16 +410,16 @@ function z(u, t, e, r, s) {
|
|
|
407
410
|
R.add(E);
|
|
408
411
|
let S = -1;
|
|
409
412
|
const b = N;
|
|
410
|
-
b.id !== void 0 ? S = l.findIndex((
|
|
413
|
+
b.id !== void 0 ? S = l.findIndex((M) => M.id === b.id) : b._id !== void 0 && (S = l.findIndex((M) => {
|
|
411
414
|
var O, g, _, p;
|
|
412
|
-
return ((g = (O =
|
|
415
|
+
return ((g = (O = M._id) == null ? void 0 : O.toString) == null ? void 0 : g.call(O)) === ((p = (_ = b._id) == null ? void 0 : _.toString) == null ? void 0 : p.call(_));
|
|
413
416
|
})), S !== -1 && (y.docsByLocalValue.has(E) || y.docsByLocalValue.set(E, []), y.docsByLocalValue.get(E).push(S));
|
|
414
417
|
}
|
|
415
418
|
});
|
|
416
419
|
}
|
|
417
420
|
}
|
|
418
421
|
return yield Promise.all(Array.from(c.entries()).map((o) => a(null, [o], function* ([i, d]) {
|
|
419
|
-
const h =
|
|
422
|
+
const h = s.models[i];
|
|
420
423
|
if (!h)
|
|
421
424
|
return;
|
|
422
425
|
const m = /* @__PURE__ */ new Set();
|
|
@@ -427,15 +430,15 @@ function z(u, t, e, r, s) {
|
|
|
427
430
|
const y = [...new Set(d.virtuals.map((E) => E.options.foreignField))], R = Array.from(m);
|
|
428
431
|
let N;
|
|
429
432
|
y.length === 1 ? N = { [String(y[0])]: { $in: R } } : N = { $or: y.map((E) => ({ [E]: { $in: R } })) };
|
|
430
|
-
const j = yield h.find(N,
|
|
433
|
+
const j = yield h.find(N, n).lean();
|
|
431
434
|
for (const E of d.virtuals) {
|
|
432
|
-
const { name: S, options: b } = E,
|
|
435
|
+
const { name: S, options: b } = E, M = j.filter((O) => {
|
|
433
436
|
const g = O[b.foreignField];
|
|
434
437
|
return g != null && m.has(String(g));
|
|
435
438
|
});
|
|
436
439
|
if (b.count) {
|
|
437
440
|
const O = /* @__PURE__ */ new Map();
|
|
438
|
-
|
|
441
|
+
M.forEach((g) => {
|
|
439
442
|
var _;
|
|
440
443
|
const p = (_ = g[b.foreignField]) == null ? void 0 : _.toString();
|
|
441
444
|
p && O.set(p, (O.get(p) || 0) + 1);
|
|
@@ -448,7 +451,7 @@ function z(u, t, e, r, s) {
|
|
|
448
451
|
});
|
|
449
452
|
} else {
|
|
450
453
|
const O = /* @__PURE__ */ new Map();
|
|
451
|
-
|
|
454
|
+
M.forEach((g) => {
|
|
452
455
|
var _;
|
|
453
456
|
const p = (_ = g[b.foreignField]) == null ? void 0 : _.toString();
|
|
454
457
|
p && (O.has(p) || O.set(p, []), O.get(p).push(g));
|
|
@@ -464,7 +467,7 @@ function z(u, t, e, r, s) {
|
|
|
464
467
|
}))), l;
|
|
465
468
|
});
|
|
466
469
|
}
|
|
467
|
-
class
|
|
470
|
+
class Ve {
|
|
468
471
|
/**
|
|
469
472
|
* Creates a new MongoDB controller instance.
|
|
470
473
|
*
|
|
@@ -509,7 +512,7 @@ class Ne {
|
|
|
509
512
|
createMany(t) {
|
|
510
513
|
return a(this, null, function* () {
|
|
511
514
|
try {
|
|
512
|
-
const e = t.map((
|
|
515
|
+
const e = t.map((n) => v(v({}, x.createGenericFields()), n)), r = yield this.collection.insertMany(e);
|
|
513
516
|
return r.insertedCount === 0 ? {
|
|
514
517
|
success: !1,
|
|
515
518
|
message: "No documents were inserted",
|
|
@@ -682,7 +685,7 @@ class Ne {
|
|
|
682
685
|
});
|
|
683
686
|
}
|
|
684
687
|
}
|
|
685
|
-
class
|
|
688
|
+
class xe {
|
|
686
689
|
/**
|
|
687
690
|
* Creates a new Mongoose controller instance.
|
|
688
691
|
*
|
|
@@ -718,8 +721,8 @@ class Ve {
|
|
|
718
721
|
return a(this, null, function* () {
|
|
719
722
|
const r = this.getDynamicVirtuals();
|
|
720
723
|
if (r && r.length > 0) {
|
|
721
|
-
const
|
|
722
|
-
return
|
|
724
|
+
const n = yield z(this.model.base, [t], r, e);
|
|
725
|
+
return n && n[0] ? n[0] : t;
|
|
723
726
|
}
|
|
724
727
|
return t;
|
|
725
728
|
});
|
|
@@ -748,10 +751,10 @@ class Ve {
|
|
|
748
751
|
* @returns A promise that resolves to a standardized response with the found document.
|
|
749
752
|
*/
|
|
750
753
|
findOne() {
|
|
751
|
-
return a(this, arguments, function* (t = {}, e = {}, r = {},
|
|
752
|
-
var
|
|
754
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
755
|
+
var u, l;
|
|
753
756
|
try {
|
|
754
|
-
const c = V(t), o = this.model.findOne(c, e, r), i = this.getDynamicVirtuals(), d = C(
|
|
757
|
+
const c = V(t), o = this.model.findOne(c, e, r), i = this.getDynamicVirtuals(), d = C(n, i);
|
|
755
758
|
d && o.populate(d);
|
|
756
759
|
const h = yield o.exec();
|
|
757
760
|
if (!h)
|
|
@@ -760,8 +763,8 @@ class Ve {
|
|
|
760
763
|
message: `No ${this.getModelName()} found.`,
|
|
761
764
|
code: D.NOT_FOUND.CODE
|
|
762
765
|
};
|
|
763
|
-
const m = yield this.populateDynamicVirtualsForDocument(h,
|
|
764
|
-
return { success: !0, result: (l = (
|
|
766
|
+
const m = yield this.populateDynamicVirtualsForDocument(h, n);
|
|
767
|
+
return { success: !0, result: (l = (u = m == null ? void 0 : m.toObject) == null ? void 0 : u.call(m)) != null ? l : m };
|
|
765
768
|
} catch (c) {
|
|
766
769
|
return f(c);
|
|
767
770
|
}
|
|
@@ -778,17 +781,17 @@ class Ve {
|
|
|
778
781
|
* @returns A promise that resolves to a standardized response with the found documents.
|
|
779
782
|
*/
|
|
780
783
|
findAll() {
|
|
781
|
-
return a(this, arguments, function* (t = {}, e = {}, r = {},
|
|
784
|
+
return a(this, arguments, function* (t = {}, e = {}, r = {}, n) {
|
|
782
785
|
try {
|
|
783
|
-
const
|
|
786
|
+
const u = V(t), l = this.model.find(u, e, r), c = this.getDynamicVirtuals(), o = C(n, c);
|
|
784
787
|
o && l.populate(o);
|
|
785
788
|
const i = yield l.exec();
|
|
786
|
-
return { success: !0, result: (yield this.populateDynamicVirtualsForDocuments(i,
|
|
789
|
+
return { success: !0, result: (yield this.populateDynamicVirtualsForDocuments(i, n)).map((h) => {
|
|
787
790
|
var m, y;
|
|
788
791
|
return (y = (m = h == null ? void 0 : h.toObject) == null ? void 0 : m.call(h)) != null ? y : h;
|
|
789
792
|
}) };
|
|
790
|
-
} catch (
|
|
791
|
-
return f(
|
|
793
|
+
} catch (u) {
|
|
794
|
+
return f(u);
|
|
792
795
|
}
|
|
793
796
|
});
|
|
794
797
|
}
|
|
@@ -803,10 +806,10 @@ class Ve {
|
|
|
803
806
|
findPaging() {
|
|
804
807
|
return a(this, arguments, function* (t = {}, e = {}) {
|
|
805
808
|
try {
|
|
806
|
-
const r = V(t),
|
|
807
|
-
e.populate && (
|
|
808
|
-
const l = yield this.model.paginate(r,
|
|
809
|
-
return { success: !0, result:
|
|
809
|
+
const r = V(t), n = this.getDynamicVirtuals(), u = v({}, e);
|
|
810
|
+
e.populate && (u.populate = C(e.populate, n));
|
|
811
|
+
const l = yield this.model.paginate(r, u), c = yield this.populateDynamicVirtualsForDocuments(l.docs, e.populate);
|
|
812
|
+
return { success: !0, result: F(v({}, l), { docs: c.map((o) => {
|
|
810
813
|
var i, d;
|
|
811
814
|
return (d = (i = o == null ? void 0 : o.toObject) == null ? void 0 : i.call(o)) != null ? d : o;
|
|
812
815
|
}) }) };
|
|
@@ -825,18 +828,18 @@ class Ve {
|
|
|
825
828
|
findPagingAggregate(t) {
|
|
826
829
|
return a(this, arguments, function* (e, r = {}) {
|
|
827
830
|
try {
|
|
828
|
-
const
|
|
829
|
-
r.populate && (
|
|
831
|
+
const n = this.getDynamicVirtuals(), u = v({}, r);
|
|
832
|
+
r.populate && (u.populate = C(r.populate, n));
|
|
830
833
|
const l = yield this.model.aggregatePaginate(
|
|
831
834
|
this.model.aggregate(e),
|
|
832
|
-
|
|
835
|
+
u
|
|
833
836
|
), c = yield this.populateDynamicVirtualsForDocuments(l.docs, r.populate);
|
|
834
|
-
return { success: !0, result:
|
|
837
|
+
return { success: !0, result: F(v({}, l), { docs: c.map((o) => {
|
|
835
838
|
var i, d;
|
|
836
839
|
return (d = (i = o == null ? void 0 : o.toObject) == null ? void 0 : i.call(o)) != null ? d : o;
|
|
837
840
|
}) }) };
|
|
838
|
-
} catch (
|
|
839
|
-
return f(
|
|
841
|
+
} catch (n) {
|
|
842
|
+
return f(n);
|
|
840
843
|
}
|
|
841
844
|
});
|
|
842
845
|
}
|
|
@@ -866,10 +869,10 @@ class Ve {
|
|
|
866
869
|
return a(this, null, function* () {
|
|
867
870
|
var e, r;
|
|
868
871
|
try {
|
|
869
|
-
const
|
|
870
|
-
return { success: !0, result: (r = (e =
|
|
871
|
-
} catch (
|
|
872
|
-
return f(
|
|
872
|
+
const n = yield this.model.create(t);
|
|
873
|
+
return { success: !0, result: (r = (e = n == null ? void 0 : n.toObject) == null ? void 0 : e.call(n)) != null ? r : n };
|
|
874
|
+
} catch (n) {
|
|
875
|
+
return f(n);
|
|
873
876
|
}
|
|
874
877
|
});
|
|
875
878
|
}
|
|
@@ -883,12 +886,12 @@ class Ve {
|
|
|
883
886
|
createMany(t) {
|
|
884
887
|
return a(this, arguments, function* (e, r = {}) {
|
|
885
888
|
try {
|
|
886
|
-
return { success: !0, result: (yield this.model.insertMany(e, r)).map((
|
|
889
|
+
return { success: !0, result: (yield this.model.insertMany(e, r)).map((u) => {
|
|
887
890
|
var l, c;
|
|
888
|
-
return (c = (l =
|
|
891
|
+
return (c = (l = u == null ? void 0 : u.toObject) == null ? void 0 : l.call(u)) != null ? c : u;
|
|
889
892
|
}) };
|
|
890
|
-
} catch (
|
|
891
|
-
return f(
|
|
893
|
+
} catch (n) {
|
|
894
|
+
return f(n);
|
|
892
895
|
}
|
|
893
896
|
});
|
|
894
897
|
}
|
|
@@ -902,12 +905,12 @@ class Ve {
|
|
|
902
905
|
*/
|
|
903
906
|
updateOne() {
|
|
904
907
|
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
905
|
-
var
|
|
908
|
+
var n, u;
|
|
906
909
|
try {
|
|
907
910
|
const l = V(t), c = yield this.model.findOneAndUpdate(l, e, v({
|
|
908
911
|
new: !0
|
|
909
912
|
}, r)).exec();
|
|
910
|
-
return c ? { success: !0, result: (
|
|
913
|
+
return c ? { success: !0, result: (u = (n = c == null ? void 0 : c.toObject) == null ? void 0 : n.call(c)) != null ? u : c } : {
|
|
911
914
|
success: !1,
|
|
912
915
|
message: `Failed to update ${this.getModelName()}.`,
|
|
913
916
|
code: D.NOT_FOUND.CODE
|
|
@@ -928,10 +931,10 @@ class Ve {
|
|
|
928
931
|
updateMany() {
|
|
929
932
|
return a(this, arguments, function* (t = {}, e = {}, r = {}) {
|
|
930
933
|
try {
|
|
931
|
-
const
|
|
932
|
-
return { success: !0, result: yield this.model.updateMany(
|
|
933
|
-
} catch (
|
|
934
|
-
return f(
|
|
934
|
+
const n = V(t);
|
|
935
|
+
return { success: !0, result: yield this.model.updateMany(n, e, r).exec() };
|
|
936
|
+
} catch (n) {
|
|
937
|
+
return f(n);
|
|
935
938
|
}
|
|
936
939
|
});
|
|
937
940
|
}
|
|
@@ -944,16 +947,16 @@ class Ve {
|
|
|
944
947
|
*/
|
|
945
948
|
deleteOne() {
|
|
946
949
|
return a(this, arguments, function* (t = {}, e = {}) {
|
|
947
|
-
var r,
|
|
950
|
+
var r, n;
|
|
948
951
|
try {
|
|
949
|
-
const
|
|
950
|
-
return l ? { success: !0, result: (
|
|
952
|
+
const u = V(t), l = yield this.model.findOneAndDelete(u, e).exec();
|
|
953
|
+
return l ? { success: !0, result: (n = (r = l == null ? void 0 : l.toObject) == null ? void 0 : r.call(l)) != null ? n : l } : {
|
|
951
954
|
success: !1,
|
|
952
955
|
message: `No ${this.getModelName()} found to delete.`,
|
|
953
956
|
code: D.NOT_FOUND.CODE
|
|
954
957
|
};
|
|
955
|
-
} catch (
|
|
956
|
-
return f(
|
|
958
|
+
} catch (u) {
|
|
959
|
+
return f(u);
|
|
957
960
|
}
|
|
958
961
|
});
|
|
959
962
|
}
|
|
@@ -967,12 +970,12 @@ class Ve {
|
|
|
967
970
|
deleteMany() {
|
|
968
971
|
return a(this, arguments, function* (t = {}, e = {}) {
|
|
969
972
|
try {
|
|
970
|
-
const r = V(t),
|
|
971
|
-
return
|
|
973
|
+
const r = V(t), n = yield this.model.deleteMany(r, e).exec();
|
|
974
|
+
return n.deletedCount === 0 ? {
|
|
972
975
|
success: !1,
|
|
973
976
|
message: "No documents found to delete.",
|
|
974
977
|
code: D.NOT_FOUND.CODE
|
|
975
|
-
} : { success: !0, result:
|
|
978
|
+
} : { success: !0, result: n };
|
|
976
979
|
} catch (r) {
|
|
977
980
|
return f(r);
|
|
978
981
|
}
|
|
@@ -989,11 +992,11 @@ class Ve {
|
|
|
989
992
|
createShortId(t, e = 4) {
|
|
990
993
|
return a(this, null, function* () {
|
|
991
994
|
try {
|
|
992
|
-
const
|
|
993
|
-
|
|
995
|
+
const n = Array.from({ length: 10 }, (c, o) => ne(t, o + e)), l = (yield Promise.all(
|
|
996
|
+
n.map((c) => this.model.exists({ shortId: c }))
|
|
994
997
|
)).findIndex((c) => !c);
|
|
995
998
|
if (l !== -1) {
|
|
996
|
-
const c =
|
|
999
|
+
const c = n[l];
|
|
997
1000
|
if (c)
|
|
998
1001
|
return { success: !0, result: c };
|
|
999
1002
|
}
|
|
@@ -1019,17 +1022,17 @@ class Ve {
|
|
|
1019
1022
|
* @param options.filter - Additional filter conditions to apply to the query.
|
|
1020
1023
|
* @returns A MongoDB query object for checking slug existence.
|
|
1021
1024
|
*/
|
|
1022
|
-
createSlugQuery({ slug: t, field: e, isObject: r, haveHistory:
|
|
1023
|
-
const l = v({},
|
|
1024
|
-
return r ?
|
|
1025
|
+
createSlugQuery({ slug: t, field: e, isObject: r, haveHistory: n = !1, filter: u }) {
|
|
1026
|
+
const l = v({}, u != null ? u : {});
|
|
1027
|
+
return r ? F(v({}, l), {
|
|
1025
1028
|
$or: [
|
|
1026
1029
|
{ [`slug.${e}`]: t },
|
|
1027
|
-
...
|
|
1030
|
+
...n ? [{ slugHistory: { $elemMatch: { [`slug.${e}`]: t } } }] : []
|
|
1028
1031
|
]
|
|
1029
|
-
}) :
|
|
1032
|
+
}) : F(v({}, l), {
|
|
1030
1033
|
$or: [
|
|
1031
1034
|
{ slug: t },
|
|
1032
|
-
...
|
|
1035
|
+
...n ? [{ slugHistory: t }] : []
|
|
1033
1036
|
]
|
|
1034
1037
|
});
|
|
1035
1038
|
}
|
|
@@ -1046,18 +1049,18 @@ class Ve {
|
|
|
1046
1049
|
* @returns A promise that resolves to a unique slug string.
|
|
1047
1050
|
*/
|
|
1048
1051
|
createUniqueSlug(t) {
|
|
1049
|
-
return a(this, arguments, function* ({ slug: e, field: r, isObject:
|
|
1052
|
+
return a(this, arguments, function* ({ slug: e, field: r, isObject: n, haveHistory: u, filter: l }) {
|
|
1050
1053
|
if (!e || typeof e != "string")
|
|
1051
1054
|
throw new Error("Invalid slug provided: must be a non-empty string");
|
|
1052
1055
|
const c = I(e);
|
|
1053
1056
|
if (!(yield this.model.exists(
|
|
1054
|
-
this.createSlugQuery({ slug: c, field: r, isObject:
|
|
1057
|
+
this.createSlugQuery({ slug: c, field: r, isObject: n, haveHistory: u, filter: l })
|
|
1055
1058
|
)))
|
|
1056
1059
|
return c;
|
|
1057
1060
|
for (let h = 1; h <= J; h++) {
|
|
1058
1061
|
const m = `${c}-${h}`;
|
|
1059
1062
|
if (!(yield this.model.exists(
|
|
1060
|
-
this.createSlugQuery({ slug: m, field: r, isObject:
|
|
1063
|
+
this.createSlugQuery({ slug: m, field: r, isObject: n, haveHistory: u, filter: l })
|
|
1061
1064
|
)))
|
|
1062
1065
|
return m;
|
|
1063
1066
|
}
|
|
@@ -1077,7 +1080,7 @@ class Ve {
|
|
|
1077
1080
|
* @returns A promise that resolves to a standardized response with the created slug(s).
|
|
1078
1081
|
*/
|
|
1079
1082
|
createSlug(t) {
|
|
1080
|
-
return a(this, arguments, function* ({ field: e, from: r, filter:
|
|
1083
|
+
return a(this, arguments, function* ({ field: e, from: r, filter: n, haveHistory: u }) {
|
|
1081
1084
|
try {
|
|
1082
1085
|
const l = r[e];
|
|
1083
1086
|
return T(l) ? { success: !0, result: Object.fromEntries(
|
|
@@ -1087,8 +1090,8 @@ class Ve {
|
|
|
1087
1090
|
slug: m,
|
|
1088
1091
|
field: h,
|
|
1089
1092
|
isObject: !0,
|
|
1090
|
-
haveHistory:
|
|
1091
|
-
filter:
|
|
1093
|
+
haveHistory: u,
|
|
1094
|
+
filter: n
|
|
1092
1095
|
});
|
|
1093
1096
|
return [h, y];
|
|
1094
1097
|
}))
|
|
@@ -1097,8 +1100,8 @@ class Ve {
|
|
|
1097
1100
|
slug: l,
|
|
1098
1101
|
field: e,
|
|
1099
1102
|
isObject: !1,
|
|
1100
|
-
haveHistory:
|
|
1101
|
-
filter:
|
|
1103
|
+
haveHistory: u,
|
|
1104
|
+
filter: n
|
|
1102
1105
|
}) };
|
|
1103
1106
|
} catch (l) {
|
|
1104
1107
|
return f(l);
|
|
@@ -1118,9 +1121,9 @@ class Ve {
|
|
|
1118
1121
|
* @returns A promise that resolves to a standardized response indicating whether the slug exists.
|
|
1119
1122
|
*/
|
|
1120
1123
|
checkSlug(t) {
|
|
1121
|
-
return a(this, arguments, function* ({ slug: e, field: r, from:
|
|
1124
|
+
return a(this, arguments, function* ({ slug: e, field: r, from: n, filter: u, haveHistory: l }) {
|
|
1122
1125
|
try {
|
|
1123
|
-
const c =
|
|
1126
|
+
const c = n[r];
|
|
1124
1127
|
if (T(c)) {
|
|
1125
1128
|
const m = Object.values(c).map((R) => I(R));
|
|
1126
1129
|
return (yield Promise.all(
|
|
@@ -1130,7 +1133,7 @@ class Ve {
|
|
|
1130
1133
|
field: r,
|
|
1131
1134
|
isObject: !0,
|
|
1132
1135
|
haveHistory: l,
|
|
1133
|
-
filter:
|
|
1136
|
+
filter: u
|
|
1134
1137
|
}))
|
|
1135
1138
|
)
|
|
1136
1139
|
)).some((R) => R) ? { success: !0, result: !0 } : { success: !0, result: !1 };
|
|
@@ -1140,7 +1143,7 @@ class Ve {
|
|
|
1140
1143
|
slug: i,
|
|
1141
1144
|
field: r,
|
|
1142
1145
|
isObject: !1,
|
|
1143
|
-
filter:
|
|
1146
|
+
filter: u
|
|
1144
1147
|
}))) !== null };
|
|
1145
1148
|
} catch (c) {
|
|
1146
1149
|
return f(c);
|
|
@@ -1171,17 +1174,17 @@ class Ve {
|
|
|
1171
1174
|
* @returns A promise that resolves to a standardized response with the array of distinct values.
|
|
1172
1175
|
*/
|
|
1173
1176
|
distinct(t) {
|
|
1174
|
-
return a(this, arguments, function* (e, r = {},
|
|
1177
|
+
return a(this, arguments, function* (e, r = {}, n = {}) {
|
|
1175
1178
|
try {
|
|
1176
|
-
return { success: !0, result: yield this.model.distinct(e, r,
|
|
1177
|
-
} catch (
|
|
1178
|
-
return f(
|
|
1179
|
+
return { success: !0, result: yield this.model.distinct(e, r, n) };
|
|
1180
|
+
} catch (u) {
|
|
1181
|
+
return f(u);
|
|
1179
1182
|
}
|
|
1180
1183
|
});
|
|
1181
1184
|
}
|
|
1182
1185
|
}
|
|
1183
1186
|
export {
|
|
1184
|
-
|
|
1185
|
-
|
|
1187
|
+
Ve as MongoController,
|
|
1188
|
+
xe as MongooseController,
|
|
1186
1189
|
x as mongo
|
|
1187
1190
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cyberskill/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.13.0",
|
|
5
5
|
"description": "CyberSkill Shared",
|
|
6
6
|
"author": "Stephen Cheng",
|
|
7
7
|
"license": "MIT",
|
|
@@ -207,7 +207,7 @@
|
|
|
207
207
|
"test:unit": "tsx src/node/cli/index.ts test:unit"
|
|
208
208
|
},
|
|
209
209
|
"dependencies": {
|
|
210
|
-
"@antfu/eslint-config": "5.
|
|
210
|
+
"@antfu/eslint-config": "5.1.0",
|
|
211
211
|
"@apollo/client": "3.13.9",
|
|
212
212
|
"@apollo/client-integration-nextjs": "0.12.3",
|
|
213
213
|
"@apollo/server": "5.0.0",
|
|
@@ -299,13 +299,13 @@
|
|
|
299
299
|
"eslint": "9.32.0",
|
|
300
300
|
"eslint-config-next": "15.4.5",
|
|
301
301
|
"glob": "11.0.3",
|
|
302
|
-
"lint-staged": "16.1.
|
|
302
|
+
"lint-staged": "16.1.4",
|
|
303
303
|
"node-modules-inspector": "1.0.0",
|
|
304
304
|
"npm-run-all2": "8.0.4",
|
|
305
305
|
"sass": "1.89.2",
|
|
306
|
-
"simple-git-hooks": "2.13.
|
|
306
|
+
"simple-git-hooks": "2.13.1",
|
|
307
307
|
"tsx": "4.20.3",
|
|
308
|
-
"typescript": "5.
|
|
308
|
+
"typescript": "5.9.2",
|
|
309
309
|
"vite": "7.0.6",
|
|
310
310
|
"vite-plugin-dts": "4.5.4",
|
|
311
311
|
"vitest": "3.2.4"
|