Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
NPM Packages
js-db
Commits
5a94b249
Commit
5a94b249
authored
Mar 28, 2020
by
Milan Wikarski
Browse files
Added integration for BEGIN and COMMIT
parent
4c1ad910
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/database.class.js
View file @
5a94b249
...
...
@@ -22,6 +22,16 @@ class Database {
*/
constructor
({
host
,
port
,
database
,
user
,
password
})
{
this
.
_db
=
new
Pool
({
host
,
port
,
database
,
user
,
password
});
this
.
_transaction
=
false
;
}
/**
* Returns `true` if a transaction is active; `false` otherwise
*
* @readonly
*/
get
transaction
()
{
return
this
.
_transaction
;
}
/**
...
...
@@ -75,6 +85,36 @@ class Database {
update
(
table
)
{
return
new
Update
(
this
,
table
);
}
/**
* Begins a transaction
*
* @returns {Promise.<Boolean>} success
*/
begin
()
{
if
(
this
.
_transaction
)
{
throw
new
Error
(
'
A transaction is already active
'
);
}
return
this
.
query
(
'
BEGIN
'
)
.
execute
()
.
then
(()
=>
(
this
.
_transaction
=
true
));
}
/**
* Commits an active transaction
*
* @returns {Promise.<Boolean>} success
*/
commit
()
{
if
(
!
this
.
_transaction
)
{
throw
new
Error
(
'
No transaction is active
'
);
}
return
this
.
query
(
'
COMMIT
'
)
.
execute
()
.
then
(()
=>
(
this
.
_transaction
=
false
));
}
}
module
.
exports
=
Database
;
tests/test.js
View file @
5a94b249
...
...
@@ -46,6 +46,8 @@ const del = async () => {
};
const
update
=
async
()
=>
{
await
db
.
begin
();
console
.
log
(
'
UPDATE
'
);
const
update
=
db
.
update
(
'
lorem
'
)
...
...
@@ -58,6 +60,8 @@ const update = async () => {
}
else
{
console
.
log
(
update
.
err
);
}
await
db
.
commit
();
};
(
async
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment