@autofleet/sadot 1.2.15-beta-7b79ed93.1 → 1.2.16-beta-e2ec42dc.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.
@@ -1,2 +1,2 @@
1
- const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../errors/index.cjs`),n=require(`../models/CustomFieldDefinition.cjs`),r=require(`../models/CustomFieldModelTypeMap.cjs`);require(`../models/index.cjs`);let i=require(`sequelize`);const a=async e=>{let{modelTypeIds:t,...i}=e,a=await n.default.sequelize.transaction(async e=>{let a=await n.default.create(i,{transaction:e});return t?.length&&await Promise.all(t.map(t=>r.default.create({customFieldDefinitionId:a.id,modelTypeId:t},{transaction:e}))),a});try{a.modelTypeIds=(await r.default.findAll({where:{customFieldDefinitionId:a.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{a.modelTypeIds=t??[]}return a},o=async(e,t={withDisabled:!1})=>{let r=t.withDisabled?n.default.unscoped():n.default,i=t.include;if(t.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};i=t.include?Array.isArray(t.include)?[...t.include,e]:[t.include,e]:e}let a=await r.scope(`userScope`).findAll({where:e,transaction:t.transaction,raw:!t.enrichWithModelTypeIds,include:i});return t.enrichWithModelTypeIds?a.map(e=>{let t=e.modelTypeMappings||e.dataValues?.modelTypeMappings;return t&&Array.isArray(t)?e.modelTypeIds=t.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e):e.modelTypeIds=[],e}):a},s=(e,t={withDisabled:!1})=>o({id:{[i.Op.in]:e}},t),c=async(e,t={withDisabled:!1})=>{let{withDisabled:i}=t,a=await(i?n.default.unscoped().scope(`userScope`):n.default.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!a)return null;let o=a.modelTypeMappings||a.dataValues?.modelTypeMappings;if(o&&Array.isArray(o))a.modelTypeIds=o.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e);else try{a.modelTypeIds=(await r.default.findAll({where:{customFieldDefinitionId:a.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{a.modelTypeIds=[]}return a},l=async(e,t,r={})=>{let{include:a,useEntityIdFromInclude:o}=r.modelOptions,s={modelType:e,...!o&&{entityId:{[i.Op.in]:t}}};return n.default.findAll({where:s,transaction:r.transaction,include:a?.(t),raw:!0})},u=e=>n.default.scope(`userScope`).findOne({where:e}),d=async(e,t)=>{let{modelTypeIds:i,...a}=t,o=await n.default.sequelize.transaction(async t=>{let o=(await n.default.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:t}))[1][0];if(i){let n=await r.default.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:t}),a=n.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=n.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await r.default.destroy({where:{id:c.map(e=>e.id)},transaction:t}),s.length>0&&await Promise.all(s.map(n=>r.default.create({customFieldDefinitionId:e,modelTypeId:n},{transaction:t}))),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:t}))}return o});try{o.modelTypeIds=(await r.default.findAll({where:{customFieldDefinitionId:o.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{o.modelTypeIds=i??[]}return o},f=async(e,t={withDisabled:!1,modelType:``})=>{let{withDisabled:r,entityIds:a,modelType:o,modelOptions:s}=t,{include:c,useEntityIdFromInclude:l}=s??{},u={modelType:o,...!l&&a&&a.length>0&&{entityId:{[i.Op.in]:a}}},d=r?n.default.unscoped():n.default,f=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(l&&c&&a){let e=c(a);f.push(...e)}return(await d.scope(`userScope`).findAll({where:u,include:f,raw:!1,transaction:t.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=(e.modelTypeMappings||e.dataValues?.modelTypeMappings||[]).map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(Boolean),e))},p=async(e,{findAll:n,modelOptions:r={},withDisabled:a=!1,...s}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=e[0]?.dataValues??{},l=new Set,u=[],d=new Set;e.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[i.Op.in]:Array.from(d)},name:{[i.Op.in]:Array.from(l)}},p=await(n??o)(f,{withDisabled:a,modelOptions:r,...s}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new t.MissingDefinitionError(Array.from(l).filter(e=>!h[e]));return h};exports.create=a,exports.findAll=o,exports.findByEntityIds=l,exports.findById=c,exports.findByIds=s,exports.findByModelTypeId=f,exports.findByWhere=u,exports.getCustomFieldDefinitionsDictionary=p,exports.update=d;
1
+ const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../errors/index.cjs`),n=require(`../models/CustomFieldDefinition.cjs`),r=require(`../models/CustomFieldModelTypeMap.cjs`);require(`../models/index.cjs`);let i=require(`sequelize`);const a=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),o=async e=>{let{modelTypeIds:t,...i}=e,a=await n.default.sequelize.transaction(async e=>{let a=await n.default.create(i,{transaction:e});return t?.length&&await r.default.bulkCreate(t.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=t??[],a},s=async(e,t={withDisabled:!1})=>{let r=t.withDisabled?n.default.unscoped():n.default,i=t.include;if(t.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};i=t.include?Array.isArray(t.include)?[...t.include,e]:[t.include,e]:e}let o=await r.scope(`userScope`).findAll({where:e,transaction:t.transaction,raw:!t.enrichWithModelTypeIds,include:i});return t.enrichWithModelTypeIds?o.map(e=>{let t=e;return e.modelTypeIds=a(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e}):o},c=(e,t={withDisabled:!1})=>s({id:{[i.Op.in]:e}},t),l=async(e,t={withDisabled:!1})=>{let{withDisabled:r}=t,i=await(r?n.default.unscoped().scope(`userScope`):n.default.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!i)return null;let o=i;return i.modelTypeIds=a(o.modelTypeMappings??o.dataValues?.modelTypeMappings),i},u=async(e,t,r={})=>{let{include:a,useEntityIdFromInclude:o}=r.modelOptions,s={modelType:e,...!o&&{entityId:{[i.Op.in]:t}}};return n.default.findAll({where:s,transaction:r.transaction,include:a?.(t),raw:!0})},d=e=>n.default.scope(`userScope`).findOne({where:e}),f=async(e,t)=>{let{modelTypeIds:i,...a}=t,o=await n.default.sequelize.transaction(async t=>{let o=(await n.default.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:t}))[1][0];if(i){let n=await r.default.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:t}),a=n.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=n.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await r.default.destroy({where:{id:c.map(e=>e.id)},transaction:t}),s.length>0&&await r.default.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:t}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:t}))}return o});return o.modelTypeIds=i??[],o},p=async(e,t={withDisabled:!1,modelType:``})=>{let{withDisabled:r,entityIds:o,modelType:s,modelOptions:c}=t,{include:l,useEntityIdFromInclude:u}=c??{},d={modelType:s,...!u&&o&&o.length>0&&{entityId:{[i.Op.in]:o}}},f=r?n.default.unscoped():n.default,p=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(u&&l&&o){let e=l(o);p.push(...e)}return(await f.scope(`userScope`).findAll({where:d,include:p,raw:!1,transaction:t.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=a(e.modelTypeMappings||e.dataValues?.modelTypeMappings),e))},m=async(e,{findAll:n,modelOptions:r={},withDisabled:a=!1,...o}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=e[0]?.dataValues??{},l=new Set,u=[],d=new Set;e.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[i.Op.in]:Array.from(d)},name:{[i.Op.in]:Array.from(l)}},p=await(n??s)(f,{withDisabled:a,modelOptions:r,...o}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new t.MissingDefinitionError(Array.from(l).filter(e=>!h[e]));return h};exports.create=o,exports.findAll=s,exports.findByEntityIds=u,exports.findById=l,exports.findByIds=c,exports.findByModelTypeId=p,exports.findByWhere=d,exports.getCustomFieldDefinitionsDictionary=m,exports.update=f;
2
2
  //# sourceMappingURL=definition.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"definition.cjs","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","Op","id","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]","MissingDefinitionError"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Batch enriches multiple custom field definitions with modelTypeIds arrays\n * Optimized to make a single query instead of N queries\n *\n * Note: This function is currently unused as we've optimized to use Sequelize associations directly\n * in queries. It's kept here for potential future use cases where batch enrichment\n * after fetching raw data might be needed.\n */\nconst _enrichWithModelTypeIdsBatch = async (\n definitions: CustomFieldDefinition[],\n): Promise<(CustomFieldDefinition & { modelTypeIds?: string[]; })[]> => {\n if (definitions.length === 0) return [];\n\n try {\n const definitionIds = definitions.map(d => d.id);\n\n // Single query to get ALL mappings for ALL definitions\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: { [Op.in]: definitionIds } },\n attributes: ['customFieldDefinitionId', 'modelTypeId'],\n raw: true,\n });\n\n // Group by definition ID\n const mappingsByDefinition = mappings.reduce((acc, m: any) => {\n acc[m.customFieldDefinitionId] = acc[m.customFieldDefinitionId] || [];\n acc[m.customFieldDefinitionId].push(m.modelTypeId);\n return acc;\n }, {} as Record<string, string[]>);\n\n // Enrich each definition\n return definitions.map(def => {\n def.modelTypeIds = mappingsByDefinition[def.id] || [];\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n } catch {\n // On error, set empty arrays for all definitions\n return definitions.map(def => {\n def.modelTypeIds = [];\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await Promise.all(\n modelTypeIds.map(modelTypeId =>\n CustomFieldModelTypeMap.create({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n }, {\n transaction,\n }),\n ),\n );\n }\n\n return customFieldDefinition;\n });\n\n // Fetch modelTypeIds using the mapping table (single query after creation)\n try {\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: result.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n\n result.modelTypeIds = mappings.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n result.modelTypeIds = modelTypeIds ?? [];\n }\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const mappings = (def as any).modelTypeMappings || (def as any).dataValues?.modelTypeMappings;\n\n if (mappings && Array.isArray(mappings)) {\n // Association data was loaded successfully\n def.modelTypeIds = mappings\n .map((m: { modelTypeId?: string; dataValues?: { modelTypeId?: string; }; }) =>\n m.modelTypeId || m.dataValues?.modelTypeId)\n .filter((id): id is string => Boolean(id));\n } else {\n // For backwards compatibility or test environments, set to empty array\n // In production with proper associations, this shouldn't happen\n def.modelTypeIds = [];\n }\n\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Try to extract modelTypeIds from the included association data first\n const mappings = (definition as any).modelTypeMappings || (definition as any).dataValues?.modelTypeMappings;\n\n if (mappings && Array.isArray(mappings)) {\n // Association data was loaded successfully\n definition.modelTypeIds = mappings\n .map((m: { modelTypeId?: string; dataValues?: { modelTypeId?: string; }; }) =>\n m.modelTypeId || m.dataValues?.modelTypeId)\n .filter((id): id is string => Boolean(id));\n } else {\n // Fallback: query the mapping table directly (e.g., in test environments where associations might not work)\n try {\n const mappingRecords = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: definition.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n definition.modelTypeIds = mappingRecords.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n definition.modelTypeIds = [];\n }\n }\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await Promise.all(\n toAdd.map(modelTypeId =>\n CustomFieldModelTypeMap.create({\n customFieldDefinitionId: id,\n modelTypeId,\n }, {\n transaction,\n }),\n ),\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n // Fetch modelTypeIds using the mapping table (single query after update)\n try {\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: result.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n\n result.modelTypeIds = mappings.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n result.modelTypeIds = modelTypeIds ?? [];\n }\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n def.modelTypeIds = mappings.map((m: any) => m.modelTypeId || m.dataValues?.modelTypeId).filter(Boolean);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"mPAqDA,MAAa,EAAS,KACpB,IACmC,CACnC,GAAM,CAAE,eAAc,GAAG,GAAmB,EAEtC,EAAS,MAAMA,EAAAA,QAAsB,UAAW,YAAY,KAAO,IAA6B,CACpG,IAAM,EAAwB,MAAMA,EAAAA,QAAsB,OAAO,EAAuB,CACtF,cACD,CAAC,CAeF,OAbI,GAAc,QAChB,MAAM,QAAQ,IACZ,EAAa,IAAI,GACfC,EAAAA,QAAwB,OAAO,CAC7B,wBAAyB,EAAsB,GAC/C,cACD,CAAE,CACD,cACD,CAAC,CACH,CACF,CAGI,GACP,CAGF,GAAI,CAOF,EAAO,cANU,MAAMA,EAAAA,QAAwB,QAAQ,CACrD,MAAO,CAAE,wBAA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,EAAU,CAAE,CAC5C,KAAM,EAAGA,EAAAA,GAAG,IAAK,MAAM,KAAK,EAAa,CAAE,CAC5C,CAKK,EAAc,MAHC,GAAY,GAGM,EAAO,CAAE,eAAc,eAAc,GAAG,EAAS,CAAC,CAEnF,EAAqB,EAAY,OAAO,GAAO,EAAa,IAAI,EAAI,KAAK,CAAC,CAC1E,EAA2B,OAAO,YAAY,EAAmB,IAAI,GAAc,CAAC,EAAW,KAAM,EAAW,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAIK,EAAAA,uBADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
