UNPKG

ezito-sqlite

Version:

The fastest and simplest async and sync library for SqLite3 in Node.js.

31 lines (26 loc) 696 B
function transaction(Database){ const args = Array.prototype.slice.call(arguments).slice(1); const callback = typeof args[0] == "function" ? args[0] : null; let before, after, undo; if (this._transaction) { before = ()=>this.run('SAVEPOINT `\t_es.\t`'); after = ()=>this.run('RELEASE `\t_es.\t`'); undo = ()=>this.run('ROLLBACK TO `\t_es.\t`'); } else { before = ()=>this.run('BEGIN'); after = ()=>this.run('COMMIT'); undo = ()=>this.run('ROLLBACK'); } before(); try { const result = callback(); after() return result; } catch (ex) { if (this._transaction) { undo(); } throw ex; } } module.exports = transaction;