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
a3b69ec6
Commit
a3b69ec6
authored
Feb 14, 2020
by
Milan Wikarski
Browse files
Added UPDATE query
parent
3198f41c
Changes
14
Hide whitespace changes
Inline
Side-by-side
src/classes/insert.class.js
View file @
a3b69ec6
...
...
@@ -4,7 +4,7 @@ const { ValuesMixin } = require('../mixins');
class
Insert
extends
ValuesMixin
(
SpecificQuery
)
{
get
canExecute
()
{
return
!
is
.
null
(
this
.
_values
);
return
is
.
not
.
null
(
this
.
_values
);
}
get
query
()
{
...
...
src/classes/query.class.js
View file @
a3b69ec6
...
...
@@ -36,7 +36,7 @@ class Query {
* @readonly
*/
get
canExecute
()
{
return
!
is
.
emptyString
(
this
.
query
);
return
is
.
not
.
emptyString
(
this
.
query
);
}
/**
...
...
src/classes/select.class.js
View file @
a3b69ec6
...
...
@@ -12,7 +12,7 @@ class Select extends WhereMixin(
OrderByMixin
(
OffsetMixin
(
LimitMixin
(
ColumnsMixin
(
SpecificQuery
))))
)
{
get
canExecute
()
{
return
!
is
.
null
(
this
.
_columns
);
return
is
.
not
.
null
(
this
.
_columns
);
}
get
query
()
{
...
...
src/classes/update.class.js
0 → 100644
View file @
a3b69ec6
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
SpecificQuery
=
require
(
'
./query.specific.class
'
);
const
{
SetMixin
,
WhereMixin
}
=
require
(
'
../mixins
'
);
class
Update
extends
WhereMixin
(
SetMixin
(
SpecificQuery
))
{
get
canExecute
()
{
return
is
.
not
.
null
(
this
.
_set
);
}
get
query
()
{
return
`UPDATE
${
this
.
table
}
${
this
.
setSQL
}
${
this
.
whereSQL
}
`
;
}
}
module
.
exports
=
Update
;
src/database.class.js
View file @
a3b69ec6
...
...
@@ -3,6 +3,7 @@ const GenericQuery = require('./classes/query.specific.class');
const
Select
=
require
(
'
./classes/select.class
'
);
const
Insert
=
require
(
'
./classes/insert.class
'
);
const
Delete
=
require
(
'
./classes/delete.class
'
);
const
Update
=
require
(
'
./classes/update.class
'
);
/**
* Class for communication with PostgreSQL database
...
...
@@ -63,6 +64,17 @@ class Database {
delete
(
table
)
{
return
new
Delete
(
this
,
table
);
}
/**
* Creates an Update object
*
* @param {String} table
* @returns {Update} update
* @memberof Database
*/
update
(
table
)
{
return
new
Update
(
this
,
table
);
}
}
module
.
exports
=
Database
;
src/mixins/columns.mixin.js
View file @
a3b69ec6
...
...
@@ -22,7 +22,7 @@ const ColumnsMixin = C =>
columns
(
columns
)
{
columns
=
force
.
array
(
columns
);
if
(
!
is
.
array
(
columns
,
'
string
'
))
{
if
(
is
.
not
.
array
(
columns
,
'
string
'
))
{
throw
TypeError
(
'
columns is of invalid type
'
);
}
...
...
src/mixins/index.js
View file @
a3b69ec6
...
...
@@ -4,5 +4,6 @@ module.exports = {
OffsetMixin
:
require
(
'
./offset.mixin
'
),
ValuesMixin
:
require
(
'
./values.mixin
'
),
WhereMixin
:
require
(
'
./where.mixin
'
),
OrderByMixin
:
require
(
'
./order-by.mixin
'
)
OrderByMixin
:
require
(
'
./order-by.mixin
'
),
SetMixin
:
require
(
'
./set.mixin
'
)
};
src/mixins/limit.mixin.js
View file @
a3b69ec6
...
...
@@ -22,7 +22,7 @@ const LimitMixin = C =>
* @param {number} limit
*/
limit
(
limit
)
{
if
(
!
is
.
numeric
(
limit
))
{
if
(
is
.
not
.
numeric
(
limit
))
{
throw
TypeError
(
'
limit is of invalid type
'
);
}
...
...
src/mixins/offset.mixin.js
View file @
a3b69ec6
...
...
@@ -22,7 +22,7 @@ const OffsetMixin = C =>
* @param {number} offset
*/
offset
(
offset
)
{
if
(
!
is
.
numeric
(
offset
))
{
if
(
is
.
not
.
numeric
(
offset
))
{
throw
TypeError
(
'
offset is of invalid type
'
);
}
...
...
src/mixins/order-by.mixin.js
View file @
a3b69ec6
...
...
@@ -23,7 +23,7 @@ const OrderByMixin = C =>
orderBy
(
orderBy
)
{
orderBy
=
force
.
array
(
orderBy
);
if
(
!
is
.
array
(
orderBy
,
'
keyValuePair
'
))
{
if
(
is
.
not
.
array
(
orderBy
,
'
keyValuePair
'
))
{
throw
TypeError
(
'
orderBy is of invalid type
'
);
}
...
...
src/mixins/set.mixin.js
0 → 100644
View file @
a3b69ec6
const
{
is
,
force
}
=
require
(
'
@creanet/js-type
'
);
const
SetMixin
=
C
=>
class
SetMixin
extends
C
{
constructor
()
{
super
(...
arguments
);
this
.
_set
=
null
;
}
get
setSQL
()
{
const
set
=
Object
.
entries
(
this
.
_set
)
.
map
(([
key
,
value
])
=>
`
${
key
}
='
${
value
}
'`
)
.
join
(
'
,
'
);
return
`SET
${
set
}
`
;
}
/**
* Sets the values of set. Valid values are:
* - `{Object}` - one record with key:value pairs
*
* @param {Object|Array<Object>} values
*/
set
(
set
)
{
if
(
is
.
not
.
object
(
set
))
{
throw
new
TypeError
(
'
values is of invalid type
'
);
}
this
.
_set
=
set
;
return
this
;
}
};
module
.
exports
=
SetMixin
;
src/mixins/values.mixin.js
View file @
a3b69ec6
...
...
@@ -4,7 +4,7 @@ const ValuesMixin = C =>
class
ValuesMixin
extends
C
{
constructor
()
{
super
(...
arguments
);
this
.
_
offset
=
null
;
this
.
_
values
=
null
;
}
get
valuesSQL
()
{
...
...
@@ -27,7 +27,7 @@ const ValuesMixin = C =>
values
(
values
)
{
values
=
force
.
array
(
values
);
if
(
!
is
.
array
(
values
,
'
object
'
))
{
if
(
is
.
not
.
array
(
values
,
'
object
'
))
{
throw
new
TypeError
(
'
values is of invalid type
'
);
}
...
...
src/mixins/where.mixin.js
View file @
a3b69ec6
...
...
@@ -22,12 +22,12 @@ const WhereMixin = C =>
* @param {Array<Object>} where
*/
where
(
where
)
{
if
(
!
is
(
where
,
[
'
string
'
,
'
object
'
]))
if
(
is
.
not
(
where
,
[
'
string
'
,
'
object
'
]))
throw
new
TypeError
(
'
where is of invalid type
'
);
if
(
is
.
object
(
where
))
{
this
.
_where
=
Object
.
entries
(
where
)
.
map
(([
key
,
value
])
=>
`
${
key
}
=
'
${
value
}
'`
)
.
map
(([
key
,
value
])
=>
`
${
key
}
=
'
${
value
}
'`
)
.
join
(
'
AND
'
);
}
else
{
this
.
_where
=
where
;
...
...
tests/test.js
View file @
a3b69ec6
...
...
@@ -11,13 +11,7 @@ const db = new DB({
const
select
=
async
()
=>
{
console
.
log
(
'
SELECT
'
);
const
select
=
db
.
select
(
'
lorem
'
)
.
columns
(
'
*
'
)
.
orderBy
({
id
:
'
DESC
'
})
.
where
(
`"id" != '2'`
)
.
limit
(
1
)
.
offset
(
1
);
const
select
=
db
.
select
(
'
lorem
'
).
columns
(
'
*
'
);
console
.
log
(
select
.
query
);
if
(
await
select
.
execute
())
{
...
...
@@ -51,13 +45,24 @@ const del = async () => {
}
};
(
async
()
=>
{
await
select
();
// await insert();
// await select();
const
update
=
async
()
=>
{
console
.
log
(
'
UPDATE
'
);
const
update
=
db
.
update
(
'
lorem
'
)
.
set
({
foo
:
'
Updated Čau
'
,
bar
:
420
})
.
where
({
id
:
4
});
await
del
();
console
.
log
(
update
.
query
);
if
(
await
update
.
execute
())
{
console
.
log
(
update
.
rows
);
}
else
{
console
.
log
(
update
.
err
);
}
};
(
async
()
=>
{
await
select
();
await
update
();
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