@ihago.santos/fluentsql 1.0.1 → 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/jest.config.mjs CHANGED
@@ -1,203 +1,25 @@
1
- /*
2
- * For a detailed explanation regarding each configuration property, visit:
3
- * https://jestjs.io/docs/en/configuration.html
4
- */
5
-
6
- const config = {
7
- // All imported modules in your tests should be mocked automatically
8
- // automock: false,
9
-
10
- // Stop running tests after `n` failures
11
- // bail: 0,
12
-
13
- // The directory where Jest should store its cached dependency information
14
- // cacheDirectory: "/private/var/folders/w0/tmhy86g10cb8k2883jbs63tm0000gn/T/jest_dx",
15
-
16
- // Automatically clear mock calls and instances between every test
1
+ export default {
17
2
  clearMocks: true,
18
-
19
- // Indicates whether the coverage information should be collected while executing the test
20
- // collectCoverage: false,
21
-
22
- // An array of glob patterns indicating a set of files for which coverage information should be collected
23
- // collectCoverageFrom: undefined,
24
-
25
- // The directory where Jest should output its coverage files
26
3
  coverageDirectory: "coverage",
27
-
28
- // An array of regexp pattern strings used to skip coverage collection
29
- // coveragePathIgnorePatterns: [
30
- // "/node_modules/"
31
- // ],
32
-
33
- // Indicates which provider should be used to instrument code for coverage
34
4
  coverageProvider: "v8",
35
-
36
- // A list of reporter names that Jest uses when writing coverage reports
37
5
  coverageReporters: [
38
6
  "json",
39
7
  "text",
40
8
  "lcov",
41
9
  "clover"
42
10
  ],
43
-
44
- // An object that configures minimum threshold enforcement for coverage results
45
11
  coverageThreshold: {
46
12
  global: {
47
- branches: 100,
13
+ branchs: 100,
48
14
  functions: 100,
49
15
  lines: 100,
50
16
  statements: 100
51
17
  }
52
18
  },
53
-
54
- // A path to a custom dependency extractor
55
- // dependencyExtractor: undefined,
56
-
57
- // Make calling deprecated APIs throw helpful error messages
58
- // errorOnDeprecated: false,
59
-
60
- // Force coverage collection from ignored files using an array of glob patterns
61
- // forceCoverageMatch: [],
62
-
63
- // A path to a module which exports an async function that is triggered once before all test suites
64
- // globalSetup: undefined,
65
-
66
- // A path to a module which exports an async function that is triggered once after all test suites
67
- // globalTeardown: undefined,
68
-
69
- // A set of global variables that need to be available in all test environments
70
- // globals: {},
71
-
72
- // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
73
- // maxWorkers: "50%",
74
-
75
- // An array of directory names to be searched recursively up from the requiring module's location
76
- // moduleDirectories: [
77
- // "node_modules"
78
- // ],
79
-
80
- // An array of file extensions your modules use
81
- // moduleFileExtensions: [
82
- // "js",
83
- // "json",
84
- // "jsx",
85
- // "ts",
86
- // "tsx",
87
- // "node"
88
- // ],
89
-
90
- // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
91
- // moduleNameMapper: {},
92
-
93
- // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
94
- // modulePathIgnorePatterns: [],
95
-
96
- // Activates notifications for test results
97
- // notify: false,
98
-
99
- // An enum that specifies notification mode. Requires { notify: true }
100
- // notifyMode: "failure-change",
101
-
102
- // A preset that is used as a base for Jest's configuration
103
- // preset: undefined,
104
-
105
- // Run tests from one or more projects
106
- // projects: undefined,
107
-
108
- // Use this configuration option to add custom reporters to Jest
109
- // reporters: undefined,
110
-
111
- // Automatically reset mock state between every test
112
- // resetMocks: false,
113
-
114
- // Reset the module registry before running each individual test
115
- // resetModules: false,
116
-
117
- // A path to a custom resolver
118
- // resolver: undefined,
119
-
120
- // Automatically restore mock state between every test
121
- // restoreMocks: false,
122
-
123
- // The root directory that Jest should scan for tests and modules within
124
- // rootDir: undefined,
125
-
126
- // A list of paths to directories that Jest should use to search for files in
127
- // roots: [
128
- // "<rootDir>"
129
- // ],
130
-
131
- // Allows you to use a custom runner instead of Jest's default test runner
132
- // runner: "jest-runner",
133
-
134
- // The paths to modules that run some code to configure or set up the testing environment before each test
135
- // setupFiles: [],
136
-
137
- // A list of paths to modules that run some code to configure or set up the testing framework before each test
138
- // setupFilesAfterEnv: [],
139
-
140
- // The number of seconds after which a test is considered as slow and reported as such in the results.
141
- // slowTestThreshold: 5,
142
-
143
- // A list of paths to snapshot serializer modules Jest should use for snapshot testing
144
- // snapshotSerializers: [],
145
-
146
- // The test environment that will be used for testing
19
+ maxWorkers: "50%",
147
20
  testEnvironment: "node",
148
-
149
- // Options that will be passed to the testEnvironment
150
- // testEnvironmentOptions: {},
151
-
152
- // Adds a location field to test results
153
- // testLocationInResults: false,
154
-
155
- // The glob patterns Jest uses to detect test files
156
- // testMatch: [
157
- // "**/__tests__/**/*.[jt]s?(x)",
158
- // "**/?(*.)+(spec|test).[tj]s?(x)"
159
- // ],
160
-
161
- // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
162
- // testPathIgnorePatterns: [
163
- // "/node_modules/"
164
- // ],
165
-
166
- // The regexp pattern or array of patterns that Jest uses to detect test files
167
- // testRegex: [],
168
-
169
- // This option allows the use of a custom results processor
170
- // testResultsProcessor: undefined,
171
-
172
- // This option allows use of a custom test runner
173
- // testRunner: "jasmine2",
174
-
175
- // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
176
- // testURL: "http://localhost",
177
-
178
- // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
179
- // timers: "real",
180
-
181
- // A map from regular expressions to paths to transformers
182
- // transform: undefined,
183
-
184
- // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
185
- // transformIgnorePatterns: [
186
- // "/node_modules/",
187
- // "\\.pnp\\.[^\\/]+$"
188
- // ],
189
-
190
- // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
191
- // unmockedModulePathPatterns: undefined,
192
-
193
- // Indicates whether each individual test should be reported during the run
194
- // verbose: undefined,
195
-
196
- // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
197
- // watchPathIgnorePatterns: [],
198
-
199
- // Whether to use watchman for file crawling
200
- // watchman: true,
21
+ rootDir: '.',
22
+ testMatch: ["<rootDir>/**/*.test.js"],
23
+ collectCoverageFrom: ["src/**/*.js"],
24
+ watchPathIgnorePatterns: ["node_modules"]
201
25
  };
202
-
203
- export default Object.assign({}, config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ihago.santos/fluentsql",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/fluentSQL.js CHANGED
@@ -4,6 +4,7 @@ export default class FluentSQLBuilder {
4
4
  #select = []
5
5
  #where = []
6
6
  #orderBy = ''
7
+ #countBy = ''
7
8
 
8
9
  constructor({ database }) {
9
10
  this.#database = database
@@ -42,6 +43,11 @@ export default class FluentSQLBuilder {
42
43
  return this
43
44
  }
44
45
 
46
+ countBy(field) {
47
+ this.#countBy = field
48
+ return this
49
+ }
50
+
45
51
  #performLimit(results) {
46
52
  return this.#limit && results.length === this.#limit
47
53
  }
@@ -76,6 +82,18 @@ export default class FluentSQLBuilder {
76
82
 
77
83
  }
78
84
 
85
+ #performCount(results) {
86
+ if(!this.#countBy) return results
87
+
88
+ const accumulator = {}
89
+ for(const result of results) {
90
+ const targetField = result[this.#countBy]
91
+ accumulator[targetField] = accumulator[targetField] ?? 0
92
+ accumulator[targetField] += 1
93
+ }
94
+ return [accumulator]
95
+ }
96
+
79
97
  build() {
80
98
  const results = []
81
99
  for (const item of this.#database) {
@@ -88,9 +106,9 @@ export default class FluentSQLBuilder {
88
106
 
89
107
  }
90
108
 
91
-
92
- const finalResult = this.#performOrderBy(results)
93
- return finalResult
109
+ const grouped = this.#performCount(results)
110
+ const orderedResult = this.#performOrderBy(grouped)
111
+ return orderedResult
94
112
  }
95
113
 
96
114
  }
@@ -84,6 +84,21 @@ describe('Test Suite for FluentSQL Builder', () => {
84
84
  expect(result).toStrictEqual(expected)
85
85
  })
86
86
 
87
+ test('#countBy given a collection it should group results by field', () => {
88
+ const result = FluentSQLBuilder.for(data)
89
+ .countBy('category')
90
+ .build()
91
+
92
+ const expected = [
93
+ {
94
+ developer: 2,
95
+ manager: 1
96
+ },
97
+ ]
98
+
99
+ expect(result).toStrictEqual(expected)
100
+ })
101
+
87
102
  test('pipeline', () => {
88
103
  const result = FluentSQLBuilder.for(data)
89
104
  .where({ category: "developer" })