The SQL statement to be executed
The callback to execute once the request has been fully completed.
Optionaloptions: RequestOptionsOptionalshouldOptional[captureThe Symbol.for('nodejs.rejection') method is called in case a
promise rejection happens when emitting an event and
captureRejections is enabled on the emitter.
It is possible to use events.captureRejectionSymbol in
place of Symbol.for('nodejs.rejection').
import { EventEmitter, captureRejectionSymbol } from 'node:events';
class MyClass extends EventEmitter {
constructor() {
super({ captureRejections: true });
}
[captureRejectionSymbol](err, event, ...args) {
console.log('rejection happened for', event, 'with', err, ...args);
this.destroy(err);
}
destroy(err) {
// Tear the resource down here.
}
}
The parameter name. This should correspond to a parameter in the SQL, or a parameter that a called procedure expects.
One of the supported data types.
Optionalvalue: unknownThe value that the parameter is to be given. The Javascript type of the argument should match that documented for data types
Optionaloptions: Readonly<ParameterOptions> | nullAdditional type options. Optional.
The parameter name. This should correspond to a parameter in the SQL,
or a parameter that a called procedure expects. The name should not start with @.
One of the supported data types.
Optionalvalue: unknownThe value that the parameter is to be given. The Javascript type of the argument should match that documented for data types.
A varchar(max), nvarchar(max) or varbinary(max) value, or the rows
of a table-valued parameter, can also be given as an async iterable
(e.g. a Readable) of chunks, or of rows. The source is read while the
request is being sent, so it does not have to fit in memory, and a
request that carries one can be executed only once.
String chunks may split UTF-16 surrogate pairs; the pairs are preserved
across chunk boundaries, including empty chunks. An unpaired surrogate
is handled by the type's encoding as it is for an in-memory value.
A Buffer chunk of 8 KB or more is sent by
reference, as socket.write would send it, so a source must not reuse
or modify a buffer it has yielded until the request has completed.
Optionaloptions: Readonly<ParameterOptions> | nullAdditional type options. Optional.
Cancels a request while waiting for a server response.
Returns an array listing the events for which the emitter has registered listeners.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns the number of listeners listening for the event named eventName.
If listener is provided, it will return how many times the listener is found
in the list of the listeners of the event.
The name of the event being listened for
Optionallistener: (...args: any[]) => voidThe event handler function
Returns a copy of the array of listeners for the event named eventName.
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
This event, describing result set columns, will be emitted before row events are emitted. This event may be emitted multiple times when more than one recordset is produced by the statement.
An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.
The request has been prepared and can be used in subsequent calls to execute and unprepare.
The request encountered an error and has not been prepared.
A row resulting from execution of the SQL statement.
An array or object (depends on ConnectionOptions.useColumnNames), where the columns can be accessed by index/name.
Each column has two properties, metadata and value:
metadata
The same data that is exposed in the columnMetadata event.
value
The column's value. It will be null for a NULL.
If there are multiple columns with the same name, then this will be an array of the values.
All rows from a result set have been provided (through row events).
This token is used to indicate the completion of a SQL statement.
As multiple SQL statements can be sent to the server in a single SQL batch, multiple done can be generated.
An done event is emitted for each SQL statement in the SQL batch except variable declarations.
For execution of SQL statements within stored procedures, doneProc and doneInProc events are used in place of done.
If you are using Connection.execSql then SQL server may treat the multiple calls with the same query as a stored procedure.
When this occurs, the doneProc and doneInProc events may be emitted instead. You must handle both events to ensure complete coverage.
The number of result rows. May be undefined if not available.
If there are more results to come (probably because multiple statements are being executed), then true.
Optionalrst: any[]Rows as a result of executing the SQL statement.
Will only be available if Connection's ConnectionOptions.rowCollectionOnDone is true.
request.on('doneInProc', function (rowCount, more, rows) { });
Indicates the completion status of a SQL statement within a stored procedure. All rows from a statement
in a stored procedure have been provided (through row events).
This event may also occur when executing multiple calls with the same query using Connection.execSql.
The number of result rows. May be undefined if not available.
If there are more results to come (probably because multiple statements are being executed), then true.
Optionalrst: any[]Rows as a result of executing the SQL statement.
Will only be available if Connection's ConnectionOptions.rowCollectionOnDone is true.
Indicates the completion status of a stored procedure. This is also generated for stored procedures
executed through SQL statements.
This event may also occur when executing multiple calls with the same query using Connection.execSql.
The number of result rows. May be undefined if not available.
If there are more results to come (probably because multiple statements are being executed), then true.
Optionalrst: any[]Rows as a result of executing the SQL statement.
Will only be available if Connection's ConnectionOptions.rowCollectionOnDone is true.
A value for an output parameter (that was added to the request with addOutputParameter).
See also Using Parameters.
The parameter name. (Does not start with '@'.)
The parameter's output value.
The same data that is exposed in the columnMetadata event.
This event gives the columns by which data is ordered, if ORDER BY clause is executed in SQL Server.
An array of column numbers in the result set by which data is ordered.
This event gives the names of the base tables of the result set, if the query was
executed in browse mode (e.g. with a FOR BROWSE clause or SET NO_BROWSETABLE ON).
The names of the base tables referenced in the query, in the order the
colInfo event's tableNum values refer to them. Each name is given
as its individual parts (e.g. ['dbo', 'employees']).
This event describes how the columns of the result set map back to the base tables
given in the tabName event, if the query was executed in browse mode (e.g. with a
FOR BROWSE clause or SET NO_BROWSETABLE ON).
One entry for each column in the result set, describing the base table the column was derived from.
This event, describing result set columns, will be emitted before row events are emitted. This event may be emitted multiple times when more than one recordset is produced by the statement.
An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.
This event, describing result set columns, will be emitted before row events are emitted. This event may be emitted multiple times when more than one recordset is produced by the statement.
An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.
This event, describing result set columns, will be emitted before row events are emitted. This event may be emitted multiple times when more than one recordset is produced by the statement.
An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.
This event, describing result set columns, will be emitted before row events are emitted. This event may be emitted multiple times when more than one recordset is produced by the statement.
An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.
Adds a one-time listener function for the event named eventName. The
next time eventName is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The
emitter.prependOnceListener() method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a
The name of the event.
The callback function
Adds the listener function to the beginning of the listeners array for the
event named eventName. No checks are made to see if the listener has
already been added. Multiple calls passing the same combination of eventName
and listener will result in the listener being added, and called, multiple
times.
server.prependListener('connection', (stream) => {
console.log('someone connected!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
The name of the event.
The callback function
Adds a one-time listener function for the event named eventName to the
beginning of the listeners array. The next time eventName is triggered, this
listener is removed, and then invoked.
server.prependOnceListener('connection', (stream) => {
console.log('Ah, we have our first user!');
});
Returns a reference to the EventEmitter, so that calls can be chained.
The name of the event.
The callback function
Returns a copy of the array of listeners for the event named eventName,
including any wrappers (such as those created by .once()).
import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));
// Returns a new Array with a function `onceWrapper` which has a property
// `listener` which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];
// Logs "log once" to the console and does not unbind the `once` event
logFnWrapper.listener();
// Logs "log once" to the console and removes the listener
logFnWrapper();
emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by `.on()` above
const newListeners = emitter.rawListeners('log');
// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');
Removes all listeners, or those of the specified eventName.
It is bad practice to remove listeners added elsewhere in the code,
particularly when the EventEmitter instance was created by some other
component or module (e.g. sockets or file streams).
Returns a reference to the EventEmitter, so that calls can be chained.
OptionaleventName: string | symbolRemoves the specified listener from the listener array for the event named
eventName.
const callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
removeListener() will remove, at most, one instance of a listener from the
listener array. If any single listener has been added multiple times to the
listener array for the specified eventName, then removeListener() must be
called multiple times to remove each instance.
Once an event is emitted, all listeners attached to it at the
time of emitting are called in order. This implies that any
removeListener() or removeAllListeners() calls after emitting and
before the last listener finishes execution will not remove them from
emit() in progress. Subsequent events behave as expected.
import { EventEmitter } from 'node:events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
const callbackA = () => {
console.log('A');
myEmitter.removeListener('event', callbackB);
};
const callbackB = () => {
console.log('B');
};
myEmitter.on('event', callbackA);
myEmitter.on('event', callbackB);
// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
// A
// B
// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
// A
Because listeners are managed using an internal array, calling this will
change the position indexes of any listener registered after the listener
being removed. This will not impact the order in which listeners are called,
but it means that any copies of the listener array as returned by
the emitter.listeners() method will need to be recreated.
When a single function has been added as a handler multiple times for a single
event (as in the example below), removeListener() will remove the most
recently added instance. In the example the once('ping')
listener is removed:
import { EventEmitter } from 'node:events';
const ee = new EventEmitter();
function pong() {
console.log('pong');
}
ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);
ee.emit('ping');
ee.emit('ping');
Returns a reference to the EventEmitter, so that calls can be chained.
Resumes the flow of data from the database. If this request is not in a paused state, calling resume has no effect.
By default EventEmitters will print a warning if more than 10 listeners are
added for a particular event. This is a useful default that helps finding
memory leaks. The emitter.setMaxListeners() method allows the limit to be
modified for this specific EventEmitter instance. The value can be set to
Infinity (or 0) to indicate an unlimited number of listeners.
Returns a reference to the EventEmitter, so that calls can be chained.
Sets a timeout for this request.
Optionaltimeout: numberThe number of milliseconds before the request is considered failed,
or 0 for no timeout. When no timeout is set for the request,
the ConnectionOptions.requestTimeout of the Connection is used.