Skip to content

CountLiteral

Wyatt Greenway edited this page Dec 13, 2022 · 7 revisions

class CountLiteral extends LiteralFieldBase 📜

Define a "count" literal for the underlying database.

Literals are special types in Mythix ORM that are used to define "literal values" for the underlying database.

This literal defines a "count" operation across all matching rows. It is used by Connection.count to get the number of matching rows for a query. When serialized using the QueryGenerator for the connection, it will turn into a database method that is appropriate for the underlying database. For example, with SQL type databases this would turn into COUNT(column), our COUNT(*). This is often used by the projection engine, to project it as a column to be selected. For example SELECT COUNT(column) AS count .... It can be used in other places in the query however, such as ORDER, GROUP BY, and HAVING clauses.

Example:

  • const { Literals } = require('mythix-orm');
    const { SQLiteConnection } = require('mythix-orm-sqlite');
    let literal1 = new Literals.CountLiteral('*', { as: 'count' });
    let literal2 = new SQLiteConnection.CountLiteral.CountLiteral('*', { as: 'count' });

Notes:

  • If no field is provided to CountLiteral then all fields (*) is assumed.
  • It is common to use the { as: 'count' } option to give the count literal an alias (name).

There are two primary ways to access literals in Mythix ORM. The first is to simply import them. The second way literals can be accessed is via the connection class itself. All Mythix ORM connection classes export all literals on the static Literals attribute of the class. So for example, you could access literals like SQLiteConnection.Literals.CountLiteral.

All built-in Mythix ORM literals--except Literal and LiteralBase--accept a field as their first argument. This field can be a fully qualified field name, an actual Field instance, or another literal. The second argument to all literal constructors is an options object, that generally contains connection-specific (and operation-specific) options... however, there are common options that can be supplied, such as as: string; which allows you to define an alias for the defined field, and noProjectionAliases: boolean;, which allows you to disable the column alias entirely.

See also: LiteralFieldBase, LiteralBase

static method CountLiteral::isAggregate(): boolean 📜

Return true, letting the caller know that this is an "aggregating literal".

Return value: boolean

Return true, informing the caller that this literal is used for aggregate operations.


static method CountLiteral::isFieldRequired(): boolean 📜

Return false, informing the engine that a field is not required for this literal type.

Return value: boolean

Return false, informing the caller that this literal does not require a field.


method CountLiteral::toString(
    connection?: Connection,
    options?: object,
)
📜

Convert this literal to a string to be used in a database query.

This method proxies the conversion of this literal to the connection by calling Connection.literalToString. If no connection is provided when this is called, then the literal will be converted to a string representing it for debugging, i.e. 'CountLiteral {}'.

Notes:

Arguments:

  • connection?: Connection

    The connection to use to stringify this literal. If none is provided, then a string representing this object will be returned instead.

  • options?: object

    A connection and operation specific set of options that can be provided. This might for example be { as: 'name' } to provided a field alias, or { isProjection: true } to define that this is being stringified for use as a field in the query projection. Normally the end-user won't care about any literal options, except as, which is commonly used to give your literal an alias.



Clone this wiki locally