@cyberskill/shared 3.8.0 → 3.10.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/graphql-codegen/graphql-codegen.util.js +1 -1
- package/dist/config/graphql-codegen/graphql-codegen.util.js.map +1 -1
- package/dist/config/lint-staged/index.js +1 -1
- package/dist/config/lint-staged/index.js.map +1 -1
- package/dist/config/vitest/vitest.e2e.js +1 -1
- package/dist/config/vitest/vitest.unit.js +1 -1
- package/dist/node/cli/index.js +5 -1
- package/dist/node/cli/index.js.map +1 -1
- package/dist/node/express/express.util.js +2 -2
- package/dist/node/express/express.util.js.map +1 -1
- package/dist/node/fs/fs.util.js +14 -14
- package/dist/node/mongo/index.js +5 -5
- package/dist/node/mongo/mongo.controller.mongoose.js +86 -85
- package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
- package/dist/node/mongo/mongo.dynamic-populate.d.ts +0 -4
- package/dist/node/mongo/mongo.dynamic-populate.js +14 -17
- package/dist/node/mongo/mongo.dynamic-populate.js.map +1 -1
- package/dist/node/mongo/mongo.util.js +2 -0
- package/dist/node/mongo/mongo.util.js.map +1 -1
- package/dist/node/path/path.constant.js +1 -1
- package/dist/node/path/path.constant.js.map +1 -1
- package/dist/node/storage/storage.util.js +41 -35
- package/dist/node/storage/storage.util.js.map +1 -1
- package/dist/node/upload/upload.util.js +34 -28
- package/dist/node/upload/upload.util.js.map +1 -1
- package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.1_@types_nod_36acd00c2670b611b011192023dc5bd9/node_modules/vitest/dist/config.js +8 -0
- package/dist/node_modules/.pnpm/{vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5 → vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.1_@types_nod_36acd00c2670b611b011192023dc5bd9}/node_modules/vitest/dist/config.js.map +1 -1
- package/package.json +8 -8
- package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5/node_modules/vitest/dist/config.js +0 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.dynamic-populate.js","names":[],"sources":["../../../src/node/mongo/mongo.dynamic-populate.ts"],"sourcesContent":["import type mongooseRaw from 'mongoose';\n\nimport { deepClone } from '#util/index.js';\n\nimport type { I_ModelWithSchema } from './mongo.internal-types.js';\nimport type { I_DynamicVirtualConfig, I_DynamicVirtualOptions, T_Input_Populate } from './mongo.type.js';\n\nimport { catchError } from '../log/index.js';\nimport { applyNestedPopulate } from './mongo.populate.js';\nimport { convertEnumToModelName } from './mongo.util.js';\n\n/**\n * Checks if value is object-like (e.g., objects, arrays, etc.), not null.\n */\nexport function isObject(value: unknown): value is object {\n return value != null && typeof value === 'object';\n}\n\n/**\n * Filters out dynamic virtuals from populate options to prevent Mongoose from trying to populate them.\n * This function creates a new populate configuration that only includes regular virtuals.\n *\n * @template T - The document type\n * @param populate - The original populate options\n * @param dynamicVirtuals - Array of dynamic virtual configurations\n * @returns Filtered populate options excluding dynamic virtuals\n */\nexport function filterDynamicVirtualsFromPopulate<T>(\n populate: T_Input_Populate | undefined,\n dynamicVirtuals: I_DynamicVirtualConfig<T>[] | undefined,\n): T_Input_Populate | undefined {\n if (!populate || !dynamicVirtuals || dynamicVirtuals.length === 0) {\n return populate;\n }\n\n const dynamicVirtualNames = new Set(dynamicVirtuals.map(v => v.name));\n\n if (Array.isArray(populate)) {\n const filtered = populate.filter((p) => {\n if (typeof p === 'string') {\n return ![...dynamicVirtualNames].some(virtualName =>\n p === virtualName || p.startsWith(`${virtualName}.`),\n );\n }\n\n if (typeof p === 'object' && p !== null) {\n const popObj = p as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return ![...dynamicVirtualNames].some(virtualName =>\n path === virtualName || path.startsWith(`${virtualName}.`),\n );\n }\n\n return true;\n });\n\n return filtered.length > 0 ? (filtered as T_Input_Populate) : undefined;\n }\n\n if (typeof populate === 'string') {\n return [...dynamicVirtualNames].some(virtualName =>\n populate === virtualName || populate.startsWith(`${virtualName}.`),\n )\n ? undefined\n : populate;\n }\n\n if (typeof populate === 'object' && populate !== null) {\n const popObj = populate as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return [...dynamicVirtualNames].some(virtualName =>\n path === virtualName || path.startsWith(`${virtualName}.`),\n )\n ? undefined\n : populate;\n }\n\n return populate;\n}\n\n/**\n * Groups documents by the resolved model name for a dynamic virtual field.\n * Used to batch population queries for dynamic virtuals.\n *\n * @template T - The document type\n * @template R - The model name type (usually string or enum)\n * @param {T[]} documents - The array of documents to process\n * @param {string} virtualName - The name of the dynamic virtual field\n * @param {I_DynamicVirtualOptions<T, R>} virtualOptions - The dynamic virtual options (must include a ref function)\n * @returns {Array<{ model: string; docs: T[] }>} An array of groups, each with a model name and the docs for that model\n */\nexport function remapDynamicPopulate<T, R extends string = string>(\n documents: T[],\n virtualName: string,\n virtualOptions: I_DynamicVirtualOptions<T, R>,\n): Array<{ model: string; docs: T[] }> {\n if (!documents.length || !virtualName || !virtualOptions?.ref) {\n return [];\n }\n\n const modelGroups = new Map<string, T[]>();\n documents.forEach((doc) => {\n try {\n const modelName = virtualOptions.ref(doc);\n\n if (modelName === undefined || modelName === null) {\n return;\n }\n\n const modelNameString = typeof modelName === 'string' ? modelName : String(modelName);\n\n if (modelNameString && modelNameString.trim() !== '') {\n const convertedModelName = convertEnumToModelName(modelNameString);\n\n if (!modelGroups.has(convertedModelName)) {\n modelGroups.set(convertedModelName, []);\n }\n\n modelGroups.get(convertedModelName)!.push(doc);\n }\n }\n catch (error) {\n catchError(new Error(`Dynamic ref function failed for virtual \"${virtualName}\": ${error instanceof Error ? error.message : String(error)}`));\n }\n });\n\n return Array.from(modelGroups.entries(), ([model, docs]) => ({ model, docs }));\n}\n\n/**\n * Type guard to check if an object is a Mongoose Document (has toObject method).\n * @param obj - The object to check\n * @returns True if obj has a toObject function\n */\nexport function isMongooseDoc(obj: unknown): obj is { toObject: () => { [key: string]: unknown } } {\n return obj !== null && typeof obj === 'object' && 'toObject' in obj && typeof (obj as { toObject: unknown }).toObject === 'function';\n}\n\n/**\n * Optimized batch population for dynamic virtuals.\n *\n * - Groups documents by model and batches queries for each model.\n * - Populates all dynamic virtuals in parallel for maximum performance.\n * - Uses direct assignment for plain objects and skips already populated fields.\n * - Supports optional projection for population queries.\n * - Only populates virtuals that are explicitly requested in populate options.\n * - Reminder: Ensure indexes exist on foreignField in referenced collections for best performance.\n *\n * @template T - The document type (must be an object)\n * @template R - The model name type (usually string or enum)\n * @param {typeof mongooseRaw} mongoose - The Mongoose instance\n * @param {T[]} documents - The array of documents to populate\n * @param {I_DynamicVirtualConfig<T, R>[]} virtualConfigs - The dynamic virtual configurations to process\n * @param {T_Input_Populate} [populate] - Population options to determine which virtuals to populate\n * @param {Record<string, 0 | 1>} [projection] - Optional projection for population queries\n * @returns {Promise<object[]>} The array of documents with dynamic virtuals populated\n */\nexport async function populateDynamicVirtuals<T extends object, R extends string = string>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n virtualConfigs: I_DynamicVirtualConfig<T, R>[],\n populate?: T_Input_Populate,\n projection?: Record<string, 0 | 1>,\n startModel?: I_ModelWithSchema,\n): Promise<T[]> {\n if (!documents.length || !virtualConfigs.length) {\n return documents;\n }\n\n if (!populate) {\n return documents;\n }\n\n const requestedVirtuals = virtualConfigs.filter((config) => {\n if (Array.isArray(populate)) {\n return populate.length > 0 && populate.some((p) => {\n if (typeof p === 'string') {\n return p === config.name || p.startsWith(`${config.name}.`);\n }\n if (p && typeof p === 'object') {\n const popObj = p as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return path === config.name || path.startsWith(`${config.name}.`);\n }\n\n return false;\n });\n }\n\n if (typeof populate === 'string') {\n return populate === config.name || populate.startsWith(`${config.name}.`);\n }\n\n if (typeof populate === 'object' && populate !== null) {\n const popObj = populate as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return (path === config.name) || path.startsWith(`${config.name}.`);\n }\n\n return false;\n });\n\n if (requestedVirtuals.length === 0) {\n return documents;\n }\n\n // toObject() already creates plain copies for Mongoose docs; deepClone the\n // plain array to avoid mutating the caller's objects.\n const plainDocuments = documents.map(doc => isMongooseDoc(doc) ? doc.toObject() : doc);\n const clonedDocuments = deepClone(plainDocuments) as T[];\n\n clonedDocuments.forEach((doc) => {\n requestedVirtuals.forEach(({ name, options }) => {\n if (!(name in doc)) {\n (doc as { [key: string]: unknown })[name as string] = options.count ? 0 : (options.justOne ? null : []);\n }\n });\n });\n\n const modelProcessingMap = new Map<string, {\n virtuals: I_DynamicVirtualConfig<T, R>[];\n localValueSets: Map<string, Set<string>>;\n docsByLocalValue: Map<string, number[]>;\n }>();\n\n for (const virtualConfig of requestedVirtuals) {\n const { name, options } = virtualConfig;\n const populateGroups = remapDynamicPopulate(clonedDocuments, name, options);\n\n for (const group of populateGroups) {\n if (!modelProcessingMap.has(group.model)) {\n modelProcessingMap.set(group.model, {\n virtuals: [],\n localValueSets: new Map(),\n docsByLocalValue: new Map(),\n });\n }\n\n const processing = modelProcessingMap.get(group.model)!;\n\n if (!processing.virtuals.some(v => v.name === name)) {\n processing.virtuals.push(virtualConfig);\n processing.localValueSets.set(name as string, new Set());\n }\n\n const localValueSet = processing.localValueSets.get(name as string)!;\n group.docs.forEach((doc) => {\n const localVal = (doc as { [key: string]: unknown })[options.localField];\n\n if (localVal != null) {\n const strVal = String(localVal);\n localValueSet.add(strVal);\n\n let idx = -1;\n\n const docWithKeys = doc as { [key: string]: unknown };\n\n if (docWithKeys['id'] !== undefined) {\n idx = clonedDocuments.findIndex((d) => {\n const dWithKeys = d as { [key: string]: unknown };\n\n return dWithKeys['id'] === docWithKeys['id'];\n });\n }\n else if (docWithKeys['_id'] !== undefined) {\n idx = clonedDocuments.findIndex((d) => {\n const dWithKeys = d as { [key: string]: unknown };\n\n return dWithKeys['_id']?.toString?.() === docWithKeys['_id']?.toString?.();\n });\n }\n\n if (idx !== -1) {\n if (!processing.docsByLocalValue.has(strVal)) {\n processing.docsByLocalValue.set(strVal, []);\n }\n processing.docsByLocalValue.get(strVal)!.push(idx);\n }\n }\n });\n }\n }\n\n await Promise.all(Array.from(modelProcessingMap.entries(), async ([modelName, processing]) => {\n const Model = mongoose.models[modelName];\n\n if (!Model) {\n return;\n }\n\n const allLocalValues = new Set<string>();\n processing.localValueSets.forEach((localValueSet) => {\n localValueSet.forEach(val => allLocalValues.add(val));\n });\n\n if (allLocalValues.size === 0) {\n return;\n }\n\n const foreignFields = [...new Set(processing.virtuals.map(v => v.options.foreignField))];\n const localValuesArray = [...allLocalValues];\n let query;\n\n if (foreignFields.length === 1) {\n query = { [String(foreignFields[0])]: { $in: localValuesArray } };\n }\n else {\n query = { $or: foreignFields.map(field => ({ [field]: { $in: localValuesArray } })) };\n }\n\n const allPopulatedData = await Model.find(query, projection).lean();\n\n for (const virtualConfig of processing.virtuals) {\n const { name, options } = virtualConfig;\n const relevantData = allPopulatedData.filter((item) => {\n const foreignVal = (item)[options.foreignField];\n\n return foreignVal != null && allLocalValues.has(String(foreignVal));\n });\n\n if (options.count) {\n const countMap = new Map<string, number>();\n\n relevantData.forEach((item) => {\n const key = (item)[options.foreignField]?.toString();\n\n if (key) {\n countMap.set(key, (countMap.get(key) || 0) + 1);\n }\n });\n\n processing.localValueSets.get(name as string)!.forEach((localValue) => {\n const docs = processing.docsByLocalValue.get(localValue) || [];\n const count = countMap.get(localValue) || 0;\n\n docs.forEach((idx) => {\n const docToUpdate = clonedDocuments[idx] as { [key: string]: unknown };\n\n if (docToUpdate[name] === undefined) {\n docToUpdate[name] = count;\n }\n });\n });\n }\n else {\n const resultMap = new Map<string, T[]>();\n\n relevantData.forEach((item) => {\n const key = (item)[options.foreignField]?.toString();\n\n if (key) {\n if (!resultMap.has(key)) {\n resultMap.set(key, []);\n }\n resultMap.get(key)!.push(item);\n }\n });\n\n processing.localValueSets.get(name as string)!.forEach((localVal) => {\n const docs = processing.docsByLocalValue.get(localVal) || [];\n const results = resultMap.get(localVal) || [];\n const value = options.justOne ? (results[0] || null) : results;\n\n docs.forEach((idx) => {\n const docToUpdate = clonedDocuments[idx] as { [key: string]: unknown };\n docToUpdate[name] = value;\n });\n });\n }\n }\n }));\n\n if (populate) {\n const normalizePopulate = (pop: T_Input_Populate): T_Input_Populate => {\n const asArray = Array.isArray(pop) ? pop : [pop];\n const grouped = new Map<string, unknown[]>();\n const passthrough: unknown[] = [];\n\n for (const entry of asArray) {\n if (typeof entry === 'string') {\n if (entry.includes('.')) {\n const parts = entry.split('.');\n const first = parts[0] || '';\n const rest = parts.slice(1).join('.');\n\n if (first) {\n if (!grouped.has(first)) {\n grouped.set(first, []);\n }\n if (rest) {\n grouped.get(first)!.push(rest);\n }\n }\n }\n else {\n passthrough.push(entry);\n }\n }\n else if (entry && typeof entry === 'object') {\n const obj = entry as { path?: string; populate?: T_Input_Populate };\n\n if (obj.path && obj.path.includes('.')) {\n const parts = obj.path.split('.');\n const first = parts[0] || '';\n const rest = parts.slice(1).join('.');\n\n if (first) {\n if (!grouped.has(first)) {\n grouped.set(first, []);\n }\n if (rest) {\n grouped.get(first)!.push(rest);\n }\n if (obj.populate) {\n grouped.get(first)!.push(obj.populate);\n }\n }\n }\n else {\n passthrough.push(entry);\n }\n }\n }\n\n const normalized: unknown[] = [...passthrough];\n grouped.forEach((nested, root) => {\n const flat: unknown[] = [];\n\n for (const n of nested) {\n if (typeof n === 'string') {\n flat.push(n);\n }\n else if (n && typeof n === 'object') {\n flat.push(n);\n }\n }\n if (flat.length > 0) {\n normalized.push({ path: root, populate: flat as unknown as T_Input_Populate });\n }\n else {\n normalized.push(root);\n }\n });\n\n return normalized as unknown as T_Input_Populate;\n };\n\n const normalizedPopulate = normalizePopulate(populate);\n\n await applyNestedPopulate(mongoose, clonedDocuments, normalizedPopulate, virtualConfigs as I_DynamicVirtualConfig<unknown, string>[], startModel);\n }\n\n return clonedDocuments;\n}\n"],"mappings":";;;;;AAcA,SAAgB,EAAS,GAAiC;AACtD,QAAwB,OAAO,KAAU,cAAlC;;AAYX,SAAgB,EACZ,GACA,GAC4B;AAC5B,KAAI,CAAC,KAAY,CAAC,KAAmB,EAAgB,WAAW,EAC5D,QAAO;CAGX,IAAM,IAAsB,IAAI,IAAI,EAAgB,KAAI,MAAK,EAAE,KAAK,CAAC;AAErE,KAAI,MAAM,QAAQ,EAAS,EAAE;EACzB,IAAM,IAAW,EAAS,QAAQ,MAAM;AACpC,OAAI,OAAO,KAAM,SACb,QAAO,CAAC,CAAC,GAAG,EAAoB,CAAC,MAAK,MAClC,MAAM,KAAe,EAAE,WAAW,GAAG,EAAY,GAAG,CACvD;AAGL,OAAI,OAAO,KAAM,YAAY,GAAY;IACrC,IAAM,IAAS,GACT,IAAO,EAAO,QAAQ,EAAO,YAAY;AAE/C,WAAO,CAAC,CAAC,GAAG,EAAoB,CAAC,MAAK,MAClC,MAAS,KAAe,EAAK,WAAW,GAAG,EAAY,GAAG,CAC7D;;AAGL,UAAO;IACT;AAEF,SAAO,EAAS,SAAS,IAAK,IAAgC,KAAA;;AAGlE,KAAI,OAAO,KAAa,SACpB,QAAO,CAAC,GAAG,EAAoB,CAAC,MAAK,MACjC,MAAa,KAAe,EAAS,WAAW,GAAG,EAAY,GAAG,CACrE,GACK,KAAA,IACA;AAGV,KAAI,OAAO,KAAa,YAAY,GAAmB;EACnD,IAAM,IAAS,GACT,IAAO,EAAO,QAAQ,EAAO,YAAY;AAE/C,SAAO,CAAC,GAAG,EAAoB,CAAC,MAAK,MACjC,MAAS,KAAe,EAAK,WAAW,GAAG,EAAY,GAAG,CAC7D,GACK,KAAA,IACA;;AAGV,QAAO;;AAcX,SAAgB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ts"],"sourcesContent":["import type mongooseRaw from 'mongoose';\n\nimport { deepClone } from '#util/index.js';\n\nimport type { I_ModelWithSchema } from './mongo.internal-types.js';\nimport type { I_DynamicVirtualConfig, I_DynamicVirtualOptions, T_Input_Populate } from './mongo.type.js';\n\nimport { catchError } from '../log/index.js';\nimport { applyNestedPopulate } from './mongo.populate.js';\nimport { convertEnumToModelName } from './mongo.util.js';\n\n/**\n * Filters out dynamic virtuals from populate options to prevent Mongoose from trying to populate them.\n * This function creates a new populate configuration that only includes regular virtuals.\n *\n * @template T - The document type\n * @param populate - The original populate options\n * @param dynamicVirtuals - Array of dynamic virtual configurations\n * @returns Filtered populate options excluding dynamic virtuals\n */\nexport function filterDynamicVirtualsFromPopulate<T>(\n populate: T_Input_Populate | undefined,\n dynamicVirtuals: I_DynamicVirtualConfig<T>[] | undefined,\n): T_Input_Populate | undefined {\n if (!populate || !dynamicVirtuals || dynamicVirtuals.length === 0) {\n return populate;\n }\n\n const dynamicVirtualNames = new Set(dynamicVirtuals.map(v => v.name));\n\n if (Array.isArray(populate)) {\n const filtered = populate.filter((p) => {\n if (typeof p === 'string') {\n return ![...dynamicVirtualNames].some(virtualName =>\n p === virtualName || p.startsWith(`${virtualName}.`),\n );\n }\n\n if (typeof p === 'object' && p !== null) {\n const popObj = p as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return ![...dynamicVirtualNames].some(virtualName =>\n path === virtualName || path.startsWith(`${virtualName}.`),\n );\n }\n\n return true;\n });\n\n return filtered.length > 0 ? (filtered as T_Input_Populate) : undefined;\n }\n\n if (typeof populate === 'string') {\n return [...dynamicVirtualNames].some(virtualName =>\n populate === virtualName || populate.startsWith(`${virtualName}.`),\n )\n ? undefined\n : populate;\n }\n\n if (typeof populate === 'object' && populate !== null) {\n const popObj = populate as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return [...dynamicVirtualNames].some(virtualName =>\n path === virtualName || path.startsWith(`${virtualName}.`),\n )\n ? undefined\n : populate;\n }\n\n return populate;\n}\n\n/**\n * Groups documents by the resolved model name for a dynamic virtual field.\n * Used to batch population queries for dynamic virtuals.\n *\n * @template T - The document type\n * @template R - The model name type (usually string or enum)\n * @param {T[]} documents - The array of documents to process\n * @param {string} virtualName - The name of the dynamic virtual field\n * @param {I_DynamicVirtualOptions<T, R>} virtualOptions - The dynamic virtual options (must include a ref function)\n * @returns {Array<{ model: string; docs: T[] }>} An array of groups, each with a model name and the docs for that model\n */\nexport function remapDynamicPopulate<T, R extends string = string>(\n documents: T[],\n virtualName: string,\n virtualOptions: I_DynamicVirtualOptions<T, R>,\n): Array<{ model: string; docs: T[] }> {\n if (!documents.length || !virtualName || !virtualOptions?.ref) {\n return [];\n }\n\n const modelGroups = new Map<string, T[]>();\n documents.forEach((doc) => {\n try {\n const modelName = virtualOptions.ref(doc);\n\n if (modelName === undefined || modelName === null) {\n return;\n }\n\n const modelNameString = typeof modelName === 'string' ? modelName : String(modelName);\n\n if (modelNameString && modelNameString.trim() !== '') {\n const convertedModelName = convertEnumToModelName(modelNameString);\n\n if (!modelGroups.has(convertedModelName)) {\n modelGroups.set(convertedModelName, []);\n }\n\n modelGroups.get(convertedModelName)!.push(doc);\n }\n }\n catch (error) {\n catchError(new Error(`Dynamic ref function failed for virtual \"${virtualName}\": ${error instanceof Error ? error.message : String(error)}`));\n }\n });\n\n return Array.from(modelGroups.entries(), ([model, docs]) => ({ model, docs }));\n}\n\n/**\n * Type guard to check if an object is a Mongoose Document (has toObject method).\n * @param obj - The object to check\n * @returns True if obj has a toObject function\n */\nexport function isMongooseDoc(obj: unknown): obj is { toObject: () => { [key: string]: unknown } } {\n return obj !== null && typeof obj === 'object' && 'toObject' in obj && typeof (obj as { toObject: unknown }).toObject === 'function';\n}\n\n/**\n * Optimized batch population for dynamic virtuals.\n *\n * - Groups documents by model and batches queries for each model.\n * - Populates all dynamic virtuals in parallel for maximum performance.\n * - Uses direct assignment for plain objects and skips already populated fields.\n * - Supports optional projection for population queries.\n * - Only populates virtuals that are explicitly requested in populate options.\n * - Reminder: Ensure indexes exist on foreignField in referenced collections for best performance.\n *\n * @template T - The document type (must be an object)\n * @template R - The model name type (usually string or enum)\n * @param {typeof mongooseRaw} mongoose - The Mongoose instance\n * @param {T[]} documents - The array of documents to populate\n * @param {I_DynamicVirtualConfig<T, R>[]} virtualConfigs - The dynamic virtual configurations to process\n * @param {T_Input_Populate} [populate] - Population options to determine which virtuals to populate\n * @param {Record<string, 0 | 1>} [projection] - Optional projection for population queries\n * @returns {Promise<object[]>} The array of documents with dynamic virtuals populated\n */\nexport async function populateDynamicVirtuals<T extends object, R extends string = string>(\n mongoose: typeof mongooseRaw,\n documents: T[],\n virtualConfigs: I_DynamicVirtualConfig<T, R>[],\n populate?: T_Input_Populate,\n projection?: Record<string, 0 | 1>,\n startModel?: I_ModelWithSchema,\n): Promise<T[]> {\n if (!documents.length || !virtualConfigs.length) {\n return documents;\n }\n\n if (!populate) {\n return documents;\n }\n\n const requestedVirtuals = virtualConfigs.filter((config) => {\n if (Array.isArray(populate)) {\n return populate.length > 0 && populate.some((p) => {\n if (typeof p === 'string') {\n return p === config.name || p.startsWith(`${config.name}.`);\n }\n if (p && typeof p === 'object') {\n const popObj = p as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return path === config.name || path.startsWith(`${config.name}.`);\n }\n\n return false;\n });\n }\n\n if (typeof populate === 'string') {\n return populate === config.name || populate.startsWith(`${config.name}.`);\n }\n\n if (typeof populate === 'object' && populate !== null) {\n const popObj = populate as { path?: string; populate?: string };\n const path = popObj.path || popObj.populate || '';\n\n return (path === config.name) || path.startsWith(`${config.name}.`);\n }\n\n return false;\n });\n\n if (requestedVirtuals.length === 0) {\n return documents;\n }\n\n // toObject() already creates plain copies for Mongoose docs; deepClone the\n // plain array to avoid mutating the caller's objects.\n const plainDocuments = documents.map(doc => isMongooseDoc(doc) ? doc.toObject() : doc);\n const clonedDocuments = deepClone(plainDocuments) as T[];\n\n clonedDocuments.forEach((doc) => {\n requestedVirtuals.forEach(({ name, options }) => {\n if (!(name in doc)) {\n (doc as { [key: string]: unknown })[name as string] = options.count ? 0 : (options.justOne ? null : []);\n }\n });\n });\n\n const modelProcessingMap = new Map<string, {\n virtuals: I_DynamicVirtualConfig<T, R>[];\n localValueSets: Map<string, Set<string>>;\n docsByLocalValue: Map<string, number[]>;\n }>();\n\n for (const virtualConfig of requestedVirtuals) {\n const { name, options } = virtualConfig;\n const populateGroups = remapDynamicPopulate(clonedDocuments, name, options);\n\n for (const group of populateGroups) {\n if (!modelProcessingMap.has(group.model)) {\n modelProcessingMap.set(group.model, {\n virtuals: [],\n localValueSets: new Map(),\n docsByLocalValue: new Map(),\n });\n }\n\n const processing = modelProcessingMap.get(group.model)!;\n\n if (!processing.virtuals.some(v => v.name === name)) {\n processing.virtuals.push(virtualConfig);\n processing.localValueSets.set(name as string, new Set());\n }\n\n const localValueSet = processing.localValueSets.get(name as string)!;\n group.docs.forEach((doc) => {\n const localVal = (doc as { [key: string]: unknown })[options.localField];\n\n if (localVal != null) {\n const strVal = String(localVal);\n localValueSet.add(strVal);\n\n let idx = -1;\n\n const docWithKeys = doc as { [key: string]: unknown };\n\n if (docWithKeys['id'] !== undefined) {\n idx = clonedDocuments.findIndex((d) => {\n const dWithKeys = d as { [key: string]: unknown };\n\n return dWithKeys['id'] === docWithKeys['id'];\n });\n }\n else if (docWithKeys['_id'] !== undefined) {\n idx = clonedDocuments.findIndex((d) => {\n const dWithKeys = d as { [key: string]: unknown };\n\n return dWithKeys['_id']?.toString?.() === docWithKeys['_id']?.toString?.();\n });\n }\n\n if (idx !== -1) {\n if (!processing.docsByLocalValue.has(strVal)) {\n processing.docsByLocalValue.set(strVal, []);\n }\n processing.docsByLocalValue.get(strVal)!.push(idx);\n }\n }\n });\n }\n }\n\n await Promise.all(Array.from(modelProcessingMap.entries(), async ([modelName, processing]) => {\n const Model = mongoose.models[modelName];\n\n if (!Model) {\n return;\n }\n\n const allLocalValues = new Set<string>();\n processing.localValueSets.forEach((localValueSet) => {\n localValueSet.forEach(val => allLocalValues.add(val));\n });\n\n if (allLocalValues.size === 0) {\n return;\n }\n\n const foreignFields = [...new Set(processing.virtuals.map(v => v.options.foreignField))];\n const localValuesArray = [...allLocalValues];\n let query;\n\n if (foreignFields.length === 1) {\n query = { [String(foreignFields[0])]: { $in: localValuesArray } };\n }\n else {\n query = { $or: foreignFields.map(field => ({ [field]: { $in: localValuesArray } })) };\n }\n\n const allPopulatedData = await Model.find(query, projection).lean();\n\n for (const virtualConfig of processing.virtuals) {\n const { name, options } = virtualConfig;\n const relevantData = allPopulatedData.filter((item) => {\n const foreignVal = (item)[options.foreignField];\n\n return foreignVal != null && allLocalValues.has(String(foreignVal));\n });\n\n if (options.count) {\n const countMap = new Map<string, number>();\n\n relevantData.forEach((item) => {\n const key = (item)[options.foreignField]?.toString();\n\n if (key) {\n countMap.set(key, (countMap.get(key) || 0) + 1);\n }\n });\n\n processing.localValueSets.get(name as string)!.forEach((localValue) => {\n const docs = processing.docsByLocalValue.get(localValue) || [];\n const count = countMap.get(localValue) || 0;\n\n docs.forEach((idx) => {\n const docToUpdate = clonedDocuments[idx] as { [key: string]: unknown };\n\n if (docToUpdate[name] === undefined) {\n docToUpdate[name] = count;\n }\n });\n });\n }\n else {\n const resultMap = new Map<string, T[]>();\n\n relevantData.forEach((item) => {\n const key = (item)[options.foreignField]?.toString();\n\n if (key) {\n if (!resultMap.has(key)) {\n resultMap.set(key, []);\n }\n resultMap.get(key)!.push(item);\n }\n });\n\n processing.localValueSets.get(name as string)!.forEach((localVal) => {\n const docs = processing.docsByLocalValue.get(localVal) || [];\n const results = resultMap.get(localVal) || [];\n const value = options.justOne ? (results[0] || null) : results;\n\n docs.forEach((idx) => {\n const docToUpdate = clonedDocuments[idx] as { [key: string]: unknown };\n docToUpdate[name] = value;\n });\n });\n }\n }\n }));\n\n if (populate) {\n const normalizePopulate = (pop: T_Input_Populate): T_Input_Populate => {\n const asArray = Array.isArray(pop) ? pop : [pop];\n const grouped = new Map<string, unknown[]>();\n const passthrough: unknown[] = [];\n\n for (const entry of asArray) {\n if (typeof entry === 'string') {\n if (entry.includes('.')) {\n const parts = entry.split('.');\n const first = parts[0] || '';\n const rest = parts.slice(1).join('.');\n\n if (first) {\n if (!grouped.has(first)) {\n grouped.set(first, []);\n }\n if (rest) {\n grouped.get(first)!.push(rest);\n }\n }\n }\n else {\n passthrough.push(entry);\n }\n }\n else if (entry && typeof entry === 'object') {\n const obj = entry as { path?: string; populate?: T_Input_Populate };\n\n if (obj.path && obj.path.includes('.')) {\n const parts = obj.path.split('.');\n const first = parts[0] || '';\n const rest = parts.slice(1).join('.');\n\n if (first) {\n if (!grouped.has(first)) {\n grouped.set(first, []);\n }\n if (rest) {\n grouped.get(first)!.push(rest);\n }\n if (obj.populate) {\n grouped.get(first)!.push(obj.populate);\n }\n }\n }\n else {\n passthrough.push(entry);\n }\n }\n }\n\n const normalized: unknown[] = [...passthrough];\n grouped.forEach((nested, root) => {\n const flat: unknown[] = [];\n\n for (const n of nested) {\n if (typeof n === 'string') {\n flat.push(n);\n }\n else if (n && typeof n === 'object') {\n flat.push(n);\n }\n }\n if (flat.length > 0) {\n normalized.push({ path: root, populate: flat as unknown as T_Input_Populate });\n }\n else {\n normalized.push(root);\n }\n });\n\n return normalized as unknown as T_Input_Populate;\n };\n\n const normalizedPopulate = normalizePopulate(populate);\n\n await applyNestedPopulate(mongoose, clonedDocuments, normalizedPopulate, virtualConfigs as I_DynamicVirtualConfig<unknown, string>[], startModel);\n }\n\n return clonedDocuments;\n}\n"],"mappings":";;;;;AAoBA,SAAgB,EACZ,GACA,GAC4B;AAC5B,KAAI,CAAC,KAAY,CAAC,KAAmB,EAAgB,WAAW,EAC5D,QAAO;CAGX,IAAM,IAAsB,IAAI,IAAI,EAAgB,KAAI,MAAK,EAAE,KAAK,CAAC;AAErE,KAAI,MAAM,QAAQ,EAAS,EAAE;EACzB,IAAM,IAAW,EAAS,QAAQ,MAAM;A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IACpC,IAAM,IAAQ,EAAI,KAAK,MAAM,IAAI,EAC3B,IAAQ,EAAM,MAAM,IACpB,IAAO,EAAM,MAAM,EAAE,CAAC,KAAK,IAAI;AAErC,IAAI,MACK,EAAQ,IAAI,EAAM,IACnB,EAAQ,IAAI,GAAO,EAAE,CAAC,EAEtB,KACA,EAAQ,IAAI,EAAM,CAAE,KAAK,EAAK,EAE9B,EAAI,YACJ,EAAQ,IAAI,EAAM,CAAE,KAAK,EAAI,SAAS;SAK9C,GAAY,KAAK,EAAM;;EAKnC,IAAM,IAAwB,CAAC,GAAG,EAAY;AAoB9C,SAnBA,EAAQ,SAAS,GAAQ,MAAS;GAC9B,IAAM,IAAkB,EAAE;AAE1B,QAAK,IAAM,KAAK,EACZ,EAAI,OAAO,KAAM,YAGR,KAAK,OAAO,KAAM,aAFvB,EAAK,KAAK,EAAE;AAMpB,GACI,EAAW,KADX,EAAK,SAAS,IACE;IAAE,MAAM;IAAM,UAAU;IAAqC,GAG7D,EAAK;IAE3B,EAEK;IAGkC,EAAS,EAEmB,GAA6D,EAAW,EAG9I"}
|
|
@@ -124,11 +124,13 @@ var f = {
|
|
|
124
124
|
async getNewRecords(e, t, n, r = {}) {
|
|
125
125
|
let i = await e.findAll(r);
|
|
126
126
|
if (!i.success) throw Error(`Failed to query existing records: ${i.message}`);
|
|
127
|
+
if ("truncated" in i && i.truncated) throw Error("getNewRecords: Results were truncated by the default limit. Use pagination or set an explicit limit to ensure complete data.");
|
|
127
128
|
return t.filter((e) => !i.result.some((t) => n(t, e)));
|
|
128
129
|
},
|
|
129
130
|
async getExistingRecords(e, t, n, r = {}) {
|
|
130
131
|
let i = await e.findAll(r);
|
|
131
132
|
if (!i.success) throw Error(`Failed to query existing records: ${i.message}`);
|
|
133
|
+
if ("truncated" in i && i.truncated) throw Error("getExistingRecords: Results were truncated by the default limit. Use pagination or set an explicit limit to ensure complete data.");
|
|
132
134
|
return i.result.filter((e) => t.some((t) => n(e, t)));
|
|
133
135
|
}
|
|
134
136
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.util.js","names":[],"sources":["../../../src/node/mongo/mongo.util.ts"],"sourcesContent":["import type migrate from 'migrate-mongo';\nimport type mongooseRaw from 'mongoose';\n\nimport aggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\nimport { randomUUID } from 'node:crypto';\n\nimport { getNestedValue, regexSearchMapper, setNestedValue } from '#util/index.js';\nimport { validate } from '#util/validate/index.js';\n\nimport type { MongoController } from './mongo.controller.js';\nimport type { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualConfig, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_Filter, T_MongoosePlugin, T_MongooseSchema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';\n\nimport { addGitIgnoreEntry, writeFileSync } from '../fs/index.js';\nimport { MIGRATE_MONGO_CONFIG, PATH } from '../path/index.js';\n\n/**\n * Converts enum values to proper model names.\n * Handles common naming conventions like converting 'USER' to 'User'.\n *\n * @param enumValue - The enum value to convert\n * @returns The converted model name\n */\nexport function convertEnumToModelName(enumValue: string): string {\n if (enumValue === enumValue.toUpperCase()) {\n return enumValue.charAt(0).toUpperCase() + enumValue.slice(1).toLowerCase();\n }\n\n return enumValue;\n}\n\n/**\n * Interface for the MongoDB utility object to enable explicit type annotation.\n * Required to avoid TS7056 (inferred type exceeds maximum serialization length).\n */\ninterface I_MongoUtils {\n createGenericFields: () => I_GenericDocument;\n applyPlugins: <T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) => void;\n applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseSchema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;\n createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseSchema<I_GenericDocument>;\n createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseSchema<T>;\n createModel: <T extends Partial<C_Document>, R extends string = string>(options: I_CreateModelOptions<T, R>) => I_ExtendedModel<T>;\n validator: {\n isRequired: <T>() => (this: T, value: unknown) => Promise<boolean>;\n isUnique: <T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) => (this: T, value: unknown) => Promise<boolean>;\n matchesRegex: (regexArray: RegExp[]) => (value: string) => Promise<boolean>;\n };\n migrate: {\n getModule: () => Promise<typeof migrate>;\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => void;\n };\n regexify: <T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]) => T_QueryFilter<T>;\n isDynamicVirtual: <T, R extends string>(options?: T_VirtualOptions<T, R>) => options is I_DynamicVirtualOptions<T, R>;\n getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T[]>;\n getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T_WithId<T>[]>;\n}\n\n/**\n * MongoDB utility object providing comprehensive database operations and utilities.\n * This object contains methods for creating generic fields, applying plugins and middlewares,\n * creating schemas and models, validation functions, migration utilities, and regex filtering.\n */\nexport const mongo: I_MongoUtils = {\n /**\n * Creates generic fields that are commonly used across MongoDB documents.\n * This function generates standard fields including a UUID, deletion flag, and timestamps\n * that can be applied to any document schema.\n *\n * @returns An object containing generic document fields (id, isDel, createdAt, updatedAt).\n */\n createGenericFields(): I_GenericDocument {\n return {\n id: randomUUID(),\n isDel: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n },\n /**\n * Applies plugins to a Mongoose schema.\n * This function filters out falsy plugins and applies the remaining valid plugins\n * to the provided schema.\n *\n * @param schema - The Mongoose schema to apply plugins to.\n * @param plugins - An array of plugin functions or false values to filter and apply.\n */\n applyPlugins<T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) {\n plugins\n .filter((plugin): plugin is T_MongoosePlugin => typeof plugin === 'function')\n .forEach(plugin => schema.plugin(plugin));\n },\n /**\n * Applies middleware functions to a Mongoose schema.\n * This function configures pre and post middleware for specified methods on the schema.\n *\n * @param schema - The Mongoose schema to apply middleware to.\n * @param middlewares - An array of middleware configurations with method, pre, and post functions.\n */\n applyMiddlewares<T extends Partial<C_Document>>(\n schema: T_MongooseSchema<T>,\n middlewares: I_MongooseModelMiddleware<T>[],\n ) {\n middlewares.forEach(({ method, pre, post }) => {\n if (method && pre) {\n schema.pre(method as RegExp, pre);\n }\n\n if (method && post) {\n schema.post(method as RegExp, post);\n }\n });\n },\n /**\n * Creates a generic Mongoose schema with common fields.\n * This function creates a base schema with UUID field and deletion flag,\n * configured with automatic timestamps.\n *\n * @param mongoose - The Mongoose instance to create the schema with.\n * @returns A Mongoose schema with generic document fields.\n */\n createGenericSchema(mongoose: typeof mongooseRaw) {\n return new mongoose.Schema<I_GenericDocument>(\n {\n id: { type: String, default: () => randomUUID(), unique: true },\n isDel: { type: Boolean, default: false },\n },\n { timestamps: true },\n );\n },\n /**\n * Creates a Mongoose schema with optional virtual fields and generic fields.\n * This function creates a new Mongoose schema from the provided schema definition,\n * optionally adds virtual fields, and includes generic fields (unless standalone is true).\n *\n * @param options - Configuration options including mongoose instance, schema definition, virtuals, and standalone flag.\n * @param options.mongoose - The Mongoose instance to use for schema creation.\n * @param options.schema - The schema definition object.\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.standalone - Whether to exclude generic fields (default: false).\n * @returns A configured Mongoose schema.\n */\n createSchema<T, R extends string = string>({\n mongoose,\n schema,\n virtuals = [],\n standalone = false,\n }: I_CreateSchemaOptions<T, R>): T_MongooseSchema<T> {\n const createdSchema = new mongoose.Schema<T>(schema, {\n toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals\n toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals\n });\n\n virtuals.forEach(({ name, options, get }) => {\n if (mongo.isDynamicVirtual<T, R>(options)) {\n const schemaStatics = createdSchema.statics as Record<string, unknown>;\n\n if (!schemaStatics['_dynamicVirtuals']) {\n schemaStatics['_dynamicVirtuals'] = [];\n }\n\n (schemaStatics['_dynamicVirtuals'] as I_DynamicVirtualConfig<T>[]).push({\n name: name as string,\n options,\n });\n\n const virtualInstance = createdSchema.virtual(name as string);\n\n if (get) {\n virtualInstance.get(get);\n }\n else {\n virtualInstance.get(function (this: T & { _populated?: { [key: string]: unknown } }) {\n return this._populated?.[name as string] || (options?.count ? 0 : (options?.justOne ? null : []));\n });\n }\n }\n else {\n const virtualInstance = createdSchema.virtual(name as string, options);\n\n if (get) {\n virtualInstance.get(get);\n }\n }\n });\n\n if (!standalone) {\n createdSchema.add(mongo.createGenericSchema(mongoose));\n }\n\n return createdSchema;\n },\n /**\n * Creates a Mongoose model with plugins, middleware, and pagination support.\n * This function creates a model from a schema with optional pagination and aggregation plugins,\n * and applies any specified middleware. If a model with the same name already exists, it returns the existing model.\n *\n * @param options - Configuration options including mongoose instance, model name, schema, and feature flags.\n * @param options.mongoose - The Mongoose instance to use for model creation.\n * @param options.name - The name of the model to create.\n * @param options.schema - The schema definition for the model.\n * @param options.pagination - Whether to enable pagination plugin (default: false).\n * @param options.aggregate - Whether to enable aggregation pagination plugin (default: false).\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.middlewares - Optional array of middleware configurations.\n * @returns A configured Mongoose model with extended functionality.\n * @throws {Error} When the model name is not provided.\n */\n createModel<T extends Partial<C_Document>, R extends string = string>({\n mongoose: currentMongooseInstance,\n name,\n schema,\n virtuals = [],\n pagination = true,\n aggregate = true,\n middlewares = [],\n }: I_CreateModelOptions<T, R>): I_ExtendedModel<T> {\n if (!name) {\n throw new Error('Model name is required.');\n }\n\n if (currentMongooseInstance.models[name]) {\n return currentMongooseInstance.models[name] as I_ExtendedModel<T>;\n }\n\n const createdSchema = mongo.createSchema({ mongoose: currentMongooseInstance, schema, virtuals });\n\n if (pagination || aggregate) {\n mongo.applyPlugins<T>(createdSchema, [\n pagination && mongoosePaginate,\n aggregate && aggregatePaginate,\n ]);\n }\n\n mongo.applyMiddlewares<T>(createdSchema, middlewares);\n\n const model = currentMongooseInstance.model<T>(name, createdSchema) as I_ExtendedModel<T>;\n\n if (virtuals.length > 0) {\n // Mongoose Model has no typed _virtualConfigs; used by MongooseController.getDynamicVirtuals()\n (model as any)._virtualConfigs = virtuals;\n }\n\n return model;\n },\n /**\n * Validation utilities for Mongoose schemas.\n * This object provides common validation functions that can be used in Mongoose schema definitions.\n */\n validator: {\n /**\n * Creates a required field validator.\n * This function returns a validator that checks if a field value is not empty\n * using the validate.isEmpty utility.\n *\n * @returns A validation function that returns true if the field is not empty.\n */\n isRequired<T>(): (this: T, value: unknown) => Promise<boolean> {\n return async function (this: T, value: unknown): Promise<boolean> {\n return !validate.isEmpty(value);\n };\n },\n /**\n * Creates a unique field validator.\n * This function returns a validator that checks if a field value is unique\n * across the specified fields in the collection.\n *\n * @param fields - An array of field names to check for uniqueness.\n * @returns A validation function that returns true if the value is unique across the specified fields.\n * @throws {Error} When fields is not a non-empty array of strings.\n */\n isUnique<T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) {\n return async function (this: T, value: unknown): Promise<boolean> {\n if (!Array.isArray(fields) || fields.length === 0) {\n throw new Error('Fields must be a non-empty array of strings.');\n }\n\n const query = { $or: fields.map(field => ({ [field]: value })) };\n const existingDocument = await this.constructor.exists(query);\n\n return !existingDocument;\n };\n },\n /**\n * Creates a regex pattern validator.\n * This function returns a validator that checks if a string value matches\n * all provided regular expressions.\n *\n * @param regexArray - An array of regular expressions to test against the value.\n * @returns A validation function that returns true if the value matches all regex patterns.\n * @throws {Error} When regexArray is not an array of valid RegExp objects.\n */\n matchesRegex(regexArray: RegExp[]): (value: string) => Promise<boolean> {\n return async (value: string): Promise<boolean> => {\n if (!Array.isArray(regexArray) || regexArray.some(r => !(r instanceof RegExp))) {\n throw new Error('regexArray must be an array of valid RegExp objects.');\n }\n\n return regexArray.every(regex => regex.test(value));\n };\n },\n },\n /**\n * Migration utilities for MongoDB.\n * This object extends the migrate-mongo library with additional configuration utilities.\n */\n migrate: {\n /**\n * Lazily loads the migrate-mongo module to avoid eager import overhead.\n * Use this to access migrate-mongo methods (up, down, status, create) programmatically.\n *\n * @returns A promise resolving to the migrate-mongo module.\n */\n async getModule(): Promise<typeof migrate> {\n return (await import('migrate-mongo')).default;\n },\n /**\n * Sets the migration configuration and updates .gitignore.\n * This function creates a migration configuration file and ensures it's properly\n * excluded from version control.\n *\n * @param options - Migration configuration options to write to the config file.\n */\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => {\n const optionsJS = `// This file is automatically generated by the Cyberskill CLI.\\nmodule.exports = ${JSON.stringify(options, null, 4)}`;\n\n writeFileSync(PATH.MIGRATE_MONGO_CONFIG, optionsJS);\n\n addGitIgnoreEntry(PATH.GIT_IGNORE, MIGRATE_MONGO_CONFIG);\n },\n },\n /**\n * Converts string values in a filter to regex patterns for case-insensitive search.\n * This function recursively processes a filter object and converts string values in specified fields\n * to MongoDB regex patterns that support accented character matching.\n *\n * @remarks\n * **Performance guard:** Input strings are capped at 200 characters to mitigate ReDoS risk.\n * The generated regex patterns include accented character alternation groups (e.g., `(a|à|á|...)`)\n * which can be polynomially complex for very long inputs. For production search on large collections,\n * consider using MongoDB `$text` search indexes instead of `$regex` for better performance.\n *\n * @param filter - The filter object to process.\n * @param fields - An array of field names to convert to regex patterns.\n * @returns A new filter object with string values converted to regex patterns.\n */\n regexify<T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]): T_QueryFilter<T> {\n if (!filter) {\n return {} as T_QueryFilter<T>;\n }\n\n let newFilter = { ...filter };\n\n if (!fields || fields.length === 0) {\n return newFilter;\n }\n\n const MAX_REGEX_INPUT_LENGTH = 200;\n\n for (const field of fields) {\n const path = field.toString().split('.');\n const value = getNestedValue(newFilter, path);\n\n if (typeof value === 'string' && value.length > 0 && value.length <= MAX_REGEX_INPUT_LENGTH) {\n const regexValue = {\n $regex: `.*${regexSearchMapper(value)}.*`,\n $options: 'i',\n };\n\n newFilter = setNestedValue(newFilter, path, regexValue);\n }\n }\n\n return newFilter;\n },\n /**\n * Checks if a virtual options object has a dynamic ref function.\n *\n * @param options - The virtual options to check.\n * @returns True if the options contain a dynamic ref function.\n */\n isDynamicVirtual<T, R extends string = string>(options?: T_VirtualOptions<T, R>): options is I_DynamicVirtualOptions<T, R> {\n return Boolean(options && typeof options.ref === 'function');\n },\n\n /**\n * Generic utility function to get new records from the database\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record already exists\n * @returns Array of records that don't exist in the database\n */\n async getNewRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const filteredRecords = recordsToCheck.filter(newRecord =>\n !existingRecords.result.some((existingRecord: T_WithId<T>) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return filteredRecords;\n },\n\n /**\n * Generic utility function to get existing records that match the filter criteria\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record exists\n * @returns Array of existing records that match the filter criteria\n * @throws {Error} When the database query fails — prevents silent data inconsistency.\n */\n async getExistingRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T_WithId<T>[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const foundRecords = existingRecords.result.filter((existingRecord: T_WithId<T>) =>\n recordsToCheck.some((newRecord: T) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return foundRecords;\n },\n};\n\nexport { applyNestedPopulate } from './mongo.populate.js';\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,EAAuB,GAA2B;AAK9D,QAJI,MAAc,EAAU,aAAa,GAC9B,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,CAAC,aAAa,GAGxE;;AAkCX,IAAa,IAAsB;CAQ/B,sBAAyC;AACrC,SAAO;GACH,IAAI,GAAY;GAChB,OAAO;GACP,2BAAW,IAAI,MAAM;GACrB,2BAAW,IAAI,MAAM;GACxB;;CAUL,aAAgB,GAA6B,GAA0C;AACnF,IACK,QAAQ,MAAuC,OAAO,KAAW,WAAW,CAC5E,SAAQ,MAAU,EAAO,OAAO,EAAO,CAAC;;CASjD,iBACI,GACA,GACF;AACE,IAAY,SAAS,EAAE,WAAQ,QAAK,cAAW;AAK3C,GAJI,KAAU,KACV,EAAO,IAAI,GAAkB,EAAI,EAGjC,KAAU,KACV,EAAO,KAAK,GAAkB,EAAK;IAEzC;;CAUN,oBAAoB,GAA8B;AAC9C,SAAO,IAAI,EAAS,OAChB;GACI,IAAI;IAAE,MAAM;IAAQ,eAAe,GAAY;IAAE,QAAQ;IAAM;GAC/D,OAAO;IAAE,MAAM;IAAS,SAAS;IAAO;GAC3C,EACD,EAAE,YAAY,IAAM,CACvB;;CAcL,aAA2C,EACvC,aACA,WACA,cAAW,EAAE,EACb,gBAAa,MACoC;EACjD,IAAM,IAAgB,IAAI,EAAS,OAAU,GAAQ;GACjD,QAAQ,EAAE,UAAU,IAAM;GAC1B,UAAU,EAAE,UAAU,IAAM;GAC/B,CAAC;AAuCF,SArCA,EAAS,SAAS,EAAE,S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,EAAgB,OAAO,MAAM,MAC1B,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAaL,MAAM,mBACF,GACA,GACA,GACA,IAAsB,EAAE,EACF;EACtB,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAO;AAExD,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AASnF,SANqB,EAAgB,OAAO,QAAQ,MAChD,EAAe,MAAM,MACjB,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAIR"}
|
|
1
|
+
{"version":3,"file":"mongo.util.js","names":[],"sources":["../../../src/node/mongo/mongo.util.ts"],"sourcesContent":["import type migrate from 'migrate-mongo';\nimport type mongooseRaw from 'mongoose';\n\nimport aggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\nimport { randomUUID } from 'node:crypto';\n\nimport { getNestedValue, regexSearchMapper, setNestedValue } from '#util/index.js';\nimport { validate } from '#util/validate/index.js';\n\nimport type { MongoController } from './mongo.controller.js';\nimport type { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualConfig, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_Filter, T_MongoosePlugin, T_MongooseSchema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';\n\nimport { addGitIgnoreEntry, writeFileSync } from '../fs/index.js';\nimport { MIGRATE_MONGO_CONFIG, PATH } from '../path/index.js';\n\n/**\n * Converts enum values to proper model names.\n * Handles common naming conventions like converting 'USER' to 'User'.\n *\n * @param enumValue - The enum value to convert\n * @returns The converted model name\n */\nexport function convertEnumToModelName(enumValue: string): string {\n if (enumValue === enumValue.toUpperCase()) {\n return enumValue.charAt(0).toUpperCase() + enumValue.slice(1).toLowerCase();\n }\n\n return enumValue;\n}\n\n/**\n * Interface for the MongoDB utility object to enable explicit type annotation.\n * Required to avoid TS7056 (inferred type exceeds maximum serialization length).\n */\ninterface I_MongoUtils {\n createGenericFields: () => I_GenericDocument;\n applyPlugins: <T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) => void;\n applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseSchema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;\n createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseSchema<I_GenericDocument>;\n createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseSchema<T>;\n createModel: <T extends Partial<C_Document>, R extends string = string>(options: I_CreateModelOptions<T, R>) => I_ExtendedModel<T>;\n validator: {\n isRequired: <T>() => (this: T, value: unknown) => Promise<boolean>;\n isUnique: <T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) => (this: T, value: unknown) => Promise<boolean>;\n matchesRegex: (regexArray: RegExp[]) => (value: string) => Promise<boolean>;\n };\n migrate: {\n getModule: () => Promise<typeof migrate>;\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => void;\n };\n regexify: <T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]) => T_QueryFilter<T>;\n isDynamicVirtual: <T, R extends string>(options?: T_VirtualOptions<T, R>) => options is I_DynamicVirtualOptions<T, R>;\n getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T[]>;\n getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T_WithId<T>[]>;\n}\n\n/**\n * MongoDB utility object providing comprehensive database operations and utilities.\n * This object contains methods for creating generic fields, applying plugins and middlewares,\n * creating schemas and models, validation functions, migration utilities, and regex filtering.\n */\nexport const mongo: I_MongoUtils = {\n /**\n * Creates generic fields that are commonly used across MongoDB documents.\n * This function generates standard fields including a UUID, deletion flag, and timestamps\n * that can be applied to any document schema.\n *\n * @returns An object containing generic document fields (id, isDel, createdAt, updatedAt).\n */\n createGenericFields(): I_GenericDocument {\n return {\n id: randomUUID(),\n isDel: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n },\n /**\n * Applies plugins to a Mongoose schema.\n * This function filters out falsy plugins and applies the remaining valid plugins\n * to the provided schema.\n *\n * @param schema - The Mongoose schema to apply plugins to.\n * @param plugins - An array of plugin functions or false values to filter and apply.\n */\n applyPlugins<T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) {\n plugins\n .filter((plugin): plugin is T_MongoosePlugin => typeof plugin === 'function')\n .forEach(plugin => schema.plugin(plugin));\n },\n /**\n * Applies middleware functions to a Mongoose schema.\n * This function configures pre and post middleware for specified methods on the schema.\n *\n * @param schema - The Mongoose schema to apply middleware to.\n * @param middlewares - An array of middleware configurations with method, pre, and post functions.\n */\n applyMiddlewares<T extends Partial<C_Document>>(\n schema: T_MongooseSchema<T>,\n middlewares: I_MongooseModelMiddleware<T>[],\n ) {\n middlewares.forEach(({ method, pre, post }) => {\n if (method && pre) {\n schema.pre(method as RegExp, pre);\n }\n\n if (method && post) {\n schema.post(method as RegExp, post);\n }\n });\n },\n /**\n * Creates a generic Mongoose schema with common fields.\n * This function creates a base schema with UUID field and deletion flag,\n * configured with automatic timestamps.\n *\n * @param mongoose - The Mongoose instance to create the schema with.\n * @returns A Mongoose schema with generic document fields.\n */\n createGenericSchema(mongoose: typeof mongooseRaw) {\n return new mongoose.Schema<I_GenericDocument>(\n {\n id: { type: String, default: () => randomUUID(), unique: true },\n isDel: { type: Boolean, default: false },\n },\n { timestamps: true },\n );\n },\n /**\n * Creates a Mongoose schema with optional virtual fields and generic fields.\n * This function creates a new Mongoose schema from the provided schema definition,\n * optionally adds virtual fields, and includes generic fields (unless standalone is true).\n *\n * @param options - Configuration options including mongoose instance, schema definition, virtuals, and standalone flag.\n * @param options.mongoose - The Mongoose instance to use for schema creation.\n * @param options.schema - The schema definition object.\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.standalone - Whether to exclude generic fields (default: false).\n * @returns A configured Mongoose schema.\n */\n createSchema<T, R extends string = string>({\n mongoose,\n schema,\n virtuals = [],\n standalone = false,\n }: I_CreateSchemaOptions<T, R>): T_MongooseSchema<T> {\n const createdSchema = new mongoose.Schema<T>(schema, {\n toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals\n toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals\n });\n\n virtuals.forEach(({ name, options, get }) => {\n if (mongo.isDynamicVirtual<T, R>(options)) {\n const schemaStatics = createdSchema.statics as Record<string, unknown>;\n\n if (!schemaStatics['_dynamicVirtuals']) {\n schemaStatics['_dynamicVirtuals'] = [];\n }\n\n (schemaStatics['_dynamicVirtuals'] as I_DynamicVirtualConfig<T>[]).push({\n name: name as string,\n options,\n });\n\n const virtualInstance = createdSchema.virtual(name as string);\n\n if (get) {\n virtualInstance.get(get);\n }\n else {\n virtualInstance.get(function (this: T & { _populated?: { [key: string]: unknown } }) {\n return this._populated?.[name as string] || (options?.count ? 0 : (options?.justOne ? null : []));\n });\n }\n }\n else {\n const virtualInstance = createdSchema.virtual(name as string, options);\n\n if (get) {\n virtualInstance.get(get);\n }\n }\n });\n\n if (!standalone) {\n createdSchema.add(mongo.createGenericSchema(mongoose));\n }\n\n return createdSchema;\n },\n /**\n * Creates a Mongoose model with plugins, middleware, and pagination support.\n * This function creates a model from a schema with optional pagination and aggregation plugins,\n * and applies any specified middleware. If a model with the same name already exists, it returns the existing model.\n *\n * @param options - Configuration options including mongoose instance, model name, schema, and feature flags.\n * @param options.mongoose - The Mongoose instance to use for model creation.\n * @param options.name - The name of the model to create.\n * @param options.schema - The schema definition for the model.\n * @param options.pagination - Whether to enable pagination plugin (default: false).\n * @param options.aggregate - Whether to enable aggregation pagination plugin (default: false).\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.middlewares - Optional array of middleware configurations.\n * @returns A configured Mongoose model with extended functionality.\n * @throws {Error} When the model name is not provided.\n */\n createModel<T extends Partial<C_Document>, R extends string = string>({\n mongoose: currentMongooseInstance,\n name,\n schema,\n virtuals = [],\n pagination = true,\n aggregate = true,\n middlewares = [],\n }: I_CreateModelOptions<T, R>): I_ExtendedModel<T> {\n if (!name) {\n throw new Error('Model name is required.');\n }\n\n if (currentMongooseInstance.models[name]) {\n return currentMongooseInstance.models[name] as I_ExtendedModel<T>;\n }\n\n const createdSchema = mongo.createSchema({ mongoose: currentMongooseInstance, schema, virtuals });\n\n if (pagination || aggregate) {\n mongo.applyPlugins<T>(createdSchema, [\n pagination && mongoosePaginate,\n aggregate && aggregatePaginate,\n ]);\n }\n\n mongo.applyMiddlewares<T>(createdSchema, middlewares);\n\n const model = currentMongooseInstance.model<T>(name, createdSchema) as I_ExtendedModel<T>;\n\n if (virtuals.length > 0) {\n // Mongoose Model has no typed _virtualConfigs; used by MongooseController.getDynamicVirtuals()\n (model as any)._virtualConfigs = virtuals;\n }\n\n return model;\n },\n /**\n * Validation utilities for Mongoose schemas.\n * This object provides common validation functions that can be used in Mongoose schema definitions.\n */\n validator: {\n /**\n * Creates a required field validator.\n * This function returns a validator that checks if a field value is not empty\n * using the validate.isEmpty utility.\n *\n * @returns A validation function that returns true if the field is not empty.\n */\n isRequired<T>(): (this: T, value: unknown) => Promise<boolean> {\n return async function (this: T, value: unknown): Promise<boolean> {\n return !validate.isEmpty(value);\n };\n },\n /**\n * Creates a unique field validator.\n * This function returns a validator that checks if a field value is unique\n * across the specified fields in the collection.\n *\n * @param fields - An array of field names to check for uniqueness.\n * @returns A validation function that returns true if the value is unique across the specified fields.\n * @throws {Error} When fields is not a non-empty array of strings.\n */\n isUnique<T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) {\n return async function (this: T, value: unknown): Promise<boolean> {\n if (!Array.isArray(fields) || fields.length === 0) {\n throw new Error('Fields must be a non-empty array of strings.');\n }\n\n const query = { $or: fields.map(field => ({ [field]: value })) };\n const existingDocument = await this.constructor.exists(query);\n\n return !existingDocument;\n };\n },\n /**\n * Creates a regex pattern validator.\n * This function returns a validator that checks if a string value matches\n * all provided regular expressions.\n *\n * @param regexArray - An array of regular expressions to test against the value.\n * @returns A validation function that returns true if the value matches all regex patterns.\n * @throws {Error} When regexArray is not an array of valid RegExp objects.\n */\n matchesRegex(regexArray: RegExp[]): (value: string) => Promise<boolean> {\n return async (value: string): Promise<boolean> => {\n if (!Array.isArray(regexArray) || regexArray.some(r => !(r instanceof RegExp))) {\n throw new Error('regexArray must be an array of valid RegExp objects.');\n }\n\n return regexArray.every(regex => regex.test(value));\n };\n },\n },\n /**\n * Migration utilities for MongoDB.\n * This object extends the migrate-mongo library with additional configuration utilities.\n */\n migrate: {\n /**\n * Lazily loads the migrate-mongo module to avoid eager import overhead.\n * Use this to access migrate-mongo methods (up, down, status, create) programmatically.\n *\n * @returns A promise resolving to the migrate-mongo module.\n */\n async getModule(): Promise<typeof migrate> {\n return (await import('migrate-mongo')).default;\n },\n /**\n * Sets the migration configuration and updates .gitignore.\n * This function creates a migration configuration file and ensures it's properly\n * excluded from version control.\n *\n * @param options - Migration configuration options to write to the config file.\n */\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => {\n const optionsJS = `// This file is automatically generated by the Cyberskill CLI.\\nmodule.exports = ${JSON.stringify(options, null, 4)}`;\n\n writeFileSync(PATH.MIGRATE_MONGO_CONFIG, optionsJS);\n\n addGitIgnoreEntry(PATH.GIT_IGNORE, MIGRATE_MONGO_CONFIG);\n },\n },\n /**\n * Converts string values in a filter to regex patterns for case-insensitive search.\n * This function recursively processes a filter object and converts string values in specified fields\n * to MongoDB regex patterns that support accented character matching.\n *\n * @remarks\n * **Performance guard:** Input strings are capped at 200 characters to mitigate ReDoS risk.\n * The generated regex patterns include accented character alternation groups (e.g., `(a|à|á|...)`)\n * which can be polynomially complex for very long inputs. For production search on large collections,\n * consider using MongoDB `$text` search indexes instead of `$regex` for better performance.\n *\n * @param filter - The filter object to process.\n * @param fields - An array of field names to convert to regex patterns.\n * @returns A new filter object with string values converted to regex patterns.\n */\n regexify<T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]): T_QueryFilter<T> {\n if (!filter) {\n return {} as T_QueryFilter<T>;\n }\n\n let newFilter = { ...filter };\n\n if (!fields || fields.length === 0) {\n return newFilter;\n }\n\n const MAX_REGEX_INPUT_LENGTH = 200;\n\n for (const field of fields) {\n const path = field.toString().split('.');\n const value = getNestedValue(newFilter, path);\n\n if (typeof value === 'string' && value.length > 0 && value.length <= MAX_REGEX_INPUT_LENGTH) {\n const regexValue = {\n $regex: `.*${regexSearchMapper(value)}.*`,\n $options: 'i',\n };\n\n newFilter = setNestedValue(newFilter, path, regexValue);\n }\n }\n\n return newFilter;\n },\n /**\n * Checks if a virtual options object has a dynamic ref function.\n *\n * @param options - The virtual options to check.\n * @returns True if the options contain a dynamic ref function.\n */\n isDynamicVirtual<T, R extends string = string>(options?: T_VirtualOptions<T, R>): options is I_DynamicVirtualOptions<T, R> {\n return Boolean(options && typeof options.ref === 'function');\n },\n\n /**\n * Generic utility function to get new records from the database\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record already exists\n * @returns Array of records that don't exist in the database\n */\n async getNewRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n if ('truncated' in existingRecords && existingRecords.truncated) {\n throw new Error('getNewRecords: Results were truncated by the default limit. Use pagination or set an explicit limit to ensure complete data.');\n }\n\n const filteredRecords = recordsToCheck.filter(newRecord =>\n !existingRecords.result.some((existingRecord: T_WithId<T>) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return filteredRecords;\n },\n\n /**\n * Generic utility function to get existing records that match the filter criteria\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record exists\n * @returns Array of existing records that match the filter criteria\n * @throws {Error} When the database query fails — prevents silent data inconsistency.\n */\n async getExistingRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T_WithId<T>[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n if ('truncated' in existingRecords && existingRecords.truncated) {\n throw new Error('getExistingRecords: Results were truncated by the default limit. Use pagination or set an explicit limit to ensure complete data.');\n }\n\n const foundRecords = existingRecords.result.filter((existingRecord: T_WithId<T>) =>\n recordsToCheck.some((newRecord: T) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return foundRecords;\n },\n};\n\nexport { applyNestedPopulate } from './mongo.populate.js';\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,EAAuB,GAA2B;AAK9D,QAJI,MAAc,EAAU,aAAa,GAC9B,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,CAAC,aAAa,GAGxE;;AAkCX,IAAa,IAAsB;CAQ/B,sBAAyC;AACrC,SAAO;GACH,IAAI,GAAY;GAChB,OAAO;GACP,2BAAW,IAAI,MAAM;GACrB,2BAAW,IAAI,MAAM;GACxB;;CAUL,aAAgB,GAA6B,GAA0C;AACnF,IACK,QAAQ,MAAuC,OAAO,KAAW,WAAW,CAC5E,SAAQ,MAAU,EAAO,OAAO,EAAO,CAAC;;CASjD,iBACI,GACA,GACF;AACE,IAAY,SAAS,EAAE,WAAQ,QAAK,cAAW;AAK3C,GAJI,KAAU,KACV,EAAO,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,EAAe,GAAW,EAAK;AAE7C,OAAI,OAAO,KAAU,YAAY,EAAM,SAAS,KAAK,EAAM,UAAU,KAAwB;IACzF,IAAM,IAAa;KACf,QAAQ,KAAK,EAAkB,EAAM,CAAC;KACtC,UAAU;KACb;AAED,QAAY,EAAe,GAAW,GAAM,EAAW;;;AAI/D,SAAO;;CAQX,iBAA+C,GAA4E;AACvH,SAAO,GAAQ,KAAW,OAAO,EAAQ,OAAQ;;CAUrD,MAAM,cACF,GACA,GACA,GACA,IAAsB,EAAE,EACZ;EACZ,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAO;AAExD,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AAGnF,MAAI,eAAe,KAAmB,EAAgB,UAClD,OAAU,MAAM,+HAA+H;AASnJ,SANwB,EAAe,QAAO,MAC1C,CAAC,EAAgB,OAAO,MAAM,MAC1B,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAaL,MAAM,mBACF,GACA,GACA,GACA,IAAsB,EAAE,EACF;EACtB,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAO;AAExD,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AAGnF,MAAI,eAAe,KAAmB,EAAgB,UAClD,OAAU,MAAM,oIAAoI;AASxJ,SANqB,EAAgB,OAAO,QAAQ,MAChD,EAAe,MAAM,MACjB,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAIR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.constant.js","names":[],"sources":["../../../src/node/path/path.constant.ts"],"sourcesContent":["import fsExtra from 'fs-extra';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { I_PackageInput, I_PackageJson } from '../package/index.js';\n\nimport { E_CommandType, formatCommand, rawCommand } from '../command/index.js';\nimport { E_PackageType, setupPackages } from '../package/index.js';\nimport { join, resolveWorkingPath } from './path.util.js';\n\nexport const WORKING_DIRECTORY = getEnv().CWD;\nexport const CYBERSKILL_PACKAGE_NAME = '@cyberskill/shared';\nexport const NODE_MODULES = 'node_modules';\nexport const BUILD_DIRECTORY = 'dist';\nexport const PUBLIC_DIRECTORY = 'public';\nexport const PACKAGE_JSON = 'package.json';\nexport const PACKAGE_LOCK_JSON = 'package-lock.json';\nexport const TSCONFIG_JSON = 'tsconfig.json';\nexport const GIT_IGNORE = '.gitignore';\nexport const SIMPLE_GIT_HOOK_JSON = '.simple-git-hooks.json';\nexport const PNPM_LOCK_YAML = 'pnpm-lock.yaml';\nexport const GIT_HOOK = '.git/hooks/';\nexport const GIT_COMMIT_EDITMSG = '.git/COMMIT_EDITMSG';\nexport const GIT_EXCLUDE = '.git/info/exclude';\nexport const MIGRATE_MONGO_CONFIG = '.migrate-mongo.config.js';\nlet _cyberskillDir: string | null = null;\n\n/**\n * Lazily computes the CyberSkill directory path.\n * Reads package.json on first access to determine whether this is the shared package itself\n * or a consumer project, avoiding filesystem reads at module load time.\n */\nexport function getCyberskillDirectory(): string {\n if (_cyberskillDir !== null) {\n return _cyberskillDir;\n }\n\n try {\n const packageJson = fsExtra.readJsonSync(resolveWorkingPath(PACKAGE_JSON)) as I_PackageJson;\n\n _cyberskillDir = packageJson.name === CYBERSKILL_PACKAGE_NAME\n ? join(WORKING_DIRECTORY, BUILD_DIRECTORY)\n : join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n catch {\n _cyberskillDir = join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n\n return _cyberskillDir;\n}\n\n/** @deprecated Use `getCyberskillDirectory()` instead. Kept for backward compatibility. */\nexport const CYBERSKILL_DIRECTORY = getCyberskillDirectory();\nexport const CYBERSKILL_CLI = 'cyberskill';\nexport const CYBERSKILL_CLI_PATH = 'src/node/cli/index.ts';\nexport const ESLINT_PACKAGE_NAME = 'eslint';\nexport const ESLINT_CLI = 'eslint';\nexport const VITEST_PACKAGE_NAME = 'vitest';\nexport const VITEST_CLI = 'vitest';\nexport const COMMIT_LINT_PACKAGE_NAME = '@commitlint/cli';\nexport const COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME = '@commitlint/config-conventional';\nexport const COMMIT_LINT_CLI = 'commitlint';\nexport const LINT_STAGED_PACKAGE_NAME = 'lint-staged';\nexport const LINT_STAGED_CLI = 'lint-staged';\nexport const TSC_PACKAGE_NAME = 'typescript';\nexport const TSC_CLI = 'tsc';\nexport const TSX_CLI = 'tsx';\nexport const GIT_CLI = 'git';\nexport const PNPM_CLI = 'pnpm';\nexport const PNPM_EXEC_CLI = 'pnpm exec';\nexport const SIMPLE_GIT_HOOKS_PACKAGE_NAME = 'simple-git-hooks';\nexport const SIMPLE_GIT_HOOK_CLI = 'simple-git-hooks';\nexport const ESLINT_INSPECT_PACKAGE_NAME = '@eslint/config-inspector';\nexport const ESLINT_INSPECT_CLI = 'eslint-config-inspector';\nexport const NODE_MODULES_INSPECT_PACKAGE_NAME = 'node-modules-inspector';\nexport const NODE_MODULES_INSPECT_CLI = 'node-modules-inspector';\nexport const MIGRATE_MONGO_PACKAGE_NAME = 'migrate-mongo';\nexport const MIGRATE_MONGO_CLI = './node_modules/migrate-mongo/bin/migrate-mongo';\nexport const STORYBOOK_PACKAGE_NAME = 'storybook';\nexport const STORYBOOK_CLI = 'storybook';\nexport const AG_KIT_PACKAGE_NAME = '@vudovn/ag-kit';\nexport const DOT_AGENT = '.agent';\n\nexport const PATH = {\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get CYBERSKILL_DIRECTORY() { return getCyberskillDirectory(); },\n WORKING_DIRECTORY,\n PUBLIC_DIRECTORY: resolveWorkingPath(PUBLIC_DIRECTORY),\n TS_CONFIG: resolveWorkingPath(TSCONFIG_JSON),\n GIT_IGNORE: resolveWorkingPath(GIT_IGNORE),\n GIT_HOOK: resolveWorkingPath(GIT_HOOK),\n GIT_COMMIT_MSG: resolveWorkingPath(GIT_COMMIT_EDITMSG),\n GIT_EXCLUDE: resolveWorkingPath(GIT_EXCLUDE),\n SIMPLE_GIT_HOOKS_JSON: resolveWorkingPath(SIMPLE_GIT_HOOK_JSON),\n PACKAGE_JSON: resolveWorkingPath(PACKAGE_JSON),\n PACKAGE_LOCK_JSON: resolveWorkingPath(PACKAGE_LOCK_JSON),\n PNPM_LOCK_YAML: resolveWorkingPath(PNPM_LOCK_YAML),\n NODE_MODULES: resolveWorkingPath(NODE_MODULES),\n MIGRATE_MONGO_CONFIG: resolveWorkingPath(MIGRATE_MONGO_CONFIG),\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get LINT_STAGED_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/lint-staged/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get COMMITLINT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/commitlint/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_UNIT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.unit.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_E2E_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.e2e.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_MAIN_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.main.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_PREVIEW_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.preview.js`); },\n DOT_AGENT: resolveWorkingPath(DOT_AGENT),\n};\n\n/**\n * Creates Git hooks configuration based on whether this is the current project.\n * This function generates a configuration object for Git hooks that includes\n * pre-commit and commit-msg hooks, with an optional pre-push hook for the current project.\n *\n * @returns A Git hooks configuration object with appropriate commands for each hook.\n */\nexport function createGitHooksConfig() {\n return {\n 'pre-commit': LINT_STAGED_CLI,\n 'commit-msg': COMMIT_LINT_CLI,\n 'pre-push': rawCommand(`${GIT_CLI} pull && ${PNPM_CLI} run --if-present test`),\n };\n}\n\n/**\n * Builds a command function based on the specified type and configuration.\n * This function creates a command executor that handles different command types\n * including CLI commands and string commands. It manages package dependencies\n * and formats commands appropriately for execution.\n *\n * The function supports:\n * - CLI commands that require package installation\n * - String commands that are executed directly\n * - Automatic package dependency management\n * - Command formatting and validation\n *\n * @param config - Configuration object containing type, packages, and command properties.\n * @param config.type - The type of command to build (CLI or STRING).\n * @param config.packages - Optional array of packages required for CLI commands.\n * @param config.command - The command string to execute.\n * @returns A function that returns a promise resolving to the formatted command string.\n * @throws {Error} When an unsupported command type is provided.\n */\nfunction buildCommand({ type, packages, command }: { type: E_CommandType; packages?: I_PackageInput[]; command: string }): () => Promise<string> {\n const uniquePackages = packages?.reduce((acc: I_PackageInput[], pkg) => {\n if (!acc.some(existingPkg => existingPkg.name === pkg.name)) {\n acc.push(pkg);\n }\n return acc;\n }, []);\n\n return async () => {\n switch (type) {\n case E_CommandType.CLI: {\n if (uniquePackages?.length) {\n await setupPackages(uniquePackages, {\n install: true,\n });\n }\n\n return formatCommand(rawCommand(`${PNPM_EXEC_CLI} ${command}`)) as string;\n }\n case E_CommandType.STRING: {\n return formatCommand(rawCommand(command)) as string;\n }\n default: {\n throw new Error('Unsupported command type');\n }\n }\n };\n}\n\nconst RE_MIGRATE_NAME = /^[\\w-]+$/;\n\nexport const command = {\n simpleGitHooks: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: SIMPLE_GIT_HOOKS_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: SIMPLE_GIT_HOOK_CLI,\n }),\n eslintInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name:\n ESLINT_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: ESLINT_INSPECT_CLI,\n }),\n nodeModulesInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: NODE_MODULES_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: NODE_MODULES_INSPECT_CLI,\n }),\n eslintCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --no-cache`,\n }),\n eslintFix: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --fix --no-cache`,\n }),\n typescriptCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSC_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${TSC_CLI} -p ${PATH.TS_CONFIG} --noEmit`,\n }),\n testUnit: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_UNIT_CONFIG}`,\n }),\n testE2e: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_E2E_CONFIG}`,\n }),\n mongoMigrateCreate: (migrateName: string) => {\n if (!RE_MIGRATE_NAME.test(migrateName)) {\n throw new Error('Migration name must only contain alphanumeric characters, underscores, and hyphens.');\n }\n\n return buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} create ${migrateName} -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n })();\n },\n mongoMigrateUp: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} up -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n mongoMigrateDown: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} down -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n commitLint: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: COMMIT_LINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n {\n name: COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${COMMIT_LINT_CLI} --edit ${PATH.GIT_COMMIT_MSG} --config ${PATH.COMMITLINT_CONFIG}`,\n }),\n lintStaged: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: LINT_STAGED_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${LINT_STAGED_CLI} --config ${PATH.LINT_STAGED_CONFIG}`,\n }),\n configureGitHook: buildCommand({\n type: E_CommandType.STRING,\n command: `${GIT_CLI} config core.hooksPath ${PATH.GIT_HOOK}`,\n }),\n build: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} run --if-present build`,\n }),\n pnpmInstallStandard: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts`,\n }),\n pnpmInstallLegacy: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --legacy-peer-deps`,\n }),\n pnpmInstallForce: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --force`,\n }),\n pnpmPruneStore: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} store prune`,\n }),\n pnpmCleanCache: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} cache delete`,\n }),\n storybookDev: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} dev`,\n }),\n storybookBuild: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} build`,\n }),\n};\n"],"mappings":";;;;;;;;AAUA,IAAa,IAAoB,GAAQ,CAAC,KAC7B,IAA0B,sBAC1B,IAAe,gBACf,IAAkB,QAClB,IAAmB,UACnB,IAAe,gBACf,IAAoB,qBACpB,IAAgB,iBAChB,IAAa,cACb,IAAuB,0BACvB,IAAiB,kBACjB,IAAW,eACX,IAAqB,uBACrB,IAAc,qBACd,IAAuB,4BAChC,IAAgC;AAOpC,SAAgB,IAAiC;AAC7C,KAAI,MAAmB,KACnB,QAAO;AAGX,KAAI;AAGA,MAFoB,GAAQ,aAAa,EAAA,eAAgC,CAAC,CAE7C,SAAA,uBACvB,EAAK,GAAmB,EAAgB,GACxC,EAAK,GAAmB,GAAc,GAAyB,EAAgB;SAEnF;AACF,MAAiB,EAAK,GAAmB,GAAc,GAAyB,EAAgB;;AAGpG,QAAO;;AAIX,IAAa,KAAuB,GAAwB,EAC/C,IAAiB,cACjB,K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AAC;CACF,YAAY,EAAa;EACrB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAgB,YAAY,EAAK;EAChD,CAAC;CACF,kBAAkB,EAAa;EAC3B,MAAM,EAAc;EACpB,SAAS,6BAAoC,EAAK;EACrD,CAAC;CACF,OAAO,EAAa;EAChB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,qBAAqB,EAAa;EAC9B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,mBAAmB,EAAa;EAC5B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,kBAAkB,EAAa;EAC3B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,cAAc,EAAa;EACvB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACL"}
|
|
1
|
+
{"version":3,"file":"path.constant.js","names":[],"sources":["../../../src/node/path/path.constant.ts"],"sourcesContent":["import fsExtra from 'fs-extra';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { I_PackageInput, I_PackageJson } from '../package/index.js';\n\nimport { E_CommandType, formatCommand, rawCommand } from '../command/index.js';\nimport { E_PackageType, setupPackages } from '../package/index.js';\nimport { join, resolveWorkingPath } from './path.util.js';\n\nexport const WORKING_DIRECTORY = getEnv().CWD;\nexport const CYBERSKILL_PACKAGE_NAME = '@cyberskill/shared';\nexport const NODE_MODULES = 'node_modules';\nexport const BUILD_DIRECTORY = 'dist';\nexport const PUBLIC_DIRECTORY = 'public';\nexport const PACKAGE_JSON = 'package.json';\nexport const PACKAGE_LOCK_JSON = 'package-lock.json';\nexport const TSCONFIG_JSON = 'tsconfig.json';\nexport const GIT_IGNORE = '.gitignore';\nexport const SIMPLE_GIT_HOOK_JSON = '.simple-git-hooks.json';\nexport const PNPM_LOCK_YAML = 'pnpm-lock.yaml';\nexport const GIT_HOOK = '.git/hooks/';\nexport const GIT_COMMIT_EDITMSG = '.git/COMMIT_EDITMSG';\nexport const GIT_EXCLUDE = '.git/info/exclude';\nexport const MIGRATE_MONGO_CONFIG = '.migrate-mongo.config.js';\nlet _cyberskillDir: string | null = null;\n\n/**\n * Lazily computes the CyberSkill directory path.\n * Reads package.json on first access to determine whether this is the shared package itself\n * or a consumer project, avoiding filesystem reads at module load time.\n */\nexport function getCyberskillDirectory(): string {\n if (_cyberskillDir !== null) {\n return _cyberskillDir;\n }\n\n try {\n const packageJson = fsExtra.readJsonSync(resolveWorkingPath(PACKAGE_JSON)) as I_PackageJson;\n\n _cyberskillDir = packageJson.name === CYBERSKILL_PACKAGE_NAME\n ? join(WORKING_DIRECTORY, BUILD_DIRECTORY)\n : join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n catch {\n _cyberskillDir = join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n\n return _cyberskillDir;\n}\n\n/** @deprecated Use `getCyberskillDirectory()` instead. Kept for backward compatibility. */\nexport const CYBERSKILL_DIRECTORY = getCyberskillDirectory();\nexport const CYBERSKILL_CLI = 'cyberskill';\nexport const CYBERSKILL_CLI_PATH = 'src/node/cli/index.ts';\nexport const ESLINT_PACKAGE_NAME = 'eslint';\nexport const ESLINT_CLI = 'eslint';\nexport const VITEST_PACKAGE_NAME = 'vitest';\nexport const VITEST_CLI = 'vitest';\nexport const COMMIT_LINT_PACKAGE_NAME = '@commitlint/cli';\nexport const COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME = '@commitlint/config-conventional';\nexport const COMMIT_LINT_CLI = 'commitlint';\nexport const LINT_STAGED_PACKAGE_NAME = 'lint-staged';\nexport const LINT_STAGED_CLI = 'lint-staged';\nexport const TSC_PACKAGE_NAME = 'typescript';\nexport const TSC_CLI = 'tsc';\nexport const TSX_CLI = 'tsx';\nexport const GIT_CLI = 'git';\nexport const PNPM_CLI = 'pnpm';\nexport const PNPM_EXEC_CLI = 'pnpm exec';\nexport const SIMPLE_GIT_HOOKS_PACKAGE_NAME = 'simple-git-hooks';\nexport const SIMPLE_GIT_HOOK_CLI = 'simple-git-hooks';\nexport const ESLINT_INSPECT_PACKAGE_NAME = '@eslint/config-inspector';\nexport const ESLINT_INSPECT_CLI = 'eslint-config-inspector';\nexport const NODE_MODULES_INSPECT_PACKAGE_NAME = 'node-modules-inspector';\nexport const NODE_MODULES_INSPECT_CLI = 'node-modules-inspector';\nexport const MIGRATE_MONGO_PACKAGE_NAME = 'migrate-mongo';\nexport const MIGRATE_MONGO_CLI = './node_modules/migrate-mongo/bin/migrate-mongo';\nexport const STORYBOOK_PACKAGE_NAME = 'storybook';\nexport const STORYBOOK_CLI = 'storybook';\nexport const AG_KIT_PACKAGE_NAME = '@vudovn/ag-kit';\nexport const DOT_AGENT = '.agent';\n\nexport const PATH = {\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get CYBERSKILL_DIRECTORY() { return getCyberskillDirectory(); },\n WORKING_DIRECTORY,\n PUBLIC_DIRECTORY: resolveWorkingPath(PUBLIC_DIRECTORY),\n TS_CONFIG: resolveWorkingPath(TSCONFIG_JSON),\n GIT_IGNORE: resolveWorkingPath(GIT_IGNORE),\n GIT_HOOK: resolveWorkingPath(GIT_HOOK),\n GIT_COMMIT_MSG: resolveWorkingPath(GIT_COMMIT_EDITMSG),\n GIT_EXCLUDE: resolveWorkingPath(GIT_EXCLUDE),\n SIMPLE_GIT_HOOKS_JSON: resolveWorkingPath(SIMPLE_GIT_HOOK_JSON),\n PACKAGE_JSON: resolveWorkingPath(PACKAGE_JSON),\n PACKAGE_LOCK_JSON: resolveWorkingPath(PACKAGE_LOCK_JSON),\n PNPM_LOCK_YAML: resolveWorkingPath(PNPM_LOCK_YAML),\n NODE_MODULES: resolveWorkingPath(NODE_MODULES),\n MIGRATE_MONGO_CONFIG: resolveWorkingPath(MIGRATE_MONGO_CONFIG),\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get LINT_STAGED_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/lint-staged/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get COMMITLINT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/commitlint/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_UNIT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.unit.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_E2E_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.e2e.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_MAIN_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.main.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_PREVIEW_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.preview.js`); },\n DOT_AGENT: resolveWorkingPath(DOT_AGENT),\n};\n\n/**\n * Creates Git hooks configuration based on whether this is the current project.\n * This function generates a configuration object for Git hooks that includes\n * pre-commit and commit-msg hooks, with an optional pre-push hook for the current project.\n *\n * @returns A Git hooks configuration object with appropriate commands for each hook.\n */\nexport function createGitHooksConfig() {\n return {\n 'pre-commit': LINT_STAGED_CLI,\n 'commit-msg': COMMIT_LINT_CLI,\n 'pre-push': rawCommand(`${GIT_CLI} pull && ${PNPM_CLI} run --if-present test`),\n };\n}\n\n/**\n * Builds a command function based on the specified type and configuration.\n * This function creates a command executor that handles different command types\n * including CLI commands and string commands. It manages package dependencies\n * and formats commands appropriately for execution.\n *\n * The function supports:\n * - CLI commands that require package installation\n * - String commands that are executed directly\n * - Automatic package dependency management\n * - Command formatting and validation\n *\n * @param config - Configuration object containing type, packages, and command properties.\n * @param config.type - The type of command to build (CLI or STRING).\n * @param config.packages - Optional array of packages required for CLI commands.\n * @param config.command - The command string to execute.\n * @returns A function that returns a promise resolving to the formatted command string.\n * @throws {Error} When an unsupported command type is provided.\n */\nfunction buildCommand({ type, packages, command }: { type: E_CommandType; packages?: I_PackageInput[]; command: string }): () => Promise<string> {\n const uniquePackages = packages?.reduce((acc: I_PackageInput[], pkg) => {\n if (!acc.some(existingPkg => existingPkg.name === pkg.name)) {\n acc.push(pkg);\n }\n return acc;\n }, []);\n\n return async () => {\n switch (type) {\n case E_CommandType.CLI: {\n if (uniquePackages?.length) {\n await setupPackages(uniquePackages, {\n install: true,\n });\n }\n\n return formatCommand(rawCommand(`${PNPM_EXEC_CLI} ${command}`)) as string;\n }\n case E_CommandType.STRING: {\n return formatCommand(rawCommand(command)) as string;\n }\n default: {\n throw new Error('Unsupported command type');\n }\n }\n };\n}\n\nconst RE_MIGRATE_NAME = /^[\\w-]+$/;\n\nexport const command = {\n simpleGitHooks: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: SIMPLE_GIT_HOOKS_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: SIMPLE_GIT_HOOK_CLI,\n }),\n eslintInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name:\n ESLINT_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: ESLINT_INSPECT_CLI,\n }),\n nodeModulesInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: NODE_MODULES_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: NODE_MODULES_INSPECT_CLI,\n }),\n eslintCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --no-cache`,\n }),\n eslintFix: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --fix --no-cache`,\n }),\n typescriptCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSC_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${TSC_CLI} -p ${PATH.TS_CONFIG} --noEmit --incremental`,\n }),\n testUnit: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_UNIT_CONFIG}`,\n }),\n testE2e: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_E2E_CONFIG}`,\n }),\n mongoMigrateCreate: (migrateName: string) => {\n if (!RE_MIGRATE_NAME.test(migrateName)) {\n throw new Error('Migration name must only contain alphanumeric characters, underscores, and hyphens.');\n }\n\n return buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} create ${migrateName} -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n })();\n },\n mongoMigrateUp: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} up -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n mongoMigrateDown: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} down -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n commitLint: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: COMMIT_LINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n {\n name: COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${COMMIT_LINT_CLI} --edit ${PATH.GIT_COMMIT_MSG} --config ${PATH.COMMITLINT_CONFIG}`,\n }),\n lintStaged: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: LINT_STAGED_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${LINT_STAGED_CLI} --config ${PATH.LINT_STAGED_CONFIG}`,\n }),\n configureGitHook: buildCommand({\n type: E_CommandType.STRING,\n command: `${GIT_CLI} config core.hooksPath ${PATH.GIT_HOOK}`,\n }),\n build: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} run --if-present build`,\n }),\n pnpmInstallStandard: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts`,\n }),\n pnpmInstallLegacy: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --legacy-peer-deps`,\n }),\n pnpmInstallForce: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --force`,\n }),\n pnpmPruneStore: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} store prune`,\n }),\n pnpmCleanCache: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} cache delete`,\n }),\n storybookDev: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} dev`,\n }),\n storybookBuild: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} build`,\n }),\n};\n"],"mappings":";;;;;;;;AAUA,IAAa,IAAoB,GAAQ,CAAC,KAC7B,IAA0B,sBAC1B,IAAe,gBACf,IAAkB,QAClB,IAAmB,UACnB,IAAe,gBACf,IAAoB,qBACpB,IAAgB,iBAChB,IAAa,cACb,IAAuB,0BACvB,IAAiB,kBACjB,IAAW,eACX,IAAqB,uBACrB,IAAc,qBACd,IAAuB,4BAChC,IAAgC;AAOpC,SAAgB,IAAiC;AAC7C,KAAI,MAAmB,KACnB,QAAO;AAGX,KAAI;AAGA,MAFoB,GAAQ,aAAa,EAAA,eAAgC,CAAC,CAE7C,SAAA,uBACvB,EAAK,GAAmB,EAAgB,GACxC,EAAK,GAAmB,GAAc,GAAyB,EAAgB;SAEnF;AACF,MAAiB,EAAK,GAAmB,GAAc,GAAyB,EAAgB;;AAGpG,QAAO;;AAIX,IAAa,KAAuB,GAAwB,EAC/C,IAAiB,cACjB,KAAsB,y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F,YAAY,EAAa;EACrB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAgB,YAAY,EAAK;EAChD,CAAC;CACF,kBAAkB,EAAa;EAC3B,MAAM,EAAc;EACpB,SAAS,6BAAoC,EAAK;EACrD,CAAC;CACF,OAAO,EAAa;EAChB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,qBAAqB,EAAa;EAC9B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,mBAAmB,EAAa;EAC5B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,kBAAkB,EAAa;EAC3B,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,cAAc,EAAa;EACvB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACL"}
|
|
@@ -5,36 +5,36 @@ import s from "localforage";
|
|
|
5
5
|
import c from "node:path";
|
|
6
6
|
import l from "node:fs/promises";
|
|
7
7
|
//#region src/node/storage/storage.util.ts
|
|
8
|
-
var u = { baseDir: "" };
|
|
9
|
-
function
|
|
8
|
+
var u = 200, d = { baseDir: "" };
|
|
9
|
+
function f(e) {
|
|
10
10
|
return `${encodeURIComponent(e)}${a}`;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function p(e) {
|
|
13
13
|
return decodeURIComponent(e.slice(0, -a.length));
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
return c.join(t,
|
|
15
|
+
function m(e, t) {
|
|
16
|
+
return c.join(t, f(e));
|
|
17
17
|
}
|
|
18
|
-
var
|
|
18
|
+
var h = {
|
|
19
19
|
_driver: r,
|
|
20
20
|
_support: !0,
|
|
21
21
|
async _initStorage(t) {
|
|
22
22
|
try {
|
|
23
23
|
let n = typeof t == "object" && t && "baseDir" in t ? t.baseDir : void 0;
|
|
24
|
-
typeof n == "string" && n.length > 0 ?
|
|
24
|
+
typeof n == "string" && n.length > 0 ? d.baseDir = n : d.baseDir = e().CYBERSKILL_STORAGE_DIRECTORY, await l.mkdir(d.baseDir, { recursive: !0 });
|
|
25
25
|
} catch (e) {
|
|
26
26
|
throw n.error("[Storage:init]", e), e;
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
async clear() {
|
|
30
|
-
let { baseDir: e } =
|
|
30
|
+
let { baseDir: e } = d;
|
|
31
31
|
e && (await l.rm(e, {
|
|
32
32
|
recursive: !0,
|
|
33
33
|
force: !0
|
|
34
34
|
}), await l.mkdir(e, { recursive: !0 }));
|
|
35
35
|
},
|
|
36
36
|
async getItem(e) {
|
|
37
|
-
let { baseDir: t } =
|
|
37
|
+
let { baseDir: t } = d, n = m(e, t);
|
|
38
38
|
try {
|
|
39
39
|
let e = await l.readFile(n, "utf8");
|
|
40
40
|
return JSON.parse(e);
|
|
@@ -44,71 +44,77 @@ var m = {
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
async iterate(e) {
|
|
47
|
-
let t = await
|
|
47
|
+
let t = await h.keys(), n = 1;
|
|
48
48
|
for (let r of t) {
|
|
49
|
-
let t = e(await
|
|
49
|
+
let t = e(await h.getItem(r), r, n);
|
|
50
50
|
if (t !== void 0) return t;
|
|
51
51
|
n += 1;
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
54
|
async key(e) {
|
|
55
|
-
return (await
|
|
55
|
+
return (await h.keys())[e] ?? null;
|
|
56
56
|
},
|
|
57
57
|
async keys() {
|
|
58
|
-
let { baseDir: e } =
|
|
58
|
+
let { baseDir: e } = d;
|
|
59
59
|
try {
|
|
60
|
-
return (await l.readdir(e)).filter((e) => e.endsWith(a)).map(
|
|
60
|
+
return (await l.readdir(e)).filter((e) => e.endsWith(a)).map(p);
|
|
61
61
|
} catch (e) {
|
|
62
62
|
if (e.code === "ENOENT") return [];
|
|
63
63
|
throw e;
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
async length() {
|
|
67
|
-
return (await
|
|
67
|
+
return (await h.keys()).length;
|
|
68
68
|
},
|
|
69
69
|
async removeItem(e) {
|
|
70
|
-
let { baseDir: t } =
|
|
70
|
+
let { baseDir: t } = d, n = m(e, t);
|
|
71
71
|
await l.rm(n, { force: !0 });
|
|
72
72
|
},
|
|
73
73
|
async setItem(e, t) {
|
|
74
|
-
let { baseDir: n } =
|
|
74
|
+
let { baseDir: n } = d, r = m(e, n);
|
|
75
75
|
return await l.mkdir(n, { recursive: !0 }), await l.writeFile(r, JSON.stringify(t), "utf8"), t;
|
|
76
76
|
}
|
|
77
|
-
},
|
|
78
|
-
async function
|
|
79
|
-
return
|
|
80
|
-
await s.defineDriver(
|
|
77
|
+
}, g = null, _ = null;
|
|
78
|
+
async function v() {
|
|
79
|
+
return g ? (await g, _) : (g = (async () => {
|
|
80
|
+
await s.defineDriver(h), d.baseDir = e().CYBERSKILL_STORAGE_DIRECTORY;
|
|
81
81
|
let t = await s.getDriver(r);
|
|
82
82
|
await t._initStorage({
|
|
83
|
-
baseDir:
|
|
83
|
+
baseDir: d.baseDir,
|
|
84
84
|
name: i,
|
|
85
85
|
storeName: o
|
|
86
|
-
}),
|
|
86
|
+
}), _ = t;
|
|
87
87
|
})().catch((e) => {
|
|
88
|
-
throw
|
|
89
|
-
}), await
|
|
88
|
+
throw g = null, e;
|
|
89
|
+
}), await g, _);
|
|
90
90
|
}
|
|
91
|
-
var
|
|
91
|
+
var y = {
|
|
92
92
|
async get(e) {
|
|
93
|
+
if (e.length > u) return n.warn(`[Storage:get] Key exceeds maximum length of ${u} characters`), null;
|
|
93
94
|
try {
|
|
94
|
-
return await (await
|
|
95
|
+
return await (await v()).getItem(e) ?? null;
|
|
95
96
|
} catch (e) {
|
|
96
97
|
return t(e, { returnValue: null });
|
|
97
98
|
}
|
|
98
99
|
},
|
|
99
|
-
async set(e,
|
|
100
|
-
|
|
100
|
+
async set(e, n) {
|
|
101
|
+
if (e.length > u) throw RangeError(`Storage key exceeds maximum length of ${u} characters`);
|
|
102
|
+
try {
|
|
103
|
+
await (await v()).setItem(e, n);
|
|
104
|
+
} catch (e) {
|
|
105
|
+
throw t(e), e;
|
|
106
|
+
}
|
|
101
107
|
},
|
|
102
108
|
async remove(e) {
|
|
103
109
|
try {
|
|
104
|
-
await (await
|
|
110
|
+
await (await v()).removeItem(e);
|
|
105
111
|
} catch (e) {
|
|
106
112
|
t(e);
|
|
107
113
|
}
|
|
108
114
|
},
|
|
109
115
|
async keys() {
|
|
110
116
|
try {
|
|
111
|
-
let e = await (await
|
|
117
|
+
let e = await (await v()).keys();
|
|
112
118
|
return Array.isArray(e) ? e : (n.warn("[Storage:keys] Invalid keys response:", e), []);
|
|
113
119
|
} catch (e) {
|
|
114
120
|
return t(e, { returnValue: [] });
|
|
@@ -116,16 +122,16 @@ var v = {
|
|
|
116
122
|
},
|
|
117
123
|
async getLogLink(n) {
|
|
118
124
|
try {
|
|
119
|
-
return `${c.join(e().CYBERSKILL_STORAGE_DIRECTORY,
|
|
125
|
+
return `${c.join(e().CYBERSKILL_STORAGE_DIRECTORY, f(n))} (key: ${n})`;
|
|
120
126
|
} catch (e) {
|
|
121
127
|
return t(e, { returnValue: null });
|
|
122
128
|
}
|
|
123
129
|
}
|
|
124
130
|
};
|
|
125
|
-
function
|
|
126
|
-
|
|
131
|
+
function b() {
|
|
132
|
+
g = null, _ = null, d.baseDir = "";
|
|
127
133
|
}
|
|
128
134
|
//#endregion
|
|
129
|
-
export {
|
|
135
|
+
export { b as resetStorageForTesting, y as storage };
|
|
130
136
|
|
|
131
137
|
//# sourceMappingURL=storage.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import localForage from 'localforage';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { LocalForageDriver, NodeFsDriverState, NodeLocalForageOptions } from './storage.type.js';\n\nimport { catchError, log } from '../log/index.js';\nimport { NODE_FS_DRIVER_NAME, STORAGE_INSTANCE_NAME, STORAGE_KEY_EXTENSION, STORAGE_STORE_NAME } from './storage.constant.js';\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n */\nfunction encodeKey(key: string): string {\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Custom localForage driver that stores JSON-encoded values on the filesystem.\n */\nconst nodeFsDriver: LocalForageDriver = {\n _driver: NODE_FS_DRIVER_NAME,\n _support: true,\n /** Ensures the storage directory exists and respects custom baseDir overrides. */\n async _initStorage(options: unknown) {\n try {\n const baseDirFromOptions = typeof options === 'object' && options !== null && 'baseDir' in options\n ? (options as { baseDir?: unknown }).baseDir\n : undefined;\n\n if (typeof baseDirFromOptions === 'string' && baseDirFromOptions.length > 0) {\n nodeFsDriverState.baseDir = baseDirFromOptions;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries by recreating the directory. Callers must ensure no concurrent operations. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Iterates through all keys, invoking the iterator until it returns a value. */\n async iterate<T, U>(iterator: (value: T, key: string, iterationNumber: number) => U): Promise<U> {\n const keys = await nodeFsDriver.keys();\n let iterationNumber = 1;\n\n for (const key of keys) {\n const value = await nodeFsDriver.getItem<T>(key);\n\n const result = iterator(value as T, key, iterationNumber);\n\n if (result !== undefined) {\n return result;\n }\n iterationNumber += 1;\n }\n\n return undefined as unknown as U;\n },\n /** Returns the key name at the given index or null when out of bounds. */\n async key(keyIndex: number): Promise<string> {\n const keys = await nodeFsDriver.keys();\n\n return keys[keyIndex] ?? null as unknown as string;\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Returns the count of stored keys. */\n async length(): Promise<number> {\n const keys = await nodeFsDriver.keys();\n\n return keys.length;\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\nlet driverInstance: LocalForageDriver | null = null;\n\n/**\n * Prepares and returns the filesystem-backed localForage driver.\n * We bypass localForage's default driver selection and explicitly initialize\n * our custom driver to ensure Node compatibility.\n */\nasync function ensureLocalForageReady(): Promise<LocalForageDriver> {\n if (initPromise) {\n await initPromise;\n return driverInstance as LocalForageDriver;\n }\n\n initPromise = (async () => {\n await localForage.defineDriver(nodeFsDriver);\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n const driver = await localForage.getDriver(NODE_FS_DRIVER_NAME);\n\n const initOptions = {\n baseDir: nodeFsDriverState.baseDir,\n name: STORAGE_INSTANCE_NAME,\n storeName: STORAGE_STORE_NAME,\n } satisfies NodeLocalForageOptions;\n\n await driver._initStorage(initOptions as unknown as Parameters<typeof driver._initStorage>[0]);\n driverInstance = driver;\n })().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return driverInstance as LocalForageDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * This object provides methods for storing, retrieving, and managing data using localForage,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n try {\n const driver = await ensureLocalForageReady();\n const result = await driver.getItem<T>(key);\n\n return result ?? null;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T): Promise<void> {\n const driver = await ensureLocalForageReady();\n\n await driver.setItem(key, value);\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureLocalForageReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n driverInstance = null;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;;;;AAWA,IAAM,IAAuC,EACzC,SAAS,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1B;WAEd,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAYvD,MAAM,IAAiB,GAAa,GAAyB;AAGzD,SAFe,MAAM,GAAwB,EAEhC,QAAQ,GAAK,EAAM;;CASpC,MAAM,OAAO,GAA4B;AACrC,MAAI;AAGA,UAFe,MAAM,GAAwB,EAEhC,WAAW,EAAI;WAEzB,GAAO;AACV,KAAW,EAAM;;;CAUzB,MAAM,OAA0B;AAC5B,MAAI;GAEA,IAAM,IAAO,OADE,MAAM,GAAwB,EACnB,MAAM;AAOhC,UALK,MAAM,QAAQ,EAAK,GAKjB,KAJH,EAAI,KAAK,yCAAyC,EAAK,EAChD,EAAE;WAKV,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,EAAE,EAAE,CAAC;;;CAWrD,MAAM,WAAW,GAAqC;AAClD,MAAI;AAGA,UAAO,GAFa,EAAK,KAAK,GAAQ,CAAC,8BAA8B,EAAU,EAAI,CAAC,CAE9D,SAAS,EAAI;WAEhC,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAI1D;AAOD,SAAgB,IAA+B;AAG3C,CAFA,IAAc,MACd,IAAiB,MACjB,EAAkB,UAAU"}
|
|
1
|
+
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import localForage from 'localforage';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { LocalForageDriver, NodeFsDriverState, NodeLocalForageOptions } from './storage.type.js';\n\nimport { catchError, log } from '../log/index.js';\nimport { NODE_FS_DRIVER_NAME, STORAGE_INSTANCE_NAME, STORAGE_KEY_EXTENSION, STORAGE_STORE_NAME } from './storage.constant.js';\n\nconst MAX_KEY_LENGTH = 200;\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n */\nfunction encodeKey(key: string): string {\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Custom localForage driver that stores JSON-encoded values on the filesystem.\n */\nconst nodeFsDriver: LocalForageDriver = {\n _driver: NODE_FS_DRIVER_NAME,\n _support: true,\n /** Ensures the storage directory exists and respects custom baseDir overrides. */\n async _initStorage(options: unknown) {\n try {\n const baseDirFromOptions = typeof options === 'object' && options !== null && 'baseDir' in options\n ? (options as { baseDir?: unknown }).baseDir\n : undefined;\n\n if (typeof baseDirFromOptions === 'string' && baseDirFromOptions.length > 0) {\n nodeFsDriverState.baseDir = baseDirFromOptions;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries by recreating the directory. Callers must ensure no concurrent operations. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Iterates through all keys, invoking the iterator until it returns a value. */\n async iterate<T, U>(iterator: (value: T, key: string, iterationNumber: number) => U): Promise<U> {\n const keys = await nodeFsDriver.keys();\n let iterationNumber = 1;\n\n for (const key of keys) {\n const value = await nodeFsDriver.getItem<T>(key);\n\n const result = iterator(value as T, key, iterationNumber);\n\n if (result !== undefined) {\n return result;\n }\n iterationNumber += 1;\n }\n\n return undefined as unknown as U;\n },\n /** Returns the key name at the given index or null when out of bounds. */\n async key(keyIndex: number): Promise<string> {\n const keys = await nodeFsDriver.keys();\n\n return keys[keyIndex] ?? null as unknown as string;\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Returns the count of stored keys. */\n async length(): Promise<number> {\n const keys = await nodeFsDriver.keys();\n\n return keys.length;\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\nlet driverInstance: LocalForageDriver | null = null;\n\n/**\n * Prepares and returns the filesystem-backed localForage driver.\n * We bypass localForage's default driver selection and explicitly initialize\n * our custom driver to ensure Node compatibility.\n */\nasync function ensureLocalForageReady(): Promise<LocalForageDriver> {\n if (initPromise) {\n await initPromise;\n return driverInstance as LocalForageDriver;\n }\n\n initPromise = (async () => {\n await localForage.defineDriver(nodeFsDriver);\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n const driver = await localForage.getDriver(NODE_FS_DRIVER_NAME);\n\n const initOptions = {\n baseDir: nodeFsDriverState.baseDir,\n name: STORAGE_INSTANCE_NAME,\n storeName: STORAGE_STORE_NAME,\n } satisfies NodeLocalForageOptions;\n\n await driver._initStorage(initOptions as unknown as Parameters<typeof driver._initStorage>[0]);\n driverInstance = driver;\n })().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return driverInstance as LocalForageDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * This object provides methods for storing, retrieving, and managing data using localForage,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n if (key.length > MAX_KEY_LENGTH) {\n log.warn(`[Storage:get] Key exceeds maximum length of ${MAX_KEY_LENGTH} characters`);\n return null;\n }\n\n try {\n const driver = await ensureLocalForageReady();\n const result = await driver.getItem<T>(key);\n\n return result ?? null;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T): Promise<void> {\n if (key.length > MAX_KEY_LENGTH) {\n throw new RangeError(`Storage key exceeds maximum length of ${MAX_KEY_LENGTH} characters`);\n }\n\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.setItem(key, value);\n }\n catch (error) {\n catchError(error);\n throw error;\n }\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureLocalForageReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n driverInstance = null;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;;;;AAWA,IAAM,IAAiB,KAEjB,IAAuC,EACzC,SAAS,IACZ;AAKD,SAAS,EAAU,GAAqB;AACpC,QAAO,GAAG,mBAAmB,EAAI,GAAG;;AAMxC,SAAS,EAAU,GAA0B;AACzC,QAAO,mBAAmB,EAAS,MAAM,GAAG,CAAC,EAAsB,OAAO,CAAC;;AAM/E,SAAS,EAAY,GAAa,GAAyB;AACvD,QAAO,EAAK,KAAK,GAAS,EAAU,EAAI,CAAC;;AAM7C,IAAM,IAAkC;CACpC,SAAS;CACT,UAAU;CAEV,MAAM,aAAa,GAAkB;AACjC,MAAI;GACA,IAAM,IA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