Class: DatabaseContext

repositories/mysqlRepository~ DatabaseContext

new DatabaseContext()

infra/database/repositories/mysqlRepository.js, line 97

Creates a MySQL connection. Uses environment variables:

  • DB_TYPE=mysql
  • MYSQL_DB_HOST= the host name e.g. localhost
  • MYSQL_DB_USER= the user name e.g. root
  • MYSQL_DB_PASS= the password for the user
  • MYSQL_DB_NAME= the name of the database e.g. IsatDataPro

Methods

close()

infra/database/repositories/mysqlRepository.js, line 408

Closes the database connection

delete(item){boolean}

infra/database/repositories/mysqlRepository.js, line 354

Deletes an item from the database

Name Type Description
item Object

The category in the collection

Throws:
  • if database not initialized

    Type
    Error
  • if item.category is invalid

    Type
    Error
Returns:
Type Description
boolean result

find(category, include, exclude, options, older){Array.<Object>}

infra/database/repositories/mysqlRepository.js, line 188

Returns database entries matching a criteria

Name Type Description
category string

the model category e.g. message_return

include Object optional

key/value pairs for equality filtering

exclude Object optional

key/value pairs for inequality filtering

options Object optional

e.g. { limit: 1, desc: 'dbTimestamp' }

Name Type Description
limit number optional

Maximum items to return

desc string optional

Property to sort descending (e.g. _ts timestamp)

asc string optional

Property to sort descending (e.g. _ts timestamp)

older Date optional

Searches for records older than the Date

Throws:
  • if database not initialized

    Type
    Error
  • if category is invalid

    Type
    Error
Returns:
Type Description
Array.<Object> a list of row objects matching the criteria

initialize()

infra/database/repositories/mysqlRepository.js, line 113

Initializes the MySQL database if required

upsert(item, filterOn, newerThan){Object}

infra/database/repositories/mysqlRepository.js, line 265

Updates an existing entity in the database or creates a new one. Null and undefined values are not pushed in an update. If the item includes a .newest prototype property it will discard update if older than that property in the database entity.

Name Type Description
item Object

The item to upsert

filterOn Object optional

Optional filter on properties defining "exists"

newerThan Object optional

Optional filter on properties involving timestamp

Throws:
  • if database not initialized

    Type
    Error
  • if item.category is invalid

    Type
    Error
  • if filterOn is not an Object

    Type
    Error
  • if multiple matching entries found in database/table

    Type
    Error
Returns:
Type Description
Object