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
8eac500f
Commit
8eac500f
authored
Feb 05, 2020
by
Milan Wikarski
Browse files
Added DELETE command
parent
6305fcbf
Changes
5
Show whitespace changes
Inline
Side-by-side
classes/delete.class.js
0 → 100644
View file @
8eac500f
const
Query
=
require
(
'
./query.class
'
);
const
{
Pool
}
=
require
(
'
pg
'
);
const
{
WhereMixin
}
=
require
(
'
../mixins
'
);
/**
* Class for execution of DELETE queries
*
* @class Insert
*/
class
Delete
extends
WhereMixin
(
Query
)
{
/**
* Creates an instance of Delete.
*
* @param {Pool} db
* @param {string} table
*/
constructor
(
db
,
table
)
{
super
(
db
);
this
.
table
=
table
;
}
get
query
()
{
return
`DELETE FROM
${
this
.
table
}
${
this
.
whereSQL
}
`
;
}
}
module
.
exports
=
Delete
;
classes/index.js
View file @
8eac500f
module
.
exports
=
{
Query
:
require
(
'
./query.class
'
),
Select
:
require
(
'
./select.class
'
),
Insert
:
require
(
'
./insert.class
'
)
Insert
:
require
(
'
./insert.class
'
),
Delete
:
require
(
'
./delete.class
'
)
};
classes/query.class.js
View file @
8eac500f
...
...
@@ -35,7 +35,7 @@ class Query {
* @readonly
*/
get
canExecute
()
{
return
this
.
_
query
!=
''
;
return
this
.
query
!=
''
;
}
/**
...
...
index.js
View file @
8eac500f
const
{
Pool
}
=
require
(
'
pg
'
);
const
{
Query
,
Select
,
Insert
}
=
require
(
'
./classes
'
);
const
{
Query
,
Select
,
Insert
,
Delete
}
=
require
(
'
./classes
'
);
/**
* Class for communication with PostgreSQL database
...
...
@@ -43,12 +43,23 @@ class DB {
/**
* Creates an Insert object
*
* @param {string} table
* @returns {Insert} insert
*/
insert
(
table
)
{
return
new
Insert
(
this
.
_db
,
table
);
}
/**
* Creates a Delete object
*
* @param {string} table
* @returns {Delete} delete
*/
delete
(
table
)
{
return
new
Delete
(
this
.
_db
,
table
);
}
}
module
.
exports
=
DB
;
tests/test.js
View file @
8eac500f
...
...
@@ -11,10 +11,7 @@ const db = new DB({
const
select
=
async
()
=>
{
console
.
log
(
'
SELECT
'
);
const
select
=
db
.
select
(
'
lorem
'
)
.
columns
(
'
*
'
)
.
where
(
'
id > 2
'
);
const
select
=
db
.
select
(
'
lorem
'
).
columns
(
'
*
'
);
console
.
log
(
select
.
query
);
if
(
await
select
.
execute
())
{
...
...
@@ -37,11 +34,26 @@ const insert = async () => {
}
};
const
del
=
async
()
=>
{
console
.
log
(
'
DELETE
'
);
const
del
=
db
.
delete
(
'
lorem
'
).
where
({
bar
:
12
});
if
(
await
del
.
execute
())
{
console
.
log
(
del
.
rows
);
}
else
{
console
.
log
(
del
.
err
);
}
};
(
async
()
=>
{
await
select
();
// await insert();
// await select();
await
del
();
await
select
();
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