typedefs

typedefs

Global type definitions used by sqler

Source:

Type Definitions

SQLERBindExpansionFunction(sql, bindsopt) → {String}

Expands bind variables that contain an array of values when they appear in the SQL statement. For example, an SQL statement with a section that contains IN (:someParam) and binds of { someParam: [1,2,3] } would become IN (:someParam, :someParam1, :someParam2) with binds of { someParam: 1, someParam1: 2, SomeParam2: 3 }

Source:
Parameters:
Name Type Attributes Description
sql String

The SQL to defragement

binds Object <optional>

An object that contains the SQL parameterized binds that will be used for parameterized array composition

Returns:
Type:
String

The defragmented SQL

SQLERCache

The cache client responsible for regulating the frequency in which a SQL file is read by a Manager.

Properties:
Name Type Description
start function

An async function() that starts caching. This could be a noop or could start any background processing and/or capture of cached keys (depending on the type of implementation).

stop function

An async function() that stops caching. This could be a noop or could stop any background processing and/or capture of cached keys (depending on the type of implementation).

get function

An async function(key) that gets a corresponding SQL statement from cache using the specified key to uniquily identify the SQL source (typically generated by a Manager). The returned object will contain the following values when present (otherwise, returns null):

  • item - The cached SQL statement
  • stored - The timestamp indicating the time when the SQL statement was stored in cache
  • ttl - The timestamp indicating the remaining time left before the SQL statement will be removed from cache
set function

An async function(key, sql, ttlOverride) that sets a SQL statement in cache, overriding the time-to-live_ (in milliseconds) that may have been set by a Manager.

drop function

An async function(key) that removes the specified key from cache

Source:
Type:
  • Object
Example
// cache options can be different depending on the needs of the implementing cache
const cacheOpts = {
 "expiresIn": 60000
};
// simple interval cache for illustration purposes
const bank = { store: {}, handles: {} };
const cache = {
 start: async () => {
   let cached, calTtl;
   for (let key in bank.handles) {
     clearInterval(bank.handles[key]);
     cached = bank.store.hasOwnProperty(key) ? bank.store[key] : null;
     calTtl = !cached|| isNaN(cached.ttl) ? cacheOpts.expiresIn : cached.ttl;
     bank.handles[key] = setInterval(() => delete bank.store[key], calTtl);
   }
 },
 stop: async () => {
   for (let key in bank.handles) {
     clearInterval(bank.handles[key]);
   }
 },
 get: async key => {
   const cached = bank.store.hasOwnProperty(key) ? bank.store[key] : null;
   if (cached) cached.ttl = Date.now() - cached.stored;
   return Promise.resolve(cached ? JSON.parse(JSON.stringify(cached)) : cached);
 },
 set: async (key, val, ttl) => {
   if (bank.handles[key]) clearInterval(bank.handles[key]);
   const calTtl = !ttl || isNaN(ttl) ? cacheOpts.expiresIn : ttl;
   bank.store[key] = { item: val, stored: Date.now(), ttl: calTtl };
   bank.handles[key] = setInterval(sql => delete bank.store[key], calTtl);
   return Promise.resolve();
 },
 drop: async () => {
   if (bank.handles[key]) {
     clearInterval(bank.handles[key]);
     delete bank.handles[key];
   }
   if (bank.store[key]) delete bank.store[key];
 }
};

// manager configuration
const conf = {
 // other required conf options here
 "db": {
   "connections": [
     {
       // other required connection conf options here
     }
   ]
 }
};

const mgr = new Manager(conf, cache);
await mgr.init();
// use the manager to execute SQL files that will
// be refreshed/re-read every 60 seconds
// can also set the cache after Manager.init()
const setCacheResults = await mgr.setCache(cache);

SQLERConfigurationOptions

Configuration options for Manager use

Properties:
Properties
Name Type Attributes Description
mainPath String <optional>

Root directory starting point to look for SQL files (defaults to require.main path or process.cwd())

