Options
All
  • Public
  • Public/Protected
  • All
Menu

Database service.

Hierarchy

  • DBService

Index

Constructors

constructor

Properties

Private closed

closed: boolean = false

Private dbURL

dbURL: string = ...

Private logErrors

logErrors: boolean = true

Private pool

pool: Pool

Private sqlPath

sqlPath: string = ...

Private tables

tables: string[] = ...

Private testing

testing: boolean = !!parseInt(process.env.TESTING)

Methods

close

  • close(): Promise<void>

create

  • create<T>(tableName: string, fields: FieldMap): Promise<T>
  • Create a new record in the table.

    Type parameters

    • T

    Parameters

    • tableName: string
    • fields: FieldMap

      Field values to create in the new record.

    Returns Promise<T>

    The new record.

deleteByFields

  • deleteByFields(tableName: string, fields: FieldMap): Promise<void>
  • Delete records matching provided query values.

    Parameters

    • tableName: string
    • fields: FieldMap

      Fields to query by.

    Returns Promise<void>

deleteByID

  • deleteByID(tableName: string, id: any): Promise<void>

deleteCustom

  • deleteCustom(tableName: string, whereClause: string, params?: any[]): Promise<void>
  • Delete records using a custom where clause.

    Parameters

    • tableName: string
    • whereClause: string

      The where clause to use in the SQL query.

    • params: any[] = []

      The query parameters.

    Returns Promise<void>

execute

  • execute<T>(stmt: string, params?: any[]): Promise<T[]>
  • Execute a SQL query.

    Type parameters

    • T = void

    Parameters

    • stmt: string

      SQL statement.

    • params: any[] = []

      Values to be inserted into the statement.

    Returns Promise<T[]>

    Query results.

executeAllFiles

  • executeAllFiles<T>(directory: string, ext?: string, sqlPath?: string): Promise<T[][]>
  • Execute all SQL files in a directory.

    Type parameters

    • T = void

    Parameters

    • directory: string

      The directory containing SQL files.

    • ext: string = '.sql'

      The extension of the SQL files.

    • sqlPath: string = null

      The SQL files path.

    Returns Promise<T[][]>

    Results of all queries.

executeFile

  • executeFile<T>(filename: string, params?: any[], sqlPath?: string): Promise<T[]>
  • Execute a SQL file.

    Type parameters

    • T = void

    Parameters

    • filename: string

      The SQL file to execute.

    • params: any[] = []

      Values to be inserted into the statement.

    • sqlPath: string = null

      The SQL files path.

    Returns Promise<T[]>

    Query results.

executeFiles

  • executeFiles<T>(filenames: string[], params?: any[][], sqlPath?: string): Promise<T[][]>
  • Execute multiple SQL files.

    Type parameters

    • T = void

    Parameters

    • filenames: string[]

      The SQL files to execute.

    • params: any[][] = []

      Values to be inserted into the statement.

    • sqlPath: string = null

      The SQL files path.

    Returns Promise<T[][]>

    Results of all queries.

executeMany

  • executeMany<T>(stmts: string[], params?: any[][]): Promise<T[][]>
  • Execute multiple SQL queries, each one right after the last

    Type parameters

    • T = void

    Parameters

    • stmts: string[]

      SQL statement.

    • params: any[][] = []

      Values to be inserted into the statement.

    Returns Promise<T[][]>

    Results of all queries.

getByFields

  • getByFields<T>(tableName: string, fields: FieldMap): Promise<T>
  • Get a record by field values.

    Type parameters

    • T

    Parameters

    • tableName: string
    • fields: FieldMap

      Fields to query by.

    Returns Promise<T>

    The first record matching the field values.

getByID

  • getByID<T>(tableName: string, id: any): Promise<T>
  • Get a record in the table by ID.

    Type parameters

    • T

    Parameters

    • tableName: string
    • id: any

      The record's ID.

    Returns Promise<T>

    The record with the given ID.

