Catalog of stored queries. This is useful for any arbitrary query that is known to be performant and correct and can be reused. Queries can be viewed/run by a user, used programatically via RunQuery, and also used by AI systems for improved reliability instead of dynamically generated SQL. Queries can also improve security since they store the SQL instead of using dynamic SQL.

Hierarchy (view full)

Implements

Constructors

Properties

AuditQueryRuns: boolean = false

When true, all executions of this query will be logged to the Audit Log system for tracking and compliance

CacheEnabled: boolean = false

When true, query results will be cached in memory with TTL expiration

CacheMaxSize: number = null

Maximum number of cached result sets for this query. NULL uses default size limit.

CacheTTLMinutes: number = null

Time-to-live in minutes for cached query results. NULL uses default TTL.

CacheValidationSQL: string = null

SQL query that returns cache validation fingerprint data (MaxUpdatedAt, RowCount). Used for smart cache refresh to determine if cached data is stale without fetching full results. When provided, enables efficient server-side cache validation before sending full query results.

The SQL should return exactly one row with two columns:

  • MaxUpdatedAt: DATETIME - The maximum __mj_UpdatedAt timestamp from the underlying data
  • RowCount: INT - The total number of rows that would be returned

Example for a simple entity query:

SELECT MAX(__mj_UpdatedAt) AS MaxUpdatedAt, COUNT(*) AS RowCount FROM [schema].[EntityView]

For complex queries with filters/joins, the validation SQL should mirror the main query's data scope but only return the fingerprint metrics.

Category: string = null

Category name from the related Query Categories entity

CategoryID: string = null

Foreign key reference to the Query Categories entity

Description: string = null

Detailed description of what the query does and what data it returns

EmbeddingModel: string = null

The AI Model name used to generate the embedding vector

EmbeddingModelID: string = null

The AI Model ID used to generate the embedding vector for this query. Required for vector similarity comparisons.

EmbeddingVector: string = null

Optional JSON-serialized embedding vector for the query, used for similarity search and query analysis

Feedback: string = null

User feedback on query accuracy, performance, or suggested improvements

ID: string = null

Unique identifier for the query record

Name: string = null

Name of the query for display and reference

OriginalSQL: string = null

The original SQL before any optimization or modification, kept for reference and comparison

QualityRank: number = null

Value indicating the quality of the query, higher values mean better quality

SQL: string = null

The actual SQL query text to execute, may include Nunjucks template parameters

Status: "Pending" | "In-Review" | "Approved" | "Rejected" | "Obsolete" = null

Current status of the query in the approval workflow

UsesTemplate: boolean = false

Automatically set to true when the SQL column contains Nunjucks template markers like {{paramName}}

__mj_CreatedAt: Date = null

Date and time when this query record was created

__mj_UpdatedAt: Date = null

Date and time when this query record was last updated

_cacheConfig: QueryCacheConfig = null
_categoryPath: string = null
_entities: QueryEntityInfo[] = null
_fields: QueryFieldInfo[] = null
_parameters: QueryParameterInfo[] = null

Accessors

  • get CacheConfig(): QueryCacheConfig
  • Gets the cache configuration for this query. If the query has no explicit cache config but category inheritance is enabled, walks up the category tree to find inherited cache settings.

    Returns QueryCacheConfig

    The cache configuration or null if caching is disabled

  • get CategoryPath(): string
  • Gets the full hierarchical path of this query's category (e.g., "/MJ/AI/Agents/"). This provides a unique filepath-like identifier for the category hierarchy. Returns empty string if the query is not categorized.

    Returns string

    The category path with leading and trailing slashes

Methods

  • Checks if a user can run this query, considering both permissions and query status. A query can be run if:

    1. The user has permission to run it (via UserHasRunPermissions)
    2. The query status is 'Approved'

    Parameters

    Returns boolean

    true if the user can run the query right now

  • Checks if a user has permission to run this query based on their roles. A user can run a query if:

    1. The query has no permissions defined (open to all)
    2. The user has at least one role that is granted permission

    Parameters

    • user: UserInfo

      The user to check permissions for

    Returns boolean

    true if the user has permission to run the query

  • Builds the hierarchical category path by walking up the parent chain.

    Returns string

    The category path (e.g., "/MJ/AI/Agents/") or empty string if uncategorized

  • Copies initialization data from a plain object to the class instance. Only copies properties that already exist on the class to prevent creating new fields. Special handling for DefaultValue fields to extract actual values from SQL Server syntax.

    Parameters

    • initData: any

      The initialization data object

    Returns void