Skip to content
Wyatt Greenway edited this page Aug 28, 2022 · 49 revisions

class Model

The base Model class for Mythix ORM. Every Mythix ORM model should inherit from this class. This class provides support for all model operations in Mythix ORM.


Notes:

  • Many model methods have both static and instance methods of the same name that do the same thing. This is because it is common to access Model methods directly on the model as well as an instance. For example, Model is both a static method, and an instance method.
  • An underscore prefix on a method in Mythix ORM implies that this method should not be overloaded unless you know exactly what you are doing. It also implies that there is another method that can and should be overloaded instead.

static property Model::_isMythixModel: boolean = true

This property assists with type checking.


method Model::_getConnection(connection?: Connection)

See static _getConnection.

Arguments:

  • connection?: Connection

    An optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to modelInstance._connection, if that also fails to find a connection, the finally the method will call static _getConnection to get the bound connection, if any is available.

Return value: Connection

Notes:

  • Pay attention that unlike static _getConnection this checks the model's instance for ._connection property. If that is a valid connection, then it will be returned before the bound connection. This is helpful when you have chosen not to bind a connection to your models. This will allow you to provide a connection directly when you create the model, which can make interacting with the model less tiresome. i.e. new Model(null, { connection }).

static method Model::_getConnection(connection?: Connection)

Get the underlying connection bound to the model's class. Connection binding is optional, so this method can also be provided a connection. If a connection is provided, then it will simply be returned. Because Mythix ORM has no globals, this is a design pattern to supply a connection if you have one available, or fallback to a "bound" connection (if one can be found).

Arguments:

  • connection?: Connection

    An optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to the bound connection, if any is available.

Return value: Connection


static method Model::bindConnection(connection?: Connection)

A Connection instance will call this method on a Model class to bind the model to the connection. Overload this to change the behavior of binding connections to models. This method should return a model class. The default behavior is to return this class. However, you might return a different class for example if you generated a new class that inherited from this class.

Arguments:

  • connection?: Connection

    The connection instance to bind to this model class.

Return value: class extends Model


static method Model::defaultScope(query: QueryEngine)

One can apply a "default scope" to any model simply by providing this static method on the model. By default it will simply return the QueryEngine instance (a query) that it was provided.

The way this works is simple. The caller provides the query as the first and only argument. The user, by overloading this method can then easily modify this provided query, changing the "default scope" whenever the model is used for queries. For example, let's say you have a User model, and by default, you only want to query against Users that have their active column set to true. In order to do this, you could easily provide a static defaultScope method on your model class that would modify the base query. See the following example:

Examples:

  • class User extends Model {
      static defaultScope(baseQuery) {
        // `baseQuery` is equal to `User.where`
        // without a default scope.
        return baseQuery.active.EQ(true);
      }
    }

Arguments:

  • query: QueryEngine

    The query that you should add onto.

Return value: QueryEngine


static method Model::getConnection(connection?: Connection)

Get the underlying connection bound to the model's class. Connection binding is optional, so this method can also be provided a connection. If a connection is provided, then it will simply be returned. Because Mythix ORM has no globals, this is a design pattern to supply a connection if you have one available, or fallback to a "bound" connection (if one can be found). This method should be overloaded if you wish to provide your own connection for a model.

Arguments:

  • connection?: Connection

    An optional connection that can be provided. If provided, it will simply be returned. Otherwise, if not provided, or a falsy value, then attempt to fallback to the bound connection, if any is available.

Return value: Connection


static method Model::getForeignKeyFieldsMap(connection?: Connection)

Return the foreign key relationships for the model.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

Return value: Map<string, array<object>>

The format is Map[modelName] = [ { targetFieldName, sourceFieldName }, ... ]


static method Model::getForeignKeysTargetField(connection?: Connection, modelName: string, fieldName: string)

Get a specific foreign key field's relationship information. This will be what is stored in the relationship field map for the specified target field. See static getForeignKeyFieldsMap for more information.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

  • modelName: string

    The model name for which you wish to fetch foreign key fields from.

  • fieldName: string

    The field name for which you wish to fetch foreign key relational information about.

Return value: object<{ targetFieldName, sourceFieldName }>

This will be the relationship info for this foreign key field which will be an object of the shape { targetFieldName, sourceFieldName }.


