Home Reference Source Repository
import PDOX from 'tsugi-node/src/util/PDOX.js'
public class | source

PDOX

Routines to handle connection chores. https://www.npmjs.com/package/mysql

Static Method Summary

Static Public Methods
public static

substituteFields(conn: *, sql: *, data: *): *

Dp field Substitution

Constructor Summary

Public Constructor
public

constructor(Configuration: Config)

Create the database conection pools.

Method Summary

Public Methods
public

allRows(sql: string, data: object, dothrow: boolean): *

Run a query and return all the rows or an error from the query.

public

allRowsDie(sql: string, data: object): *

Run a query and return all the rows from the query and throw any error.

public

cop(): *

Get a connection promise from the pool Make sure to do a cop.release()

public

insertKey(sql: string, data: object): *

Run an INSERT and return the generated key, throw on error

public

query(sql: string, data: object): *

Run a query and return the number of affected rows, throw on error

public

queryChanged(sql: string, data: object): *

Run a query and return the number of changed rows, throw on error

public

queryFull(sql: string, data: object, returnval: number, dothrow: boolean): *

Run a query and return the appropriate result for the query

public

setPrefix(sql: *): *

Replace {p} with the configured database table prefix.

Static Public Methods

public static substituteFields(conn: *, sql: *, data: *): * source

Dp field Substitution

Params:

NameTypeAttributeDescription
conn *
sql *
data *

Return:

*

Public Constructors

public constructor(Configuration: Config) source

Create the database conection pools.

Params:

NameTypeAttributeDescription
Configuration Config

object

Public Methods

public allRows(sql: string, data: object, dothrow: boolean): * source

Run a query and return all the rows or an error from the query.

let sql = 'SELECT * FROM lti_key WHERE key_key = :key_key';
pdox.allRows(sql,{ key_key: thekey }, false).then(
     function(rows) {
         console.log("Rows:",rows.length);
     },
     function(reason) {
          console.log("Bummer",reason);
     }
);

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix - must be SELECT

data object

The key-value pairs for substitution

dothrow boolean

Whether to throw or return the error

Return:

*

public allRowsDie(sql: string, data: object): * source

Run a query and return all the rows from the query and throw any error.

let sql = 'SELECT * FROM {p}lti_key WHERE key_key = :key_key';
pdox.allRowsDie(sql,{ key_key: thekey }).then(
     function(rows) {
         console.log("Rows:",rows.length);
     },
     function(reason) {
          console.log("Bummer",reason);
     }
);

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix.

data object

The key-value pairs for substitution

Return:

*

public cop(): * source

Get a connection promise from the pool Make sure to do a cop.release()

var thekey = '12345';
pdox.cop.then( function(connection) {
    let sql = 'SELECT * FROM lti_key WHERE key_key = :key';
    connection.query(sql, { key: thekey }, function(err, rows, fields) {
        if (err) {
            console.log('Could not load data query');
        } else {
            console.log("Rows:",rows.length);
        }
        connection.release();
    });
});

Return:

*

public insertKey(sql: string, data: object): * source

Run an INSERT and return the generated key, throw on error

sql = "INSERT INTO {p}lti_unit_test (name,email)
       VALUES ('tsugi', 'tsugi@zap.com')";
pdox.insertKey(sql).then( function(retval) {
     console.log("INSERT retval:",retval);
});

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix - must be an INSERT to a table with an auto-increment field.

data object

The key-value pairs for substitution (optional)

Return:

*

public query(sql: string, data: object): * source

Run a query and return the number of affected rows, throw on error

sql = "DELETE FROM {p}lti_unit_test WHERE name='tsugi'";
pdox.query(sql).then( function(retval) {
     console.log("DELETE retval:",retval);
});

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix - must not be a SELECT

data object

The key-value pairs for substitution (optional)

Return:

*

public queryChanged(sql: string, data: object): * source

Run a query and return the number of changed rows, throw on error

sql = "UPDATE {p}lti_unit_test SET email=:new WHERE name='tsugi'";
pdox.queryChanged(sql, {new:'tsugi@fred.com'}).then(
    function(retval) {
        console.log("UPDATE retval:",retval);
    }
);

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix - must not be a SELECT

data object

The key-value pairs for substitution (optional)

Return:

*

public queryFull(sql: string, data: object, returnval: number, dothrow: boolean): * source

Run a query and return the appropriate result for the query

This has more parameters and is typically used by methods with simpler signatures.

Params:

NameTypeAttributeDescription
sql string

The SQL to use - it is ok to use {p} for the database prefix - must not be SELECT.

data object

The key-value pairs for substitution

returnval number

What to return from the function. 0=rows affected, 1=rows changed, 2=last insert id

dothrow boolean

Whether to throw or return the error

Return:

*

public setPrefix(sql: *): * source

Replace {p} with the configured database table prefix.

Params:

NameTypeAttributeDescription
sql *

Return:

*