sql-love 0.9.5 → 0.9.6

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/CHANGELOG.md CHANGED
@@ -1,10 +1,11 @@
1
1
  ## Change Log
2
2
 
3
- ### v0.9.5 - 5 Dec 2023
3
+ ### v0.9.6 - 5 Dec 2023
4
4
 
5
5
  - Utility functions (extractGrouped, extractIndexed, getPagination, runPrisma, runMysql)
6
6
  - Added `SelectBuilder.setDefaultEngine(engine)`
7
7
  - More documentation
8
+ - Fix `.sortField()` logic
8
9
 
9
10
  ### v0.9.4 - 30 Nov 2023
10
11
 
package/README.md CHANGED
@@ -1,12 +1,12 @@
1
- <img alt="SQL Love Logo" src="https://github.com/kensnyder/sql-love/raw/main/assets/sql-love-logo.png?v=0.9.5" width="250" height="208" />
1
+ <img alt="SQL Love Logo" src="https://github.com/kensnyder/sql-love/raw/main/assets/sql-love-logo.png?v=0.9.6" width="250" height="208" />
2
2
 
3
3
  # sql-love
4
4
 
5
- [![NPM Link](https://badgen.net/npm/v/sql-love?v=0.9.5)](https://npmjs.com/package/sql-love)
6
- [![Dependencies](https://badgen.net/static/dependencies/0/green?v=0.9.5)](https://npmjs.com/package/sql-love)
7
- [![Build Status](https://github.com/kensnyder/sql-love/actions/workflows/node.js.yml/badge.svg?v=0.9.5)](https://github.com/kensnyder/sql-love/actions)
8
- [![Code Coverage](https://codecov.io/gh/kensnyder/sql-love/branch/main/graph/badge.svg?v=0.9.5)](https://codecov.io/gh/kensnyder/sql-love)
9
- [![ISC License](https://badgen.net/static/license/ISC/green?v=0.9.5)](https://opensource.org/licenses/ISC)
5
+ [![NPM Link](https://badgen.net/npm/v/sql-love?v=0.9.6)](https://npmjs.com/package/sql-love)
6
+ [![Dependencies](https://badgen.net/static/dependencies/0/green?v=0.9.6)](https://npmjs.com/package/sql-love)
7
+ [![Build Status](https://github.com/kensnyder/sql-love/actions/workflows/node.js.yml/badge.svg?v=0.9.6)](https://github.com/kensnyder/sql-love/actions)
8
+ [![Code Coverage](https://codecov.io/gh/kensnyder/sql-love/branch/main/graph/badge.svg?v=0.9.6)](https://codecov.io/gh/kensnyder/sql-love)
9
+ [![ISC License](https://badgen.net/static/license/ISC/green?v=0.9.6)](https://opensource.org/licenses/ISC)
10
10
 
11
11
  Classes for parsing and building SQL select queries in Node
12
12
 
@@ -342,13 +342,13 @@ export default class SelectBuilder {
342
342
  orderBy(column: string, direction?: 'ASC' | 'DESC'): this;
343
343
  /**
344
344
  * Sort by the given column, with a map of columns to translate
345
- * @param column The column name such as "created_at" or "-created_at" for descending
346
- * @param [mapNames] Column names to translate from one name to another
345
+ * @param fieldName The file name such as "created_at" or "-created_at" for descending
346
+ * @param [mapNames] Column names to translate from field to column expression
347
347
  * @example
348
348
  * query.sortField('-modified_at'); // ORDER BY modified_at DESC
349
- * query.sortField('created_at', {'created_at': 'created'}); // ORDER BY created
349
+ * query.sortField('-created', {'created': 'created_at'}); // ORDER BY created_at DESC
350
350
  */
351
- sortField(column: string, mapNames?: Record<string, string>): this;
351
+ sortField(fieldName: string, mapNames?: Record<string, string>): this;
352
352
  /**
353
353
  * Check to see if the given string is 0 or all digits not starting with 0
354
354
  * @param string The input string
@@ -716,16 +716,20 @@ export default class SelectBuilder {
716
716
  }
717
717
  /**
718
718
  * Sort by the given column, with a map of columns to translate
719
- * @param column The column name such as "created_at" or "-created_at" for descending
720
- * @param [mapNames] Column names to translate from one name to another
719
+ * @param fieldName The file name such as "created_at" or "-created_at" for descending
720
+ * @param [mapNames] Column names to translate from field to column expression
721
721
  * @example
722
722
  * query.sortField('-modified_at'); // ORDER BY modified_at DESC
723
- * query.sortField('created_at', {'created_at': 'created'}); // ORDER BY created
723
+ * query.sortField('-created', {'created': 'created_at'}); // ORDER BY created_at DESC
724
724
  */
725
- sortField(column, mapNames = {}) {
726
- column.replace(/^-(.+)/, '$1 DESC');
727
- column = mapNames[column] || column;
728
- this.orderBy(column);
725
+ sortField(fieldName, mapNames = {}) {
726
+ let direction = 'ASC';
727
+ if (fieldName.startsWith('-')) {
728
+ direction = 'DESC';
729
+ fieldName = fieldName.slice(1);
730
+ }
731
+ const column = mapNames[fieldName] || fieldName;
732
+ this.orderBy(column, direction);
729
733
  return this;
730
734
  }
731
735
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sql-love",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "description": "Build SQL queries with JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -450,7 +450,7 @@ describe('sortField()', () => {
450
450
  const query = new SelectBuilder('SELECT * FROM posts');
451
451
  query.sortField('posts.created_at');
452
452
  const { sql, bindings } = query.compile();
453
- expect(trim(sql)).toBe('SELECT * FROM posts ORDER BY posts.created_at');
453
+ expect(trim(sql)).toBe('SELECT * FROM posts ORDER BY posts.created_at ASC');
454
454
  expect(bindings).toEqual([]);
455
455
  });
456
456
  it('should handle minus signs', () => {
@@ -464,12 +464,12 @@ describe('sortField()', () => {
464
464
  });
465
465
  it('should handle mapping', () => {
466
466
  const query = new SelectBuilder('SELECT * FROM posts');
467
- query.sortField('created_at', {
467
+ query.sortField('-created_at', {
468
468
  created_at: 'posts.created_timestamp',
469
469
  });
470
470
  const { sql, bindings } = query.compile();
471
471
  expect(trim(sql)).toBe(
472
- 'SELECT * FROM posts ORDER BY posts.created_timestamp'
472
+ 'SELECT * FROM posts ORDER BY posts.created_timestamp DESC'
473
473
  );
474
474
  expect(bindings).toEqual([]);
475
475
  });
@@ -809,16 +809,20 @@ export default class SelectBuilder {
809
809
 
810
810
  /**
811
811
  * Sort by the given column, with a map of columns to translate
812
- * @param column The column name such as "created_at" or "-created_at" for descending
813
- * @param [mapNames] Column names to translate from one name to another
812
+ * @param fieldName The file name such as "created_at" or "-created_at" for descending
813
+ * @param [mapNames] Column names to translate from field to column expression
814
814
  * @example
815
815
  * query.sortField('-modified_at'); // ORDER BY modified_at DESC
816
- * query.sortField('created_at', {'created_at': 'created'}); // ORDER BY created
816
+ * query.sortField('-created', {'created': 'created_at'}); // ORDER BY created_at DESC
817
817
  */
818
- sortField(column: string, mapNames: Record<string, string> = {}) {
819
- column.replace(/^-(.+)/, '$1 DESC');
820
- column = mapNames[column] || column;
821
- this.orderBy(column);
818
+ sortField(fieldName: string, mapNames: Record<string, string> = {}) {
819
+ let direction = 'ASC' as 'ASC' | 'DESC';
820
+ if (fieldName.startsWith('-')) {
821
+ direction = 'DESC';
822
+ fieldName = fieldName.slice(1);
823
+ }
824
+ const column = mapNames[fieldName] || fieldName;
825
+ this.orderBy(column, direction);
822
826
  return this;
823
827
  }
824
828