Home Reference Source

src/util/constants.js

/**
 * The available DataTypes
 * @typedef {Object} DataTypes
 * @property {string} bit Represents a bit data type
 * @property {string} tinyint Represents a tiny integer data type
 * @property {string} smallint Represents a small integer data type
 * @property {string} int Represents an integer data type
 * @property {string} bigint Represents a big integer data type
 * @property {DecimalFunction} decimal Represents a decimal data type
 * @property {string} numeric Represents a numeric data type
 * @property {string} float Represents a floating point data type
 * @property {string} real Represents a real number data type
 * @property {string} date Represents a date data type (YYYY-MM-DD Format)
 * @property {string} time Represents a time data type (HH:MI:SS Format)
 * @property {string} datetime Represents a date/time data type (YYYY-MM-DD HH:MI:SS Format)
 * @property {string} timestamp Represents a timestamp since the Unix epoch (`1970-01-01 00:00:00` UTC)
 * @property {string} year Represents a year data type (YY or YYYY Format)
 * @property {MaxFunction} char Represents a fixed length (<=4000 characters) string data type
 * @property {MaxFunction} varchar Represents a variable length string data type (<=8000 characters)
 * @property {string} text Represents a variable length string data type (<=2GB size)
 * @property {MaxFunction} nchar Represents a fixed length (4000 characters) unicode string data type
 * @property {MaxFunction} nvarchar Represents a variable length unicode string data type (<=4000 characters)
 * @property {string} ntext Represents a variable length unicode string data type (<=1GB size)
 * @property {string} binary Represents a fixed length binary data type (<=8000 bytes)
 * @property {MaxFunction} varbinary Represents a variable length binary data type (<=8000 bytes)
 * @property {string} image Represents a variable length binary data type (<=2GB size)
 * @property {string} json Represents a JSON data type
 */
exports.DataTypes = {
    bit: 'BIT',
    tinyint: 'TINYINT',
    smallint: 'SMALLINT',
    int: 'INT',
    bigint: 'BIGINT',
    decimal: (p = 5, q = 2) => `DECIMAL(${p},${q})`,
    numeric: 'NUMERIC',
    float: 'FLOAT',
    real: 'REAL',
    date: 'DATE',
    time: 'TIME',
    datetime: 'DATETIME',
    timestamp: 'TIMESTAMP',
    year: 'YEAR',
    char: (max = 255) => `CHAR(${max})`,
    varchar: (max = 255) => `VARCHAR(${max})`,
    text: 'TEXT',
    nchar: (max = 255) => `NCHAR(${max})`,
    nvarchar: (max = 255) => `NVARCHAR(${max})`,
    ntext: 'NTEXT',
    binary: 'BINARY',
    varbinary: (max) => `VARBINARY(${max})`,
    image: 'IMAGE',
    json: 'JSON'
};

/**
 * Function for creating a decimal data type
 * @typedef {Function} DecimalFunction
 * @param {number} [p=5] How many digits the decimal should be
 * @param {number} [q=2] How many places to the right of the decimal, anything left over from p will be how many to the right
 * @example
 * decimal(5, 2) // 000.00
 */
/**
 * Function for creating a maximum character limit data type
 * @typedef {Function} MaxFunction
 * @param {number} [max=255] Maximum digits that this max can hold, should be reduced as 255 will lead to high memory usage
 */
/**
 * Options for each key in the {@link TableOptions}
 * @typedef {Object} KeyOptions
 * @property {string} name The name for the key
 * @property {DataType} type The data type for the key, one of {@link DataTypes}
 * @property {boolean} [unique=false] Whether the key should be unique
 * @property {boolean} [primary=false] If the key is the primary key for the DB
 * @property {boolean} [nullable=null] Whether the key is nullable or not
 */
exports.DataTypeDefaults = {
    unique: false,
    primary: false,
    nullable: null
};