1
+ {"version":3,"file":"definition.cjs","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","Op","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]","MissingDefinitionError"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const defWithMappings = def as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"mPAuBA,MAAM,EAAmC,GACnC,CAAC,GAAY,CAAC,MAAM,QAAQ,EAAS,CAChC,EAAE,CAGJ,EACJ,IAAK,GAAmB,EAAE,aAAe,EAAE,YAAY,YAAY,CACnE,OAAO,GAAM,EAAQ,EAAI,CAGjB,EAAS,KACpB,IACmC,CACnC,GAAM,CAAE,eAAc,GAAG,GAAmB,EAEtC,EAAS,MAAMA,EAAAA,QAAsB,UAAW,YAAY,KAAO,IAA6B,CACpG,IAAM,EAAwB,MAAMA,EAAAA,QAAsB,OAAO,EAAuB,CACtF,cACD,CAAC,CAYF,OAVI,GAAc,QAChB,MAAMC,EAAAA,QAAwB,WAC5B,EAAa,IAAI,IAAgB,CAC/B,wBAAyB,EAAsB,GAC/C,cACD,EAAE,CACH,CAAE,cAAa,CAChB,CAGI,GACP,CAIF,MAFA,GAAO,aAAe,GAAgB,EAAE,CAEjC,GAYI,EAAU,MACrB,EACA,EAA4B,CAAE,aAAc,GAAO,GACd,CACrC,IAAM,EAAa,EAAQ,aACvBD,EAAAA,QAAsB,UAAU,CAChCA,EAAAA,QAGAE,EAAwD,EAAQ,QAGpE,GAAI,EAAQ,uBAAwB,CAClC,IAAMC,EAAwC,CAC5C,YAAa,oBACb,SAAU,GACV,WAAY,CAAC,cAAc,CAC5B,CAED,AAME,EANE,EAAQ,QAEK,MAAM,QAAQ,EAAQ,QAAQ,CACzC,CAAC,GAAG,EAAQ,QAAS,EAAyB,CAC9C,CAAC,EAAQ,QAAS,EAAyB,CAEhC,EAInB,IAAM,EAAc,MAAM,EAAW,MAAM,YAAY,CAAC,QAAQ,CAC9D,QACA,YAAa,EAAQ,YACrB,IAAK,CAAC,EAAQ,uBACd,Q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,eAAc,eAAc,GAAG,EAAS,CAAC,CAEnF,EAAqB,EAAY,OAAO,GAAO,EAAa,IAAI,EAAI,KAAK,CAAC,CAC1E,EAA2B,OAAO,YAAY,EAAmB,IAAI,GAAc,CAAC,EAAW,KAAM,EAAW,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAII,EAAAA,uBADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
@@ -1,2 +1,2 @@
1
- import{MissingDefinitionError as e}from"../errors/index.js";import t from"../models/CustomFieldDefinition.js";import n from"../models/CustomFieldModelTypeMap.js";import"../models/index.js";import{Op as r}from"sequelize";const i=async e=>{let{modelTypeIds:r,...i}=e,a=await t.sequelize.transaction(async e=>{let a=await t.create(i,{transaction:e});return r?.length&&await Promise.all(r.map(t=>n.create({customFieldDefinitionId:a.id,modelTypeId:t},{transaction:e}))),a});try{a.modelTypeIds=(await n.findAll({where:{customFieldDefinitionId:a.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{a.modelTypeIds=r??[]}return a},a=async(e,n={withDisabled:!1})=>{let r=n.withDisabled?t.unscoped():t,i=n.include;if(n.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};i=n.include?Array.isArray(n.include)?[...n.include,e]:[n.include,e]:e}let a=await r.scope(`userScope`).findAll({where:e,transaction:n.transaction,raw:!n.enrichWithModelTypeIds,include:i});return n.enrichWithModelTypeIds?a.map(e=>{let t=e.modelTypeMappings||e.dataValues?.modelTypeMappings;return t&&Array.isArray(t)?e.modelTypeIds=t.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e):e.modelTypeIds=[],e}):a},o=(e,t={withDisabled:!1})=>a({id:{[r.in]:e}},t),s=async(e,r={withDisabled:!1})=>{let{withDisabled:i}=r,a=await(i?t.unscoped().scope(`userScope`):t.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!a)return null;let o=a.modelTypeMappings||a.dataValues?.modelTypeMappings;if(o&&Array.isArray(o))a.modelTypeIds=o.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e);else try{a.modelTypeIds=(await n.findAll({where:{customFieldDefinitionId:a.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{a.modelTypeIds=[]}return a},c=async(e,n,i={})=>{let{include:a,useEntityIdFromInclude:o}=i.modelOptions,s={modelType:e,...!o&&{entityId:{[r.in]:n}}};return t.findAll({where:s,transaction:i.transaction,include:a?.(n),raw:!0})},l=e=>t.scope(`userScope`).findOne({where:e}),u=async(e,r)=>{let{modelTypeIds:i,...a}=r,o=await t.sequelize.transaction(async r=>{let o=(await t.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:r}))[1][0];if(i){let t=await n.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:r}),a=t.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=t.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await n.destroy({where:{id:c.map(e=>e.id)},transaction:r}),s.length>0&&await Promise.all(s.map(t=>n.create({customFieldDefinitionId:e,modelTypeId:t},{transaction:r}))),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:r}))}return o});try{o.modelTypeIds=(await n.findAll({where:{customFieldDefinitionId:o.id},attributes:[`modelTypeId`],raw:!0})).map(e=>e.modelTypeId)}catch{o.modelTypeIds=i??[]}return o},d=async(e,n={withDisabled:!1,modelType:``})=>{let{withDisabled:i,entityIds:a,modelType:o,modelOptions:s}=n,{include:c,useEntityIdFromInclude:l}=s??{},u={modelType:o,...!l&&a&&a.length>0&&{entityId:{[r.in]:a}}},d=i?t.unscoped():t,f=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(l&&c&&a){let e=c(a);f.push(...e)}return(await d.scope(`userScope`).findAll({where:u,include:f,raw:!1,transaction:n.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=(e.modelTypeMappings||e.dataValues?.modelTypeMappings||[]).map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(Boolean),e))},f=async(t,{findAll:n,modelOptions:i={},withDisabled:o=!1,...s}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=t[0]?.dataValues??{},l=new Set,u=[],d=new Set;t.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[r.in]:Array.from(d)},name:{[r.in]:Array.from(l)}},p=await(n??a)(f,{withDisabled:o,modelOptions:i,...s}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new e(Array.from(l).filter(e=>!h[e]));return h};export{i as create,a as findAll,c as findByEntityIds,s as findById,o as findByIds,d as findByModelTypeId,l as findByWhere,f as getCustomFieldDefinitionsDictionary,u as update};
1
+ import{MissingDefinitionError as e}from"../errors/index.js";import t from"../models/CustomFieldDefinition.js";import n from"../models/CustomFieldModelTypeMap.js";import"../models/index.js";import{Op as r}from"sequelize";const i=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),a=async e=>{let{modelTypeIds:r,...i}=e,a=await t.sequelize.transaction(async e=>{let a=await t.create(i,{transaction:e});return r?.length&&await n.bulkCreate(r.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=r??[],a},o=async(e,n={withDisabled:!1})=>{let r=n.withDisabled?t.unscoped():t,a=n.include;if(n.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};a=n.include?Array.isArray(n.include)?[...n.include,e]:[n.include,e]:e}let o=await r.scope(`userScope`).findAll({where:e,transaction:n.transaction,raw:!n.enrichWithModelTypeIds,include:a});return n.enrichWithModelTypeIds?o.map(e=>{let t=e;return e.modelTypeIds=i(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e}):o},s=(e,t={withDisabled:!1})=>o({id:{[r.in]:e}},t),c=async(e,n={withDisabled:!1})=>{let{withDisabled:r}=n,a=await(r?t.unscoped().scope(`userScope`):t.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!a)return null;let o=a;return a.modelTypeIds=i(o.modelTypeMappings??o.dataValues?.modelTypeMappings),a},l=async(e,n,i={})=>{let{include:a,useEntityIdFromInclude:o}=i.modelOptions,s={modelType:e,...!o&&{entityId:{[r.in]:n}}};return t.findAll({where:s,transaction:i.transaction,include:a?.(n),raw:!0})},u=e=>t.scope(`userScope`).findOne({where:e}),d=async(e,r)=>{let{modelTypeIds:i,...a}=r,o=await t.sequelize.transaction(async r=>{let o=(await t.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:r}))[1][0];if(i){let t=await n.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:r}),a=t.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=t.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await n.destroy({where:{id:c.map(e=>e.id)},transaction:r}),s.length>0&&await n.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:r}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:r}))}return o});return o.modelTypeIds=i??[],o},f=async(e,n={withDisabled:!1,modelType:``})=>{let{withDisabled:a,entityIds:o,modelType:s,modelOptions:c}=n,{include:l,useEntityIdFromInclude:u}=c??{},d={modelType:s,...!u&&o&&o.length>0&&{entityId:{[r.in]:o}}},f=a?t.unscoped():t,p=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(u&&l&&o){let e=l(o);p.push(...e)}return(await f.scope(`userScope`).findAll({where:d,include:p,raw:!1,transaction:n.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=i(e.modelTypeMappings||e.dataValues?.modelTypeMappings),e))},p=async(t,{findAll:n,modelOptions:i={},withDisabled:a=!1,...s}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=t[0]?.dataValues??{},l=new Set,u=[],d=new Set;t.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[r.in]:Array.from(d)},name:{[r.in]:Array.from(l)}},p=await(n??o)(f,{withDisabled:a,modelOptions:i,...s}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new e(Array.from(l).filter(e=>!h[e]));return h};export{a as create,o as findAll,l as findByEntityIds,c as findById,s as findByIds,f as findByModelTypeId,u as findByWhere,p as getCustomFieldDefinitionsDictionary,d as update};
2
2
  //# sourceMappingURL=definition.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"definition.js","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","id","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Batch enriches multiple custom field definitions with modelTypeIds arrays\n * Optimized to make a single query instead of N queries\n *\n * Note: This function is currently unused as we've optimized to use Sequelize associations directly\n * in queries. It's kept here for potential future use cases where batch enrichment\n * after fetching raw data might be needed.\n */\nconst _enrichWithModelTypeIdsBatch = async (\n definitions: CustomFieldDefinition[],\n): Promise<(CustomFieldDefinition & { modelTypeIds?: string[]; })[]> => {\n if (definitions.length === 0) return [];\n\n try {\n const definitionIds = definitions.map(d => d.id);\n\n // Single query to get ALL mappings for ALL definitions\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: { [Op.in]: definitionIds } },\n attributes: ['customFieldDefinitionId', 'modelTypeId'],\n raw: true,\n });\n\n // Group by definition ID\n const mappingsByDefinition = mappings.reduce((acc, m: any) => {\n acc[m.customFieldDefinitionId] = acc[m.customFieldDefinitionId] || [];\n acc[m.customFieldDefinitionId].push(m.modelTypeId);\n return acc;\n }, {} as Record<string, string[]>);\n\n // Enrich each definition\n return definitions.map(def => {\n def.modelTypeIds = mappingsByDefinition[def.id] || [];\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n } catch {\n // On error, set empty arrays for all definitions\n return definitions.map(def => {\n def.modelTypeIds = [];\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await Promise.all(\n modelTypeIds.map(modelTypeId =>\n CustomFieldModelTypeMap.create({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n }, {\n transaction,\n }),\n ),\n );\n }\n\n return customFieldDefinition;\n });\n\n // Fetch modelTypeIds using the mapping table (single query after creation)\n try {\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: result.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n\n result.modelTypeIds = mappings.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n result.modelTypeIds = modelTypeIds ?? [];\n }\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const mappings = (def as any).modelTypeMappings || (def as any).dataValues?.modelTypeMappings;\n\n if (mappings && Array.isArray(mappings)) {\n // Association data was loaded successfully\n def.modelTypeIds = mappings\n .map((m: { modelTypeId?: string; dataValues?: { modelTypeId?: string; }; }) =>\n m.modelTypeId || m.dataValues?.modelTypeId)\n .filter((id): id is string => Boolean(id));\n } else {\n // For backwards compatibility or test environments, set to empty array\n // In production with proper associations, this shouldn't happen\n def.modelTypeIds = [];\n }\n\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Try to extract modelTypeIds from the included association data first\n const mappings = (definition as any).modelTypeMappings || (definition as any).dataValues?.modelTypeMappings;\n\n if (mappings && Array.isArray(mappings)) {\n // Association data was loaded successfully\n definition.modelTypeIds = mappings\n .map((m: { modelTypeId?: string; dataValues?: { modelTypeId?: string; }; }) =>\n m.modelTypeId || m.dataValues?.modelTypeId)\n .filter((id): id is string => Boolean(id));\n } else {\n // Fallback: query the mapping table directly (e.g., in test environments where associations might not work)\n try {\n const mappingRecords = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: definition.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n definition.modelTypeIds = mappingRecords.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n definition.modelTypeIds = [];\n }\n }\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await Promise.all(\n toAdd.map(modelTypeId =>\n CustomFieldModelTypeMap.create({\n customFieldDefinitionId: id,\n modelTypeId,\n }, {\n transaction,\n }),\n ),\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n // Fetch modelTypeIds using the mapping table (single query after update)\n try {\n const mappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: result.id },\n attributes: ['modelTypeId'],\n raw: true,\n });\n\n result.modelTypeIds = mappings.map((m: { modelTypeId: string; }) => m.modelTypeId);\n } catch {\n result.modelTypeIds = modelTypeIds ?? [];\n }\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n def.modelTypeIds = mappings.map((m: any) => m.modelTypeId || m.dataValues?.modelTypeId).filter(Boolean);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"4NAqDA,MAAa,EAAS,KACpB,IACmC,CACnC,GAAM,CAAE,eAAc,GAAG,GAAmB,EAEtC,EAAS,MAAMA,EAAsB,UAAW,YAAY,KAAO,IAA6B,CACpG,IAAM,EAAwB,MAAMA,EAAsB,OAAO,EAAuB,CACtF,cACD,CAAC,CAeF,OAbI,GAAc,QAChB,MAAM,QAAQ,IACZ,EAAa,IAAI,GACfC,EAAwB,OAAO,CAC7B,wBAAyB,EAAsB,GAC/C,cACD,CAAE,CACD,cACD,CAAC,CACH,CACF,CAGI,GACP,CAGF,GAAI,CAOF,EAAO,cANU,MAAMA,EAAwB,QAAQ,CACrD,MAAO,CAAE,wBAAyB,EAAO,GAAI,CAC7C,WAAY,CAAC,cAAc,CAC3B,IAAK,GACN,CAAC,EAE6B,IAAK,GAAgC,EAAE,YAAY,MAC5E,CACN,EAAO,aAAe,GAAgB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AAW,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAI,EADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
1
+ {"version":3,"file":"definition.js","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const defWithMappings = def as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"4NAuBA,MAAM,EAAmC,GACnC,CAAC,GAAY,CAAC,MAAM,QAAQ,EAAS,CAChC,EAAE,CAGJ,EACJ,IAAK,GAAmB,EAAE,aAAe,EAAE,YA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
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -53,8 +53,8 @@
53
53
  "npm-watch": "^0.11.0",
54
54
  "supertest": "^7.0.0",
55
55
  "@autofleet/errors": "^3.1.41",
56
+ "@autofleet/node-common": "^4.2.23",
56
57
  "@autofleet/logger": "^4.2.45",
57
- "@autofleet/node-common": "^4.2.22",
58
58
  "@autofleet/zehut": "^4.7.5"
59
59
  },
60
60
  "peerDependencies": {