Member Junction
    Preparing search index...
    Index

    Constructors

    Properties

    Accessors

    Methods

    addDefaultPermissionsForEntity addDefaultRolesForApplication addEntityToApplicationForSchema applicationExists applyAdvancedGeneration applyCategoryInfoSettings applyDefaultInViewUpdates applyEntityIcon applyEntityImportance applyEntitySearchConfig applyFieldCategories applyFormLayout applyFullTextSearchUpdates applyLLMFieldDescriptions applyLLMForeignKeys applyLLMPrimaryKeys applyNameFieldUpdates applySchemaPrefixesFromConfig applySearchableFieldUpdates applySearchPredicateUpdates applySmartFieldIdentification applySoftPKFKConfig applyValueListConfig applyVEFieldCategories boolLit buildEntityPairRelationshipSQL buildExistingCategorySet buildExternalFieldRemoveSQL buildInsertRelationshipSQL buildISAValidationSQL buildParentChainContext buildSourceEntityContext checkAndRemoveMetadataForDeletedTables checkDropSQLObject checkEntityFieldSequenceIntegrity cleanupStaleEntityRelationships coalesce conditionalInsert createDefaultConstraintForSpecialDateField createExcludeTablesAndSchemasFilter createNewApplication createNewEntities createNewEntity createNewEntityDisplayName createNewEntityFieldsFromSchema createNewEntityInsertSQL createNewEntityName createNewUUID decorateSingleVirtualEntityWithLLM decorateVirtualEntitiesWithLLM deleteUnneededEntityFields deriveEntityNameFromView detectAndSetGeoCodingSupport dropAndCreateDefaultConstraintForSpecialDateField dropExistingDefaultConstraint dropIfExists ensureCreatedAtUpdatedAtFieldsExist ensureDeletedAtFieldsExist ensureSpecialDateFieldExistsAndHasCorrectDefaultValue entityHasExternalDataSourceColumn entityIdSubquery externalObjectIsSyncable extractEntitiesFromConfig extractFieldValueLists extractISARelationshipsFromConfig extractOrganicKeysFromConfig extractTablesFromConfig extractVirtualEntitiesFromConfig findFieldSourceParent generateNewEntityDescriptions generateValidatorFunctionFromCheckConstraint getApplicationIDForSchema getEntityNamingOptions getNewEntityNameRule getPendingEntityFieldINSERTSQL getPendingEntityFieldsSELECTSQL groupFieldsByEntityPair hasSoftPrimaryKeyInConfig iif isFieldEligibleForNameField isFieldEligibleForUserSearch isLikelyLogOrAuditEntity isSchemaNew loadGeneratedCode loadSchemaInfoRecords manageEntityFields manageEntityFieldValuesAndValidatorFunctions manageEntityRelationships manageExternalEntities manageExternalEntityRelationships manageManyToManyEntityRelationships manageMetadata manageOneToManyEntityRelationships manageParentEntityFields manageSingleEntityParentFields manageSingleExternalEntity manageSingleVirtualEntity manageSingleVirtualEntityField manageVirtualEntities markupEntityName newEntityNameWithAdvancedGeneration nextAvailableEntityFieldSequence parentFieldNeedsUpdate parseCheckConstraintValues parseDefaultValue primaryKeyFlagsChanged processEntitiesBatched processEntityAdvancedGeneration processEntityConfigs processISARelationshipConfig processOrganicKeyConfig processVirtualEntityConfig qi qs qsql removeStaleOneToManyRelationships resolveAllowCachingForSchema resolveEntityNamesToIDs resolvePrimaryKeyFlags runQuery runQueryWithParams sanitizeCodeType selectNameFieldWinner selectTop setDefaultColumnWidthWhereNeeded shouldCreateNewEntity simpleNewEntityName syncEntityFieldValues updateEntityFieldDisplayNameWhereNull updateEntityFieldRelatedEntityNameFieldMap updateExistingEntitiesFromSchema updateExistingEntityFieldsFromSchema updateSchemaInfoFromDatabase utcNow validateExtendedType validateISARelationships AddEntityRequiringViewRegen addNewEntitiesToModifiedList invalidateSoftPKFKConfigCache

    Constructors

    Properties

    _sqlUtilityObject: SQLUtilityBase = ...

    Accessors

    • get dbProvider(): CodeGenDatabaseProvider

      Returns the CodeGenDatabaseProvider for the current database platform. Lazily initialized from dbPlatform() configuration.

      Lookup goes through MJGlobal.ClassFactory keyed by the platform string, which matches the @RegisterClass(CodeGenDatabaseProvider, '<platform>') decorators on the concrete providers ('sqlserver', 'postgresql'). Mismatched keys silently fall back to the abstract base class — fail loud with an explicit error so a misconfigured platform doesn't ship as a runtime breakage in dialect-specific methods.

      Returns CodeGenDatabaseProvider

    Methods

    • Adds default permissions for a newly created entity based on config settings. Shared by both table-backed entity creation and virtual entity creation.

      Parameters

      Returns Promise<void>

    • Adds default ApplicationRole records for a newly created application based on config settings. This grants configured roles access to the new application automatically.

      Parameters

      Returns Promise<void>

    • Adds a newly created entity to the application(s) that match its schema name. If no application exists for the schema and config allows it, creates one. Shared by both table-backed entity creation and virtual entity creation.

      Parameters

      Returns Promise<void>

    • Generate SQL UPDATEs for DefaultInView on the identified default view fields

      Parameters

      • sqlStatements: string[]
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult

      Returns void

    • Applies entity importance analysis to MJApplicationEntity records. Only called for NEW entities to set DefaultForNewUser.

      Parameters

      • pool: CodeGenConnection
      • entityId: string
      • importance: {
            confidence: string;
            defaultForNewUser: boolean;
            entityCategory: string;
            reasoning: string;
        }

      Returns Promise<void>

    • Generate SQL UPDATEs for entity-level AllowUserSearchAPI.

      Guardrail: even if the LLM proposes enabling AllowUserSearchAPI on an entity that looks like an audit/log/run-history table by name, refuse — those tables grow unboundedly, the LIKE fan-out across them dominates global-search latency, and they're virtually never the right target for a user-facing search box. The team can still enable search on such an entity by setting AutoUpdateAllowUserSearchAPI=0 and flipping the flag manually.

      Parameters

      • sqlStatements: string[]
      • entity: {
            AllowUserSearchAPI?: boolean;
            AutoUpdateAllowUserSearchAPI?: boolean;
            ID: string;
            Name?: string;
            SchemaName?: string;
        }
      • result: SmartFieldIdentificationResult

      Returns void

    • Applies category, display name, extended type, and code type to entity fields. Enforces stability rules: fields with existing categories cannot move to NEW categories. All SQL updates are batched into a single execution for performance.

      Parameters

      • pool: CodeGenConnection
      • entity: EntityInfo
      • fields: {
            AutoUpdateCategory: boolean;
            AutoUpdateDisplayName: boolean;
            Category: string | null;
            CodeType: string;
            DisplayName: string;
            ExtendedType: string;
            GeneratedFormSection: string;
            ID: string;
            Name: string;
        }[]
      • fieldCategories: {
            category: string;
            codeType?: string | null;
            displayName?: string;
            extendedType?: string | null;
            fieldName: string;
            reason?: string;
        }[]
      • existingCategories: Set<string>

      Returns Promise<void>

    • Apply form layout generation results to set category on entity fields. Delegates to shared methods for category assignment, icon, and category info persistence.

      Parameters

      • pool: CodeGenConnection

        Database connection pool

      • entity: EntityInfo
      • fields: {
            AutoUpdateCategory: boolean;
            AutoUpdateDisplayName: boolean;
            Category: string | null;
            CodeType: string;
            DisplayName: string;
            ExtendedType: string;
            GeneratedFormSection: string;
            ID: string;
            Name: string;
        }[]

        Entity fields

      • result: FormLayoutResult

        Form layout result from LLM

      • isNewEntity: boolean = false

        If true, apply entityImportance; if false, skip it

      Returns Promise<void>

    • Generate SQL UPDATEs for entity-level and field-level FullTextSearch configuration

      Parameters

      • sqlStatements: string[]
      • entity: {
            AutoUpdateFullTextSearch?: boolean;
            FullTextSearchEnabled?: boolean;
            ID: string;
        }
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult

      Returns void

    • Applies LLM-generated field descriptions to entity fields that lack descriptions. All SQL updates are batched into a single execution for performance.

      Parameters

      • pool: CodeGenConnection
      • entity: EntityInfo
      • fieldDescriptions: {
            category: string | null;
            codeType:
                | "SQL"
                | "HTML"
                | "Other"
                | "CSS"
                | "JavaScript"
                | "TypeScript"
                | null;
            description: string;
            displayName: string
            | null;
            extendedType: string | null;
            fieldName: string;
        }[]
      • schema: string

      Returns Promise<boolean>

    • Applies LLM-identified foreign keys to entity fields. Sets RelatedEntityID, RelatedEntityFieldName, and IsSoftForeignKey=1. Only applies high and medium confidence FKs. All SQL updates are batched into a single execution for performance.

      Parameters

      • pool: CodeGenConnection
      • entity: EntityInfo
      • foreignKeys: {
            confidence: "high" | "medium" | "low";
            fieldName: string;
            relatedEntityName: string;
            relatedFieldName: string;
        }[]
      • schema: string

      Returns Promise<boolean>

    • Applies LLM-identified primary keys to entity fields. Sets IsPrimaryKey=1 and IsSoftPrimaryKey=1 for identified fields. First clears any default PK that was set by field-sync (field #1 fallback). All SQL updates are batched into a single execution for performance.

      Parameters

      Returns Promise<boolean>

    • Generate SQL UPDATEs for IsNameField — SINGLE-WINNER semantics.

      The LLM's result.nameFields is a RANKED candidate list (it may propose several fields that "together" name the record, e.g. FirstName + LastName), but every downstream consumer — EntityInfo.NameField, the base-view generator's FK-name virtual columns, RelatedEntityNameFieldMap resolution — assumes exactly ONE name field per entity. Historically this method flagged EVERY candidate and never cleared anything, so flags accumulated across runs (57 core entities ended up with 2–4 IsNameField=1 rows) and the FK-name pick silently DRIFTED between CodeGen runs as the first-by-sequence winner changed (observed: Conversation Details flipping MessageRole, reshaping every view that joins to it).

      Winner selection (deterministic, mirrors EntityInfo.NameField's runtime preference and the v5.41 metadata backfill):

      1. Stability — if exactly one currently-flagged field is still eligible, it stays the winner regardless of what the LLM proposes. An already-valid single name field never drifts.
      2. Repair — if several flagged fields are eligible, prefer the one literally named Name, else the first in field order (fields arrive ordered by Sequence from the metadata query).
      3. Fresh pick — if no flagged field is eligible, take the first eligible LLM candidate in ranked order.

      Every NON-winner with IsNameField=1 and AutoUpdateIsNameField=1 is CLEARED, so historical accumulation self-heals on the next analysis pass. Fields pinned with AutoUpdateIsNameField=0 are never touched in either direction — the user owns them, and EntityInfo.NameField's literal-Name preference arbitrates at runtime.

      Parameters

      • sqlStatements: string[]
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult

      Returns void

    • Applies schema-level entity name prefix/suffix rules from additionalSchemaInfo.json. The file may contain a top-level "Schemas" array with entries like:

      { "name": "CRM", "entityNamePrefix": "CRM: ", "entityNameSuffix": "" }
      

      These are merged into the cached SchemaInfo records so that getNewEntityNameRule() can resolve them during entity creation. Config-file rules (mj.config.cjs) still take priority over these — this just provides a fallback from DBAutoDoc output.

      Returns void

    • Generate SQL UPDATEs for IncludeInUserSearchAPI on the identified searchable fields.

      Guardrails: even if the SmartFieldIdentificationResult flags a field as searchable, we refuse to enable IncludeInUserSearchAPI when the field is a primary key, a non-text type (LIKE forces an implicit per-row CONVERT and never seeks an index), or an unbounded text type whose parent entity is not FTX-enabled (the LIKE path cannot seek MAX/ntext/text columns; FTX is the right tool there). These are always-wrong cases — they cause the data provider to emit unindexed scans on every search.

      Parameters

      • sqlStatements: string[]
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult
      • Optionalentity: { FullTextSearchEnabled?: boolean }

      Returns void

    • Generate SQL UPDATEs for UserSearchPredicateAPI on each searchable field

      Parameters

      • sqlStatements: string[]
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult

      Returns void

    • Apply smart field identification results to entity fields and entity-level search configuration

      Parameters

      • pool: CodeGenConnection
      • entity: {
            AllowUserSearchAPI?: boolean;
            AutoUpdateAllowUserSearchAPI?: boolean;
            AutoUpdateFullTextSearch?: boolean;
            FullTextSearchEnabled?: boolean;
            ID: string;
            Name?: string;
            SchemaName?: string;
        }
      • fields: Record<string, unknown>[]
      • result: SmartFieldIdentificationResult

      Returns Promise<void>

    • Applies soft PK/FK configuration from a JSON file specified in mj.config.cjs (additionalSchemaInfo property). For soft PKs: Sets BOTH IsPrimaryKey=1 AND IsSoftPrimaryKey=1 (IsPrimaryKey is source of truth, IsSoftPrimaryKey protects from schema sync). For soft FKs: Sets RelatedEntityID/RelatedEntityFieldName + IsSoftForeignKey=1 (RelatedEntityID is source of truth, IsSoftForeignKey protects from schema sync). All UPDATE statements are logged to migration files via LogSQLAndExecute() for CI/CD traceability.

      Parameters

      Returns Promise<boolean>

    • Applies soft value-list configuration from Fields[] entries in additionalSchemaInfo.json. Runs AFTER manageEntityFieldValuesAndValidatorFunctions() so CHECK-constraint-derived values are already in the database. If EntityFieldValue rows already exist for a field (from CHECK constraints), the soft-enum pass skips that field — CHECK is authoritative.

      Parameters

      Returns Promise<boolean>

    • Applies category assignments from VE decoration results using the shared category methods. Loads field records from DB (needs ID, Name, Category, AutoUpdateCategory, AutoUpdateDisplayName) then delegates to the shared methods.

      Parameters

      Returns Promise<boolean>

    • Handles relationship management for an entire entity pair at once. Matches N FK fields to N existing relationship records using a 1:1 mapping:

      1. Exact matches (relationship.JoinField === fk.Name) — already correct, skip
      2. Unmatched FK fields assigned to unmatched relationships (update JoinField)
      3. Remaining FK fields with no available relationship — create new records

      Parameters

      • relationships: Record<string, unknown>[]
      • fkFields: Record<string, unknown>[]
      • md: Metadata
      • relationshipCountMap: Map<number, number>

      Returns string

    • Builds a set of existing category names from entity fields. Used to enforce category stability (prevent renaming).

      Parameters

      • fields: { Category: string | null }[]

      Returns Set<string>

    • Builds the DELETE for EntityFields no longer present in the freshly-introspected remote object, or '' when nothing should be removed. Field-name matching is case-insensitive. Pure/testable (no DB access) — the data-loss-sensitive computation guarded by externalObjectIsSyncable.

      Parameters

      • schema: string
      • existingFields: { ID: string; Name: string }[]
      • introspectedFieldNames: string[]

      Returns string

    • Builds SQL to INSERT a new EntityRelationship record for a discovered FK field.

      Parameters

      • f: Record<string, unknown>
      • md: Metadata
      • relationshipCountMap: Map<number, number>

      Returns string

    • Builds the provider-neutral query behind validateISARelationships: one row per DECLARED IS-A child (Entity.ParentID IS NOT NULL) carrying everything the severity rules need. LEFT JOINs throughout so an unresolvable ParentID still returns a row (that is a hard error, not a missing row).

      Parameters

      • schema: string

      Returns string

    • Builds IS-A parent chain context for an entity, computing which parent each inherited field originates from. Used to provide the LLM with inheritance awareness during form layout generation.

      Returns an empty object for entities without parents, so it can be safely spread into the entity object passed to generateFormLayout().

      Parameters

      • entity: { ParentID?: string }
      • fields: {
            AllowUpdateAPI?: boolean;
            IsPrimaryKey?: boolean;
            IsVirtual?: boolean;
            Name: string;
        }[]

      Returns {
          IsChildEntity?: boolean;
          ParentChain?: { entityID: string; entityName: string }[];
      }

    • Parses a view definition SQL and resolves referenced tables to MJ entities. Returns enriched source entity context (all fields with descriptions and categories) for the LLM to use when decorating virtual entity fields.

      Parameters

      • viewDefinition: string

      Returns {
          Description: string;
          Fields: {
              Category: string | null;
              Description: string;
              IsForeignKey: boolean;
              IsPrimaryKey: boolean;
              Name: string;
              Type: string;
          }[];
          Name: string;
      }[]

    • This method will look for situations where entity metadata exist in the entities metadata table but the underlying table has been deleted. In this case, the metadata for the entity should be removed. This method is called as part of the manageMetadata method and is not intended to be called directly.

      Parameters

      Returns Promise<boolean>

    • INTEGRITY CHECK — in a well-formed entity every base (non-virtual) field sequences BEFORE the virtual/related fields, so the EntityField order matches the base view's SELECT [base].*, <joins> column output. The positional save-capture in the data providers (e.g. SQLServerDataProvider's @ResultTable) relies on that alignment. CodeGen assigns newly-discovered columns a temporary maxSequence + 100000 offset that updateExistingEntityFieldsFromSchema is supposed to renumber; if a base column is left sequenced AFTER a virtual field, the save-capture would mis-route values by position. The providers now compensate (saves stay correct), but the metadata is still wrong — so we scan for it after every metadata pass and log a prominent warning to drive the root cause out over time.

      Parameters

      Returns Promise<void>

    • Public entry point for cleaning up stale EntityRelationship records. This must be called AFTER deleteUnneededEntityFields() has run, so that stale EntityField records (for dropped columns) are removed before we check which relationships are still valid. Called from sql_codegen.ts after the second manageEntityFields() pass.

      Parameters

      • pool: CodeGenConnection

        database connection

      • excludeSchemas: string[]

        schemas to exclude from FK field lookup

      Returns Promise<boolean>

    • Returns ISNULL/COALESCE expression. Both platforms support COALESCE, but SQL Server also has ISNULL.

      Parameters

      • expr: string
      • fallback: string

      Returns string

    • Generates SQL for conditional INSERT (IF NOT EXISTS pattern). Delegates to the database provider.

      Parameters

      • checkQuery: string
      • insertSQL: string

      Returns string

    • Creates the default constraint for a special date field. This method is called as part of the ensureSpecialDateFieldExistsAndHasCorrectDefaultValue method and is not intended to be called directly.

      Parameters

      Returns Promise<void>

    • Creates a new application using direct SQL INSERT to ensure it's captured in SQL logging. The Path field is auto-generated from Name using the same slug logic as MJApplicationEntityServer.

      Parameters

      • pool: CodeGenConnection

        SQL connection pool

      • appID: string

        Pre-generated UUID for the application

      • appName: string

        Name of the application

      • schemaName: string

        Schema name for SchemaAutoAddNewEntities

      • currentUser: UserInfo

        Current user for entity operations (unused but kept for signature compatibility)

      Returns Promise<string | null>

      The application ID if successful, null otherwise

    • Parameters

      • newEntityUUID: string
      • newEntityName: string
      • newEntity: any
      • newEntitySuffix: string
      • newEntityDisplayName: string | null

      Returns string

    • Applies LLM-assisted field decoration to a single virtual entity. Parses the view SQL to identify source entities, enriches the LLM prompt with their field metadata (descriptions, categories), then applies PKs, FKs, descriptions, and categories.

      Parameters

      • pool: CodeGenConnection
      • entity: EntityInfo
      • ag: AdvancedGeneration
      • currentUser: UserInfo
      • availableEntities: { BaseTable: string; Name: string; PrimaryKeyField: string; SchemaName: string }[]

      Returns Promise<{ decorated: boolean; skipped: boolean }>

      Whether the entity was decorated, skipped, or encountered an error.

    • Iterates over all virtual entities and applies LLM-assisted field decoration to identify primary keys, foreign keys, and field descriptions. Only runs if the VirtualEntityFieldDecoration advanced generation feature is enabled. Idempotent: skips entities that already have soft PK/FK annotations.

      Parameters

      Returns Promise<void>

    • Derives an entity name from a view name by removing common prefixes (vw, v_) and converting to a human-friendly format.

      Parameters

      • viewName: string

      Returns string

    • Drops and recreates the default constraint for a special date field. This method is called as part of the ensureSpecialDateFieldExistsAndHasCorrectDefaultValue method and is not intended to be called directly.

      Parameters

      Returns Promise<void>

    • Generates a conditional existence check + DROP statement. Delegates to the database provider.

      Parameters

      • objectType: "VIEW" | "PROCEDURE" | "FUNCTION"
      • schema: string
      • name: string

      Returns string

    • This method ensures that the __mj_CreatedAt and __mj_UpdatedAt fields exist in each entity that has TrackRecordChanges set to true. If the fields do not exist, they are created. If the fields exist but have incorrect default values, the default values are updated. The default value that is to be used for these special fields is GETUTCDATE() which is the UTC date and time. This method is called as part of the manageEntityFields method and is not intended to be called directly.

      Parameters

      Returns Promise<boolean>

    • This method handles the validation of the existence of the specified special date field and if it does exist it makes sure the default value is set correctly, if it doesn't exist it makes sure that it is created. This method is called as part of the ensureCreatedAtUpdatedAtFieldsExist method and is not intended to be called directly.

      Parameters

      • pool: CodeGenConnection
      • entity: any
      • fieldName: string
      • currentFieldData: any
      • allowNull: boolean

      Returns Promise<boolean>

    • Returns a parenthesized subquery that resolves an entity's ID by its BaseTable + SchemaName. Used in logged SQL to keep INSERT statements portable across databases (avoids hardcoding runtime UUIDs that may differ on a fresh database).

      Parameters

      • tableName: string
      • schemaName: string

      Returns string

    • Whether an introspected remote object has columns we can safely sync. When false (object missing or zero columns), manageSingleExternalEntity skips the field sync rather than treating it as "the entity now has no fields" (which would DELETE every EntityField). Pure/testable.

      Parameters

      • obj: { Columns?: unknown[] } | null | undefined

      Returns boolean

    • Extracts the top-level "Entities" array from the additionalSchemaInfo config file. Each entry identifies an entity by BaseTable + SchemaName and declares arbitrary Entity-table attributes to set (e.g., AllowMultipleSubtypes, TrackRecordChanges).

      Parameters

      • config: Record<string, unknown>

      Returns EntityConfig[]

    • Extracts organic key configurations from the additionalSchemaInfo config file. Walks all schema-keyed table arrays and collects OrganicKeys arrays with their owning schema and table name.

      Parameters

      • config: Record<string, unknown>

      Returns { OrganicKeys: OrganicKeyConfig[]; SchemaName: string; TableName: string }[]

    • Extracts a flat array of table configs from the config file, handling both formats:

      1. Schema-as-key (template format): { "dbo": [{ "TableName": "Orders", ... }] }
      2. Flat tables array (legacy format): { "Tables": [{ "SchemaName": "dbo", "TableName": "Orders", ... }] } Returns a normalized array where each entry has SchemaName, TableName, PrimaryKey[], and ForeignKeys[].

      Parameters

      • config: Record<string, unknown>

      Returns SoftPKFKTableConfig[]

    • For an inherited field, walks the parent chain to find which specific parent entity originally defines this field (by matching non-virtual fields on each parent).

      Parameters

      • fieldName: string
      • parentChain: { entityID: string; entityName: string }[]
      • allEntities: EntityInfo[]

      Returns { entityID: string; entityName: string } | null

    • Generates a TypeScript field validator function from the text of a SQL CHECK constraint.

      Parameters

      • data: any

        the data object containing the entity name, column name, and constraint definition

      • allEntityFields: any[]

        all of the entity fields in the system

      • currentUser: UserInfo

        the current user

      • generateNewCode: boolean

        a flag indicating whether or not to generate new code, this is set to false when we are just loading the generated code from the database.

      Returns Promise<ValidatorResult>

      a data structure with the function text, function name, function description, and a success flag

    • Resolves entity name prefix/suffix rules for a given schema. The resolution order is:

      1. Config file (mj.config.cjs NameRulesBySchema) - highest priority
      2. SchemaInfo database metadata (EntityNamePrefix/EntityNameSuffix columns) - fallback

      If both sources define rules for the same schema, the config file wins and a console warning is emitted to alert the user of the override.

      Parameters

      • schemaName: string

      Returns
          | {
              EntityNamePrefix: string;
              EntityNameSuffix: string;
              SchemaName: string;
          }
          | undefined

    • This method builds a SQL Statement that will insert a row into the EntityField table with information about a new field.

      Parameters

      • newEntityFieldUUID: string
      • n: any

        the new field

      Returns string

    • Creates a SQL statement to retrieve all of the pending entity fields that need to be created in the metadata. This method looks for fields that exist in the underlying database but are NOT in the metadata.

      IMPORTANT: The sequence calculation uses a dynamic offset based on the maximum existing sequence for each entity, plus 100,000, plus the column sequence. This ensures no collision with existing sequences while maintaining deterministic ordering. The spUpdateExistingEntityFieldsFromSchema stored procedure runs AFTER this method and will correct the sequences to ensure they are in the correct sequential order starting from 1. In a migration, the spUpdateExistingEntityFieldsFromSchema runs afterwards as well so this behavior ensures CodeGen works consistently.

      Parameters

      • OptionalentityIDs: string[]

      Returns string

      • The SQL statement to retrieve pending entity fields.
    • Groups FK fields by their entity pair key (ParentEntityID|ChildEntityID). This ensures all FK fields for the same entity pair are processed together, preventing the ping-pong bug where multi-FK pairs swap JoinFields on each run.

      Parameters

      • entityFields: Record<string, unknown>[]

      Returns Map<string, Record<string, unknown>[]>

    • Checks if a table has a soft primary key defined in the additionalSchemaInfo JSON file (configured in mj.config.cjs)

      Parameters

      • schemaName: string
      • tableName: string

      Returns boolean

    • Returns an IIF/CASE expression. SQL Server: IIF(cond, t, f), PostgreSQL: CASE WHEN cond THEN t ELSE f END

      Parameters

      • condition: string
      • trueVal: string
      • falseVal: string

      Returns string

    • Returns true if the field is a sensible target for IsNameField (i.e. could serve as the entity's human-readable display name). A name field must be BOUNDED TEXT ON THE BASE TABLE — so we reject primary keys, uniqueidentifiers, all non-text types, unbounded (MAX) text, and VIRTUAL fields. This stops Smart Field Identification from flagging a PK/uniqueidentifier as a name field, which would corrupt every related-entity name virtual field that joins to this entity (those resolve their SQL type from the related entity's NameField). Virtual fields are rejected because a view-only name column forces the FK-name join to target the VIEW instead of the base table — the self-referencing case is unbuildable on PostgreSQL and SQL Server (see the self-FK skip in the view generator), and a name that is itself a borrowed FK-name resolves circularly.

      Parameters

      • field: Record<string, unknown>

      Returns boolean

    • Returns true if the field is a sensible target for LIKE-based user search. Mirrors the runtime guard in GenericDatabaseProvider.isTextSearchableType / the Phase 1 hygiene migration so CodeGen stops re-introducing invalid flags.

      Parameters

      • field: Record<string, unknown>
      • ftxEnabled: boolean

      Returns boolean

    • Heuristic: does the entity name match the shape of an audit / log / run-history / change-tracking table? These are the entities that drive the most LIKE-scan time and almost never belong in global search.

      Parameters

      • name: string | undefined
      • _schemaName: string | undefined

      Returns boolean

    • Loads existing SchemaInfo records from the database into the cache so that entity name prefix/suffix rules are available before createNewEntities() runs. This is a read-only SELECT — it does NOT create or update any records.

      Parameters

      Returns Promise<boolean>

    • Manages the creation, updating and deletion of entity field records in the metadata based on the database schema.

      Parameters

      • pool: CodeGenConnection
      • excludeSchemas: string[]
      • skipCreatedAtUpdatedAtDeletedAtFieldValidation: boolean
      • skipEntityFieldValues: boolean
      • currentUser: UserInfo
      • skipAdvancedGeneration: boolean
      • skipDeleteUnneededFields: boolean = false
      • OptionalentityFilter: string[]

        Optional list of entity NAMES to scope the field-management work to. When provided, the three SP/inline-SQL passes (delete unneeded, create new from schema, update existing from schema) filter to those entities only. Other steps remain unscoped — they are cheap enough that scoping them adds complexity without measurable benefit. An empty array short-circuits the entire method as a no-op, which is the typical "no schema changes since last run" case in Pass 2. undefined (default) preserves prior full-scan behavior.

      Returns Promise<boolean>

    • This method creates and updates relationships in the metadata based on foreign key relationships in the database.

      Parameters

      • pool: CodeGenConnection
      • excludeSchemas: string[]

        specify any schemas to exclude here and any relationships to/from the specified schemas will be ignored

      • md: Metadata
      • batchItems: number = 5

      Returns Promise<boolean>

    • Baseline foreign-key consumption for an external entity: for each introspected single-column relationship whose referenced remote object is ALSO an imported external entity in the same data source, set the FK field's RelatedEntityID + RelatedEntityFieldName + IsSoftForeignKey=1. The standard manageEntityRelationships pass then materializes these into EntityRelationship records. Composite FKs and references to objects that aren't imported are skipped (logged) — those are the follow-up hardening. Returns true if any FK field was updated.

      Parameters

      Returns Promise<boolean>

    • Manages M->M relationships between entities in the metadata based on foreign key relationships in the database. NOT IMPLEMENTED IN CURRENT VERSION IN BASE CLASS. M->M relationships ARE supported fully, but they are not AUTO generated by this method, instead an administrator must manually create these relationships in the metadata.

      Parameters

      Returns Promise<boolean>

    • Manages 1->M relationships between entities in the metadata based on foreign key relationships in the database.

      Parameters

      • pool: CodeGenConnection
      • excludeSchemas: string[]

        specify any schemas to exclude here and any relationships to/from the specified schemas will be ignored

      • md: Metadata
      • batchItems: number = 5

      Returns Promise<boolean>

    • Manages virtual EntityField records for IS-A parent entity fields. For each entity with ParentID set (IS-A child), creates/updates virtual field records that mirror the parent entity's base table fields (excluding PKs, timestamps, and virtual fields). Runs collision detection to prevent child table columns from shadowing parent fields.

      Parameters

      Returns Promise<{ anyUpdates: boolean; success: boolean }>

    • Parameters

      • pool: CodeGenConnection
      • virtualEntity: any
      • veField: any
      • fieldSequence: number
      • makePrimaryKey: boolean
      • singleColumnPrimaryKey: boolean = true
      • reconcilePrimaryKey: boolean = false

      Returns Promise<{ newFieldID: string | null; success: boolean; updatedField: boolean }>

    • Applies entity name prefix/suffix rules to a given entity name. Rules are resolved from mj.config.cjs first, then from SchemaInfo database metadata as a fallback.

      Parameters

      • schemaName: string

        the database schema name

      • entityName: string

        the base entity name to apply prefix/suffix to

      Returns string

    • Returns a sequence value safe to use for an EntityField INSERT under the given EntityID, defaulting to candidate when it doesn't collide. Otherwise returns MAX(Sequence) + 1 for that entity.

      Why this exists: several insert paths (virtual-entity field sync, IS-A parent field sync, schema-derived field creation) compute a deterministic sequence up-front, but a partial prior run can leave rows at that exact ordinal under the target EntityID. SS hides the collision because retried runs typically succeed before failing; PG raises UQ_EntityField_EntityID_Sequence (~4 errors per advanced-generation run). The values are temporary anyway — spUpdateExistingEntityFieldsFromSchema renumbers them on the next pass.

      Parameters

      Returns Promise<number>

    • Parameters

      • constraintDefinition: string
      • fieldName: string
      • entityName: string

      Returns string[] | null

    • This method takes the stored DEFAULT CONSTRAINT value from the database and parses it to retrieve the actual default value. This is necessary because the default value is sometimes wrapped in parentheses and sometimes wrapped in single quotes. This method removes the wrapping characters and returns the actual default value. Some common raw values that exist in SQL Server include 'getdate()', '(getdate())', 'N''SomeValue''', etc. and this method will remove those wrapping characters to get the actual underlying default value. NOTE: For future versions of MemberJunction where multiple back-end providers could be used, this method will be moved to the Provider architecture so that database-specific versions can be implemented, along with many other aspects of this current codebase.

      Parameters

      • sqlDefaultValue: string

      Returns string

    • Whether a field's PK/Unique flags need to change. In RECONCILE mode (external entities, whose introspected PK set is authoritative across ALL fields) we sync in BOTH directions — setting the flags when a column becomes a PK AND clearing them when it stops being one (H5: otherwise a stale PK column keeps IsPrimaryKey=1 when the remote PK moves, yielding duplicate PKs). In NON-reconcile mode (virtual entities, where makePrimaryKey is a one-time first-column bootstrap, not authoritative) we only SET on acquisition and never clear — avoiding wiping a legitimately-configured PK, and avoiding a spurious UPDATE (+ __mj_UpdatedAt bump) every run when nothing changed. Pure function; unit-tested.

      Parameters

      • current: { isPrimaryKey: boolean; isUnique: boolean }
      • want: { wantPrimaryKey: boolean; wantUnique: boolean }
      • makePrimaryKey: boolean
      • reconcile: boolean

      Returns boolean

    • Process entities in batches with parallel execution. Batch size is configurable via advancedGeneration.batchSize in mj.config.cjs (default: 5).

      Parameters

      Returns Promise<boolean>

    • Process advanced generation for a single entity

      Parameters

      • pool: CodeGenConnection

        Database connection pool

      • entity: EntityInfo

        Entity to process

      • fieldsByEntity: Map<string, any[]>

        Fields grouped by normalized EntityID (built once by the batch driver)

      • ag: AdvancedGeneration

        AdvancedGeneration instance

      • currentUser: UserInfo

        User context

      Returns Promise<void>

    • Processes Entity attribute configurations from the additionalSchemaInfo config. For each entry in the top-level "Entities" array, looks up the entity by BaseTable + SchemaName and applies any declared attribute updates to the Entity table. Reserved keys (BaseTable, SchemaName) are excluded from the UPDATE statement. Must run AFTER entities are created.

      Parameters

      Returns Promise<{ success: boolean; updatedCount: number }>

    • Processes IS-A relationship configurations from the additionalSchemaInfo config. For each configured relationship, looks up both entities by name (or by table name within the given schema) and sets Entity.ParentID on the child entity. Must run AFTER entities are created but BEFORE manageParentEntityFields().

      Parameters

      Returns Promise<{ success: boolean; updatedCount: number }>

    • Processes organic key configurations from additionalSchemaInfo. For each configured organic key:

      1. Creates transitive bridge views if TransitiveView is defined
      2. Upserts EntityOrganicKey records (matched on EntityID + Name)
      3. Upserts EntityOrganicKeyRelatedEntity records (matched on EntityOrganicKeyID + RelatedEntityID)

      All SQL is executed AND logged via LogSQLAndExecute for complete CI/CD traceability. Must run AFTER entities are created.

      Parameters

      Returns Promise<{ createdCount: number; success: boolean; updatedCount: number }>

    • Processes virtual entity configurations from the additionalSchemaInfo config. For each configured virtual entity, checks if it already exists and creates it if not. Uses the spCreateVirtualEntity stored procedure. Must run BEFORE manageVirtualEntities() so newly created entities get field-synced.

      Parameters

      Returns Promise<{ createdCount: number; success: boolean }>

    • Produces a schema-qualified object reference. SQL Server: [schema].[object], PostgreSQL: schema."object"

      Parameters

      • schema: string
      • object: string

      Returns string

    • Quotes mixed-case identifiers in a SQL string for the current platform. Delegates to the database provider's quoteSQLForExecution method.

      Parameters

      • sql: string

      Returns string

    • Removes stale One-To-Many EntityRelationship records whose RelatedEntityJoinField no longer corresponds to a valid FK field in the database. Only removes relationships where AutoUpdateFromSchema = true and Type = 'One To Many'.

      Parameters

      • pool: CodeGenConnection
      • allRelationships: Record<string, unknown>[]
      • entityFields: Record<string, unknown>[]

      Returns Promise<void>

    • Resolves the AllowCaching default for a new entity in the given schema. AllowCachingBySchema entries override the global AllowCaching default. The ${mj_core_schema} placeholder is expanded so core-schema rules apply regardless of how the core schema is named in this deployment.

      Parameters

      • schemaName: string

      Returns boolean

    • Resolves a list of entity names to their UUIDs via the live Metadata cache. Names that don't resolve are dropped silently — they may be entities that have been queued for creation but aren't yet in the metadata.

      Two safety properties:

      1. new Metadata() is constructed OUTSIDE the per-name try/catch so a metadata-load failure (e.g., refresh failed upstream) surfaces as an error instead of being silently treated as "no entities resolved" — which would misleadingly degrade Pass 2 to an unscoped full scan.
      2. The try/catch is narrowly scoped to EntityByName because that method throws on null/empty names (_newEntityList and _modifiedEntityList can pick those up from result-set rows whose EntityName column is null). The pre-filter on string type + trim covers the common case; the catch is a safety net for any other lookup failure (e.g., name shape we didn't anticipate).

      Parameters

      • entityNames: string[]

      Returns string[]

    • Desired PK/Unique flags for a virtual/external entity field. IsPrimaryKey mirrors makePrimaryKey; IsUnique is true ONLY for a SINGLE-column PK — a column of a COMPOSITE key is not unique on its own, so composite-key columns get IsUnique=false (M4). Pure function so the rule is unit-tested.

      Parameters

      • makePrimaryKey: boolean
      • singleColumnPrimaryKey: boolean

      Returns { wantPrimaryKey: boolean; wantUnique: boolean }

    • Sanitizes an LLM-supplied codeType against the CK_EntityField_CodeType CHECK constraint. Uses MJEntityFieldSchema.shape.CodeType from @memberjunction/core-entities as the single source of truth — no hardcoded enum duplication. Values that fail Zod validation (e.g. 'Python', 'Markdown', 'javascript' wrong case) coerce to 'Other', preserving null/undefined. Logs any coercion so the underlying prompt drift is visible instead of silently failing the batch UPDATE at the DB.

      Parameters

      • codeType: string | null | undefined
      • fieldName: string
      • entityName: string

      Returns string | null | undefined

    • Picks the single IsNameField winner for an entity per the rules documented on applyNameFieldUpdates: stable current winner → deterministic repair (literal Name, then field order) → first eligible LLM candidate. Returns null when nothing eligible exists (the entity simply has no name field — FK-name virtual columns are skipped for it, which beats pointing them at a PK or blob).

      Parameters

      • fields: Record<string, unknown>[]
      • rankedCandidates: string[]

      Returns Record<string, unknown> | null

    • Wraps a SELECT query with a row limit. SQL Server: SELECT TOP N ... , PostgreSQL: SELECT ... LIMIT N

      Parameters

      • n: number
      • selectBody: string
      • fromAndWhere: string
      • OptionalorderBy: string

      Returns string

    • This method updates the DefaultColumnWidth field in the EntityField metadata. The default logic uses a stored procedure called spSetDefaultColumnWidthWhereNeeded which is part of the MJ Core Schema. You can override this method to implement custom logic for setting default column widths. It is NOT recommended to modify the stored procedure in the MJ Core Schema because your changes will be overriden during a future upgrade.

      Parameters

      Returns Promise<boolean>

    • This method is responsible for generating a Display Name for each field where a display name is not already set. The approach in the base class uses a simple algorithm that looks for case changes in the field name and inserts spaces at those points. It also strips the trailing 'ID' from the field name if it exists. Override this method in a sub-class if you would like to implement a different approach for generating display names.

      Parameters

      Returns Promise<boolean>

    • This method handles updating entity field related name field maps which is basically the process of finding the related entity field that is the "name" field for the related entity.

      Parameters

      Returns Promise<boolean>

    • Syncs SchemaInfo records from database schemas, capturing extended properties as descriptions. Creates new SchemaInfo records for schemas that don't exist yet and updates descriptions from schema extended properties for existing records.

      Parameters

      • pool: CodeGenConnection

        SQL connection pool

      • excludeSchemas: string[]

        Array of schema names to exclude from processing

      Returns Promise<boolean>

      Promise - true if successful, false otherwise

    • FORWARD VALIDATION — verifies that every DECLARED IS-A relationship (any Entity with a non-null ParentID) actually satisfies what the IS-A runtime requires. Never mutates metadata.

      Channel-agnostic BY DESIGN: it validates the END STATE of Entity.ParentID, so it covers the additionalSchemaInfo "ISARelationships" config, an @lookup on ParentID in a metadata-sync file, and any future channel — one check instead of one per declaration mechanism.

      Reports ONLY provable-cannot-work defects (hard errors) — never inference. If a declared IS-A merely "looks off" but would still function, it passes silently: flagging it would misfire on correct declarations (see the note in the loop). The hard errors: - Child has a composite PK. The runtime routes ONE shared PK value between child and parent; it has no model for a multi-column subtype key. - Parent has a composite PK. Same reason, from the other side. - Child PK type <> parent PK type. Parent and child SHARE one PK value (Save() writes the child's PK into the parent's PK via ParentEntityFieldNames; loads match the child by the parent's PK value), so the value must be legal as BOTH PKs. Note a physical FK already guarantees matching types — this only ever fires on a soft/declared IS-A, which is exactly the case with no DB constraint to catch it. - ParentID does not resolve to an existing entity. DEFENSE-IN-DEPTH ONLY: the FK_Entity_ParentID constraint (Entity.ParentID -> Entity.ID) makes this state unstorable and vwEntities has no WHERE clause, so it is not reachable in a healthy database. It is kept because the parent JOIN must be a LEFT JOIN regardless, and without this branch an unresolved parent would silently SKIP the remaining checks (ParentPKType would be NULL) rather than fail. Covered by unit test, not live.

      Timing: runs AFTER every ParentID-writing pass (config + any previously-synced @lookup) and BEFORE the 2nd-pass manageParentEntityFields() materializes IS-A virtual fields + view JOINs, so a broken declaration fails before it produces generated code.

      Parameters

      Returns Promise<{ errorCount: number; success: boolean }>

    • Adds a list of entity names to the modified entity list if they're not already in there

      Parameters

      • entityNames: string[]

      Returns void

    • Drops the cached soft PK/FK config so the next getSoftPKFKConfig call re-reads the additionalSchemaInfo file from disk. REQUIRED for the long-lived in-process (RSU) CodeGen path: the cache is process-static (load-once), which is correct for a one-shot mj codegen CLI run but STALE in a long-running MJAPI where RSU rewrites additionalSchemaInfo on every ApplyAll. Without invalidation, a connector's FIRST ApplyAll writes its soft PKs to the file but CodeGen returns the pre-write cached config → "No primary key found" → the entity is never created → no entity map → 0 rows sync until an MJAPI restart. RunInProcess calls this at the start of every run (which always follows RSU's WriteAdditionalSchemaInfo step), so each in-process run sees the freshly-written file. Deterministic + event-driven (no mtime/TOCTOU race). The CLI Run() path does not call this, so its load-once-per-process behavior is unchanged.

      Returns void