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
7187a7a6
Commit
7187a7a6
authored
Feb 06, 2020
by
Milan Wikarski
Browse files
Added ORDER BY mixin
parent
6a74ce3a
Changes
13
Hide whitespace changes
Inline
Side-by-side
.prettierrc
0 → 100644
View file @
7187a7a6
{
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}
package.json
View file @
7187a7a6
...
...
@@ -19,8 +19,8 @@
"author"
:
"Milan Wikarski,Marcel Odumorek"
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
pg
"
:
"
^7.8.2
"
,
"
@creanet/js-type
"
:
"
1.0.0
"
"
@creanet/js-type
"
:
"
^1.2.0
"
,
"
pg
"
:
"
^7.8.2
"
},
"devDependencies"
:
{
"
dotenv
"
:
"
^8.2.0
"
...
...
src/classes/insert.class.js
View file @
7187a7a6
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
SpecificQuery
=
require
(
'
./query.specific.class
'
);
const
{
ValuesMixin
}
=
require
(
'
../mixins
'
);
class
Insert
extends
ValuesMixin
(
SpecificQuery
)
{
get
canExecute
()
{
return
this
.
_values
!=
null
;
return
!
is
.
null
(
this
.
_values
)
;
}
get
query
()
{
...
...
src/classes/query.class.js
View file @
7187a7a6
/** @typedef {import('../database.class.js') Database} */
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
class
Query
{
/**
* Creates an instance of Query.
...
...
@@ -34,7 +36,7 @@ class Query {
* @readonly
*/
get
canExecute
()
{
return
this
.
query
!=
''
;
return
!
is
.
emptyString
(
this
.
query
)
;
}
/**
...
...
src/classes/select.class.js
View file @
7187a7a6
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
SpecificQuery
=
require
(
'
./query.specific.class
'
);
const
{
ColumnsMixin
,
LimitMixin
,
OffsetMixin
,
WhereMixin
WhereMixin
,
OrderByMixin
}
=
require
(
'
../mixins
'
);
class
Select
extends
WhereMixin
(
OffsetMixin
(
LimitMixin
(
ColumnsMixin
(
SpecificQuery
)))
OrderByMixin
(
OffsetMixin
(
LimitMixin
(
ColumnsMixin
(
SpecificQuery
)))
)
)
{
get
canExecute
()
{
return
this
.
_columns
!=
null
;
return
!
is
.
null
(
this
.
_columns
)
;
}
get
query
()
{
return
`SELECT
${
this
.
columnsSQL
}
FROM
${
this
.
table
}
${
this
.
whereSQL
}
${
this
.
limitSQL
}
${
this
.
offsetSQL
}
`
;
return
`SELECT
${
this
.
columnsSQL
}
FROM
${
this
.
table
}
${
this
.
whereSQL
}
${
this
.
orderBySQL
}
${
this
.
limitSQL
}
${
this
.
offsetSQL
}
`
;
}
}
...
...
src/mixins/columns.mixin.js
View file @
7187a7a6
const
is
=
require
(
'
@creanet/js-type
'
);
const
{
is
,
force
}
=
require
(
'
@creanet/js-type
'
);
const
ColumnsMixin
=
C
=>
class
ColumnsMixin
extends
C
{
...
...
@@ -8,10 +8,6 @@ const ColumnsMixin = C =>
}
get
columnsSQL
()
{
if
(
is
.
string
(
this
.
_columns
))
{
return
this
.
_columns
;
}
return
this
.
_columns
.
join
(
'
,
'
);
}
...
...
@@ -24,8 +20,11 @@ const ColumnsMixin = C =>
* @param {string|string[]} columns
*/
columns
(
columns
)
{
if
(
!
is
(
columns
,
[
'
string
'
,
'
array
'
]))
columns
=
force
.
array
(
columns
);
if
(
!
is
.
array
(
columns
,
'
string
'
))
{
throw
TypeError
(
'
columns is of invalid type
'
);
}
this
.
_columns
=
columns
;
...
...
src/mixins/index.js
View file @
7187a7a6
...
...
@@ -3,5 +3,6 @@ module.exports = {
LimitMixin
:
require
(
'
./limit.mixin
'
),
OffsetMixin
:
require
(
'
./offset.mixin
'
),
ValuesMixin
:
require
(
'
./values.mixin
'
),
WhereMixin
:
require
(
'
./where.mixin
'
)
WhereMixin
:
require
(
'
./where.mixin
'
),
OrderByMixin
:
require
(
'
./order-by.mixin
'
)
};
src/mixins/limit.mixin.js
View file @
7187a7a6
const
is
=
require
(
'
@creanet/js-type
'
);
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
LimitMixin
=
C
=>
class
LimitMixin
extends
C
{
...
...
@@ -8,7 +8,7 @@ const LimitMixin = C =>
}
get
limitSQL
()
{
if
(
this
.
_limit
==
null
)
{
if
(
is
.
null
(
this
.
_limit
)
)
{
return
''
;
}
...
...
@@ -22,7 +22,9 @@ const LimitMixin = C =>
* @param {number} limit
*/
limit
(
limit
)
{
if
(
!
is
(
limit
,
'
numeric
'
))
throw
TypeError
(
'
limit is of invalid type
'
);
if
(
!
is
.
numeric
(
limit
))
{
throw
TypeError
(
'
limit is of invalid type
'
);
}
this
.
_limit
=
limit
;
...
...
src/mixins/offset.mixin.js
View file @
7187a7a6
const
is
=
require
(
'
@creanet/js-type
'
);
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
OffsetMixin
=
C
=>
class
OffsetMixin
extends
C
{
...
...
@@ -8,7 +8,7 @@ const OffsetMixin = C =>
}
get
offsetSQL
()
{
if
(
this
.
_offset
==
null
)
{
if
(
is
.
null
(
this
.
_offset
)
)
{
return
''
;
}
...
...
@@ -22,7 +22,9 @@ const OffsetMixin = C =>
* @param {number} offset
*/
offset
(
offset
)
{
if
(
!
is
(
offset
,
'
numeric
'
))
throw
TypeError
(
'
offset is of invalid type
'
);
if
(
!
is
.
numeric
(
offset
))
{
throw
TypeError
(
'
offset is of invalid type
'
);
}
this
.
_offset
=
offset
;
...
...
src/mixins/order-by.mixin.js
0 → 100644
View file @
7187a7a6
const
{
is
,
force
}
=
require
(
'
@creanet/js-type
'
);
const
OrderByMixin
=
C
=>
class
OrderByMixin
extends
C
{
constructor
()
{
super
(...
arguments
);
this
.
_orderBy
=
null
;
}
get
orderBySQL
()
{
if
(
is
.
null
(
this
.
_orderBy
))
{
return
''
;
}
// This assumes Object.entries(obj).length === 2
const
orderBy
=
this
.
_orderBy
.
map
(
obj
=>
Object
.
entries
(
obj
)[
0
].
join
(
'
'
))
.
join
(
'
,
'
);
return
`ORDER BY
${
orderBy
}
`
;
}
orderBy
(
orderBy
)
{
orderBy
=
force
.
array
(
orderBy
);
if
(
!
is
.
array
(
orderBy
,
'
keyValuePair
'
))
{
throw
TypeError
(
'
orderBy is of invalid type
'
);
}
this
.
_orderBy
=
orderBy
;
return
this
;
}
};
module
.
exports
=
OrderByMixin
;
src/mixins/values.mixin.js
View file @
7187a7a6
const
is
=
require
(
'
@creanet/js-type
'
);
const
{
is
,
force
}
=
require
(
'
@creanet/js-type
'
);
const
ValuesMixin
=
C
=>
class
ValuesMixin
extends
C
{
...
...
@@ -22,14 +22,13 @@ const ValuesMixin = C =>
* - `{Object}` - one record with key:value pairs
* - `{Array<Object>} - multiple records
*
* @param {Array<Object>} values
* @param {
Object|
Array<Object>} values
*/
values
(
values
)
{
if
(
!
is
(
values
,
[
'
array
'
,
'
object
'
]))
throw
new
TypeError
(
'
values is of invalid type
'
);
values
=
force
.
array
(
values
);
if
(
!
is
.
array
(
values
))
{
values
=
[
values
]
;
if
(
!
is
.
array
(
values
,
'
object
'
))
{
throw
new
TypeError
(
'
values is of invalid type
'
)
;
}
this
.
_values
=
values
;
...
...
src/mixins/where.mixin.js
View file @
7187a7a6
const
is
=
require
(
'
@creanet/js-type
'
);
const
{
is
}
=
require
(
'
@creanet/js-type
'
);
const
WhereMixin
=
C
=>
class
WhereMixin
extends
C
{
...
...
@@ -8,7 +8,7 @@ const WhereMixin = C =>
}
get
whereSQL
()
{
if
(
this
.
_where
==
null
)
{
if
(
is
.
null
(
this
.
_where
)
)
{
return
''
;
}
...
...
tests/test.js
View file @
7187a7a6
...
...
@@ -11,7 +11,13 @@ const db = new DB({
const
select
=
async
()
=>
{
console
.
log
(
'
SELECT
'
);
const
select
=
db
.
select
(
'
lorem
'
).
columns
(
'
*
'
);
const
select
=
db
.
select
(
'
lorem
'
)
.
columns
(
'
*
'
)
.
orderBy
({
id
:
'
DESC
'
})
.
where
(
`"id" != '2'`
)
.
limit
(
1
)
.
offset
(
1
);
console
.
log
(
select
.
query
);
if
(
await
select
.
execute
())
{
...
...
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