How to use the mssql.Transaction function in mssql

To help you get started, we’ve selected a few mssql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_prepared_statement_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var preparedSatement1 = new sql.PreparedStatement(connection);
    var preparedSatement2 = new sql.PreparedStatement(transaction);
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_request_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var request1 = new sql.Request(connection);
    var request2 = new sql.Request(preparedStatment);
    var request3 = new sql.Request(transaction);
    var request4 = new sql.Request();
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
connection.connect().then(() => { });
    connection.close().then(() => { });
    connection.query('SELECT 1').then((recordset) => { });
    connection.query('SELECT 1 as value').then(res => { });
    connection.query`SELECT ${1}`.then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table').then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });

    var preparedStatment = new sql.PreparedStatement(connection);
    preparedStatment.prepare("SELECT @myValue").then(() => { });
    preparedStatment.execute({ myValue: 1 }).then((recordSet) => { });
    preparedStatment.unprepare().then(() => { });

    var transaction = new sql.Transaction(connection);
    transaction.begin().then(() => { });
    transaction.commit().then(() => { });
    transaction.rollback().then(() => { });

    var request = new sql.Request();
    request.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    request.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    request.batch`create procedure #temporary as select * from table;select ${1} as value`.then((recordset) => { });
    request.batch`create procedure #temporary as select * from table;select ${1} as value`.then((recordset) => { });
    request.bulk(new sql.Table("table_name")).then(() => { });
    request.query('SELECT 1').then((recordset) => { });
    request.query`SELECT ${1} as value`.then(res => { });
    request.query('SELECT 1 as value').then(res => { });
    request.query`SELECT ${1}`.then((recordset) => { });
    request.query`SELECT ${1}`.then((recordset) => { });
    request.execute('procedure_name').then((recordset) => { });
github instana / nodejs-sensor / test / tracing / database / mssql / app.js View on Github external
app.post('/transaction-promise', function(req, res) {
  var transaction = new sql.Transaction();
  var results;
  transaction
    .begin()
    .then(function() {
      return new sql.Request(transaction)
        .input('username', sql.NVarChar(40), 'titus')
        .input('email', sql.NVarChar(40), 'titus@flavius.com')
        .query('INSERT INTO UserTable (name, email) VALUES (@username, @email)');
    })
    .then(function() {
      return new sql.Request(transaction).query("SELECT name, email FROM UserTable WHERE name=N'titus'");
    })
    .then(function(_results) {
      results = _results;
    })
    .then(function() {
github totaljs / node-sqlagent / sqlserver.js View on Github external
(Agent.debug || self.debug) && console.log(self.debugname, item.name, item.$query);
			self.$events.query && self.emit('query', item.name, item.$query, item.$params);

			var request = new database.Request(self.$transaction ? self.$transaction : self.db);
			item.$params && prepare_params_request(request, item.$params);

			request.query(item.$query, function(err, rows) {
				self.$bind(item, err, rows ? rows.recordset : []);
				next();
			});
			return;
		}

		if (item.type === 'begin') {
			(Agent.debug || self.debug) && console.log(self.debugname, 'begin transaction');
			self.$transaction = new database.Transaction(self.db);
			self.$transaction.begin(function(err) {
				if (err) {
					self.errors.push(err.message);
					self.command.length = 0;
					next(false);
					return;
				}
				self.isTransaction = true;
				self.isRollback = false;
				next();
			});
			return;
		}

		if (item.type === 'end') {
			self.isTransaction = false;
github alice-si / etheroscope / db / db.js View on Github external
return new Promise(function (resolve, reject) {
      let dataPointsTable = getNewDataPointsTable()
      values.forEach((elem) => {
        dataPointsTable.rows.add(contractAddress, method, elem[2], elem[1])
      })

      var transaction = new mssql.Transaction(pool)
      var request = new mssql.Request(transaction)

      transaction.begin()
        .then(() => {
          return request.bulk(dataPointsTable)
        })
        .then(() => {
          var sql =
            "update variables set cachedFrom='" + from + "' where contractHash='" + contractAddress +
             "' and variableName='" + method + "';" +
            "update variables set cachedUpTo='" + to + "' where contractHash='" + contractAddress +
             "' and variableName='" + method + "';"
          return request.query(sql)
        })
        .then(() => {
          return transaction.commit()
github yinfxs / ibird / lib / utils / db / mssql.js View on Github external
app.getConnection().then(function (conn) {
        const transaction = new sql.Transaction(conn);
        transaction.begin().then(function () {
            new sql.Request(transaction).query(command).then(function () {
                transaction.commit().then(success).catch(function (err) {
                    transaction.rollback().done(error(err));
                })
            }).catch(error);
        }).catch(error);
    }).catch(error);
};
github EasyERP / EasyERP_open_source / helpers / importer / connector / msSql.js View on Github external
function importData(query, callback) {
        var transaction = new sql.Transaction();

        transaction.begin(function (err) {
            var request;

            if (err) {
                return callback(err);
            }

            request = new sql.Request(transaction);
            request.query(query, function (err, recordset) {
                if (err) {
                    return callback(err);
                }

                callback(null, recordset);
                transaction.commit(function () {
github LeanKit-Labs / seriate / src / transactionContext.js View on Github external
}
						}
						args.unshift( isolationLevel );
					}

					if ( this.options.atTransactionStart ) {
						var startStepAction = this.options.atTransactionStart( this.options.dataForHooks );
						this.step( "__beforeHook__", startStepAction );
					}

					if ( this.options.atTransactionEnd ) {
						var endStepAction = this.options.atTransactionEnd( this.options.dataForHooks );
						this.step( "__afterHook__", endStepAction );
					}

					this.transaction = this.transaction || new sql.Transaction( this.connection );
					this.transaction.begin.apply( this.transaction, args );
				},
				success: function() {
github DFEAGILEDEVOPS / MTC / tslib / src / sql / sql.service.ts View on Github external
async modifyWithTransaction (requests: Array): Promise {

    const transaction = new Transaction(await ConnectionPoolService.getInstance())
    await transaction.begin()
    for (let index = 0; index < requests.length; index++) {
      const request = requests[index]
      const modify = async () => {
        const req = new Request(transaction)
        this.addParamsToRequest(request.params, req)
        return req.query(request.sql)
      }
      try {
        await retry(modify, retryConfig, dbLimitReached)
      } catch (error) {
        this.logger.error(`error thrown from statement within transaction:${request.sql}`)
        this.logger.error('rolling back transaction...')
        await transaction.rollback()
        throw error
      }