express-sweet-generator 1.0.0 → 1.0.3

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/README.md +16 -43
  3. package/bin/express-sweet-cli.js +220 -214
  4. package/package.json +1 -1
  5. package/templates/{.env → cjs/.env} +0 -0
  6. package/templates/{app.js → cjs/app.js} +0 -0
  7. package/templates/{bin → cjs/bin}/www.ejs +0 -0
  8. package/templates/{config → cjs/config}/authentication.js +1 -1
  9. package/templates/{config → cjs/config}/config.js +0 -0
  10. package/templates/{config → cjs/config}/database.js +6 -12
  11. package/templates/{config → cjs/config}/view.js +0 -0
  12. package/templates/{ecosystem.config.js.ejs → cjs/ecosystem.config.js.ejs} +5 -1
  13. package/templates/{models → cjs/models}/BookModel.js +0 -0
  14. package/templates/{models → cjs/models}/CommentModel.js +0 -0
  15. package/templates/{models → cjs/models}/ProfileModel.js +0 -0
  16. package/templates/{models → cjs/models}/UserModel.js +0 -0
  17. package/templates/{nginx.sample.conf.ejs → cjs/nginx.sample.conf.ejs} +0 -0
  18. package/templates/{package.json.ejs → cjs/package.json.ejs} +2 -7
  19. package/templates/{views/partials/.gitkeep → cjs/public/assets/home.css} +0 -0
  20. package/templates/cjs/public/assets/home.js +0 -0
  21. package/templates/{public/assets/styles → cjs/public/assets}/login.css +0 -2
  22. package/templates/{public/assets/scripts → cjs/public/assets}/login.js +0 -3
  23. package/templates/{public/assets/images → cjs/public/assets}/logo.svg +0 -0
  24. package/templates/cjs/public/assets/user.css +0 -0
  25. package/templates/{public/assets/scripts → cjs/public/assets}/user.js +0 -3
  26. package/templates/cjs/public/assets/users.css +0 -0
  27. package/templates/{public/assets/scripts → cjs/public/assets}/users.js +11 -9
  28. package/templates/{public → cjs/public}/favicon.ico +0 -0
  29. package/templates/{public → cjs/public}/rekognition-test-image/add-face-to-collection.jpg +0 -0
  30. package/templates/{public → cjs/public}/rekognition-test-image/compare-faces-source.jpg +0 -0
  31. package/templates/{public → cjs/public}/rekognition-test-image/compare-faces-target.jpg +0 -0
  32. package/templates/{public → cjs/public}/rekognition-test-image/detect-faces.jpg +0 -0
  33. package/templates/{routes → cjs/routes}/api/users.js +7 -33
  34. package/templates/{routes → cjs/routes}/home.js +0 -3
  35. package/templates/{routes → cjs/routes}/login.js +0 -3
  36. package/templates/{routes → cjs/routes}/users.js +3 -9
  37. package/templates/{views → cjs/views}/error.hbs +1 -2
  38. package/templates/cjs/views/home.hbs +5 -0
  39. package/templates/{views → cjs/views}/layout/default.hbs +13 -18
  40. package/templates/cjs/views/login.hbs +16 -0
  41. package/templates/cjs/views/partials/.gitkeep +0 -0
  42. package/templates/cjs/views/user.hbs +35 -0
  43. package/templates/cjs/views/users.hbs +18 -0
  44. package/templates/esm/.env +1 -0
  45. package/templates/esm/app.js +10 -0
  46. package/templates/esm/bin/www.ejs +67 -0
  47. package/templates/esm/config/authentication.js +81 -0
  48. package/templates/esm/config/config.js +78 -0
  49. package/templates/esm/config/database.js +35 -0
  50. package/templates/esm/config/view.js +36 -0
  51. package/templates/esm/ecosystem.config.js.ejs +35 -0
  52. package/templates/esm/models/BookModel.js +30 -0
  53. package/templates/esm/models/CommentModel.js +30 -0
  54. package/templates/esm/models/ProfileModel.js +49 -0
  55. package/templates/esm/models/UserModel.js +113 -0
  56. package/templates/esm/nginx.sample.conf.ejs +48 -0
  57. package/templates/esm/package.json.ejs +21 -0
  58. package/templates/esm/public/assets/home.css +0 -0
  59. package/templates/esm/public/assets/home.js +0 -0
  60. package/templates/esm/public/assets/login.css +16 -0
  61. package/templates/esm/public/assets/login.js +29 -0
  62. package/templates/{public/assets/images/logo-dark.svg → esm/public/assets/logo.svg} +2 -2
  63. package/templates/esm/public/assets/user.css +0 -0
  64. package/templates/esm/public/assets/user.js +50 -0
  65. package/templates/esm/public/assets/users.css +0 -0
  66. package/templates/esm/public/assets/users.js +85 -0
  67. package/templates/esm/public/favicon.ico +0 -0
  68. package/templates/esm/public/rekognition-test-image/add-face-to-collection.jpg +0 -0
  69. package/templates/esm/public/rekognition-test-image/compare-faces-source.jpg +0 -0
  70. package/templates/esm/public/rekognition-test-image/compare-faces-target.jpg +0 -0
  71. package/templates/esm/public/rekognition-test-image/detect-faces.jpg +0 -0
  72. package/templates/esm/routes/api/users.js +98 -0
  73. package/templates/esm/routes/home.js +8 -0
  74. package/templates/esm/routes/login.js +8 -0
  75. package/templates/esm/routes/users.js +31 -0
  76. package/templates/esm/views/error.hbs +4 -0
  77. package/templates/esm/views/home.hbs +5 -0
  78. package/templates/esm/views/layout/default.hbs +57 -0
  79. package/templates/esm/views/login.hbs +16 -0
  80. package/templates/esm/views/partials/.gitkeep +0 -0
  81. package/templates/esm/views/user.hbs +35 -0
  82. package/templates/esm/views/users.hbs +18 -0
  83. package/templates/public/assets/scripts/docs.js +0 -1
  84. package/templates/public/assets/scripts/home.js +0 -3
  85. package/templates/public/assets/styles/docs.css +0 -2200
  86. package/templates/public/assets/styles/home.css +0 -1
  87. package/templates/public/assets/styles/user.css +0 -1
  88. package/templates/public/assets/styles/users.css +0 -1
  89. package/templates/views/home.hbs +0 -11
  90. package/templates/views/login.hbs +0 -27
  91. package/templates/views/user.hbs +0 -42
  92. package/templates/views/users.hbs +0 -30
@@ -0,0 +1,85 @@
1
+ // Set up data table.
2
+ const dt = $('#table')
3
+ .on('draw.dt', () => {
4
+ $("#table_processing").hide();
5
+ })
6
+ .DataTable({
7
+ processing: true,
8
+ serverSide: true,
9
+ searchDelay: 500,
10
+ ajax: {
11
+ // URL to get list data.
12
+ url: '/api/users',
13
+ // Set action column elements.
14
+ dataSrc: res => {
15
+ return res.data.map(row => Object.assign(row, {actions: ''}));
16
+ },
17
+ // For Ajax, send a cookie.
18
+ xhrFields: {
19
+ withCredentials: true
20
+ }
21
+ },
22
+ columns: [
23
+ {data: 'id', width: 100},
24
+ {data: 'email'},
25
+ {data: 'name'},
26
+ {data: 'modified', width: 200},
27
+ {data: 'actions', width: 200}
28
+ ],
29
+ columnDefs: [
30
+ {
31
+ targets: -1,
32
+ orderable: false,
33
+ render: (data, type, row) =>
34
+ `<a href="/users/${row.id}" class="btn btn-success">Edit</a>
35
+ <button on-delete data-id="${row.id}" type="button" class="btn btn-danger mx-2">Delete</button>`
36
+ }
37
+ ],
38
+ dom: `<'row'<'col-12'f>><'row'<'col-12'tr>><'row'<'col-12 dataTables_pager'p>>`,
39
+ scrollX: true,
40
+ responsive: true,
41
+ pageLength: 30,
42
+ fnServerParams: params => {
43
+ params.offset = params.start;
44
+ delete params.start;
45
+ params.limit = params.length;
46
+ delete params.length;
47
+ const cols = Object.assign({}, params.columns);
48
+ delete params.columns;
49
+ const {column: index, dir} = params.order[0];
50
+ params.order = cols[index].data;
51
+ params.dir = dir;
52
+ params.search = params.search.value;
53
+ }
54
+ });
55
+
56
+ // Set user delete action.
57
+ $('#table').on('click', '[on-delete]', async event => {
58
+ try {
59
+ // Row element.
60
+ const tr = $(event.currentTarget).closest('tr');
61
+
62
+ // Email of the user to be deleted.
63
+ const email = tr.find('td:nth-of-type(2)').text();
64
+
65
+ // Confirmation of deletion.
66
+ if (!window.confirm(`Do you want to delete the user of mail ${email}?`))
67
+ return;
68
+
69
+ // Get the ID of the user to delete.
70
+ const id = $(event.currentTarget).data('id');
71
+
72
+ // Send user deletion request.
73
+ const res = await $.ajax({
74
+ type: 'DELETE',
75
+ url: `/api/users/${id}`
76
+ });
77
+
78
+ // Remove row element from table.
79
+ tr.fadeOut(() => {
80
+ dt.row(tr).remove().draw();
81
+ });
82
+ } catch(e) {
83
+ alert('An unexpected error has occurred.');
84
+ }
85
+ });
Binary file
@@ -0,0 +1,98 @@
1
+ import {Router} from 'express';
2
+ import * as sweet from 'express-sweet';
3
+ import UserModel from '../../models/UserModel';
4
+ const router = Router();
5
+ const Authentication = sweet.services.Authentication;
6
+
7
+ // Login.
8
+ router.post('/login', async (req, res, next) => {
9
+ const isAuthenticated = await Authentication.authenticate(req, res, next);
10
+ res.json(isAuthenticated);
11
+ });
12
+
13
+ // Logout.
14
+ router.get('/logout', (req, res) => {
15
+ Authentication.logout(req);
16
+ res.redirect('/');
17
+ });
18
+
19
+ // Get a list of users.
20
+ router.get('/', async (req, res) => {
21
+ const data = await UserModel.paginate({
22
+ offset: req.query.offset,
23
+ limit: req.query.limit,
24
+ search: req.query.search,
25
+ order: req.query.order,
26
+ dir: req.query.dir
27
+ });
28
+ data.draw = req.query.draw;
29
+ res.json(data);
30
+ });
31
+
32
+ // Create User.
33
+ router.post('/', async (req, res) => {
34
+ // Email duplication check.
35
+ const emailExists = (await UserModel.count({
36
+ where: {
37
+ email: req.body.email
38
+ }
39
+ })) > 0;
40
+
41
+ // Returns an error if the email exists.
42
+ if (emailExists)
43
+ return void res.json({error: 'Email is already in use.'});
44
+
45
+ // Add new user.
46
+ const result = await UserModel.create({
47
+ email: req.body.email,
48
+ password: req.body.password,
49
+ name: req.body.name
50
+ });
51
+
52
+ // Returns the ID of the added user.
53
+ res.json({id: result.id});
54
+ });
55
+
56
+ // Update User.
57
+ router.put('/:id(\\d+)', async (req, res) => {
58
+ // Email duplication check.
59
+ const emailExists = (await UserModel.count({
60
+ where: {
61
+ id: {[UserModel.Op.ne]: req.params.id},
62
+ email: req.body.email
63
+ }
64
+ })) > 0;
65
+
66
+ // Returns an error if the email exists.
67
+ if (emailExists)
68
+ return void res.json({error: 'Email is already in use.'});
69
+
70
+ // Update data.
71
+ const set = {
72
+ email: req.body.email,
73
+ name: req.body.name
74
+ };
75
+
76
+ // Password with leading and trailing spaces removed.
77
+ const password = req.body.password.replace(/(^[\s ]+)|([\s ]+$)/g, '');
78
+
79
+ // If there is a password entered, set the password in the update data.
80
+ if (password)
81
+ set.password = password;
82
+
83
+ // Update user data.
84
+ await UserModel.update(set, {
85
+ where: {
86
+ id: req.params.id
87
+ }
88
+ });
89
+ res.json(true);
90
+ });
91
+
92
+ // Delete user.
93
+ router.delete('/:id(\\d+)', async (req, res) => {
94
+ await UserModel.destroy({where: {id: req.params.id}});
95
+ res.json(true);
96
+ });
97
+
98
+ export default router;
@@ -0,0 +1,8 @@
1
+ import {Router} from 'express';
2
+ const router = Router();
3
+
4
+ router.get('/', (req, res) => {
5
+ res.render('home');
6
+ });
7
+
8
+ export default router;
@@ -0,0 +1,8 @@
1
+ import {Router} from 'express';
2
+ const router = Router();
3
+
4
+ router.get('/', (req, res) => {
5
+ res.render('login');
6
+ });
7
+
8
+ export default router;
@@ -0,0 +1,31 @@
1
+ import {Router} from 'express';
2
+ import UserModel from '../models/UserModel';
3
+ const router = Router();
4
+
5
+ // Edit User.
6
+ router.get('/:id(\\d+)', async (req, res) => {
7
+ // Find a user that matches your ID.
8
+ const user = await UserModel.findByPk(req.params.id, {
9
+ attributes: ['id', 'email', 'name'],
10
+ raw: true
11
+ });
12
+
13
+ // Redirect to user list if user not found.
14
+ if (!user)
15
+ return void res.redirect('/users');
16
+
17
+ // Display user edit page.
18
+ res.render('user', {user, edit: true});
19
+ });
20
+
21
+ // Create User.
22
+ router.get('/new', async (req, res) => {
23
+ res.render('user', {edit: false});
24
+ });
25
+
26
+ // User List.
27
+ router.get('/', async (req, res) => {
28
+ res.render('users');
29
+ });
30
+
31
+ export default router;
@@ -0,0 +1,4 @@
1
+ {{!< default}}
2
+ <h1>{{message}}</h1>
3
+ <h2>{{error.status}}</h2>
4
+ <pre>{{error.stack}}</pre>
@@ -0,0 +1,5 @@
1
+ {{!< default}}
2
+ {{#contentFor 'title'}}Home{{/contentFor}}
3
+ {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cache_busting '/assets/home.css' baseUrl}}">{{/contentFor}}
4
+ {{#contentFor 'script'}}<script src="{{cache_busting '/assets/home.js' baseUrl}}"></script>{{/contentFor}}
5
+ <h1 class="mt-5 text-center">Welcome to Express Sweet</h1>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>{{{block "title"}}} | Exspress Sweet Template</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
8
+ <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.24/css/dataTables.bootstrap5.min.css">
9
+ {{{block "style"}}}
10
+ </head>
11
+ <body>
12
+ {{#if session}}
13
+ <header class="navbar navbar-expand-md navbar-light">
14
+ <nav class="container-xxl flex-wrap flex-md-nowrap" aria-label="Main navigation">
15
+ <a class="navbar-brand p-0 me-2" href="/">
16
+ <img src="{{baseUrl}}/assets/logo.svg" height="32">
17
+ </a>
18
+ <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#bdNavbar" aria-controls="bdNavbar" aria-expanded="false" aria-label="Toggle navigation">
19
+ <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" class="bi" fill="currentColor" viewBox="0 0 16 16">
20
+ <path fill-rule="evenodd" d="M2.5 11.5A.5.5 0 0 1 3 11h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4A.5.5 0 0 1 3 7h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4A.5.5 0 0 1 3 3h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z"></path>
21
+ </svg>
22
+ </button>
23
+ <div class="collapse navbar-collapse" id="bdNavbar">
24
+ <ul class="navbar-nav flex-row flex-wrap pt-2 py-md-0">
25
+ <li class="nav-item col-6 col-md-auto">
26
+ <a class="nav-link p-2 {{#if (eq currentPath '/')}}active{{/if}}" href="{{baseUrl}}">Home</a>
27
+ </li>
28
+ <li class="nav-item col-6 col-md-auto">
29
+ <a class="nav-link p-2 {{#if (eq currentPath '/users')}}active{{/if}}" href="{{baseUrl}}/users">Users</a>
30
+ </li>
31
+ </ul>
32
+ <a class="my-md-0 ms-md-auto" href="{{baseUrl}}/api/users/logout">Logout</a>
33
+ </div>
34
+ </nav>
35
+ </header>
36
+ <div class="container-xxl my-md-4">
37
+ {{{body}}}
38
+ </div>
39
+ {{else}}
40
+ {{{body}}}
41
+ {{/if}}
42
+ <script>
43
+ // Application base URL.
44
+ globalThis.baseUrl = '{{baseUrl}}';
45
+ // User session.
46
+ {{#if session}}
47
+ globalThis.$$session = {{{json_stringify session}}};
48
+ {{/if}}
49
+ </script>
50
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
51
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
52
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js" integrity="sha512-UdIMMlVx0HEynClOIFSyOrPggomfhBKJE28LKl8yR3ghkgugPnG6iLfRfHwushZl1MOPSY6TsuBDGPK2X4zYKg==" crossorigin="anonymous"></script>
53
+ <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
54
+ <script src="https://cdn.datatables.net/1.10.24/js/dataTables.bootstrap5.min.js"></script>
55
+ {{{block "script"}}}
56
+ </body>
57
+ </html>
@@ -0,0 +1,16 @@
1
+ {{!< default}}
2
+ {{#contentFor 'title'}}Login{{/contentFor}}
3
+ {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cache_busting '/assets/login.css' baseUrl}}">{{/contentFor}}
4
+ {{#contentFor 'script'}}<script src="{{cache_busting '/assets/login.js' baseUrl}}"></script>{{/contentFor}}
5
+ <form id="form" class="w-100 mx-auto my-0 login-form">
6
+ <h1 class="h3 mb-3 font-weight-normal text-center">Please login</h1>
7
+ <div class="mb-3">
8
+ <label class="form-label">Email address</label>
9
+ <input type="email" class="form-control" name="email" required autofocus value="robin@example.com">
10
+ </div>
11
+ <div class="mb-3">
12
+ <label class="form-label">Password</label>
13
+ <input type="password" class="form-control" name="password" required value="password">
14
+ </div>
15
+ <button class="btn btn-primary d-block w-100" type="submit">Login</button>
16
+ </form>
File without changes
@@ -0,0 +1,35 @@
1
+ {{!< default}}
2
+ {{#contentFor 'title'}}User Information{{/contentFor}}
3
+ {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cache_busting '/assets/user.css' baseUrl}}">{{/contentFor}}
4
+ {{#contentFor 'script'}}<script src="{{cache_busting '/assets/user.js' baseUrl}}"></script>{{/contentFor}}
5
+ <div class="row justify-content-center">
6
+ <div class="col-12 col-md-7">
7
+ <h3 class="font-weight-bold">
8
+ {{ifx edit 'Edit a user' 'Create a new user'}}
9
+ </h3>
10
+ <form id="form">
11
+ <div class="mb-3">
12
+ <label class="form-label">Email</label>
13
+ <input type="email" class="form-control" name="email" value="{{user.email}}" required autofocus>
14
+ </div>
15
+ <div class="mb-3">
16
+ <label class="form-label">Name</label>
17
+ <input type="text" class="form-control" name="name" value="{{user.name}}" required>
18
+ </div>
19
+ <div class="mb-3">
20
+ <label class="form-label">Password</label>
21
+ <input type="password" class="form-control" name="password" aria-describedby="passwordHelp" {{#unless edit}}required{{/unless}}>
22
+ {{#if edit}}
23
+ <div id="passwordHelp" class="form-text">Enter only if you want to change the password.</div>
24
+ {{/if}}
25
+ </div>
26
+ <div class="d-flex justify-content-between mt-3">
27
+ <a href="{{baseUrl}}/users" class="btn btn-secondary">Back to User List</a>
28
+ <button type="submit" class="btn btn-primary">Save user information</button>
29
+ </div>
30
+ </form>
31
+ </div>
32
+ </div>
33
+
34
+ {{#if edit}}<input type="hidden" id="id" value="{{user.id}}">{{/if}}
35
+ <input type="hidden" id="edit" value="{{ifx edit 1 0}}">
@@ -0,0 +1,18 @@
1
+ {{!< default}}
2
+ {{#contentFor 'title'}}User Management{{/contentFor}}
3
+ {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cache_busting '/assets/users.css' baseUrl}}">{{/contentFor}}
4
+ {{#contentFor 'script'}}<script src="{{cache_busting '/assets/users.js' baseUrl}}"></script>{{/contentFor}}
5
+ <div class="d-flex mb-4">
6
+ <a href="/users/new" class="btn btn-primary ms-auto">Create new user</a>
7
+ </div>
8
+ <table class="table w-100" id="table">
9
+ <thead>
10
+ <tr>
11
+ <th scope="col">User ID</th>
12
+ <th scope="col">Email</th>
13
+ <th scope="col">Name</th>
14
+ <th scope="col">Modified</th>
15
+ <th scope="col">#</th>
16
+ </tr>
17
+ </thead>
18
+ </table>
@@ -1 +0,0 @@
1
- // Main processing.
@@ -1,3 +0,0 @@
1
- /*Home page script*/
2
-
3
- // Main processing.