prorm API Reference
    Preparing search index...

    Class Model

    Base Model class for decorator-based model definitions

    This class can be extended to create models using TypeScript decorators:

    import { Model, DataTypes } from 'orm';
    import { Table, Attribute, PrimaryKey, AutoIncrement } from 'orm/decorators';

    @Table({ tableName: 'users' })
    export class User extends Model {
    @Attribute(DataTypes.INTEGER)
    @PrimaryKey
    @AutoIncrement
    declare id: number;

    @Attribute(DataTypes.STRING)
    declare name: string;
    }

    // Then initialize with prorm:
    prorm.addModel(User);
    await User.sync();
    Index
    prorm?: Prorm
    modelName?: string
    tableName?: string
    schema?: string
    rawAttributes?: Record<string, any>
    associations?: Record<string, any>
    options?: ModelOptions
    primaryKeyAttribute?: string
    _scopes?: Record<string, any>
    _pendingAssociations?: {
        type: "belongsTo" | "hasOne" | "hasMany" | "belongsToMany";
        target: any;
        options?: any;
    }[]

    Associations declared via User.hasMany(...)/.belongsTo(...)/etc before the model has been registered with prorm.addModel(). Queued here and replayed by Prorm.addModel() once the model (and its prorm/models[name] entry) exists, so association calls can be made either before or after addModel().

    _pendingScopes?: Record<string, any>

    Scopes added via User.addScope(...) before the model has been registered with prorm.addModel(). Merged into the model's options.scopes by Prorm.addModel() so addScope() can be called either before or after addModel().

    • Parameters

      • scopeName: string
      • scopeDef: any

      Returns void

    • Parameters

      • target: any
      • Optionaloptions: any

      Returns any

    • Parameters

      • target: any
      • Optionaloptions: any

      Returns any

    • Parameters

      • target: any
      • Optionaloptions: any

      Returns any

    • Parameters

      • target: any
      • Optionaloptions: any

      Returns any

    • Returns string | undefined

    • Create a database view over this model.

      A view is a saved query, and this is that query written against the model it selects from — so from is implied and can be left out:

      await Customer.createView('customer_info', {
      attributes: ['firstName', 'lastName', 'email'],
      where: { country: 'USA' },
      });

      Pass from anyway to select from somewhere else while keeping the call on this model. Omit the definition entirely for a view of the whole table.

      Parameters

      • viewName: string

        Name of the view to create

      • definition: ModelViewDefinition = {}

        Query options; from defaults to this model

      • Optionaloptions: Record<string, unknown>

        View options, e.g. { replace: true }

      Returns Promise<void>

    • Create a materialized view over this model. As createView, with from defaulting to this model.

      Parameters

      • viewName: string

        Name of the materialized view

      • definition: ModelViewDefinition = {}

        Query options; from defaults to this model

      • Optionaloptions: Record<string, unknown>

        Materialized view options, e.g. { uniqueIndex }

      Returns Promise<void>

    • Drop a view created from this model.

      Parameters

      • viewName: string

        Name of the view to drop

      • Optionaloptions: Record<string, unknown>

        Drop options, e.g. { ifExists: false }

      Returns Promise<void>

    • Parameters

      Returns Promise<ModelInstance<any> | null>

    • Get the model attribute name for a database column (static version)

      Parameters

      • columnName: string

        The database column name

      Returns string

      The model attribute name (or the column name if no mapping exists)

      User.getAttributeName('first_name'); // 'firstName' if field: 'first_name' is set
      User.getAttributeName('id'); // 'id'
    • Parameters

      • Optionalvalues: Record<string, any>
      • Optionaloptions: Record<string, any>

      Returns ModelInstance

    • Parameters

      Returns Promise<[ModelInstance<any>, boolean]>

    • Check if a hook is registered on the model

      Parameters

      • hookName: string

        The name of the hook (e.g., 'beforeCreate', 'afterSave')

      Returns boolean

      True if the hook is registered, false otherwise

    • Check if a hook is registered on the model (alias for hasHook)

      Parameters

      • hookName: string

        The name of the hook (e.g., 'beforeCreate', 'afterSave')

      Returns boolean

      True if the hook is registered, false otherwise

    • Remove a hook from the model

      Parameters

      • hookName: string

        The name of the hook (e.g., 'beforeCreate', 'afterSave')

      • OptionalhookOrHookId: string | HookHandler

        Optional: The specific hook callback function OR hook ID to remove. If not provided, removes all hooks of this type

      Returns typeof Model

      The model for chaining