express-sweet-generator 2.0.8 → 2.0.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-sweet-generator",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "description": "EXPRESS SWEET application generator.",
5
5
  "main": "bin/express-sweet-cli.js",
6
6
  "files": [
@@ -1 +1,2 @@
1
- NODE_ENV=development
1
+ NODE_ENV=development
2
+ LOG_VALIDATION_ERRORS=true
@@ -4,7 +4,7 @@
4
4
  module.exports = class extends Error {
5
5
  /**
6
6
  * Creates a new NotFoundError instance.
7
- * @param {string} [message='The requested record could not be found.'] - The error message.
7
+ * @param {string} [message='The requested record could not be found.'] The error message.
8
8
  */
9
9
  constructor(message = 'The requested record could not be found.') {
10
10
  super(message);
@@ -7,7 +7,10 @@ module.exports = (req, res, next) => {
7
7
  const result = validationResult(req);
8
8
  if (result.isEmpty())
9
9
  next();
10
- else
10
+ else {
11
+ // Log validation errors only if LOG_VALIDATION_ERRORS is enabled
12
+ if (process.env.LOG_VALIDATION_ERRORS === 'true')
13
+ console.error('Validation errors:', result.array());
11
14
  res.status(400).end();
12
- // res.status(400).json({errors: result.array()});
15
+ }
13
16
  }
@@ -59,52 +59,58 @@ module.exports = class extends expressExtension.database.Model {
59
59
  });
60
60
  }
61
61
 
62
- static async paginate(options) {
63
- options = Object.assign({
62
+ static async paginate(paginationOptions) {
63
+ paginationOptions = Object.assign({
64
64
  start: 0,
65
65
  length: 30,
66
66
  order: null,
67
67
  dir: 'asc',
68
68
  search: null,
69
- }, options);
69
+ }, paginationOptions);
70
+
70
71
  const recordsTotal = await super.count();
71
- let where;
72
- if (options.search)
73
- where = {
74
- [super.Op.or]: [
75
- {email: {[super.Op.like]: `%${options.search}%`}},
76
- {name: {[super.Op.like]: `%${options.search}%`}}
77
- ]
78
- };
79
- const recordsFiltered = await super.count({where});
72
+
73
+ const whereClause = {};
74
+ if (paginationOptions.search)
75
+ whereClause[super.Op.or] = [
76
+ {email: {[super.Op.like]: `%${paginationOptions.search}%`}},
77
+ {name: {[super.Op.like]: `%${paginationOptions.search}%`}}
78
+ ];
79
+
80
+ const recordsFiltered = await super.count({where: whereClause});
81
+
80
82
  const data = await super.findAll({
81
83
  attributes: ['id', 'name', 'email', 'icon', 'modified'],
82
- where,
83
- order: [[options.order, options.dir]],
84
- offset: parseInt(options.start, 10),
85
- limit: parseInt(options.length),
84
+ where: whereClause,
85
+ order: [[paginationOptions.order, paginationOptions.dir]],
86
+ offset: parseInt(paginationOptions.start, 10),
87
+ limit: parseInt(paginationOptions.length),
86
88
  raw: true
87
89
  });
88
90
  return {recordsTotal, recordsFiltered, data};
89
91
  }
90
92
 
91
- static async createUser(set) {
93
+ static async createUser(userData) {
92
94
  let transaction;
93
95
  try {
94
- set = Object.assign({
96
+ userData = Object.assign({
95
97
  email: null,
96
98
  name: null,
97
99
  password: null,
98
100
  icon: null
99
- }, set);
101
+ }, userData);
100
102
  transaction = await super.begin();
101
103
  const user = await super.create({
102
- name: set.name,
103
- email: set.email,
104
- password: set.password,
104
+ name: userData.name,
105
+ email: userData.email,
106
+ password: userData.password,
105
107
  }, {transaction});
106
- await this.#updateUserIcon(user, set.icon, transaction);
108
+
109
+ await this.#updateUserIcon(user, userData.icon, transaction);
110
+
107
111
  await transaction.commit();
112
+
113
+ console.log('Successfully created a new user with ID:', user.id);
108
114
  } catch (error) {
109
115
  if (transaction)
110
116
  await transaction.rollback();
@@ -112,27 +118,31 @@ module.exports = class extends expressExtension.database.Model {
112
118
  }
113
119
  }
114
120
 
115
- static async updateUser(userId, set) {
121
+ static async updateUser(userId, userData) {
116
122
  let transaction;
117
123
  try {
118
- set = Object.assign({
124
+ userData = Object.assign({
119
125
  email: null,
120
126
  name: null,
121
127
  password: null,
122
128
  icon: null
123
- }, set);
129
+ }, userData);
124
130
  const user = await this.getUser(userId);
125
131
  if (!user)
126
132
  throw new NotFoundError();
127
133
  transaction = await super.begin();
128
- user.email = set.email;
129
- user.name = set.name;
134
+ user.email = userData.email;
135
+ user.name = userData.name;
130
136
  user.modified = super.literal('CURRENT_TIMESTAMP');
131
- if (set.password)
132
- user.password = set.password;
137
+ if (userData.password)
138
+ user.password = userData.password;
139
+
133
140
  await user.save({transaction});
134
- await this.#updateUserIcon(user, set.icon, transaction);
141
+ await this.#updateUserIcon(user, userData.icon, transaction);
142
+
135
143
  await transaction.commit();
144
+
145
+ console.log(`Successfully updated user with ID: ${userId}`);
136
146
  } catch (error) {
137
147
  if (transaction)
138
148
  await transaction.rollback();
@@ -144,26 +154,25 @@ module.exports = class extends expressExtension.database.Model {
144
154
  user.icon = `/upload/${user.id}.${Media.getExtensionFromDataUrl(dataUrl)}`;
145
155
  await user.save({transaction});
146
156
  const iconPath = `${global.APP_DIR}/public${user.icon}`;
147
- console.log(`Write ${iconPath}`);
148
157
  Media.writeDataUrlToFile(iconPath, dataUrl);
149
158
  }
150
159
 
151
160
  static async deleteUser(userId) {
152
- console.log(`Delete a user whose ID is ${userId}`);
153
- return super.destroy({where: {id: userId}});
161
+ await super.destroy({where: {id: userId}});
162
+ console.log(`Successfully deleted user with ID: ${userId}`);
154
163
  }
155
164
 
156
165
  static async emailExists(email, excludeUserId = null) {
157
- const where = {email};
166
+ const whereClause = {email};
158
167
  if (excludeUserId)
159
- where.id = {[super.Op.ne]: excludeUserId};
160
- return await super.count({where}) > 0;
168
+ whereClause.id = {[super.Op.ne]: excludeUserId};
169
+ return await super.count({where: whereClause}) > 0;
161
170
  }
162
171
 
163
172
  static async userIdExists(userId) {
164
- const where = {email};
173
+ const whereClause = {email};
165
174
  if (excludeUserId)
166
- where.id = {[super.Op.ne]: excludeUserId};
167
- return await super.count({where}) > 0;
175
+ whereClause.id = {[super.Op.ne]: excludeUserId};
176
+ return await super.count({where: whereClause}) > 0;
168
177
  }
169
178
  }
@@ -1 +1,2 @@
1
- NODE_ENV=development
1
+ NODE_ENV=development
2
+ LOG_VALIDATION_ERRORS=true
@@ -4,7 +4,7 @@
4
4
  export default class extends Error {
5
5
  /**
6
6
  * Creates a new NotFoundError instance.
7
- * @param {string} [message='The requested record could not be found.'] - The error message.
7
+ * @param {string} [message='The requested record could not be found.'] The error message.
8
8
  */
9
9
  constructor(message = 'The requested record could not be found.') {
10
10
  super(message);
@@ -7,7 +7,10 @@ export default (req, res, next) => {
7
7
  const result = validationResult(req);
8
8
  if (result.isEmpty())
9
9
  next();
10
- else
10
+ else {
11
+ // Log validation errors only if LOG_VALIDATION_ERRORS is enabled
12
+ if (process.env.LOG_VALIDATION_ERRORS === 'true')
13
+ console.error('Validation errors:', result.array());
11
14
  res.status(400).end();
12
- // res.status(400).json({errors: result.array()});
15
+ }
13
16
  }
@@ -59,52 +59,58 @@ export default class extends expressExtension.database.Model {
59
59
  });
60
60
  }
61
61
 
62
- static async paginate(options) {
63
- options = Object.assign({
62
+ static async paginate(paginationOptions) {
63
+ paginationOptions = Object.assign({
64
64
  start: 0,
65
65
  length: 30,
66
66
  order: null,
67
67
  dir: 'asc',
68
68
  search: null,
69
- }, options);
69
+ }, paginationOptions);
70
+
70
71
  const recordsTotal = await super.count();
71
- let where;
72
- if (options.search)
73
- where = {
74
- [super.Op.or]: [
75
- {email: {[super.Op.like]: `%${options.search}%`}},
76
- {name: {[super.Op.like]: `%${options.search}%`}}
77
- ]
78
- };
79
- const recordsFiltered = await super.count({where});
72
+
73
+ const whereClause = {};
74
+ if (paginationOptions.search)
75
+ whereClause[super.Op.or] = [
76
+ {email: {[super.Op.like]: `%${paginationOptions.search}%`}},
77
+ {name: {[super.Op.like]: `%${paginationOptions.search}%`}}
78
+ ];
79
+
80
+ const recordsFiltered = await super.count({where: whereClause});
81
+
80
82
  const data = await super.findAll({
81
83
  attributes: ['id', 'name', 'email', 'icon', 'modified'],
82
- where,
83
- order: [[options.order, options.dir]],
84
- offset: parseInt(options.start, 10),
85
- limit: parseInt(options.length),
84
+ where: whereClause,
85
+ order: [[paginationOptions.order, paginationOptions.dir]],
86
+ offset: parseInt(paginationOptions.start, 10),
87
+ limit: parseInt(paginationOptions.length),
86
88
  raw: true
87
89
  });
88
90
  return {recordsTotal, recordsFiltered, data};
89
91
  }
90
92
 
91
- static async createUser(set) {
93
+ static async createUser(userData) {
92
94
  let transaction;
93
95
  try {
94
- set = Object.assign({
96
+ userData = Object.assign({
95
97
  email: null,
96
98
  name: null,
97
99
  password: null,
98
100
  icon: null
99
- }, set);
101
+ }, userData);
100
102
  transaction = await super.begin();
101
103
  const user = await super.create({
102
- name: set.name,
103
- email: set.email,
104
- password: set.password,
104
+ name: userData.name,
105
+ email: userData.email,
106
+ password: userData.password,
105
107
  }, {transaction});
106
- await this.#updateUserIcon(user, set.icon, transaction);
108
+
109
+ await this.#updateUserIcon(user, userData.icon, transaction);
110
+
107
111
  await transaction.commit();
112
+
113
+ console.log('Successfully created a new user with ID:', user.id);
108
114
  } catch (error) {
109
115
  if (transaction)
110
116
  await transaction.rollback();
@@ -112,27 +118,31 @@ export default class extends expressExtension.database.Model {
112
118
  }
113
119
  }
114
120
 
115
- static async updateUser(userId, set) {
121
+ static async updateUser(userId, userData) {
116
122
  let transaction;
117
123
  try {
118
- set = Object.assign({
124
+ userData = Object.assign({
119
125
  email: null,
120
126
  name: null,
121
127
  password: null,
122
128
  icon: null
123
- }, set);
129
+ }, userData);
124
130
  const user = await this.getUser(userId);
125
131
  if (!user)
126
132
  throw new NotFoundError();
127
133
  transaction = await super.begin();
128
- user.email = set.email;
129
- user.name = set.name;
134
+ user.email = userData.email;
135
+ user.name = userData.name;
130
136
  user.modified = super.literal('CURRENT_TIMESTAMP');
131
- if (set.password)
132
- user.password = set.password;
137
+ if (userData.password)
138
+ user.password = userData.password;
139
+
133
140
  await user.save({transaction});
134
- await this.#updateUserIcon(user, set.icon, transaction);
141
+ await this.#updateUserIcon(user, userData.icon, transaction);
142
+
135
143
  await transaction.commit();
144
+
145
+ console.log(`Successfully updated user with ID: ${userId}`);
136
146
  } catch (error) {
137
147
  if (transaction)
138
148
  await transaction.rollback();
@@ -144,26 +154,25 @@ export default class extends expressExtension.database.Model {
144
154
  user.icon = `/upload/${user.id}.${Media.getExtensionFromDataUrl(dataUrl)}`;
145
155
  await user.save({transaction});
146
156
  const iconPath = `${global.APP_DIR}/public${user.icon}`;
147
- console.log(`Write ${iconPath}`);
148
157
  Media.writeDataUrlToFile(iconPath, dataUrl);
149
158
  }
150
159
 
151
160
  static async deleteUser(userId) {
152
- console.log(`Delete a user whose ID is ${userId}`);
153
- return super.destroy({where: {id: userId}});
161
+ await super.destroy({where: {id: userId}});
162
+ console.log(`Successfully deleted user with ID: ${userId}`);
154
163
  }
155
164
 
156
165
  static async emailExists(email, excludeUserId = null) {
157
- const where = {email};
166
+ const whereClause = {email};
158
167
  if (excludeUserId)
159
- where.id = {[super.Op.ne]: excludeUserId};
160
- return await super.count({where}) > 0;
168
+ whereClause.id = {[super.Op.ne]: excludeUserId};
169
+ return await super.count({where: whereClause}) > 0;
161
170
  }
162
171
 
163
172
  static async userIdExists(userId) {
164
- const where = {email};
173
+ const whereClause = {email};
165
174
  if (excludeUserId)
166
- where.id = {[super.Op.ne]: excludeUserId};
167
- return await super.count({where}) > 0;
175
+ whereClause.id = {[super.Op.ne]: excludeUserId};
176
+ return await super.count({where: whereClause}) > 0;
168
177
  }
169
178
  }