IUFDatabase defines the methods a database will implement.

interface IUFDatabase {
    delete(aSql, aParameterValues?): Promise<number>;
    fieldAs<T>(aSql, aParameterValues, aDefault): Promise<T>;
    fieldOrFailAs<T>(aSql, aParameterValues?): Promise<T>;
    getUniqueCode(aTable, aColumn, aLength): Promise<string>;
    insert(aSql, aParameterValues?): Promise<number>;
    insertObject<T>(aTable, aData, aPrimaryKey?, anIgnoreFields?): Promise<T>;
    rowAs<T>(aSql, aParameterValues?): Promise<undefined | T>;
    rowOrFailAs<T>(aSql, aParameterValues?): Promise<T>;
    rowsAs<T>(aSql, aParameterValues?): Promise<T[]>;
    transaction(aCallback): Promise<void>;
    update(aSql, aParameterValues?): Promise<number>;
    updateObject<T>(aTable, aPrimaryValue, aData, aPrimaryKey?, anIgnoreFields?): Promise<void>;
}

Implemented by

Methods

  • Performs a database delete and returns the number of deleted records.

    Parameters

    • aSql: string

      Sql delete statement

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<number>

    number of deleted records.

  • Execute a sql to get a single value as a certain type.

    Type Parameters

    • T

    Parameters

    • aSql: string

      Sql statement to perform

    • aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    • aDefault: T

      Default value to return if the sql statement did not have any results

    Returns Promise<T>

    result from sql statement or aDefault

  • Execute a sql to get a single value as a certain type. If no value can be found, the method will throw an error.

    Type Parameters

    • T

    Parameters

    • aSql: string

      Sql statement to perform

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<T>

    result from sql statement

    Throws

    if no row (and thus field) can be found

  • Generates a unique code with UFText.generateCode to be used in some table.

    Parameters

    • aTable: string

      Table to use unique code with

    • aColumn: string

      Name of column in table that contains the unique code

    • aLength: number

      Number of characters the code should exist of

    Returns Promise<string>

    an unique code

  • Performs an insert and returns the id of the created record.

    Parameters

    • aSql: string

      Sql insert statement

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<number>

    id of created record or 0 if there is no id.

  • Inserts a data from an object. The method creates an insert into statement using the property names inside the object.

    The aData structure can contain a primary key property, when building the sql statement it will be skipped. After the insert statement the generated id (if any) will be assigned to the primary key field.

    Type Parameters

    • T extends object

      should be an object type not a class; since the method might create a new object

    Parameters

    • aTable: string

      Name of table

    • aData: T

      Data to insert (should be some form of object)

    • Optional aPrimaryKey: string

      Name of primary key field

    • Optional anIgnoreFields: string[]

      Fields to ignore in aData

    Returns Promise<T>

    Either aData if there is no primary key or a new object with values copied from aData with the new primary key value.

  • Execute a sql to get a row as a certain type.

    Type Parameters

    • T

    Parameters

    • aSql: string

      Sql statement to perform

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<undefined | T>

    result from sql statement; undefined when no row could be found

  • Execute a sql to get a row as a certain type. If no row can be found, the method will throw an error.

    Type Parameters

    • T

    Parameters

    • aSql: string

      Sql statement to perform

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<T>

    result from sql statement

    Throws

    error if no row can be found

  • Execute a sql to get multiple rows as a certain type.

    Type Parameters

    • T

    Parameters

    • aSql: string

      Sql statement to perform

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<T[]>

    result from sql statement

  • Execute a function within a transaction.

    Parameters

    • aCallback: ((aDatabase) => Promise<void>)

      A function that will be called with await. It will be called with a single parameter, which can be used to communicate with the database. The parameter might be a different instance then the instance the transaction call originated from.

        • (aDatabase): Promise<void>
        • Parameters

          Returns Promise<void>

    Returns Promise<void>

    Throws

    any exception that occurred while calling aCallback

  • Performs an update and returns the number of changed records.

    Parameters

    • aSql: string

      Sql update statement

    • Optional aParameterValues: UFDynamicObject

      Values to use in case the statement contains parameters

    Returns Promise<number>

    number of changed records.

  • Updates a record in a table assuming it has a single primary key column.

    Type Parameters

    • T extends object

    Parameters

    • aTable: string

      Name of table

    • aPrimaryValue: any

      Primary key vale

    • aData: T

      Object containing field names and their new values.

    • Optional aPrimaryKey: string

      Name of primary key

    • Optional anIgnoreFields: string[]

      Fields to ignore in aData

    Returns Promise<void>