Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ orm.connect("mysql://username:password@host/database", function (err, db) {
});
```

#### ES6: await/async
```js
await db.transactionAsync(async () => {
let persons = await Person.findAsync({ surname: "Doe" })

for (let person of persons) {
await person.removeAsync();
}
});
```
**make sure use await in `db.transactionAsync` to catch error when function scope throw error**
**and make sure function scope use async and await to ensure commit, or catch error throw then to rollback**
15 changes: 15 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ function Plugin(db) {
});
};

db.transactionAsync = async function (cb) {
await db.driver.execQueryAsync("BEGIN");
debug("init transaction!");

try {
await cb();
debug("transaction committed!");
await db.driver.execQueryAsync("COMMIT");
} catch(err) {
debug("transaction rolled back!");
await db.driver.execQueryAsync("ROLLBACK");
throw err;
}
}

return {
define: function (Model) {
}
Expand Down