@ihago.santos/fluentsql 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/fluentSQL.js +7 -7
- package/test/fluentSQL.test.js +2 -2
package/package.json
CHANGED
package/src/fluentSQL.js
CHANGED
|
@@ -4,7 +4,7 @@ export default class FluentSQLBuilder {
|
|
|
4
4
|
#select = []
|
|
5
5
|
#where = []
|
|
6
6
|
#orderBy = ''
|
|
7
|
-
#
|
|
7
|
+
#countBy = ''
|
|
8
8
|
|
|
9
9
|
constructor({ database }) {
|
|
10
10
|
this.#database = database
|
|
@@ -43,8 +43,8 @@ export default class FluentSQLBuilder {
|
|
|
43
43
|
return this
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
this.#
|
|
46
|
+
countBy(field) {
|
|
47
|
+
this.#countBy = field
|
|
48
48
|
return this
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -82,12 +82,12 @@ export default class FluentSQLBuilder {
|
|
|
82
82
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
#
|
|
86
|
-
if(!this.#
|
|
85
|
+
#performCount(results) {
|
|
86
|
+
if(!this.#countBy) return results
|
|
87
87
|
|
|
88
88
|
const accumulator = {}
|
|
89
89
|
for(const result of results) {
|
|
90
|
-
const targetField = result[this.#
|
|
90
|
+
const targetField = result[this.#countBy]
|
|
91
91
|
accumulator[targetField] = accumulator[targetField] ?? 0
|
|
92
92
|
accumulator[targetField] += 1
|
|
93
93
|
}
|
|
@@ -106,7 +106,7 @@ export default class FluentSQLBuilder {
|
|
|
106
106
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
const grouped = this.#
|
|
109
|
+
const grouped = this.#performCount(results)
|
|
110
110
|
const orderedResult = this.#performOrderBy(grouped)
|
|
111
111
|
return orderedResult
|
|
112
112
|
}
|
package/test/fluentSQL.test.js
CHANGED
|
@@ -84,9 +84,9 @@ describe('Test Suite for FluentSQL Builder', () => {
|
|
|
84
84
|
expect(result).toStrictEqual(expected)
|
|
85
85
|
})
|
|
86
86
|
|
|
87
|
-
test('#
|
|
87
|
+
test('#countBy given a collection it should group results by field', () => {
|
|
88
88
|
const result = FluentSQLBuilder.for(data)
|
|
89
|
-
.
|
|
89
|
+
.countBy('category')
|
|
90
90
|
.build()
|
|
91
91
|
|
|
92
92
|
const expected = [
|