getCustom

  • getCustom<T>(tableName: string, whereClause: string, params?: any[]): Promise<T>
  • Get a record using a custom where clause.

    Type parameters

    • T

    Parameters

    • tableName: string
    • whereClause: string

      The where clause to use in the SQL query.

    • params: any[] = []

      The query parameters.

    Returns Promise<T>

    The first record from the resulting set.

getStaticTable

  • getStaticTable(tableName: string): Promise<any[]>

initDB

  • initDB(options?: InitDBOptions): Promise<void>

list

  • list<T>(tableName: string, orderOptions?: OrderOptions): Promise<T[]>
  • List all records in the table.

    Type parameters

    • T

    Parameters

    • tableName: string
    • Optional orderOptions: OrderOptions

      Options for ordering the results.

    Returns Promise<T[]>

    All records, ordered if applicable.

listByFields

  • listByFields<T>(tableName: string, fields: FieldMap, orderOptions?: OrderOptions): Promise<T[]>
  • List all records matching provided query values.

    Type parameters

    • T

    Parameters

    • tableName: string
    • fields: FieldMap

      Fields to query by.

    • Optional orderOptions: OrderOptions

      Options for ordering the results.

    Returns Promise<T[]>

    All records matching the provided query values, ordered if applicable.

listCustom

  • listCustom<T>(tableName: string, whereClause: string, orderOptions?: OrderOptions, params?: any[]): Promise<T[]>
  • List all records using a custom where clause.

    Type parameters

    • T

    Parameters

    • tableName: string
    • whereClause: string

      The where clause to use in the SQL query.

    • Optional orderOptions: OrderOptions

      Options for ordering the results.

    • params: any[] = []

      The query parameters.

    Returns Promise<T[]>

    All records from the resulting set.

Private logError

  • logError(stmt: string, params: any[], res: any, err: Error): void
  • Provides information on an error.

    Parameters

    • stmt: string

      The SQL statement.

    • params: any[]

      The query parameters.

    • res: any

      The query response.

    • err: Error

      The error thrown.

    Returns void

Private populateStaticTable

  • populateStaticTable(tableName: string): Promise<void>
  • Populates a static database table from a CSV file.

    Parameters

    • tableName: string

      The table to populate.

    Returns Promise<void>

Private populateTable

  • populateTable<T>(tableName: string, values: T[]): Promise<void>
  • Populates a database table.

    Type parameters

    • T

    Parameters

    • tableName: string

      The table to populate.

    • values: T[]

      Values to be inserted into the table.

    Returns Promise<void>

Private pruneRecords

  • pruneRecords(): Promise<void>

Private transformParams

  • transformParams(stmt: string): string
  • Transforms question mark query param indicators.

    Parameters

    • stmt: string

      The SQL statement.

    Returns string

    The query with transformed parameters.

updateByFields

  • updateByFields<T>(tableName: string, fields: FieldMap, fieldValues: FieldMap): Promise<T[]>
  • Update records matching provided query values.

    Type parameters

    • T

    Parameters

    • tableName: string
    • fields: FieldMap

      Fields to query by.

    • fieldValues: FieldMap

      The updated field values.

    Returns Promise<T[]>

    The updated records.

updateByID

  • updateByID<T>(tableName: string, id: any, fieldValues: FieldMap): Promise<T>
  • Update a record in the table by ID.

    Type parameters

    • T

    Parameters

    • tableName: string
    • id: any

      The record's ID.

    • fieldValues: FieldMap

      The updated field values.

    Returns Promise<T>

    The updated record.

updateCustom

  • updateCustom<T>(tableName: string, whereClause: string, fieldValues: FieldMap, params?: any[]): Promise<T[]>
  • Update records using a custom where clause.

    Type parameters

    • T

    Parameters

    • tableName: string
    • whereClause: string

      The where clause to use in the SQL query.

    • fieldValues: FieldMap

      The updated field values.

    • params: any[] = []

      The query parameters.

    Returns Promise<T[]>

    The updated records.

wipeTestDB

  • wipeTestDB(): Promise<void>

Generated using TypeDoc