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
e18947da
Commit
e18947da
authored
Mar 29, 2020
by
Milan Wikarski
Browse files
Added tape class
parent
0ddcce02
Changes
6
Hide whitespace changes
Inline
Side-by-side
0
deleted
100644 → 0
View file @
0ddcce02
src/classes/query.class.js
View file @
e18947da
...
...
@@ -15,7 +15,10 @@ class Query {
this
.
executed
=
false
;
this
.
success
=
null
;
this
.
rows
=
null
;
this
.
response
=
null
;
this
.
err
=
null
;
this
.
_onsuccess
=
null
;
...
...
@@ -57,7 +60,6 @@ class Query {
* - returns `true` if successful; `false` otherwise
*
* @returns {Promise.<boolean>} success
* @memberof Query
*/
async
execute
()
{
if
(
!
this
.
canExecute
)
throw
new
Error
(
'
This query cannot be executed yet
'
);
...
...
@@ -65,13 +67,16 @@ class Query {
return
new
Promise
(
resolve
=>
{
this
.
_db
.
query
(
this
.
query
,
this
.
data
,
(
err
,
res
)
=>
{
this
.
executed
=
true
;
this
.
response
=
res
;
if
(
err
)
{
this
.
success
=
false
;
this
.
err
=
err
;
resolve
(
false
);
}
else
{
this
.
rows
=
res
.
rows
;
this
.
success
=
true
;
if
(
this
.
_onsuccess
instanceof
Function
)
{
this
.
_onsuccess
();
...
...
src/classes/query.specific.class.js
View file @
e18947da
...
...
@@ -19,7 +19,6 @@ class SpecificQuery extends Query {
*
* @param {Database} db
* @param {String} table
* @memberof SpecificQuery
*/
constructor
(
db
,
table
)
{
super
(
db
);
...
...
src/classes/tape.class.js
0 → 100644
View file @
e18947da
/** @typedef {import("./query.class")} Query */
/** @typedef {import("./query.generic.class")} GenericQuery */
/** @typedef {import("../database.class")} Database */
class
Tape
{
/**
* Creates an instance of Tape.
* @param {Database} db
*/
constructor
(
db
)
{
this
.
_db
=
db
;
/** @type {Array.<Query>} */
this
.
commands
=
[];
}
/**
* Records a new command to the tape
* @param {Query} query
*/
add
(
query
)
{
this
.
commands
.
push
(
query
);
return
this
;
}
/**
* Builds the query
* @returns {GenericQuery} query
*/
build
()
{
return
this
.
_db
.
query
(
this
.
commands
.
map
(
item
=>
item
.
query
).
join
(
'
;
\n
'
));
}
}
module
.
exports
=
Tape
;
src/database.class.js
View file @
e18947da
...
...
@@ -4,6 +4,7 @@ const Select = require('./classes/select.class');
const
Insert
=
require
(
'
./classes/insert.class
'
);
const
Delete
=
require
(
'
./classes/delete.class
'
);
const
Update
=
require
(
'
./classes/update.class
'
);
const
Tape
=
require
(
'
./classes/tape.class
'
);
/**
* Class for communication with PostgreSQL database
...
...
@@ -22,7 +23,6 @@ class Database {
*/
constructor
({
host
,
port
,
database
,
user
,
password
})
{
this
.
_db
=
new
Pool
({
host
,
port
,
database
,
user
,
password
});
this
.
_transaction
=
false
;
}
/**
...
...
@@ -39,7 +39,7 @@ class Database {
*
* @param {String} query
* @param {Array.<any>} data
* @returns {Query} query
* @returns {
Generic
Query} query
*/
query
(
query
,
data
=
[])
{
return
new
GenericQuery
(
this
,
query
,
data
);
...
...
@@ -80,7 +80,6 @@ class Database {
*
* @param {String} table
* @returns {Update} update
* @memberof Database
*/
update
(
table
)
{
return
new
Update
(
this
,
table
);
...
...
@@ -92,11 +91,7 @@ class Database {
* @returns {Promise.<GenericQuery>} success
*/
begin
()
{
if
(
this
.
_transaction
)
{
throw
new
Error
(
'
A transaction is already active
'
);
}
return
this
.
query
(
'
BEGIN
'
).
onsuccess
(()
=>
(
this
.
_transaction
=
true
));
return
this
.
query
(
'
BEGIN
'
);
}
/**
...
...
@@ -105,11 +100,16 @@ class Database {
* @returns {Promise.<GenericQuery>} success
*/
commit
()
{
if
(
!
this
.
_transaction
)
{
throw
new
Error
(
'
No transaction is active
'
);
}
return
this
.
query
(
'
COMMIT
'
);
}
return
this
.
query
(
'
COMMIT
'
).
onsuccess
(()
=>
(
this
.
_transaction
=
false
));
/**
* Creates and returns a new `Tape` instance
*
* @returns {Tape} tape
*/
tape
()
{
return
new
Tape
(
this
);
}
}
...
...
tests/test.js
View file @
e18947da
...
...
@@ -65,9 +65,34 @@ const update = async () => {
};
(
async
()
=>
{
await
select
();
await
update
();
await
select
();
// await select();
// await update();
// await select();
const
query
=
db
.
tape
()
.
add
(
db
.
begin
())
.
add
(
db
.
update
(
'
lorem
'
)
.
set
({
foo
:
'
Updated Čau
'
,
bar
:
420
})
.
where
({
id
:
4
})
)
.
add
(
db
.
insert
(
'
lorem
'
).
values
([
{
foo
:
'
Ahoj
'
,
bar
:
12
},
{
foo
:
'
Čau
'
,
bar
:
52
}
])
)
.
add
(
db
.
select
(
'
lorem
'
).
columns
(
'
*
'
))
.
add
(
db
.
commit
())
.
build
();
if
(
await
query
.
execute
())
{
console
.
log
(
query
.
response
);
}
else
{
console
.
log
(
query
.
err
);
}
process
.
exit
();
})();
...
...
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