Skip to content

TypesReference

Wyatt Greenway edited this page Nov 4, 2022 · 1 revision

This article is intended as a quick overview of the database types that Mythix ORM provides. See the documentation for any given type for more information.

All database types that Mythix ORM provides can be accessed via the Types import from the mythix-orm module. For example,

const { Types } = require('mythix-orm');

let stringType = Types.STRING(128);
...

A type can be instantiated either by its wrapper, or directly via its type class. For example, both of the following ways to construct types are valid, and equivalent:

const { Types } = require('mythix-orm');

let stringType1 = Types.STRING(128);
let stringType2 = new Types.StringType(128);
...

Most types don't require that you "call" the wrapper, though some do require arguments, and so require that you provide them when creating the type (i.e. FOREIGN_KEY type requires arguments). Most types provided by Mythix ORM have "sane" defaults for options that can be provided to them.

const { Types } = require('mythix-orm');

// This...
let stringType1 = Types.STRING;

// ... is the same as this...
let stringType2 = Types.STRING(256);

There are two kinds of database types in Mythix ORM: "concrete" types, and "virtual" types. Concrete types are types that are backed by storage (a database), these include types like Types.STRING, Types.INTEGER, etc... Virtual types are types like Types.Model and Types.Models. These don't have any storage backing them (at least not directly), and instead define relationships between data.

A keen observer will notice the naming convention employed here. Concrete types always have ALL_UPPER_CASE_NAMES, whereas virtual types have CamelCase names. This makes it easy to look at any model's fields, and immediately know what fields are stored in the database, and what fields aren't (directly) stored.


Concrete Types

A 64+ bit integer type. In most SQL-type databases this would be a BIGINT type.

A "blob" type, for storing random blobs of raw data (i.e. such as images). In most SQL-type databases this would be a BLOB or BYTEA type.

A boolean type for storing true and false values. In most SQL-type databases this would be a BOOLEAN type.

A character type for storing a single character. In most SQL-type databases this would be a CHAR type.

A date type for storing just a date (without a time). In most SQL-type databases this would be a BIGINT type.

Note: Mythix ORM will always attempt to store all date and time types as timestamps, generally backed by a BIGINT database type.

A date type for storing a date as well as a time. In most SQL-type databases this would be a BIGINT type. Timestamps are stored with millisecond resolution in most databases.

Note: Mythix ORM will always attempt to store all date and time types as timestamps, generally backed by a BIGINT database type.

A foreign key type. This type will mimic the concrete type of its target, and also simultaneously defines a relationship with its target.

An integer type that can specify integer storage in multiple different byte sizes. For example, this is used to specify TINYINT, SMALLINT, and MEDIUMINT in MySQL. In most SQL-type databases this would be a INT or INTEGER type.

A high-resolution floating-point type, often used for things like currency. In most SQL-type databases this would be a NUMERIC or DECIMAL type.

A low-resolution floating-point type. In most SQL-type databases this would be a FLOAT or DOUBLE type.

A type that will serialize and deserialize data, by default in JSON format. Its underlying concrete type must be specified, and would generally be a Types.STRING or Types.TEXT concrete type (a string-based storage type is not required). Though this defaults to serializing and deserializing JSON, it can be configured to serialize and deserialize using any algorithm desired.

A variable length string type, generally stored in the table-space of the underlying database. In most SQL-type databases this would be a VARCHAR type.

A variable length string type, generally stored outside the table-space of the underlying database (making it slightly slower than Types.STRING). This string type generally has a much larger capacity than Types.STRING. In most SQL-type databases this would be a TEXT type.

A UUID version 1 type, backed by the concrete type Types.STRING. It will calculate its own length based on the length of a UUID version 1, and of any prefix given to it. In most SQL-type databases this would be a VARCHAR type.

Note: This type uses the uuid Node module to validate and generate UUIDs.

A UUID version 3 type, backed by the concrete type Types.STRING. It will calculate its own length based on the length of a UUID version 3, and of any prefix given to it. In most SQL-type databases this would be a VARCHAR type.

Note: This type uses the uuid Node module to validate and generate UUIDs.

A UUID version 4 type, backed by the concrete type Types.STRING. It will calculate its own length based on the length of a UUID version 4, and of any prefix given to it. In most SQL-type databases this would be a VARCHAR type.

Note: This type uses the uuid Node module to validate and generate UUIDs.

A UUID version 5 type, backed by the concrete type Types.STRING. It will calculate its own length based on the length of a UUID version 5, and of any prefix given to it. In most SQL-type databases this would be a VARCHAR type.

Note: This type uses the uuid Node module to validate and generate UUIDs.

An XID type, backed by the concrete type Types.STRING. It will calculate its own length based on the length of an XID, and of any prefix given to it. In most SQL-type databases this would be a VARCHAR type.

Note: This type uses the xid-js Node module to validate and generate XIDs.

Virtual Types

A virtual type for defining a 1-to-1 relationship with another model. The relationship can define one or more "through tables".

Note: Notice its singular name

A virtual type for defining a 1-to-many, or many-to-many relationship with another model(s). The relationship can define one or more "through tables".

Note: Notice its plural name

Clone this wiki locally