privatePath String <optional>

Current working directory where generated files will be located (if any, defaults to process.cwd())

debug Boolean <optional>

Truthy to turn on debugging

univ SQLERUniversalOptions

The SQLERUniversalOptions

db Object

The public facing database configuration

Name Type Description
dialects Object

An object that contains Dialect implementation details where each property name matches a dialect name and the value contains either the module class or a string that points to a Dialect implementation for the given dialect (e.g. { dialects: { 'oracle': 'sqler-oracle' } }). When using a directory path the dialect path will be prefixed with process.cwd() before loading.

connections Array.<SQLERConnectionOptions>

The connections options that will be used.

Source:
Type:
  • Object

SQLERConnectionOptions

Options for connections used by Manager

Properties:
Properties
Name Type Attributes Default Description
id String

Identifies the connection within a SQLERPrivateOptions

dialect String

The database dialect (e.g. mysql, mssql, oracle, etc.)

name String

The name given to the database used as the property name on the Manager to access generated SQL functions (e.g. name = 'example' would result in a SQL function connection container manager.db.example). The name will also be used as the cwd relative directory used when no dir is defined

dir String <optional>
name

The alternative dir where *.sql files will be found relative to mainPath passed into a Manager constructor. The directory path will be used as the basis for generating SQL statements from discovered SQL files. Each will be made accessible in the manager by name followed by an object for each name separated by period(s) within the file name with the last entry as the executable SQLERPreparedFunction. For example, a connection named "conn1" and a SQL file named "user.team.details.sql" will be accessible within the manager as "mgr.db.conn1.user.team.details()". But when dir is set to "myDir" the SQL files will be loaded from the "myDir" directory (relative to mainPath) instead of the default directory that matches the connection name "conn1".

version Float <optional>

A version that can be used for version substitutions within an SQL statement

service String <optional>