static method Model::getForeignKeysTargetFieldNames(connection?: Connection, modelName: string)

Return all the foreign key target field names. This will return the target field names of all foreign key fields specified on the model.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

  • modelName: string

    The model name for which you wish to fetch foreign key fields from.

Return value: Array<string>

The format is Array[] = modelName


static method Model::getForeignKeysTargetModelNames(connection?: Connection)

Return all the foreign key target model names. This will be all models targeted by all foreign keys defined by foreign key fields on this model. This will only return the models name's.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

Return value: Array<string>

The format is Array[] = modelName


static method Model::getForeignKeysTargetModels(connection?: Connection)

Return all the foreign key target models. This will be all models targeted by all foreign keys defined by foreign key fields on this model.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

Return value: Map<string, Model>

The format is Map[modelName] = Model


static method Model::getModelName()

Get the model name for this model. By default this will be the name of the class itself, i.e. this.name.

Return value: string

The name the model.


static method Model::getPluralModelName()

Get the plural model name for this model. By default this will be the singular name of the model, converted to plural using the Inflection library.

Return value: string

The name the model in plural form.

Notes:

  • This will return the "plural" name of the model.

See also: static getSingularName


static method Model::getQueryEngine(connection?: Connection, options?: object)

This method is called any time a Model.where or Model.$ property is accessed. It returns a query, based off this model class (as the root model). It will first call static getUnscopedQueryEngine to get the root query for the model, and then it will call static defaultScope to apply any default scope to the root query. Finally, it will return the query to the user to start interacting with.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

  • options?: object

    Any extra options to pass to the QueryEngine constructor.

Return value: QueryEngine


static method Model::getQueryEngineClass(connection?: Connection)

This method is called every time a Model.where or Model.$ property is accessed. It should return a class that inherits from (or is) QueryEngine. By default, it will call this.getConnection().getQueryEngineClass() to get the query class defined in the connection options. Though this can be overloaded per-model (creating a different type of QueryEngine per-model), the QueryEngine class to instantiate to use for queries will generally be supplied by the connection.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

Return value: class QueryEngine


static method Model::getSingularName()

Get the model name for this model. By default this will be the name of the class itself, i.e. this.name.

Return value: string

The name the model in singular form.

Notes:

See also: static getPluralModelName


static method Model::getTableName(connection?: Connection)

Get the table name for this model. By default Mythix ORM will take the model's name, and convert it to snake_case.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

Return value: string

The name of the table for this model.


static method Model::getUnscopedQueryEngine(connection?: Connection, options?: object)

This method is called any time a query.unscoped() call is made. It will return the query's "root class" where with no static defaultScope scope applied. Calling "unscoped()" will reset the query, so make sure you always call it first: Model.where.unscoped()...

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

  • options?: object

    Any extra options to pass to the QueryEngine constructor.

Return value: QueryEngine


static method Model::isForeignKeyTargetModel(connection?: Connection, modelName: string)

Check if the specified model is a model pointed to by a foreign key field.

Arguments:

  • connection?: Connection

    An optional connection to pass through. This is needed if your models are not bound to a connection.

  • modelName: string

    The model name for which you wish to fetch foreign key fields from.

Return value: boolean

true if the specified modelName model is pointed to by one of the foreign key fields, false otherwise.


static method Model::isModel(value: any)

Check to see if the provided value is an instance of a Mythix ORM Model. Unlike static isModelClass, which checks if a class is a Model, this will check to see if an instance is an instance of a Mythix ORM Model. It will return true if the provided value is an instanceof Model, or if the value's constructor property has a truthy _isMythixModel property (value.constructor._isMythixModel).

Arguments:

  • value: any

    Value to check.

Return value: boolean


static method Model::isModelClass(value: Function)

Use this method to check if a class is a Mythix ORM model. It will return true if the provided value is a class that inherits from Model, or if the provided value has an attribute named _isMythixModel that is truthy.

Arguments:

  • value: Function

    Value to check.

Return value: boolean


static method Model::toString(showFields?: boolean)

Like everywhere in Javascript, we can call .toString() to a get a string representation of our Model class. The optional showFields argument, if true, will list the models fields as well. Without the showFields argument, the model name alone will be returned as a string.

Arguments:

  • showFields?: boolean

    If true, then list the models fields.

Return value: string



Clone this wiki locally