express-sweet 2.0.3 → 2.0.5

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,6 +1,47 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [2.0.5] - 2025/2/4
5
+ ### Added
6
+ - Added a Handlebars helper, available for use in views, to replace HTML tags in a string.
7
+
8
+ Example:
9
+
10
+ ```html
11
+ {{!-- results in: lorem ipsum dolor sit amet --}}
12
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>'}}}
13
+
14
+ {{!-- results in: lorem ipsum <strong>dolor</strong> sit amet --}}
15
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>' '<strong>' ''}}}
16
+
17
+ {{!-- results in: 🍩lorem ipsum 🍩dolor🍩 🍩sit🍩 amet🍩 --}}
18
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>' [] '🍩'}}}
19
+ ```
20
+
21
+ ## [2.0.4] - 2025/1/14
22
+ ### Changed
23
+ - Refactored variable names.
24
+ - Fix Docker demo.
25
+ - Changed the directory name for storing custom error classes in the demo from `exceptions` to `errors`.
26
+ - Added a section to the README.md API reference about considerations when using the `hasMany` association with the `findAll` method.
27
+
28
+ ### Added
29
+ - **`findObjectInArray` Handlebars helper:** This helper is added to the view, allowing you to find an object in an array based on a specified field name and value.
30
+
31
+ Example:
32
+
33
+ ```html
34
+ {{!--
35
+ items is an array of objects: [{id: 123, name: 'Item A'}, {id: 456, name: 'Item B'}]
36
+ This code will output: "Item A"
37
+ --}}
38
+ {{#each items}}
39
+ {{#if (eq id 123)}}
40
+ {{lookup (findObjectInArray ../items 'id' id) 'name'}}
41
+ {{/if}}
42
+ {{/each}}
43
+ ```
44
+
4
45
  ## [2.0.3] - 2024/9/3
5
46
  ### Changed
6
47
  - Add `"type": "module"` to the `package.json` of the esm app generated by `express-sweet-generator`. It is fully esm compliant.
@@ -66,7 +107,7 @@ All notable changes to this project will be documented in this file.
66
107
  ## [1.1.1] - 2024/3/23
67
108
  ### Changed
68
109
  - When cookie security is enabled (`config/authentication.js#cookie_secure`) and the application is running on the HTTP protocol, the following warning message is output upon user authentication.