The service name defined by the underlying database (may be required depending on the implementing Dialect

binds Object <optional>

The global object that contains bind variable values that will be included in all SQL calls made under the connection for parameter binds if not overridden by individual "binds" passed into the SQLERPreparedFunction

substitutes Object <optional>

Key/value pairs that define global/static substitutions that will be made in prepared statements by replacing occurances of keys with corresponding values

host String <optional>

The database host override for a value specified in SQLERPrivateOptions

port String <optional>

The database port override for a value specified in SQLERPrivateOptions

protocol String <optional>

The database protocol override for a value specified in SQLERPrivateOptions

dateFormatter function | Boolean <optional>

A function(date) that will be used to format bound dates into string values for SQLERPreparedFunction calls. Set to a truthy value to perform date.toISOString(). Gets overridden by the same option set on SQLERExecOptions.

driverOptions Object <optional>

Options passed directly into the Dialect driver

log Boolean | Array.<String> <optional>

When logging is turned on for a given Manager, the specified tags will prefix the log output. Explicity set to false to disable connection log level logging even if it is turned on via the Manager.

logError Boolean | Array.<String> <optional>

When logging is turned on for a given Manager, the specified tags will prefix the error log output. Explicity set to false to disable connection error level logging even if it is turned on via the Manager.

pool Object <optional>

The connection pool options (overrides any driverOptions that may pertain the pool)

Name Type Attributes Description
max Number <optional>

The maximum number of connections in the pool. When pool.min and pool.max are the same, pool.increment should typically be set to zero. (overrides any driverOptions that may pertain the pool max)

min Number <optional>

The minumum number of connections in the pool. When pool.min and pool.max are the same, pool.increment should typically be set to zero. (overrides any driverOptions that may pertain the pool min)

idle Number <optional>

The maximum time, in milliseconds, that a connection can be idle before being released (overrides any driverOptions that may pertain the pool idle)

increment Number <optional>

The number of connections that are opened whenever a connection request exceeds the number of currently open connections. When pool.min and pool.max are the same, pool.increment should typically be set to zero. (overrides any driverOptions that may pertain the pool increment)

timeout Number <optional>

The number of milliseconds that a connection request should wait in the queue before the request is terminated (overrides any driverOptions that may pertain the pool timeout)

alias String <optional>

When supported, the alias of this pool in the connection pool cache (overrides any driverOptions that may pertain the pool alias)

Source:
Type:
  • Object

SQLERExecErrorOptions

Options for handling any errors that occur during execution.

Properties:
Name Type Attributes Description
handler function <optional>

A function(error) that will handle any errors thrown. The errors should contain a sqler property containing

includeBindValues Boolean <optional>

Truthy to include the bind parameter values error.sqler.

returnErrors Boolean <optional>

Truthy to return any errors that may occur. Otherwise, throw any errors that may occur.

Source:
Type:
  • Object

SQLERExecMeta

Internally generated metadata that is passed into Dialect.exec by a Manager for determining SQL sources.

Properties:
Name Type Description
name String

The composed name given to a given SQL file

path String

The path to the SQL file

Source:
Type:
  • Object

SQLERExecOptions

Options that are passed to generated SQLERPreparedFunction. NOTE: Either transaction.commit or trnasaction.rollback must be invoked when autoCommit is falsy and a valid transactionId is supplied to ensue underlying connections are completed and closed.

Properties:
Name Type Attributes Default Description
name String <optional>

A name to assign to the execution.

type String <optional>

The type of CRUD operation that is being executed (i.e. CREATE, READ, UPDATE, DELETE). Mandatory only when the generated/prepared SQL function was generated from a SQL file that was not prefixed with a valid CRUD type.

stream Number <optional>

A value stream >= 0, indicates that the execution will be streamed (ideal for large reads/writes). The resulting rows will contain either a stream.Readable[] or a stream.Writable[] (depending upon the type) instead of the default Object[] rows. When supported by the dialect, streams will be batched using the indicated stream count as the batch size (a value of 0 is the same as a value of 1).

binds Object <optional>
{}

The key/value pair of binding parameters that will be bound in the SQL statement.

autoCommit Boolean <optional>
true

Truthy to perform a commits the transaction at the end of the prepared function execution. NOTE: When falsy the underlying connection will remain open until the returned SQLERExecResults commit or rollback is called. See AutoCommit for more details.

transactionId String <optional>

A transaction ID returned from a prior call to const tx = await manager.db.myConnectionName.beginTransaction(); options.transactionId = tx.id that will be used when executing the SQLERPreparedFunction. The generated transactionId helps to isolate executions to a single open connection in order to prevent inadvertently making changes on database connections used by other transactions that may also be in progress. The transactionId is ignored when there is no transaction in progress with the specified transactionId.

prepareStatement Boolean <optional>

Truthy to generate or use an existing prepared statement for the SQL being executed via the SQLERPreparedFunction. Prepared statements may help optimize SQL that is executed many times across the same connection with similar or different bind values. Care must be taken not to drain the connection pool since the connection remains open until the SQL executions have completed and unprepare has been called on the SQLERExecResults. returned from the SQLERPreparedFunction call.

dateFormatter function | Boolean <optional>

A function(date) that will be used to format bound dates into string values for SQLERPreparedFunction calls. Set to a truthy value to perform date.toISOString(). Overrides the same option set on SQLERConnectionOptions.

numOfPreparedFuncs Number

The total number of SQLERPreparedFunction(s) that reside within the manager

driverOptions Object <optional>

Options that may override the SQLERConnectionOptions for driverOptions that may be passed into the Manager constructor

Source:
Type:
  • Object

SQLERExecResults

Results returned from invoking a SQLERPreparedFunction.

Properties:
Name Type Attributes Description
rows Array.<Object> | Array.<Stream.Readable> | Array.<Stream.Writable> <optional>

The execution array of model objects representing each row or undefined when executing a non-read SQL statement. When streaming the resulting rows will contain either a stream.Readable[] or a stream.Writable[] instead of the default Object[] rows.

unprepare function <optional>

A no-argument async function that unprepares an outstanding prepared statement. Will not be available when the SQLERPreparedFunction is called when the specified prepareStatement is falsy on the SQLERExecOptions passed into the SQLERPreparedFunction. When a prepared statement is used in conjunction with a SQLERTransaction transactionId on the SQLERExecOptions, unprepare will be implicitly called when transaction.commit or transaction.rollback are called (of course, unprepare can still be explicitly called as well). NOTE: A call to unprepare must be invoked when a prepareStatement is truthy to ensue underlying statements and/or connections are completed and closed.

error Error <optional>

Any caught error that occurred when a SQLERPreparedFunction was invoked with the errorOpts flag set to a truthy value.

raw Object

The raw results from the execution (driver-specific execution results).

Source:
Type:
  • Object

SQLERInitOptions

Options that are used during initialization

Properties:
Name Type Description
numOfPreparedFuncs Number

The total number of SQLERPreparedFunction(s) registered on the Dialect

Source:
Type:
  • Object

SQLERInterpolateFunction(dest, source, interpolatoropt, validatoropt, onlyInterpolatedopt) → {Object}

Interpolates values from a source object to a destination object. When a value is a string surrounded by ${}, it will be assumed to be a interpolated property that resides on another property on the source or an interpolated property on the interpolator. For example source.someProp = '${SOME_VALUE}' will be interpreted as dest.someProp = dest.SOME_VALUE when the interpolator is omitted and dest.someProp = interpolator.SOME_VALUE when an interpolator is specified. Typically only used by implementing Dialect constructors within a SQLERTrack.

Source:
Parameters:
Name Type Attributes Default Description
dest Object

The destination where the sources will be set (also the interpolated source when interpolator is omitted).

source Object

The source of the values to interpolate (e.g. SQLERConnectionOptions, SQLERExecOptions, etc.).

interpolator Object <optional>
dest

An alternative source to use for extracting interpolated values from.

validator SQLERInterpolateValidationFunction <optional>

A validation function for each property/value being interpolated to determine if it will be interolated.

onlyInterpolated Boolean <optional>

Truthy to indicate that the only values that will be set from the source/interpolator will be values that have been interpolated. NOTE: Truthy values will not prevent source/interpolator objects from getting set on dest, just non-interpoalted property values will be skipped (i.e. property values that do not contain ${} interpolation designations).

Returns:
Type:
Object

The passed destination

SQLERInterpolateValidationFunction(srcPropNames, srcPropValue) → {Boolean}

A validation for validating interpolation used by a SQLERInterpolateFunction

Source:
Parameters:
Name Type Description
srcPropNames Array.<String>

Property path(s) to the value being validated (e.g. source.my.path = 123 would equate to a invocation to validator(['my','path'], 123)).

srcPropValue *

The value being validated for interpolation

Returns:
Type:
Boolean

Flag indicating whether or not to include the interpolated property/value

SQLEROperationOptions

Options for operational methods on a Manager (e.g. Manager.init, Manager.state, Manager.close, etc.).

Properties:
Name Type Attributes Description
connections Object <optional>

An object that contains connection names as properties. Each optionally containing an object with errorOpts and/or executeInSeries that will override any global options set directly on the SQLEROperationOptions. For example, opts.connections.myConnection.executeInseries would override opts.executeInSeries for the connection named myConnection, but would use opts.executeInSeries for any other connections that ae not overridden.

executeInSeries Boolean <optional>

Set to truthy to execute the operation in series, otherwise executes operation in parallel.

errorOpts SQLERExecErrorOptions | Boolean <optional>

Set to truthy to return any errors. Otherise throw any errors as they are encountered. options can also be set instead.

Source:
Type:
  • Object

SQLEROperationResults

Results returned from invoking an operational method on a Manager (e.g. Manager.init, Manager.state, Manager.close, etc.).

Properties:
Name Type Description
result Object

An object that contains a property name that matches each connection that was processed (the property value is the number of operations processed per connection).

errors Array.<Error>

Any errors that may have occurred on the operational methods. Should only be populated when SQLEROperationOptions are used with a truthy value set on errorOpts. Each will contain meta properties set by Asynchro.

Source:
Type:
  • Object

SQLERPositionalBindsFunction(sql, bindsObject, bindsArray, placeholderopt) → {String}

Converts a SQL statement that contains named bind parameters into a SQL statement that contains unnamed/positional bind parameters (using ?). Each bound parameter is pushed to the array in the position that corresponds to the position within the SQL statement.

Source:
Parameters:
Name Type Attributes Default Description
sql String

The SQL statement that contains the bind parameters

bindsObject Object

An object that contains the bind parameters as property names/values

bindsArray Array

The array that will be populated with the bind parameters

placeholder String | function <optional>
?

Either a string value that will be used for the postional placeholder or a function(name, index) that returns a value that will be used as the positional placeholder.

Throws:

Thrown when a bound parameter is not within the orgiginating SQL statement

Type
Error
Returns:
Type:
String

The converted SQL statement

(async) SQLERPreparedFunction(optsopt, fragsopt, errorOptsopt) → {SQLERExecResults}

Prepared functions are auto-generated async functions that execute an SQL statement from an SQL file source.

Source:
Parameters:
Name Type Attributes Description
opts SQLERExecOptions <optional>

The SQL execution options

frags Array.<String> <optional>

Consists of any fragment segment names present in the SQL being executed that will be included in the final SQL statement. Any fragments present in the SQL source will be excluded from the final SQL statement when there is no matching fragment name.

errorOpts SQLERExecErrorOptions | Boolean <optional>

Either the error handling options or a boolean flag indicating that any errors that occur during execution should be returned in the SQLERExecResults rather then being thrown.

Returns:
Type:
SQLERExecResults

The execution results

SQLERPrivateOptions

Private options for global Manager use

Properties:
Name Type Attributes Description
username String <optional>

The username to connect to the database

password String <optional>

The password to connect to the database

host String <optional>

The host to connect to for the database

port String <optional>

The port to connect to for the database (when not included in the host)

protocol String <optional>

The protocol to use when connecting to the database

privatePath String <optional>

The private path set by an originating Manager constructor (when not already set) that may be used by an implementing Dialect for private data use (e.g. TNS files, etc.)

Source:
Type:
  • Object

SQLERState

The current state of the managed Dialect

Properties:
Properties
Name Type Attributes Description
pending Number

The number of transactions that are pending commit or roolback plus any prepared statements that are pending unprepare.

connections Object <optional>

The connection state

Name Type Attributes Description
count Number <optional>

The number of connections

inUse Number <optional>

The number of connections that are in use

Source:
Type:
  • Object

SQLERStreamReadableProcessor(opts, readStream, readeropt) → {Stream.Readable}

Function that internally handles the specified SQLERExecOptions stream batch size and emits a batch event when the batch size threshold has been reached on a specified stream.Readable.

Source:
Parameters:
Name Type Attributes Description
opts SQLERExecOptions

The execution options

readStream Stream.Readable

The readable stream that will produce the SQL results

reader SQLERStreamReader <optional>

The function that will process the batch read

Returns:
Type:
Stream.Readable

The readable stream that was passed

(async) SQLERStreamReader(batch) → {*}

Processes a read batch of streamed objects for a particular dialect implementation.

Source:
Parameters:
Name Type Description
batch Array.<Object>

The batch of streamed objects based upon a predefined stream batch size in SQLERExecOptions.

Returns:
Type:
*

A value that will be emitted on a readable stream.

SQLERStreamWritable(opts, writter) → {Stream.Writable}

Function that will generate a Stream.Writable that internally handles the specified SQLERExecOptions stream batch size and emits a batch event when the batch size threshold has been reached.

Source:
Parameters:
Name Type Description
opts SQLERExecOptions

The execution options

writter SQLERStreamWritter

The function that will process the batch write

Returns:
Type:
Stream.Writable

The writable stream that will handle the sqler internals

(async) SQLERStreamWritter(batch) → {*}

Writes a batch of streamed objects for a particular dialect implementation and returns the raw results.

Source:
Parameters:
Name Type Description
batch Array.<Object>

The batch of streamed objects based upon a predefined stream batch size in SQLERExecOptions.

Returns:
Type:
*

The raw batch execution results from the dialect execution and will be emitted on a writable stream.

SQLERTrack

A tracking mechanism that is shared between all Dialect implementations for a given Manager. A track provides a means to share data, etc. from one Dialect to another. Properties can also be added by a Dialect for use in other Dialects. Typically only used by implementing Dialect constructors.

Properties:
Name Type Description
interpolate SQLERInterpolateFunction

An interpolation function that can be used by Dialect implementations to interpolate configuration option values from underlying drivers within a Dialect (immutable). The convenience of doing so negates the need for an application that uses a Manager to import/require a database driver just to access driver constants, etc.

positionalBinds SQLERPositionalBindsFunction

A function that will convert an SQL statement with named binds into positional binds.

bindExpansions SQLERBindExpansionFunction

A function that takes an SQL statement with named binds and expands the bind variables into an array of values when they appear in the SQL statement

readable SQLERStreamReadableProcessor

A function that uses a specified readable stream for batching large number of read executions.

writable SQLERStreamWritable

A function that will generate a writable stream for batching large number of write executions.

Source:
Type:
  • Object

SQLERTransaction

Transaction that symbolizes a unit of work performed within a Manager connection.

Properties:
Properties
Name Type Description
id String

The unique identifier for the transaction.

commit SQLERTransactionCommit

Rolls back the outstanding transaction.

rollback SQLERTransactionRollback

Commits the outstanding transaction.

state Object

The state of the transaction

Name Type Description
committed Number

True when the transaction has been committed.

rolledback Number

True when the transaction has been rolledback.

pending Number

The number of pending SQL statements executed within the scope of the given transaction that have not been committed or rolledback.

isReleased Boolean

Truthy when the transaction has been released.

Source:
Type:
  • Object

(async) SQLERTransactionCommit(isReleaseopt)

Function that commits an outstanding transaction.

Source:
Parameters:
Name Type Attributes Description
isRelease Boolean <optional>

When truthy, releases the connection back to the connection pool after a commit has been performed. It's essential that connections be released back into the connection pool to ensure that there are enough available connections for other independent executions that use the same pool.

SQLERTransactionOptions

Options for a SQLERTransaction that can be passed into a manager.connectionName.beginTransaction(transactionDriverOptions) function.

Source:
Type:
  • Object

(async) SQLERTransactionRollback(isReleaseopt)

Function that rolls back an outstanding transaction.

Source:
Parameters:
Name Type Attributes Description
isRelease Boolean <optional>

When truthy, releases the connection back to the connection pool after a rollback has been performed. It's essential that connections be released back into the connection pool to ensure that there are enough available connections for other independent executions that use the same pool.

SQLERUniversalOptions

The universal configuration that, for security and sharing purposes, remains external to an application

Properties:
Name Type Description
db Object

The database options that contain private sensitive configuration. Each property should correspond to a SQLERPrivateOptions instance and the property name should be linked to a SQLERConnectionOptions id within conf.db.connections. Each SQLERPrivateOptions instance will be used to connect to the underlying database (e.g. { db: myConnId: { host: "someDbhost.example.com", username: "someUser", password: "somePass" } })

Source:
Type:
  • Object

9.0.0 (2021-08-18)

Full Changelog

Breaking Changes:

Features: