Skip to content

index

Manager

The database(s) manager entry point that autogenerates/manages SQL execution functions from underlying SQL statement files. Vendor-specific implementations should implement typedefs.Dialect and pass the class or module path into the constructor as conf.db.dialects.myDialectClassOrModulePath. See README.md for more details about SQL related features. A manager will contain the following properties:

  • db - The database accessible object where all of the constructed connections reside. For example
  • db.<CONN_NAME> - There will be a property assigned for each database connection configured during construction. For example, when <CONN_NAME> is myConn, the manager instance will be accessible via manager.db.myConn.
  • db.<CONN_NAME>.<PREPARED_FUNC_PATHS> The generated SQL executable typedefs.SQLERPreparedFunction(s). Assuming a <CONN_NAME> of myConn and a path of /db/myConn/read.my.table.sql, the accessible typedefs.SQLERPreparedFunction may be accessible via db.myConn.read.my.table().
  • db.<CONN_NAME>.beginTransaction - A function that accepts a single typedefs.SQLERTransactionOptions that begins a transaction for a given database connection pool.

Kind: global class

new Manager(conf, [cache], [logging])

Creates a new database manager. Vendor-specific implementations should have constructors that accept properties defined by typedefs.Dialect.

ParamTypeDescription
conftypedefs.SQLERConfigurationOptionsThe configuration options
[cache]typedefs.SQLERCachethe typedefs.SQLERCache like instance that will handle the logevity of the SQL statement before the SQL statement is re-read from the SQL file
[logging]function | Booleanthe function(dbNames) that will return a name/dialect specific function(obj1OrMsg [, obj2OrSubst1, ..., obj2OrSubstN])) that will handle database logging. Pass true to use the console. Omit to disable logging altogether.

manager.addConnection(conn, [priv], [cache], [logging], [returnErrors]) ⇒ typedefs.SQLEROperationResults

Adds a connection configuration to the manager and initializes the database connection

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the truthy result from the dialect initialization for the given connection (or the errors when return errors flag is set)

ParamTypeDescription
conntypedefs.SQLERConnectionOptionsThe connection options that will be added to the manager
[priv]typedefs.SQLERPrivateOptionsThe private options that contain the connection credentials that should match priv[conn.id]. When omitted, an attempt to use the private options passed into the constructor to make a privPassedIntoConstructor[conn.id] match.
[cache]typedefs.SQLERCachethe typedefs.SQLERCache like instance that will handle the logevity of the SQL statement before the SQL statement is re-read from the SQL file
[logging]function | Booleanthe function(dbNames) that will return a name/dialect specific function(obj1OrMsg [, obj2OrSubst1, ..., obj2OrSubstN])) that will handle database logging. Pass true to use the console. Omit to disable logging altogether.
[returnErrors]BooleanTruthy to return errors, otherwise, any encountered errors will be thrown

manager.init([returnErrors]) ⇒ typedefs.SQLEROperationResults

Initializes the configured database connections

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the truthy result from the dialect initialization for the given connection (or the errors when return errors flag is set)

ParamTypeDescription
[returnErrors]BooleanTruthy to return errors, otherwise, any encountered errors will be thrown

manager.state([opts], [...connNames]) ⇒ typedefs.SQLEROperationResults

Composes the typedefs.SQLERState on either all the connections used by the manager or on the specified connection names.

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the typedefs.SQLERState for the given connection

ParamTypeDescription
[opts]typedefs.SQLEROperationOptionsThe typedefs.SQLEROperationOptions to use
[...connNames]StringThe connection names to perform the check on (defaults to all connections)

manager.preparedFunctionCount([...connNames]) ⇒ typedefs.SQLEROperationResults

Gets the number of prepared SQL functions on either all the connections used by the manager or on the specified connection names.

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the number of generated prepared SQL functions for the given connection

ParamTypeDescription
[...connNames]StringThe connection names to perform the check on (defaults to all connections)

manager.close() ⇒ typedefs.SQLEROperationResults

Closes all database pools/connections/etc.

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results

manager.scan([removeOrphans], ...connNames) ⇒ typedefs.SQLEROperationResults

Scans directories/subdirectories for SQL files and generates prepared functions on either all the connections used by the manager or on the specified connection names.

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the number of SQL prepared functions for the given connection

ParamTypeDefaultDescription
[removeOrphans]BooleantrueTruthy to remove orphaned prepared functions that no longer have an SQL file associated with them
...connNamesStringThe connection names to scan for SQL files for when generating prepared functions

manager.setCache([cache], [isTransfer], ...connNames) ⇒ typedefs.SQLEROperationResults

Sets the caching mechanism that will be used that will determine the frequency of reading SQL source files. Omit the connection names to set the cache on all connections used by the manager.

Kind: instance method of Manager
Returns: typedefs.SQLEROperationResults - The results, each result[connectionName] containing the number of cached keys transferred for the given connection

ParamTypeDescription
[cache]typedefs.SQLERCachethe typedefs.SQLERCache like instance that will handle the logevity of the SQL statement before the SQL statement is re-read from the SQL file
[isTransfer]BooleanTruthy when the passed cache is present and any existing SQL (either cached or non-cached) should be transferred to it (if any)
...connNamesStringThe connection names to set the cache for

manager.getCacheKey(path, connName) ⇒ String

Gets a cache key for a given absolute path to an SQL file

Kind: instance method of Manager
Returns: String - The cache key for the given path

ParamTypeDescription
pathStringThe absolute path to the SQL file
connNameStringThe connection name to get the cache key for

manager.generateCacheKey(dialect, connName, methodName, ext) ⇒ String

Generates a key used for caching SQL methods

Kind: instance method of Manager
Returns: String - The key used for caching

ParamTypeDescription
dialectStringThe database dialect
connNameStringThe connection name
methodNameStringThe SQL method name
extStringThe SQL file extension

Manager.OPERATION_TYPES ⇒ Array.<String>

Kind: static property of Manager
Returns: Array.<String> - The operation types

Manager.POSITIONAL_BINDS_REGEXP ⇒ RegExp

Kind: static property of Manager
Returns: RegExp - A regular expression that globally matches each named bind parameters in a SQL statement. A single capture group is defined for each parameter name (match on entire bind name syntax)

Manager.namedBindSequence(sql, count) ⇒ Array.<String>

Duplicates a SQL statement by sequentially incrementing named bind parameters by appending an increacing numeric count to each bind parameter

Kind: static method of Manager
Returns: Array.<String> - The SQL statements that have been duplicated with sequentially numeric suffixes

ParamTypeDescription
sqlStringThe SQL statement that contains the bind names that will be sequenced
countIntegerThe total number of duplicates to make

Example

js
Manager.namedBindSequence('SELECT * FROM EXAMPLE X WHERE X.A = :a AND X.B = :b AND X.C = :c', 2);
// produces:
[
  'SELECT * FROM EXAMPLE X WHERE X.A = :a1 AND X.B = :b1 AND X.C = :c1',
  'SELECT * FROM EXAMPLE X WHERE X.A = :a2 AND X.B = :b2 AND X.C = :c2'
]

Released under the MIT License.