69
- ```sh
110
+ ```bash
70
111
  Warning: Cookie security must be disabled for user authentication to work over the HTTP protocol (config/authentication.js#cookie_secure)
71
112
  ```
72
113
 
@@ -323,7 +364,7 @@ All notable changes to this project will be documented in this file.
323
364
 
324
365
  // Load updated data.
325
366
  await book.reload();
326
- } catch (err) {
367
+ } catch (error) {
327
368
  if (transaction)
328
369
  await transaction.rollback();
329
370
  }
@@ -445,4 +486,6 @@ All notable changes to this project will be documented in this file.
445
486
  [2.0.0]: https://github.com/takuya-motoshima/express-sweet/compare/v1.1.1...v2.0.0
446
487
  [2.0.1]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.0...v2.0.1
447
488
  [2.0.2]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.1...v2.0.2
448
- [2.0.3]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.2...v2.0.3
489
+ [2.0.3]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.2...v2.0.3
490
+ [2.0.4]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.3...v2.0.4
491
+ [2.0.5]: https://github.com/takuya-motoshima/express-sweet/compare/v2.0.4...v2.0.5
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2024 Takuya Motoshima
1
+ Copyright 2025 Takuya Motoshima
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,43 +1,45 @@
1
1
  # EXPRESS SWEET
2
- EXPRESS SWEET is an extension of EXPRESS.
2
+
3
+ EXPRESS SWEET is a powerful Express.js extension that streamlines your workflow and boosts productivity with a suite of utilities and enhancements.
3
4
 
4
5
  - [EXPRESS SWEET](#express-sweet)
5
- - [EXPRESS SWEET Generator](#express-sweet-generator)
6
- - [API](#api)
7
- - [Environment variable](#environment-variable)
8
- - [Base configuration](#base-configuration)
9
- - [Routing](#routing)
10
- - [Basic Routing](#basic-routing)
11
- - [Nested Routing](#nested-routing)
12
- - [Default Routing](#default-routing)
13
- - [Routing Methods](#routing-methods)
14
- - [View](#view)
15
- - [Configuration](#configuration)
16
- - [Syntax](#syntax)
17
- - [Layout](#layout)
18
- - [Comparison Helper](#comparison-helper)
19
- - [HTML Helper](#html-helper)
20
- - [Object Helper](#object-helper)
21
- - [String Helper](#string-helper)
22
- - [Date Helper](#date-helper)
23
- - [Number Helper](#number-helper)
24
- - [Math Helper](#math-helper)
25
- - [Model](#model)
26
- - [Configuration](#configuration-1)
27
- - [Accessing Model](#accessing-model)
28
- - [Creating Model](#creating-model)
29
- - [Database class extends sequelize.Sequelize](#database-class-extends-sequelizesequelize)
30
- - [Instance Methods](#instance-methods)
31
- - [Model Class extends sequelize.Model](#model-class-extends-sequelizemodel)
32
- - [Class Properties](#class-properties)
33
- - [Class Methods](#class-methods)
34
- - [Authentication](#authentication)
35
- - [Configuration](#configuration-2)
36
- - [Login Procedure](#login-procedure)
37
- - [Logout Procedure](#logout-procedure)
38
- - [Release Notes](#release-notes)
39
- - [Author](#author)
40
- - [License](#license)
6
+ - [EXPRESS SWEET Generator](#express-sweet-generator)
7
+ - [API](#api)
8
+ - [Environment variable](#environment-variable)
9
+ - [Base configuration](#base-configuration)
10
+ - [Routing](#routing)
11
+ - [Basic Routing](#basic-routing)
12
+ - [Nested Routing](#nested-routing)
13
+ - [Default Routing](#default-routing)
14
+ - [Routing Methods](#routing-methods)
15
+ - [View](#view)
16
+ - [Configuration](#configuration)
17
+ - [Syntax](#syntax)
18
+ - [Layout](#layout)
19
+ - [Array Helper](#array-helper)
20
+ - [Comparison Helper](#comparison-helper)
21
+ - [HTML Helper](#html-helper)
22
+ - [Object Helper](#object-helper)
23
+ - [String Helper](#string-helper)
24
+ - [Date Helper](#date-helper)
25
+ - [Number Helper](#number-helper)
26
+ - [Math Helper](#math-helper)
27
+ - [Model](#model)
28
+ - [Configuration](#configuration-1)
29
+ - [Accessing Model](#accessing-model)
30
+ - [Creating Model](#creating-model)
31
+ - [Database class extends sequelize.Sequelize](#database-class-extends-sequelizesequelize)
32
+ - [Instance Methods](#instance-methods)
33
+ - [Model Class extends sequelize.Model](#model-class-extends-sequelizemodel)
34
+ - [Class Properties](#class-properties)
35
+ - [Class Methods](#class-methods)
36
+ - [Authentication](#authentication)
37
+ - [Configuration](#configuration-2)
38
+ - [Login Procedure](#login-procedure)
39
+ - [Logout Procedure](#logout-procedure)
40
+ - [Release Notes](#release-notes)
41
+ - [Author](#author)
42
+ - [License](#license)
41
43
 
42
44
  ## EXPRESS SWEET Generator
43
45
  Use the application generator tool, `express-sweet-generator`, to quickly create an application skeleton.
@@ -45,11 +47,11 @@ Use the application generator tool, `express-sweet-generator`, to quickly create
45
47
  ![Image](screencaps/demonstration.webp)
46
48
 
47
49
  1. Install and launch the application generator as a global npm package.
48
- ```sh
50
+ ```bash
49
51
  npm install -g express-sweet-generator
50
52
  ```
51
53
  1. Display the command options with the -h option.
52
- ```sh
54
+ ```bash
53
55
  express-sweet -h
54
56
 
55
57
  Usage: express-sweet [options] [dir]
@@ -63,11 +65,11 @@ Use the application generator tool, `express-sweet-generator`, to quickly create
63
65
  -h, --help output usage information
64
66
  ```
65
67
  1. For example, the following creates an Express app named `myapp`. The app will be created in a folder named `myapp` in the current working directory.
66
- ```sh
68
+ ```bash
67
69
  express-sweet -o esm myapp
68
70
  ```
69
71
  1. Then install dependencies.
70
- ```sh
72
+ ```bash
71
73
  cd myapp/
72
74
  npm install
73
75
  ```
@@ -165,16 +167,16 @@ Use the application generator tool, `express-sweet-generator`, to quickly create
165
167
  }
166
168
  ```
167
169
  1. The DB to be accessed can be defined for each environment. Specify the environment in the `.env` file
168
- ```sh
170
+ ```bash
169
171
  NODE_ENV=development
170
172
  ```
171
173
  1. Run the application with the following command.
172
- ```sh
174
+ ```bash
173
175
  npm start
174
176
  ```
175
177
  1. Then, load `http://localhost:3000/` in your browser to access the app.
176
178
  The generated app has the following directory structure:
177
- ```sh
179
+ ```bash
178
180
  .
179
181
  ├── .env
180
182
  ├── app.js
@@ -192,7 +194,7 @@ Use the application generator tool, `express-sweet-generator`, to quickly create
192
194
  │ ├── config.js
193
195
  │ ├── database.js
194
196
  │ └── view.js
195
- ├── exceptions
197
+ ├── errors
196
198
  │ └── UserNotFound.js
197
199
  ├── models
198
200
  │ ├── BookModel.js
@@ -291,17 +293,17 @@ and [here](configuration_sample/cjs/config.js) to download a sample CJS configur
291
293
  // return !!req.xhr;
292
294
  }
293
295
  ```
294
- - `hook_handle_error: (err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void`
296
+ - `hook_handle_error: (error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void`
295
297
  Hooks the default behavior on request errors.
296
- If unset, simply returns an error HTTP status. (`res.status(err.status||500).end();`)
298
+ If unset, simply returns an error HTTP status. (`res.status(error.status||500).end();`)
297
299
  ```js
298
- hook_handle_error: (err, req, res, next) => {
299
- if (err.status === 404)
300
- // If the URL cannot be found, a 404 error screen (views/error-404.hbs) is displayed.
301
- res.render('error-404');
300
+ hook_handle_error: (error, req, res, next) => {
301
+ if (error.status === 404)
302
+ // If the URL cannot be found, a 404 error screen (views/error/404.hbs) is displayed.
303
+ res.render('error/404');
302
304
  else
303
- // For other errors, unknown error screen (views/error-unknown.hbs) is displayed.
304
- res.render('error-unknown');
305
+ // For other errors, unknown error screen (views/error/500.hbs) is displayed.
306
+ res.render('error/500');
305
307
  },
306
308
  ```
307
309
 
@@ -503,6 +505,33 @@ There are three ways to use a layout, listed in precedence order.
503
505
  Layouts can be nested: just include a declarative layout tag within any layout template to have its content included in the declared "parent" layout.
504
506
  Be aware that too much nesting can impact performances, and stay away from infinite loops.
505
507
 
508
+ #### Array Helper
509
+ - `findObjectInArray()`
510
+ Finds an object in an array based on the specified field name and value.
511
+
512
+ **Parameters:**
513
+ - array: Array<{[key: string]: any}>
514
+ The array of objects to search.
515
+ - fieldName: string
516
+ The name of the field to search.
517
+ - fieldValue: any
518
+ fieldValue The value to search for.
519
+
520
+ **Return:**
521
+ `object|null` The first object in the array that matches the criteria, or null if no match is found.
522
+
523
+ ```js
524
+ {{!--
525
+ items is an array of objects: [{id: 123, name: 'Item A'}, {id: 456, name: 'Item B'}]
526
+ This code will output: "Item A"
527
+ --}}
528
+ {{#each items}}
529
+ {{#if (eq id 123)}}
530
+ {{lookup (findObjectInArray ../items 'id' id) 'name'}}
531
+ {{/if}}
532
+ {{/each}}
533
+ ```
534
+
506
535
  #### Comparison Helper
507
536
  - `eq()`
508
537
  Determine whether or not two values are equal (===).
@@ -813,6 +842,31 @@ There are three ways to use a layout, listed in precedence order.
813
842
  {{cacheBusting '/assets/style.css' '//example.com'}}
814
843
  ```
815
844
 
845
+ - `stripTags()`
846
+ Removes HTML tags from a string, optionally allowing specific tags.
847
+
848
+ **Parameters:**
849
+ - str: string
850
+ The string to remove HTML tags from.
851
+ - allowedTags: string|string[]
852
+ An array of allowed HTML tags. Default is an empty array.
853
+ - replacement: string
854
+ The string to replace HTML tags with. Default is blank.
855
+
856
+ **Return:**
857
+ `string` The string with HTML tags removed.
858
+
859
+ ```js
860
+ {{!-- results in: lorem ipsum dolor sit amet --}}
861
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>'}}}
862
+
863
+ {{!-- results in: lorem ipsum <strong>dolor</strong> sit amet --}}
864
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>' '<strong>' ''}}}
865
+
866
+ {{!-- results in: 🍩lorem ipsum 🍩dolor🍩 🍩sit🍩 amet🍩 --}}
867
+ {{{stripTags '<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>' [] '🍩'}}}
868
+ ```
869
+
816
870
  #### Object Helper
817
871
  - `jsonStringify()`
818
872
  Stringify an object using JSON.stringify.
@@ -1613,10 +1667,12 @@ See [here](https://sequelize.org/api/v6/class/src/model.js~model) for more infor
1613
1667
  // ]
1614
1668
  await UserModel.findAll({
1615
1669
  attributes: ['id', 'name'],
1616
- include: [{
1617
- association: 'profile',
1618
- attributes: ['userId', 'address', 'tel']
1619
- }]
1670
+ include: [
1671
+ {
1672
+ association: 'profile',
1673
+ attributes: ['userId', 'address', 'tel']
1674
+ },
1675
+ ]
1620
1676
  });
1621
1677
  ```
1622
1678
  - `public static belongsTo()`
@@ -1776,6 +1832,61 @@ See [here](https://sequelize.org/api/v6/class/src/model.js~model) for more infor
1776
1832
  }
1777
1833
  });
1778
1834
  ```
1835
+
1836
+ **Using `hasMany` with `findAll`**
1837
+
1838
+ When using `hasMany` association with `findAll` in Sequelize, you need to be aware of the `raw` option's behavior.
1839
+
1840
+ If you want to retrieve child rows as an array within the parent row, you should **disable the `raw` option**.
1841
+
1842
+ Enabling the `raw` option will result in the parent row being duplicated for each child row. This behavior is also discussed in this [GitHub issue](https://github.com/sequelize/sequelize/issues/11988).
1843
+
1844
+ **Example:**
1845
+
1846
+ **With `raw: true`:**
1847
+
1848
+ ```json
1849
+ [
1850
+ {
1851
+ "id": 1,
1852
+ "name": "Robin",
1853
+ {
1854
+ "userId": 1,
1855
+ "text": "From Robin #1"
1856
+ }
1857
+ },
1858
+ {
1859
+ "id": 1,
1860
+ "name": "Robin",
1861
+ {
1862
+ "userId": 1,
1863
+ "text": "From Robin #2"
1864
+ }
1865
+ }
1866
+ ]
1867
+ ```
1868
+
1869
+ **With `raw: false` (default):**
1870
+
1871
+ ```json
1872
+ [
1873
+ {
1874
+ "id": 1,
1875
+ "name": "Robin",
1876
+ "comments": [
1877
+ {
1878
+ "userId": 1,
1879
+ "text": "From Robin #1"
1880
+ },
1881
+ {
1882
+ "userId": 1,
1883
+ "text": "From Robin #2"
1884
+ }
1885
+ ]
1886
+ }
1887
+ ]
1888
+ ```
1889
+
1779
1890
  - `public static belongsToMany()`
1780
1891
  Create an N:M association with a join table.
1781
1892
  Defining through is required.
@@ -38,12 +38,11 @@ module.exports = {
38
38
  * This is a hook that rewrites the base URL.
39
39
  * If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL.
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
+ * @type {(baseUrl: string): string}
41
42
  * @example
42
43
  * rewrite_base_url: baseUrl => {
43
44
  * return `${baseUrl}/admin`;
44
45
  * }
45
- *
46
- * @type {(baseUrl: string): string}
47
46
  */
48
47
  rewrite_base_url: baseUrl => {
49
48
  return baseUrl;
@@ -52,8 +51,9 @@ module.exports = {
52
51
  /**
53
52
  * This is a hook for error handling.
54
53
  * For example, you can use it when you want to send an external notification of the error received by this hook.
54
+ * @type {(error: any): void|Promise<void>}
55
55
  * @example
56
- * error_handler: async err => {
56
+ * error_handler: async error => {
57
57
  * // Notify system administrator of error.
58
58
  * return new Promise((resolve, reject) => {
59
59
  * const sendmail = require('sendmail')();
@@ -61,16 +61,14 @@ module.exports = {
61
61
  * from: 'no-reply@example.com',
62
62
  * to: 'administrator@example.com',
63
63
  * subject: 'Error occurred',
64
- * text: err.message
65
- * }, err => {
66
- * err ? reject(err) : resolve();
64
+ * text: error.message
65
+ * }, error => {
66
+ * error ? reject(error) : resolve();
67
67
  * });
68
68
  * });
69
69
  * }
70
- *
71
- * @type {(err: any): void|Promise<void>}
72
70
  */
73
- error_handler: err => {
74
- console.error(`An error has occurred. Error message: ${err.message}`);
71
+ error_handler: error => {
72
+ console.error(`An error has occurred. Error message: ${error.message}`);
75
73
  }
76
74
  }
@@ -38,12 +38,11 @@ export default {
38
38
  * This is a hook that rewrites the base URL.
39
39
  * If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL.
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
+ * @type {(baseUrl: string): string}
41
42
  * @example
42
43
  * rewrite_base_url: baseUrl => {
43
44
  * return `${baseUrl}/admin`;
44
45
  * }
45
- *
46
- * @type {(baseUrl: string): string}
47
46
  */
48
47
  rewrite_base_url: baseUrl => {
49
48
  return baseUrl;
@@ -52,8 +51,9 @@ export default {
52
51
  /**
53
52
  * This is a hook for error handling.
54
53
  * For example, you can use it when you want to send an external notification of the error received by this hook.
54
+ * @type {(error: any): void|Promise<void>}
55
55
  * @example
56
- * error_handler: async err => {
56
+ * error_handler: async error => {
57
57
  * // Notify system administrator of error.
58
58
  * return new Promise((resolve, reject) => {
59
59
  * const sendmail = require('sendmail')();
@@ -61,16 +61,14 @@ export default {
61
61
  * from: 'no-reply@example.com',
62
62
  * to: 'administrator@example.com',
63
63
  * subject: 'Error occurred',
64
- * text: err.message
65
- * }, err => {
66
- * err ? reject(err) : resolve();
64
+ * text: error.message
65
+ * }, error => {
66
+ * error ? reject(error) : resolve();
67
67
  * });
68
68
  * });
69
69
  * }
70
- *
71
- * @type {(err: any): void|Promise<void>}
72
70
  */
73
- error_handler: err => {
74
- console.error(`An error has occurred. Error message: ${err.message}`);
71
+ error_handler: error => {
72
+ console.error(`An error has occurred. Error message: ${error.message}`);
75
73
  }
76
74
  }