Class Model<TData, TVirtual>

Base Model class

Type Parameters

Indexable

  • [key: string]: unknown

Constructors

Properties

_changed: Set<string>
_data: Record<string, unknown>
_isNew: boolean
_originalData: Record<string, unknown>
_virtualFields: Partial<TVirtual> & JsonObject
_fieldMappings: Map<string, string> = ...
_relations: Map<string, NormalizedRelationConfig> = ...
_revisionHandlers?: Partial<
    Record<
        RevisionHandlerName,
        (this: Model, ...args: unknown[]) => unknown,
    >,
>
_views: Map<
    string,
    ModelViewDefinition<ModelInstance<JsonObject, JsonObject>, JsonObject>,
> = ...
options: JsonObject
tableName: string

Accessors

Methods

  • Parameters

    • key: string
    • value: unknown

    Returns void

  • Get property value (data or virtual)

    Parameters

    • key: any

      Property name (camelCase or snake_case)

    Returns unknown

    Property value

  • Persist this instance and synchronize any many-to-many relations.

    ⚠️ Only through relations whose join tables contain just foreign keys are managed automatically. If the join table carries additional editable columns (role flags, metadata, timestamps that should be user-controlled, etc.), mutate that table directly instead of relying on saveAll, because the DAL currently treats relation membership as an ID-only diff.

    Parameters

    • joinOptions: {} = {}

      Optional map of relation names to include. When omitted, every declared through relation is synchronized.

    Returns Promise<Model<TData, TVirtual>>

    This instance

  • Set property value

    Parameters

    • key: any

      Property name (camelCase or snake_case)

    • value: any

      Property value

    Returns void

  • Get the database field name for a given property name

    Parameters

    • propertyName: string

      Property name (camelCase or snake_case)

    Returns string

    Database field name (snake_case)

  • Register a camelCase to snake_case field mapping

    Parameters

    • camelCase: string

      camelCase property name

    • snakeCase: string

      snake_case database column name

    Returns void

  • Batch-insert associations into a many-to-many junction table.

    This helper creates associations between a single source record and multiple target records by inserting rows into a junction table. It automatically handles:

    • Schema namespace prefixing for test isolation
    • Duplicate associations via ON CONFLICT DO NOTHING
    • Input validation with helpful error messages

    Type Parameters

    Parameters

    • this: ModelConstructor<TData, TVirtual, ModelInstance<TData, TVirtual>, string> & typeof Model & {
          _fieldMappings: Map<string, string>;
          _relations: Map<string, NormalizedRelationConfig>;
          _revisionHandlers?: Partial<
              Record<
                  RevisionHandlerName,
                  (this: Model, ...args: unknown[]) => unknown,
              >,
          >;
          _views: Map<
              string,
              ModelViewDefinition<ModelInstance<JsonObject, JsonObject>, JsonObject>,
          >;
          dal: DataAccessLayer;
          options?: JsonObject;
          schema: ModelSchema<TData, TVirtual>;
          tableName: string;
      }
    • relationName: string

      Name of the relation defined in the manifest (must have a through junction table)

    • sourceId: string

      ID of the source record to associate from

    • targetIds: string[]

      Array of target record IDs to associate with

    • Optionaloptions: { onConflict?: "error" | "ignore" }

      Optional configuration

      • OptionalonConflict?: "error" | "ignore"

        How to handle duplicate associations: 'ignore' (default) or 'error'

    Returns Promise<void>

    If relation not found, has no junction table, or inputs are invalid

    // Associate a thing with multiple files
    await Thing.addManyRelated('files', thingId, [file1.id, file2.id]);

    // Associate a review with teams
    await Review.addManyRelated('teams', reviewId, teamIds, { onConflict: 'error' });
  • Register relation metadata for the model

    Parameters

    • name: string

      Relation name

    • config: JsonObject

      Relation configuration

    Returns void

  • Get list of database column names including specified sensitive fields

    Parameters

    • includeSensitive: string[] = []

      Array of sensitive field names to include

    Returns string[]

    Array of database column names

  • Get list of non-sensitive database column names for safe querying Excludes fields marked as sensitive and virtual fields

    Returns string[]

    Array of database column names

  • Get list of sensitive field names in the schema

    Returns string[]

    Array of field names (camelCase)